Debian Codegraph @

ini/ini_get_array.c

raw symbols SCIP
1 /*
2 INI LIBRARY
3
4 Value interpretation functions for arrays of values
5 and corresponding memory cleanup functions.
6
7 Copyright (C) Dmitri Pal <[email protected]> 2010
8
9 INI Library is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 INI Library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with INI Library. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "config.h"
24 #include <stdio.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <ctype.h>
29 #include <locale.h>
30 #include "trace.h"
31 #include "collection.h"
32 #include "collection_tools.h"
33 #include "ini_defines.h"
34 #include "ini_config.h"
35
36 /*
37 * Internal contants to indicate how
38 * to process the lists of strings.
39 */
40 #define EXCLUDE_EMPTY 0
41 #define INCLUDE_EMPTY 1
42
43 /* Arrays of stings */
44 static char **get_str_cfg_array(struct collection_item *item,
45 int include,
46 const char *sep,
47 int *size,
48 int *error)
49 {
50 char *copy = NULL;
51 char *dest = NULL;
52 char locsep[4];
53 int lensep;
54 char *buff;
55 int count = 0;
56 int len = 0;
57 int resume_len;
58 char **array;
59 char *start;
60 int i, j;
61 int dlen;
62
63 TRACE_FLOW_STRING("get_str_cfg_array", "Entry");
64
65 /* Do we have the item ? */
66 if ((item == NULL) ||
67 (col_get_item_type(item) != COL_TYPE_STRING)) {
68 TRACE_ERROR_NUMBER("Invalid argument.", EINVAL);
69 if (error) *error = EINVAL;
70 return NULL;
71 }
72
73 /* Handle the separators */
74 if (sep == NULL) {
75 locsep[0] = ',';
76 locsep[1] = '\0';
77 lensep = 2;
78 }
79 else {
80 strncpy(locsep, sep, 3);
81 locsep[3] = '\0';
82 lensep = strlen(locsep) + 1;
83 }
84
85 /* Allocate memory for the copy of the string */
86 copy = malloc(col_get_item_length(item));
87 if (copy == NULL) {
88 TRACE_ERROR_NUMBER("Failed to allocate memory.", ENOMEM);
89 if (error) *error = ENOMEM;
90 return NULL;
91 }
92
93 /* Loop through the string */
94 dest = copy;
95 buff = col_get_item_data(item);
96 start = buff;
97 dlen = col_get_item_length(item);
98 for(i = 0; i < dlen; i++) {
99 for(j = 0; j < lensep; j++) {
100 if(buff[i] == locsep[j]) {
101 /* If we found one of the separators trim spaces around */
102 resume_len = len;
103 while (len > 0) {
104 if (isspace(start[len - 1])) len--;
105 else break;
106 }
107 TRACE_INFO_STRING("Current:", start);
108 TRACE_INFO_NUMBER("Length:", len);
109 if (len > 0) {
110 /* Save block aside */
111 memcpy(dest, start, len);
112 count++;
113 dest += len;
114 *dest = '\0';
115 dest++;
116 }
117 else if(include) {
118 count++;
119 *dest = '\0';
120 dest++;
121 }
122 if (locsep[j] == '\0') break; /* We are done */
123
124 /* Move forward and trim spaces if any */
125 start += resume_len + 1;
126 i++;
127 TRACE_INFO_STRING("Other pointer :", buff + i);
128 while ((i < dlen) && (isspace(*start))) {
129 i++;
130 start++;
131 }
132 len = -1; /* Len will be increased in the loop */
133 i--; /* i will be increas so we need to step back */
134 TRACE_INFO_STRING("Remaining buffer after triming spaces:", start);
135 break;
136 }
137 }
138 len++;
139 }
140
141 /* Now we know how many items are there in the list */
142 array = malloc((count + 1) * sizeof(char *));
143 if (array == NULL) {
144 free(copy);
145 TRACE_ERROR_NUMBER("Failed to allocate memory.", ENOMEM);
146 if (error) *error = ENOMEM;
147 return NULL;
148 }
149
150 /* Loop again to fill in the pointers */
151 start = copy;
152 for (i = 0; i < count; i++) {
153 TRACE_INFO_STRING("Token :", start);
154 TRACE_INFO_NUMBER("Item :", i);
155 array[i] = start;
156 /* Move to next item */
157 while(*start) start++;
158 start++;
159 }
160 array[count] = NULL;
161
162 if (error) *error = EOK;
163 if (size) *size = count;
164 /* If count is 0 the copy needs to be freed */
165 if (count == 0) free(copy);
166 TRACE_FLOW_STRING("get_str_cfg_array", "Exit");
167 /* coverity[leaked_storage : SUPPRESS] */ /* false positive warning */
168 return array;
169 }
170
171 /* Get array of strings from item eliminating empty tokens */
172 char **get_string_config_array(struct collection_item *item,
173 const char *sep, int *size, int *error)
174 {
175 TRACE_FLOW_STRING("get_string_config_array", "Called.");
176 return get_str_cfg_array(item, EXCLUDE_EMPTY, sep, size, error);
177 }
178 /* Get array of strings from item preserving empty tokens */
179 char **get_raw_string_config_array(struct collection_item *item,
180 const char *sep, int *size, int *error)
181 {
182 TRACE_FLOW_STRING("get_raw_string_config_array", "Called.");
183 return get_str_cfg_array(item, INCLUDE_EMPTY, sep, size, error);
184 }
185
186 /* Special function to free string config array */
187 void free_string_config_array(char **str_config)
188 {
189 TRACE_FLOW_STRING("free_string_config_array", "Entry");
190
191 if (str_config != NULL) {
192 if (*str_config != NULL) free(*str_config);
193 free(str_config);
194 }
195
196 TRACE_FLOW_STRING("free_string_config_array", "Exit");
197 }
198
199 /* Get an array of long values.
200 * NOTE: For now I leave just one function that returns numeric arrays.
201 * In future if we need other numeric types we can change it to do strtoll
202 * internally and wrap it for backward compatibility.
203 */
204 long *get_long_config_array(struct collection_item *item, int *size, int *error)
205 {
206 const char *str;
207 char *endptr;
208 long val = 0;
209 long *array;
210 int count = 0;
211 int err;
212
213 TRACE_FLOW_STRING("get_long_config_array", "Entry");
214
215 /* Do we have the item ? */
216 if ((item == NULL) ||
217 (col_get_item_type(item) != COL_TYPE_STRING) ||
218 (size == NULL)) {
219 TRACE_ERROR_NUMBER("Invalid argument.", EINVAL);
220 if (error) *error = EINVAL;
221 return NULL;
222 }
223
224 /* Assume that we have maximum number of different numbers */
225 array = (long *)malloc(sizeof(long) * col_get_item_length(item)/2);
226 if (array == NULL) {
227 TRACE_ERROR_NUMBER("Failed to allocate memory.", ENOMEM);
228 if (error) *error = ENOMEM;
229 return NULL;
230 }
231
232 /* Now parse the string */
233 str = (const char *)col_get_item_data(item);
234 while (*str) {
235
236 errno = 0;
237 val = strtol(str, &endptr, 10);
238 err = errno;
239
240 if (err) {
241 TRACE_ERROR_NUMBER("Conversion failed", err);
242 free(array);
243 if (error) *error = err;
244 return NULL;
245 }
246
247 if (endptr == str) {
248 TRACE_ERROR_NUMBER("Nothing processed", EIO);
249 free(array);
250 if (error) *error = EIO;
251 return NULL;
252 }
253
254 /* Save value */
255 array[count] = val;
256 count++;
257 /* Are we done? */
258 if (*endptr == 0) break;
259 /* Advance to the next valid number */
260 for (str = endptr; *str; str++) {
261 if (isdigit(*str) || (*str == '-') || (*str == '+')) break;
262 }
263 }
264
265 *size = count;
266 if (error) *error = EOK;
267
268 TRACE_FLOW_NUMBER("get_long_config_value returning", val);
269 return array;
270
271 }
272
273 /* Get an array of double values */
274 double *get_double_config_array(struct collection_item *item, int *size, int *error)
275 {
276 const char *str;
277 char *endptr;
278 double val = 0;
279 double *array;
280 int count = 0;
281 struct lconv *loc;
282
283 TRACE_FLOW_STRING("get_double_config_array", "Entry");
284
285 /* Do we have the item ? */
286 if ((item == NULL) ||
287 (col_get_item_type(item) != COL_TYPE_STRING) ||
288 (size == NULL)) {
289 TRACE_ERROR_NUMBER("Invalid argument.", EINVAL);
290 if (error) *error = EINVAL;
291 return NULL;
292 }
293
294 /* Assume that we have maximum number of different numbers */
295 array = (double *)malloc(sizeof(double) * col_get_item_length(item)/2);
296 if (array == NULL) {
297 TRACE_ERROR_NUMBER("Failed to allocate memory.", ENOMEM);
298 if (error) *error = ENOMEM;
299 return NULL;
300 }
301
302 /* Get locale information so that we can check for decimal point character.
303 * Based on the man pages it is unclear if this is an allocated memory or not.
304 * Seems like it is a static thread or process local structure so
305 * I will not try to free it after use.
306 */
307 loc = localeconv();
308
309 /* Now parse the string */
310 str = (const char *)col_get_item_data(item);
311 while (*str) {
312 TRACE_INFO_STRING("String to convert",str);
313 errno = 0;
314 val = strtod(str, &endptr);
315 if ((errno == ERANGE) ||
316 ((errno != 0) && (val == 0)) ||
317 (endptr == str)) {
318 TRACE_ERROR_NUMBER("Conversion failed", EIO);
319 free(array);
320 if (error) *error = EIO;
321 return NULL;
322 }
323 /* Save value */
324 array[count] = val;
325 count++;
326 /* Are we done? */
327 if (*endptr == 0) break;
328 TRACE_INFO_STRING("End pointer after conversion",endptr);
329 /* Advance to the next valid number */
330 for (str = endptr; *str; str++) {
331 if (isdigit(*str) || (*str == '-') || (*str == '+') ||
332 /* It is ok to do this since the string is null terminated */
333 ((*str == *(loc->decimal_point)) && isdigit(str[1]))) break;
334 }
335 }
336
337 *size = count;
338 if (error) *error = EOK;
339
340 TRACE_FLOW_NUMBER("get_double_config_value returning", val);
341 return array;
342
343 }
344
345
346 /* Special function to free long config array */
347 void free_long_config_array(long *array)
348 {
349 if (array != NULL) free(array);
350 }
351
352 /* Special function to free double config array */
353 void free_double_config_array(double *array)
354 {
355 if (array != NULL) free(array);
356 }