File: string_array.c

package info (click to toggle)
f-irc 1.36-1
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 632 kB
  • ctags: 904
  • sloc: ansic: 12,538; makefile: 61
file content (185 lines) | stat: -rw-r--r-- 3,498 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* GPLv2 applies
 * SVN revision: $Revision: 749 $
 * (C) 2006-2014 by folkert@vanheusden.com
 */
#include <stdlib.h>
#include <string.h>

#include "string_array.h"
#include "utils.h"

void init_string_array(string_array_t *p)
{
	p -> data = NULL;
	p -> n = 0;
}

void free_string_array(string_array_t *p)
{
	int i = 0;

	for(i=0; i < p -> n; i++)
		myfree(p -> data[i]);

	myfree(p -> data);
	p -> data = NULL;

	p -> n = 0;
}

void add_to_string_array(string_array_t *p, const char *what)
{
	p -> data = (const char **)realloc(p -> data, (p -> n + 1) * sizeof(char *));

	p -> data[p -> n] = strdup(what);
	p -> n++;
}

int find_str_in_string_array(const string_array_t *p, const char *what, BOOL ignore_case)
{
	int i = 0;

	for(i=0; i<p -> n; i++)
	{
		if (ignore_case && strcasecmp(p -> data[i], what) == 0)
			return i;

		if (!ignore_case && strcmp(p -> data[i], what) == 0)
			return i;
	}

	return -1;
}

void del_nr_from_string_array(string_array_t *p, int nr)
{
	int n_to_move = (p -> n - nr) - 1;

	myfree(p -> data[nr]);

	if (n_to_move > 0)
		memmove(&p -> data[nr], &p -> data[nr + 1], n_to_move * sizeof(char *));

	p -> n--;
}

void del_str_from_string_array(string_array_t *p, const char *what)
{
	int idx = find_str_in_string_array(p, what, FALSE);

	if (idx != -1)
		del_nr_from_string_array(p, idx);
}

int string_array_get_n(const string_array_t *p)
{
	return p -> n;
}

const char* string_array_get(const string_array_t *p, int idx)
{
	return p -> data[idx];
}

void insert_into_string_array(string_array_t *p, int idx, const char *what)
{
	int n_to_move = p -> n - idx;

	p -> data = (const char **)realloc(p -> data, (p -> n + 1) * sizeof(char *));

	if (n_to_move > 0)
		memmove(&p -> data[idx + 1], &p -> data[idx], sizeof(char *) * n_to_move);

	p -> data[idx] = strdup(what);

	p -> n++;
}

void replace_in_string_array(string_array_t *p, int idx, const char *what)
{
	myfree(p -> data[idx]);

	p -> data[idx] = strdup(what);
}

void split_string(const char *in, const char *split, BOOL clean, string_array_t *parts)
{
	char *copy = strdup(in), *pc = copy;
	int split_len = strlen(split);

	for(;;)
	{
		char *term = strstr(pc, split);

		if (term)
			*term = 0x00;

		add_to_string_array(parts, pc);

		if (!term)
			break;

		pc = term + split_len;

		if (clean)
		{
			while(strncmp(pc, split, split_len) == 0)
				pc += split_len;
		}
	}

	free(copy);
}

void free_splitted_string(string_array_t *parts)
{
	free_string_array(parts);
}

static int cmp(const void *a, const void *b) 
{ 
	const char **ia = (const char **)a;
	const char **ib = (const char **)b;

	return strcmp(*ia, *ib);
} 

void sort_string_array(string_array_t *p)
{
	qsort(p -> data, p -> n, sizeof(char *), cmp);
}

int partial_match_search_string_array(const string_array_t *p, const char *what)
{
        int imin = 0, imax = p -> n - 1, what_len = strlen(what);

        while(imax >= imin)
        {
                int imid = (imin + imax) / 2, cmp = 0;

		if (memcmp(p -> data[imid], what, what_len) == 0)
			return imid;

                cmp = strcasecmp(p -> data[imid], what);

                if (cmp < 0)
                        imin = imid + 1;
                else /* if (cmp > 0) */
                        imax = imid - 1;
        }

	return -1;
}

BOOL dump_string_array(const string_array_t *p, const char *name, FILE *fh)
{
	int index = 0;

	for(index=0; index<p -> n; index++)
	{
		if (fprintf(fh, "%s=%s\n", name, p -> data[index]) == -1)
			return FALSE;
	}

	return TRUE;
}