Debian Codegraph @

ini/ini_get_array_valueobj.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]> 2012
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_configobj.h"
35 #include "ini_valueobj.h"
36
37 /*
38 * Internal contants to indicate how
39 * to process the lists of strings.
40 */
41 #define EXCLUDE_EMPTY 0
42 #define INCLUDE_EMPTY 1
43
44 /* Maximum number of separators supported. Do not make it less than 3. */
45 #define MAX_SEP_LEN 3
46
47 /* Arrays of stings */
48 static char **get_str_cfg_array(struct value_obj *vo,
49 int include,
50 const char *sep,
51 int *size,
52 int *error)
53 {
54 char *copy = NULL;
55 char *dest = NULL;
56 char locsep[MAX_SEP_LEN + 1];
57 uint32_t lensep;
58 const char *buff;
59 uint32_t count = 0;
60 uint32_t len = 0;
61 uint32_t resume_len;
62 char **array;
63 const char *start;
64 char *start_array;
65 uint32_t i, j;
66 uint32_t dlen;
67
68 TRACE_FLOW_ENTRY();
69
70 /* Do we have the vo ? */
71 if (vo == NULL) {
72 TRACE_ERROR_NUMBER("Invalid argument.", EINVAL);
73 if (error) *error = EINVAL;
74 return NULL;
75 }
76
77 /* Get value and length - no error checking as we checked it above
78 * and there is no other reson the function could to fail.
79 */
80 value_get_concatenated(vo, &buff);
81 value_get_concatenated_len(vo, &dlen);
82
83 /* Handle the separators */
84 if (sep == NULL) {
85 locsep[0] = ',';
86 locsep[1] = '\0';
87 lensep = 2;
88 }
89 else {
90 strncpy(locsep, sep, MAX_SEP_LEN);
91 locsep[MAX_SEP_LEN] = '\0';
92 lensep = strlen(locsep) + 1;
93 }
94
95 /* Allocate memory for the copy of the string */
96 TRACE_INFO_NUMBER("Length to allocate is :", dlen);
97 /* Always reserve one more byte
98 * for the case when the string consist of delimeters */
99 copy = malloc(dlen + 1);
100 if (copy == NULL) {
101 TRACE_ERROR_NUMBER("Failed to allocate memory.", ENOMEM);
102 if (error) *error = ENOMEM;
103 return NULL;
104 }
105
106 /* Suppress warning */
107 start = buff;
108
109 /* Loop through the string */
110 dest = copy;
111 for(i = 0; i < dlen; i++) {
112 for(j = 0; j < lensep; j++) {
113 if(buff[i] == locsep[j]) {
114 /* If we found one of the separators trim spaces around */
115 resume_len = len;
116 while (len > 0) {
117 if (isspace(start[len - 1])) len--;
118 else break;
119 }
120 TRACE_INFO_STRING("Current:", start);
121 TRACE_INFO_NUMBER("Length:", len);
122 if (len > 0) {
123 /* Save block aside */
124 memcpy(dest, start, len);
125 count++;
126 dest += len;
127 *dest = '\0';
128 dest++;
129 }
130 else if(include) {
131 count++;
132 *dest = '\0';
133 dest++;
134 }
135
136 /* Move forward and trim spaces if any */
137 start += resume_len + 1;
138 i++;
139 TRACE_INFO_STRING("Other pointer :", buff + i);
140 while ((i < dlen) && (isspace(*start))) {
141 i++;
142 start++;
143 }
144 len = -1; /* Len will be increased in the loop */
145 i--; /* i will be increased so we need to step back */
146 TRACE_INFO_STRING("Remaining buffer after triming spaces:",
147 start);
148 break;
149 }
150 }
151 len++;
152 }
153
154 /* Save last segment */
155 TRACE_INFO_STRING("Current:", start);
156 TRACE_INFO_NUMBER("Length:", len);
157 if (len > 0) {
158 /* Save block aside */
159 memcpy(dest, start, len);
160 count++;
161 dest += len;
162 *dest = '\0';
163 }
164 else if(include && dlen && count) {
165 TRACE_INFO_NUMBER("Include :", include);
166 TRACE_INFO_NUMBER("dlen :", dlen);
167 TRACE_INFO_NUMBER("Count :", count);
168 count++;
169 *dest = '\0';
170 }
171
172 /* Now we know how many items are there in the list */
173 array = malloc((count + 1) * sizeof(char *));
174 if (array == NULL) {
175 free(copy);
176 TRACE_ERROR_NUMBER("Failed to allocate memory.", ENOMEM);
177 if (error) *error = ENOMEM;
178 return NULL;
179 }
180
181 /* Loop again to fill in the pointers */
182 start_array = copy;
183 for (i = 0; i < count; i++) {
184 TRACE_INFO_STRING("Token :", start_array);
185 TRACE_INFO_NUMBER("Item :", i);
186 array[i] = start_array;
187 /* Move to next item */
188 while(*start_array) start_array++;
189 start_array++;
190 }
191 array[count] = NULL;
192
193 if (error) *error = EOK;
194 if (size) *size = count;
195 /* If count is 0 the copy needs to be freed */
196 if (count == 0) free(copy);
197
198 TRACE_FLOW_EXIT();
199 return array;
200 }
201
202 /* Get array of strings from item eliminating empty tokens */
203 char **ini_get_string_config_array(struct value_obj *vo,
204 const char *sep, int *size, int *error)
205 {
206 TRACE_FLOW_ENTRY();
207 return get_str_cfg_array(vo, EXCLUDE_EMPTY, sep, size, error);
208 }
209 /* Get array of strings from item preserving empty tokens */
210 char **ini_get_raw_string_config_array(struct value_obj *vo,
211 const char *sep, int *size, int *error)
212 {
213 TRACE_FLOW_ENTRY();
214 return get_str_cfg_array(vo, INCLUDE_EMPTY, sep, size, error);
215 }
216
217 /* Special function to free string config array */
218 void ini_free_string_config_array(char **str_config)
219 {
220 TRACE_FLOW_ENTRY();
221
222 if (str_config != NULL) {
223 if (*str_config != NULL) free(*str_config);
224 free(str_config);
225 }
226
227 TRACE_FLOW_EXIT();
228 }
229
230 /* Get an array of long values.
231 * NOTE: For now I leave just one function that returns numeric arrays.
232 * In future if we need other numeric types we can change it to do strtoll
233 * internally and wrap it for backward compatibility.
234 */
235 long *ini_get_long_config_array(struct value_obj *vo, int *size, int *error)
236 {
237 const char *str;
238 char *endptr;
239 long val = 0;
240 long *array;
241 uint32_t count = 0;
242 int err;
243 uint32_t dlen;
244
245 TRACE_FLOW_ENTRY();
246
247 /* Do we have the vo ? */
248 if (vo == NULL) {
249 TRACE_ERROR_NUMBER("Invalid value object argument.", EINVAL);
250 if (error) *error = EINVAL;
251 return NULL;
252 }
253
254 /* Do we have the size ? */
255 if (size == NULL) {
256 TRACE_ERROR_NUMBER("Invalid size argument.", EINVAL);
257 if (error) *error = EINVAL;
258 return NULL;
259 }
260
261 /* Get value and length - no error checking as we checked it above
262 * and there is no other reson the function could to fail.
263 */
264 value_get_concatenated(vo, &str);
265 value_get_concatenated_len(vo, &dlen);
266
267 /* Assume that we have maximum number of different numbers */
268 array = (long *)malloc(sizeof(long) * dlen/2);
269 if (array == NULL) {
270 TRACE_ERROR_NUMBER("Failed to allocate memory.", ENOMEM);
271 if (error) *error = ENOMEM;
272 return NULL;
273 }
274
275 /* Now parse the string */
276 while (*str) {
277
278 errno = 0;
279 val = strtol(str, &endptr, 10);
280 err = errno;
281
282 if (err) {
283 TRACE_ERROR_NUMBER("Conversion failed", err);
284 free(array);
285 if (error) *error = err;
286 return NULL;
287 }
288
289 if (endptr == str) {
290 TRACE_ERROR_NUMBER("Nothing processed", EIO);
291 free(array);
292 if (error) *error = EIO;
293 return NULL;
294 }
295
296 /* Save value */
297 array[count] = val;
298 count++;
299 /* Are we done? */
300 if (*endptr == 0) break;
301 /* Advance to the next valid number */
302 for (str = endptr; *str; str++) {
303 if (isdigit(*str) || (*str == '-') || (*str == '+')) break;
304 }
305 }
306
307 *size = count;
308 if (error) *error = EOK;
309
310 TRACE_FLOW_EXIT();
311 return array;
312
313 }
314
315 /* Get an array of double values */
316 double *ini_get_double_config_array(struct value_obj *vo, int *size, int *error)
317 {
318 const char *str;
319 char *endptr;
320 double val = 0;
321 double *array;
322 int count = 0;
323 struct lconv *loc;
324 uint32_t dlen;
325
326 TRACE_FLOW_ENTRY();
327
328 /* Do we have the vo ? */
329 if (vo == NULL) {
330 TRACE_ERROR_NUMBER("Invalid value object argument.", EINVAL);
331 if (error) *error = EINVAL;
332 return NULL;
333 }
334
335 /* Do we have the size ? */
336 if (size == NULL) {
337 TRACE_ERROR_NUMBER("Invalid size argument.", EINVAL);
338 if (error) *error = EINVAL;
339 return NULL;
340 }
341
342 /* Get value and length - no error checking as we checked it above
343 * and there is no other reson the function could to fail.
344 */
345 value_get_concatenated(vo, &str);
346 value_get_concatenated_len(vo, &dlen);
347
348
349 /* Assume that we have maximum number of different numbers */
350 array = (double *)malloc(sizeof(double) * dlen/2);
351 if (array == NULL) {
352 TRACE_ERROR_NUMBER("Failed to allocate memory.", ENOMEM);
353 if (error) *error = ENOMEM;
354 return NULL;
355 }
356
357 /* Get locale information so that we can check for decimal point character.
358 * Based on the man pages it is unclear if this is an allocated memory or not.
359 * Seems like it is a static thread or process local structure so
360 * I will not try to free it after use.
361 */
362 loc = localeconv();
363
364 /* Now parse the string */
365 while (*str) {
366 TRACE_INFO_STRING("String to convert",str);
367 errno = 0;
368 val = strtod(str, &endptr);
369 if ((errno == ERANGE) ||
370 ((errno != 0) && (val == 0)) ||
371 (endptr == str)) {
372 TRACE_ERROR_NUMBER("Conversion failed", EIO);
373 free(array);
374 if (error) *error = EIO;
375 return NULL;
376 }
377 /* Save value */
378 array[count] = val;
379 count++;
380 /* Are we done? */
381 if (*endptr == 0) break;
382 TRACE_INFO_STRING("End pointer after conversion",endptr);
383 /* Advance to the next valid number */
384 for (str = endptr; *str; str++) {
385 if (isdigit(*str) || (*str == '-') || (*str == '+') ||
386 /* It is ok to do this since the string is null terminated */
387 ((*str == *(loc->decimal_point)) && isdigit(str[1]))) break;
388 }
389 }
390
391 *size = count;
392 if (error) *error = EOK;
393
394 TRACE_FLOW_EXIT();
395 return array;
396
397 }
398
399 /* Special function to free long config array */
400 void ini_free_long_config_array(long *array)
401 {
402 free(array);
403 }
404
405 /* Special function to free double config array */
406 void ini_free_double_config_array(double *array)
407 {
408 free(array);
409 }