Freeciv-3.1: /fast/freeciv/nightly/S3_1/utility/string_vector.c Source File

Freeciv-3.1

Loading...
Searching...
No Matches
string_vector.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13#ifdef HAVE_CONFIG_H
14#include <fc_config.h>
15#endif
16
17#include <stdlib.h> /* qsort() */
18#include <string.h>
19
20/* utility */
21#include "astring.h"
22#include "mem.h"
23#include "shared.h"
24#include "support.h"
25
26#include "string_vector.h"
27
28/* The string vector structure. */
29 struct strvec {
30 char **vec;
31 size_t size;
32};
33
34/**********************************************************************/
37 static void string_free(char *string)
38{
39 if (string) {
40 free(string);
41 }
42}
43
44/**********************************************************************/
47 static char *string_duplicate(const char *string)
48{
49 if (string) {
50 return fc_strdup(string);
51 }
52 return NULL;
53}
54
55/**********************************************************************/
58 struct strvec *strvec_new(void)
59{
60 struct strvec *psv = fc_malloc(sizeof(struct strvec));
61
62 psv->vec = NULL;
63 psv->size = 0;
64
65 return psv;
66}
67
68/**********************************************************************/
71 void strvec_destroy(struct strvec *psv)
72{
73 strvec_clear(psv);
74 free(psv);
75}
76
77/**********************************************************************/
80 void strvec_reserve(struct strvec *psv, size_t reserve)
81{
82 if (reserve == psv->size) {
83 return;
84 } else if (reserve == 0) {
85 strvec_clear(psv);
86 return;
87 } else if (!psv->vec) {
88 /* Initial reserve */
89 psv->vec = fc_calloc(reserve, sizeof(char *));
90 } else if (reserve > psv->size) {
91 /* Expand the vector. */
92 psv->vec = fc_realloc(psv->vec, reserve * sizeof(char *));
93 memset(psv->vec + psv->size, 0, (reserve - psv->size) * sizeof(char *));
94 } else {
95 /* Shrink the vector: free the extra strings. */
96 size_t i;
97
98 for (i = psv->size - 1; i >= reserve; i--) {
99 string_free(psv->vec[i]);
100 }
101 psv->vec = fc_realloc(psv->vec, reserve * sizeof(char *));
102 }
103 psv->size = reserve;
104}
105
106/**********************************************************************/
110 void strvec_store(struct strvec *psv, const char *const *vec, size_t size)
111{
112 if (size == (size_t) -1) {
113 strvec_clear(psv);
114 for (; *vec; vec++) {
115 strvec_append(psv, *vec);
116 }
117 } else {
118 size_t i;
119
120 strvec_reserve(psv, size);
121 for (i = 0; i < size; i++, vec++) {
122 strvec_set(psv, i, *vec);
123 }
124 }
125}
126
127/**********************************************************************/
136 void strvec_from_str(struct strvec *psv, char separator, const char *str)
137{
138 const char *p;
139 char *new_str;
140
141 strvec_clear(psv);
142 while ((p = strchr(str, separator))) {
143 new_str = fc_malloc(p - str + 1);
144 memcpy(new_str, str, p - str);
145 new_str[p - str] = '\0';
146 psv->size++;
147 psv->vec = fc_realloc(psv->vec, psv->size * sizeof(char *));
148 psv->vec[psv->size - 1] = new_str;
149 str = p + 1;
150 }
151 if ('\0' != *str) {
152 strvec_append(psv, str);
153 }
154}
155
156/**********************************************************************/
159 void strvec_clear(struct strvec *psv)
160{
161 size_t i;
162 char **p;
163
164 if (!psv->vec) {
165 return;
166 }
167
168 for (i = 0, p = psv->vec; i < psv->size; i++, p++) {
169 string_free(*p);
170 }
171 free(psv->vec);
172 psv->vec = NULL;
173 psv->size = 0;
174}
175
176/**********************************************************************/
179 void strvec_remove_duplicate(struct strvec *psv,
180 int (*cmp_func) (const char *, const char *))
181{
182 size_t i, j;
183 const char *str1, *str2;
184
185 if (!psv->vec || 1 == psv->size) {
186 return;
187 }
188
189 for (i = 1; i < psv->size; i++) {
190 if ((str1 = psv->vec[i])) {
191 for (j = 0; j < i; j++) {
192 if ((str2 = psv->vec[j])
193 && 0 == cmp_func(str2, str1)) {
194 strvec_remove(psv, i);
195 i--;
196 break;
197 }
198 }
199 }
200 }
201}
202
203/**********************************************************************/
207 void strvec_remove_empty(struct strvec *psv)
208{
209 size_t i;
210 char *str;
211
212 if (!psv->vec) {
213 return;
214 }
215
216 for (i = 0; i < psv->size;) {
217 str = psv->vec[i];
218
219 if (!str) {
220 strvec_remove(psv, i);
221 continue;
222 }
223
224 remove_leading_trailing_spaces(str);
225 if (str[0] == '\0') {
226 strvec_remove(psv, i);
227 continue;
228 }
229
230 i++;
231 }
232}
233
234/**********************************************************************/
237 void strvec_copy(struct strvec *dest, const struct strvec *src)
238{
239 size_t i;
240 char **p;
241 char *const *l;
242
243 if (!src->vec) {
244 strvec_clear(dest);
245 return;
246 }
247
248 strvec_reserve(dest, src->size);
249 for (i = 0, p = dest->vec, l = src->vec; i < dest->size; i++, p++, l++) {
250 string_free(*p);
251 *p = string_duplicate(*l);
252 }
253}
254
255/**********************************************************************/
258 void strvec_sort(struct strvec *psv, int (*sort_func) (const char *const *,
259 const char *const *))
260{
261 qsort(psv->vec, psv->size, sizeof(const char *),
262 (int (*) (const void *, const void *)) sort_func);
263}
264
265/**********************************************************************/
268 void strvec_prepend(struct strvec *psv, const char *string)
269{
270 strvec_reserve(psv, psv->size + 1);
271 memmove(psv->vec + 1, psv->vec, (psv->size - 1) * sizeof(char *));
272 psv->vec[0] = string_duplicate(string);
273}
274
275/**********************************************************************/
278 void strvec_append(struct strvec *psv, const char *string)
279{
280 strvec_reserve(psv, psv->size + 1);
281 psv->vec[psv->size - 1] = string_duplicate(string);
282}
283
284/**********************************************************************/
287 void strvec_insert(struct strvec *psv, size_t svindex, const char *string)
288{
289 if (svindex <= 0) {
290 strvec_prepend(psv, string);
291 } else if (svindex >= psv->size) {
292 strvec_append(psv, string);
293 } else {
294 strvec_reserve(psv, psv->size + 1);
295 memmove(psv->vec + svindex + 1, psv->vec + svindex,
296 (psv->size - svindex - 1) * sizeof(char *));
297 psv->vec[svindex] = string_duplicate(string);
298 }
299}
300
301/**********************************************************************/
305 bool strvec_set(struct strvec *psv, size_t svindex, const char *string)
306{
307 if (strvec_index_valid(psv, svindex)) {
308 string_free(psv->vec[svindex]);
309 psv->vec[svindex] = string_duplicate(string);
310 return TRUE;
311 }
312 return FALSE;
313}
314
315/**********************************************************************/
319 bool strvec_remove(struct strvec *psv, size_t svindex)
320{
321 if (!strvec_index_valid(psv, svindex)) {
322 return FALSE;
323 }
324
325 if (psv->size == 1) {
326 /* It is the last. */
327 strvec_clear(psv);
328 return TRUE;
329 }
330
331 string_free(psv->vec[svindex]);
332 memmove(psv->vec + svindex, psv->vec + svindex + 1,
333 (psv->size - svindex - 1) * sizeof(char *));
334 psv->vec[psv->size - 1] = NULL; /* Do not attempt to free this data. */
335 strvec_reserve(psv, psv->size - 1);
336
337 return TRUE;
338}
339
340/**********************************************************************/
343 size_t strvec_size(const struct strvec *psv)
344{
345 return psv->size;
346}
347
348/**********************************************************************/
351 bool are_strvecs_equal(const struct strvec *stv1,
352 const struct strvec *stv2)
353{
354 int i;
355
356 if (strvec_size(stv1) != strvec_size(stv2)) {
357 return FALSE;
358 }
359
360 for (i = 0; i < strvec_size(stv1); i++) {
361 if (0 != strcmp(stv1->vec[i], stv2->vec[i])) {
362 return FALSE;
363 }
364 }
365
366 return TRUE;
367}
368
369/**********************************************************************/
372 const char *const *strvec_data(const struct strvec *psv)
373{
374 return (const char **) psv->vec;
375}
376
377/**********************************************************************/
380 bool strvec_index_valid(const struct strvec *psv, size_t svindex)
381{
382 return svindex < psv->size;
383}
384
385/**********************************************************************/
388 const char *strvec_get(const struct strvec *psv, size_t svindex)
389{
390 return strvec_index_valid(psv, svindex) ? psv->vec[svindex] : NULL;
391}
392
393/**********************************************************************/
400 void strvec_to_str(const struct strvec *psv, char separator,
401 char *buf, size_t buf_len)
402{
403 int len;
404
405 strvec_iterate(psv, str) {
406 len = fc_snprintf(buf, buf_len, "%s", str) + 1;
407 if (1 >= len) {
408 /* Truncated. */
409 return;
410 }
411
412 buf += len;
413 buf_len -= len;
414 if (0 < buf_len) {
415 *(buf - 1) = separator;
416 }
417 } strvec_iterate_end;
418
419 buf[0] = '\0';
420}
421
422/**********************************************************************/
428 const char *strvec_to_or_list(const struct strvec *psv,
429 struct astring *astr)
430{
431 fc_assert_ret_val(NULL != psv, NULL);
432 return astr_build_or_list(astr, (const char **) psv->vec, psv->size);
433}
434
435/**********************************************************************/
441 const char *strvec_to_and_list(const struct strvec *psv,
442 struct astring *astr)
443{
444 fc_assert_ret_val(NULL != psv, NULL);
445 return astr_build_and_list(astr, (const char **) psv->vec, psv->size);
446}
astr_build_or_list
const char * astr_build_or_list(struct astring *astr, const char *const *items, size_t number)
Definition astring.c:329
astr_build_and_list
const char * astr_build_and_list(struct astring *astr, const char *const *items, size_t number)
Definition astring.c:367
str
#define str
Definition astring.c:76
astring.h
cmp_func
static gint cmp_func(gconstpointer a_p, gconstpointer b_p)
Definition repodlgs.c:370
fc_assert_ret_val
#define fc_assert_ret_val(condition, val)
Definition log.h:194
mem.h
fc_calloc
#define fc_calloc(n, esz)
Definition mem.h:38
fc_strdup
#define fc_strdup(str)
Definition mem.h:43
fc_realloc
#define fc_realloc(ptr, sz)
Definition mem.h:36
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
len
int len
Definition packhand.c:125
remove_leading_trailing_spaces
void remove_leading_trailing_spaces(char *s)
Definition shared.c:443
shared.h
size
size_t size
Definition specvec.h:72
strvec_destroy
void strvec_destroy(struct strvec *psv)
Definition string_vector.c:71
strvec_sort
void strvec_sort(struct strvec *psv, int(*sort_func)(const char *const *, const char *const *))
Definition string_vector.c:258
strvec_to_str
void strvec_to_str(const struct strvec *psv, char separator, char *buf, size_t buf_len)
Definition string_vector.c:400
strvec_set
bool strvec_set(struct strvec *psv, size_t svindex, const char *string)
Definition string_vector.c:305
strvec_index_valid
bool strvec_index_valid(const struct strvec *psv, size_t svindex)
Definition string_vector.c:380
strvec_data
const char *const * strvec_data(const struct strvec *psv)
Definition string_vector.c:372
strvec_store
void strvec_store(struct strvec *psv, const char *const *vec, size_t size)
Definition string_vector.c:110
string_free
static void string_free(char *string)
Definition string_vector.c:37
strvec_append
void strvec_append(struct strvec *psv, const char *string)
Definition string_vector.c:278
strvec_from_str
void strvec_from_str(struct strvec *psv, char separator, const char *str)
Definition string_vector.c:136
strvec_reserve
void strvec_reserve(struct strvec *psv, size_t reserve)
Definition string_vector.c:80
strvec_to_or_list
const char * strvec_to_or_list(const struct strvec *psv, struct astring *astr)
Definition string_vector.c:428
string_duplicate
static char * string_duplicate(const char *string)
Definition string_vector.c:47
strvec_get
const char * strvec_get(const struct strvec *psv, size_t svindex)
Definition string_vector.c:388
strvec_prepend
void strvec_prepend(struct strvec *psv, const char *string)
Definition string_vector.c:268
strvec_copy
void strvec_copy(struct strvec *dest, const struct strvec *src)
Definition string_vector.c:237
strvec_new
struct strvec * strvec_new(void)
Definition string_vector.c:58
strvec_clear
void strvec_clear(struct strvec *psv)
Definition string_vector.c:159
strvec_size
size_t strvec_size(const struct strvec *psv)
Definition string_vector.c:343
strvec_to_and_list
const char * strvec_to_and_list(const struct strvec *psv, struct astring *astr)
Definition string_vector.c:441
strvec_remove_duplicate
void strvec_remove_duplicate(struct strvec *psv, int(*cmp_func)(const char *, const char *))
Definition string_vector.c:179
are_strvecs_equal
bool are_strvecs_equal(const struct strvec *stv1, const struct strvec *stv2)
Definition string_vector.c:351
strvec_remove
bool strvec_remove(struct strvec *psv, size_t svindex)
Definition string_vector.c:319
strvec_remove_empty
void strvec_remove_empty(struct strvec *psv)
Definition string_vector.c:207
strvec_insert
void strvec_insert(struct strvec *psv, size_t svindex, const char *string)
Definition string_vector.c:287
string_vector.h
strvec_iterate
#define strvec_iterate(psv, str)
Definition string_vector.h:61
strvec_iterate_end
#define strvec_iterate_end
Definition string_vector.h:68
astring
Definition astring.h:36
strvec
Definition string_vector.c:29
strvec::vec
char ** vec
Definition string_vector.c:30
strvec::size
size_t size
Definition string_vector.c:31
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
support.h
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47