Debian Codegraph @

ini/ini_configmod.c

raw symbols SCIP
1 /*
2 INI LIBRARY
3
4 Implementation of the modification interface.
5
6 Copyright (C) Dmitri Pal <[email protected]> 2014
7
8 INI Library is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 INI Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with INI Library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "config.h"
23 #include <errno.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <inttypes.h>
27 #include "trace.h"
28 #include "ref_array.h"
29 #include "simplebuffer.h"
30 #include "collection.h"
31 #include "ini_comment.h"
32 #include "ini_defines.h"
33 #include "ini_valueobj.h"
34 #include "ini_configobj.h"
35 #include "ini_config_priv.h"
36 #include "ini_configmod.h"
37
38 /* Which kind of search we should use? */
39 #define EXACT(a) ((a == INI_VA_MOD_E) || (a == INI_VA_MODADD_E)) ? 1 : 0
40
41 static void cb(const char *property,
42 int property_len,
43 int type,
44 void *data,
45 int length,
46 void *ext_data);
47
48
49 /* Delete value by key or position */
50 int ini_config_delete_value(struct ini_cfgobj *ini_config,
51 const char *section,
52 int position,
53 const char *key,
54 int idx)
55 {
56 int error = EOK;
57 struct value_obj *vo = NULL;
58 struct collection_item *item = NULL;
59
60 TRACE_FLOW_ENTRY();
61
62 /* Check arguments */
63 if (!ini_config) {
64 TRACE_ERROR_STRING("Invalid argument","ini_config");
65 return EINVAL;
66 }
67
68 if (!section) {
69 TRACE_ERROR_STRING("Invalid argument","section");
70 return EINVAL;
71 }
72
73 if (!key) {
74 TRACE_ERROR_STRING("Invalid argument","key");
75 return EINVAL;
76 }
77
78 if (idx < 0) {
79 TRACE_ERROR_STRING("Invalid argument","idx");
80 return EINVAL;
81 }
82
83 error = col_extract_item(ini_config->cfg,
84 section,
85 position,
86 key,
87 idx,
88 COL_TYPE_ANY,
89 &item);
90 if (error) {
91 TRACE_ERROR_NUMBER("Item not found or error",
92 error);
93 return error;
94 }
95
96 vo = *((struct value_obj **)(col_get_item_data(item)));
97 value_destroy(vo);
98
99 col_delete_item(item);
100
101 TRACE_FLOW_EXIT();
102 return error;
103 }
104
105
106 /* Updates a comment for value that is found by seaching for a specific key */
107 int ini_config_update_comment(struct ini_cfgobj *ini_config,
108 const char *section,
109 const char *key,
110 const char *comments[],
111 size_t count_comment,
112 int idx)
113 {
114 int error = EOK;
115 struct ini_comment *ic = NULL;
116 struct value_obj *vo = NULL;
117 struct collection_item *item = NULL;
118
119 TRACE_FLOW_ENTRY();
120
121 /* Check arguments */
122 if (!ini_config) {
123 TRACE_ERROR_STRING("Invalid argument","ini_config");
124 return EINVAL;
125 }
126
127 if (!section) {
128 TRACE_ERROR_STRING("Invalid argument","section");
129 return EINVAL;
130 }
131
132 if (!key) {
133 TRACE_ERROR_STRING("Invalid argument","key");
134 return EINVAL;
135 }
136
137 if (idx < 0) {
138 TRACE_ERROR_STRING("Invalid argument","idx");
139 return EINVAL;
140 }
141
142 /* Look for the exact item */
143 error = col_get_dup_item(ini_config->cfg,
144 section,
145 key,
146 COL_TYPE_ANY,
147 idx,
148 1,
149 &item);
150
151 if (error) {
152 TRACE_ERROR_NUMBER("Item not found or error",
153 error);
154 return error;
155 }
156
157 /* If item not found return error */
158 if (!item) {
159 TRACE_ERROR_NUMBER("Item not found.", ENOENT);
160 return ENOENT;
161 }
162
163 /* Build comment */
164 if (comments) {
165 error = ini_comment_construct(comments,
166 count_comment,
167 &ic);
168 if (error) {
169 TRACE_ERROR_NUMBER("Failed to construct comment", error);
170 return error;
171 }
172 }
173
174 vo = *((struct value_obj **)(col_get_item_data(item)));
175
176 /* Replace comment with the new one. Old one is freed by the function */
177 error = value_put_comment(vo, ic);
178 if (error) {
179 TRACE_ERROR_NUMBER("Faile to update comment.",
180 error);
181 ini_comment_destroy(ic);
182 return error;
183 }
184
185 TRACE_FLOW_EXIT();
186 return EOK;
187 }
188
189 /* Add or modify a value that stores array of integers */
190 int ini_config_add_int_arr_value(struct ini_cfgobj *ini_config,
191 const char *section,
192 const char *key,
193 int *value_int_arr,
194 size_t count_int,
195 char sep,
196 const char *comments[],
197 size_t count_comment,
198 int border,
199 int position,
200 const char *other_key,
201 int idx,
202 enum INI_VA flags)
203 {
204 int error = EOK;
205 int ret = 0;
206 char **str_arr = NULL;
207 size_t i = 0;
208 size_t j = 0;
209
210 TRACE_FLOW_ENTRY();
211
212 if (!count_int) {
213 TRACE_ERROR_STRING("Invalid argument","count_int");
214 return EINVAL;
215 }
216
217 str_arr = calloc(count_int, sizeof(char *));
218 if (!str_arr) {
219 TRACE_ERROR_STRING("Failed to allocate memory for string array",
220 ENOMEM);
221 return ENOMEM;
222 }
223
224 for (i = 0; i < count_int; i++) {
225 ret = asprintf(&str_arr[i], "%d", value_int_arr[i]);
226 if (ret == -1) {
227 TRACE_ERROR_NUMBER("Asprintf failed.", ret);
228 /* The main reason is propbaly memory allocation */
229 for (j = 0; j < i; j++) free(str_arr[j]);
230 free(str_arr);
231 return ENOMEM;
232 }
233 }
234
235 error = ini_config_add_str_arr_value(ini_config,
236 section,
237 key,
238 str_arr,
239 count_int,
240 sep,
241 comments,
242 count_comment,
243 border,
244 position,
245 other_key,
246 idx,
247 flags);
248
249 for (i = 0; i < count_int; i++) free(str_arr[i]);
250 free(str_arr);
251
252 TRACE_FLOW_RETURN(error);
253 return error;
254 }
255
256 /* Add or modify a value that stores array of long ints */
257 int ini_config_add_long_arr_value(struct ini_cfgobj *ini_config,
258 const char *section,
259 const char *key,
260 long *value_long_arr,
261 size_t count_long,
262 char sep,
263 const char *comments[],
264 size_t count_comment,
265 int border,
266 int position,
267 const char *other_key,
268 int idx,
269 enum INI_VA flags)
270 {
271 int error = EOK;
272 int ret = 0;
273 char **str_arr = NULL;
274 size_t i = 0;
275 size_t j = 0;
276
277 TRACE_FLOW_ENTRY();
278
279 if (!count_long) {
280 TRACE_ERROR_STRING("Invalid argument","count_long");
281 return EINVAL;
282 }
283
284 str_arr = calloc(count_long, sizeof(char *));
285 if (!str_arr) {
286 TRACE_ERROR_STRING("Failed to allocate memory for string array",
287 ENOMEM);
288 return ENOMEM;
289 }
290
291 for (i = 0; i < count_long; i++) {
292 ret = asprintf(&str_arr[i], "%ld", value_long_arr[i]);
293 if (ret == -1) {
294 TRACE_ERROR_NUMBER("Asprintf failed.", ret);
295 /* The main reason is propbaly memory allocation */
296 for (j = 0; j < i; j++) free(str_arr[j]);
297 free(str_arr);
298 return ENOMEM;
299 }
300 }
301
302 error = ini_config_add_str_arr_value(ini_config,
303 section,
304 key,
305 str_arr,
306 count_long,
307 sep,
308 comments,
309 count_comment,
310 border,
311 position,
312 other_key,
313 idx,
314 flags);
315
316 for (i = 0; i < count_long; i++) free(str_arr[i]);
317 free(str_arr);
318
319 TRACE_FLOW_RETURN(error);
320 return error;
321 }
322
323 /* Add or modify a value that stores array of doubles */
324 int ini_config_add_double_arr_value(struct ini_cfgobj *ini_config,
325 const char *section,
326 const char *key,
327 double *value_double_arr,
328 size_t count_double,
329 char sep,
330 const char *comments[],
331 size_t count_comment,
332 int border,
333 int position,
334 const char *other_key,
335 int idx,
336 enum INI_VA flags)
337 {
338 int error = EOK;
339 int ret = 0;
340 char **str_arr = NULL;
341 size_t i = 0;
342 size_t j = 0;
343
344 TRACE_FLOW_ENTRY();
345
346 if (!count_double) {
347 TRACE_ERROR_STRING("Invalid argument","count_double");
348 return EINVAL;
349 }
350
351 str_arr = calloc(count_double, sizeof(char *));
352 if (!str_arr) {
353 TRACE_ERROR_STRING("Failed to allocate memory for string array",
354 ENOMEM);
355 return ENOMEM;
356 }
357
358 for (i = 0; i < count_double; i++) {
359 ret = asprintf(&str_arr[i], "%f", value_double_arr[i]);
360 if (ret == -1) {
361 TRACE_ERROR_NUMBER("Asprintf failed.", ret);
362 /* The main reason is propbaly memory allocation */
363 for (j = 0; j < i; j++) free(str_arr[j]);
364 free(str_arr);
365 return ENOMEM;
366 }
367 }
368
369 error = ini_config_add_str_arr_value(ini_config,
370 section,
371 key,
372 str_arr,
373 count_double,
374 sep,
375 comments,
376 count_comment,
377 border,
378 position,
379 other_key,
380 idx,
381 flags);
382
383 for (i = 0; i < count_double; i++) free(str_arr[i]);
384 free(str_arr);
385
386 TRACE_FLOW_RETURN(error);
387 return error;
388 }
389
390 /* Add or modify a value that stores array of strings */
391 int ini_config_add_const_str_arr_value(struct ini_cfgobj *ini_config,
392 const char *section,
393 const char *key,
394 const char *value_str_arr[],
395 size_t count_str,
396 char sep,
397 const char *comments[],
398 size_t count_comment,
399 int border,
400 int position,
401 const char *other_key,
402 int idx,
403 enum INI_VA flags)
404 {
405 int error = EOK;
406 size_t len = 0;
407 size_t i = 0;
408 struct simplebuffer *sbobj = NULL;
409 char sp[3] = " ";
410
411 TRACE_FLOW_ENTRY();
412
413 if (!count_str) {
414 TRACE_ERROR_STRING("Invalid argument","count_str");
415 return EINVAL;
416 }
417
418 error = simplebuffer_alloc(&sbobj);
419 if (error) {
420 TRACE_ERROR_NUMBER("Failed to allocate dynamic string.", error);
421 return error;
422 }
423
424 sp[0] = sep;
425
426 for (i = 0; i < count_str - 1; i++) {
427 len = strlen(value_str_arr[i]);
428 if ((error = simplebuffer_add_str(sbobj,
429 value_str_arr[i],
430 len,
431 INI_VALUE_BLOCK)) ||
432 (error = simplebuffer_add_str(sbobj,
433 sp,
434 2,
435 INI_VALUE_BLOCK))) {
436 TRACE_ERROR_NUMBER("String append failed.", error);
437 simplebuffer_free(sbobj);
438 return error;
439 }
440 }
441 len = strlen(value_str_arr[count_str - 1]);
442 error = simplebuffer_add_str(sbobj,
443 value_str_arr[count_str - 1],
444 len,
445 INI_VALUE_BLOCK);
446 if (error) {
447 TRACE_ERROR_NUMBER("String append failed.", error);
448 simplebuffer_free(sbobj);
449 return error;
450 }
451
452 error = ini_config_add_str_value(ini_config,
453 section,
454 key,
455 (const char *)simplebuffer_get_buf(sbobj),
456 comments,
457 count_comment,
458 border,
459 position,
460 other_key,
461 idx,
462 flags);
463
464 simplebuffer_free(sbobj);
465
466 TRACE_FLOW_RETURN(error);
467 return error;
468 }
469
470 /* Add or modify a value that stores array of strings */
471 int ini_config_add_str_arr_value(struct ini_cfgobj *ini_config,
472 const char *section,
473 const char *key,
474 char *value_str_arr[],
475 size_t count_str,
476 char sep,
477 const char *comments[],
478 size_t count_comment,
479 int border,
480 int position,
481 const char *other_key,
482 int idx,
483 enum INI_VA flags)
484 {
485 const char **const_str_arr = (const char **)(intptr_t)value_str_arr;
486
487 return ini_config_add_const_str_arr_value(ini_config,
488 section,
489 key,
490 const_str_arr,
491 count_str,
492 sep,
493 comments,
494 count_comment,
495 border,
496 position,
497 other_key,
498 idx,
499 flags);
500 }
501
502 /* Function to add integer value */
503 int ini_config_add_int_value(struct ini_cfgobj *ini_config,
504 const char *section,
505 const char *key,
506 int value,
507 const char **comments,
508 size_t count_comment,
509 int border,
510 int position,
511 const char *other_key,
512 int idx,
513 enum INI_VA flags)
514 {
515 int error = EOK;
516 int ret = 0;
517 char *strval = NULL;
518
519 TRACE_FLOW_ENTRY();
520
521 ret = asprintf(&strval, "%d", value);
522 if (ret == -1) {
523 TRACE_ERROR_NUMBER("Asprintf failed.", ret);
524 /* The main reason is propbaly memory allocation */
525 return ENOMEM;
526 }
527
528 /* Call string function */
529 error = ini_config_add_str_value(ini_config,
530 section,
531 key,
532 strval,
533 comments,
534 count_comment,
535 border,
536 position,
537 other_key,
538 idx,
539 flags);
540 TRACE_FLOW_RETURN(error);
541 free(strval);
542 return error;
543 }
544
545
546 /* Function to add long value */
547 int ini_config_add_long_value(struct ini_cfgobj *ini_config,
548 const char *section,
549 const char *key,
550 long value,
551 const char **comments,
552 size_t count_comment,
553 int border,
554 int position,
555 const char *other_key,
556 int idx,
557 enum INI_VA flags)
558 {
559 int error = EOK;
560 int ret = 0;
561 char *strval = NULL;
562
563 TRACE_FLOW_ENTRY();
564
565 ret = asprintf(&strval, "%ld", value);
566 if (ret == -1) {
567 TRACE_ERROR_NUMBER("Asprintf failed.", ret);
568 /* The main reason is propbaly memory allocation */
569 return ENOMEM;
570 }
571
572 /* Call string function */
573 error = ini_config_add_str_value(ini_config,
574 section,
575 key,
576 strval,
577 comments,
578 count_comment,
579 border,
580 position,
581 other_key,
582 idx,
583 flags);
584 TRACE_FLOW_RETURN(error);
585 free(strval);
586 return error;
587 }
588
589
590 /* Function to add ulong value */
591 int ini_config_add_ulong_value(struct ini_cfgobj *ini_config,
592 const char *section,
593 const char *key,
594 unsigned long value,
595 const char **comments,
596 size_t count_comment,
597 int border,
598 int position,
599 const char *other_key,
600 int idx,
601 enum INI_VA flags)
602 {
603 int error = EOK;
604 int ret = 0;
605 char *strval = NULL;
606
607 TRACE_FLOW_ENTRY();
608
609 ret = asprintf(&strval, "%lu", value);
610 if (ret == -1) {
611 TRACE_ERROR_NUMBER("Asprintf failed.", ret);
612 /* The main reason is propbaly memory allocation */
613 return ENOMEM;
614 }
615
616 /* Call string function */
617 error = ini_config_add_str_value(ini_config,
618 section,
619 key,
620 strval,
621 comments,
622 count_comment,
623 border,
624 position,
625 other_key,
626 idx,
627 flags);
628 TRACE_FLOW_RETURN(error);
629 free(strval);
630 return error;
631 }
632
633
634 /* Function to add unsigned value */
635 int ini_config_add_unsigned_value(struct ini_cfgobj *ini_config,
636 const char *section,
637 const char *key,
638 unsigned value,
639 const char **comments,
640 size_t count_comment,
641 int border,
642 int position,
643 const char *other_key,
644 int idx,
645 enum INI_VA flags)
646 {
647 int error = EOK;
648 int ret = 0;
649 char *strval = NULL;
650
651 TRACE_FLOW_ENTRY();
652
653 ret = asprintf(&strval, "%u", value);
654 if (ret == -1) {
655 TRACE_ERROR_NUMBER("Asprintf failed.", ret);
656 /* The main reason is propbaly memory allocation */
657 return ENOMEM;
658 }
659
660 /* Call string function */
661 error = ini_config_add_str_value(ini_config,
662 section,
663 key,
664 strval,
665 comments,
666 count_comment,
667 border,
668 position,
669 other_key,
670 idx,
671 flags);
672 TRACE_FLOW_RETURN(error);
673 free(strval);
674 return error;
675 }
676
677 /* Function to add int32 value */
678 int ini_config_add_int32_value(struct ini_cfgobj *ini_config,
679 const char *section,
680 const char *key,
681 int32_t value,
682 const char **comments,
683 size_t count_comment,
684 int border,
685 int position,
686 const char *other_key,
687 int idx,
688 enum INI_VA flags)
689 {
690 int error = EOK;
691 int ret = 0;
692 char *strval = NULL;
693
694 TRACE_FLOW_ENTRY();
695
696 ret = asprintf(&strval, "%"PRId32, value);
697 if (ret == -1) {
698 TRACE_ERROR_NUMBER("Asprintf failed.", ret);
699 /* The main reason is propbaly memory allocation */
700 return ENOMEM;
701 }
702
703 /* Call string function */
704 error = ini_config_add_str_value(ini_config,
705 section,
706 key,
707 strval,
708 comments,
709 count_comment,
710 border,
711 position,
712 other_key,
713 idx,
714 flags);
715 TRACE_FLOW_RETURN(error);
716 free(strval);
717 return error;
718 }
719
720 /* Function to add uint32 value */
721 int ini_config_add_uint32_value(struct ini_cfgobj *ini_config,
722 const char *section,
723 const char *key,
724 uint32_t value,
725 const char **comments,
726 size_t count_comment,
727 int border,
728 int position,
729 const char *other_key,
730 int idx,
731 enum INI_VA flags)
732 {
733 int error = EOK;
734 int ret = 0;
735 char *strval = NULL;
736
737 TRACE_FLOW_ENTRY();
738
739 ret = asprintf(&strval, "%"PRIu32, value);
740 if (ret == -1) {
741 TRACE_ERROR_NUMBER("Asprintf failed.", ret);
742 /* The main reason is propbaly memory allocation */
743 return ENOMEM;
744 }
745
746 /* Call string function */
747 error = ini_config_add_str_value(ini_config,
748 section,
749 key,
750 strval,
751 comments,
752 count_comment,
753 border,
754 position,
755 other_key,
756 idx,
757 flags);
758 TRACE_FLOW_RETURN(error);
759 free(strval);
760 return error;
761 }
762
763 /* Function to add int64 value */
764 int ini_config_add_int64_value(struct ini_cfgobj *ini_config,
765 const char *section,
766 const char *key,
767 int64_t value,
768 const char **comments,
769 size_t count_comment,
770 int border,
771 int position,
772 const char *other_key,
773 int idx,
774 enum INI_VA flags)
775 {
776 int error = EOK;
777 int ret = 0;
778 char *strval = NULL;
779
780 TRACE_FLOW_ENTRY();
781
782 ret = asprintf(&strval, "%"PRId64, value);
783 if (ret == -1) {
784 TRACE_ERROR_NUMBER("Asprintf failed.", ret);
785 /* The main reason is propbaly memory allocation */
786 return ENOMEM;
787 }
788
789 /* Call string function */
790 error = ini_config_add_str_value(ini_config,
791 section,
792 key,
793 strval,
794 comments,
795 count_comment,
796 border,
797 position,
798 other_key,
799 idx,
800 flags);
801 TRACE_FLOW_RETURN(error);
802 free(strval);
803 return error;
804 }
805
806 /* Function to add uint64 value */
807 int ini_config_add_uint64_value(struct ini_cfgobj *ini_config,
808 const char *section,
809 const char *key,
810 uint64_t value,
811 const char **comments,
812 size_t count_comment,
813 int border,
814 int position,
815 const char *other_key,
816 int idx,
817 enum INI_VA flags)
818 {
819 int error = EOK;
820 int ret = 0;
821 char *strval = NULL;
822
823 TRACE_FLOW_ENTRY();
824
825 ret = asprintf(&strval, "%"PRIu64, value);
826 if (ret == -1) {
827 TRACE_ERROR_NUMBER("Asprintf failed.", ret);
828 /* The main reason is propbaly memory allocation */
829 return ENOMEM;
830 }
831
832 /* Call string function */
833 error = ini_config_add_str_value(ini_config,
834 section,
835 key,
836 strval,
837 comments,
838 count_comment,
839 border,
840 position,
841 other_key,
842 idx,
843 flags);
844 TRACE_FLOW_RETURN(error);
845 free(strval);
846 return error;
847 }
848
849
850 /* Function to add double value */
851 int ini_config_add_double_value(struct ini_cfgobj *ini_config,
852 const char *section,
853 const char *key,
854 double value,
855 const char **comments,
856 size_t count_comment,
857 int border,
858 int position,
859 const char *other_key,
860 int idx,
861 enum INI_VA flags)
862 {
863 int error = EOK;
864 int ret = 0;
865 char *strval = NULL;
866
867 TRACE_FLOW_ENTRY();
868
869 ret = asprintf(&strval, "%f", value);
870 if (ret == -1) {
871 TRACE_ERROR_NUMBER("Asprintf failed.", ret);
872 /* The main reason is propbaly memory allocation */
873 return ENOMEM;
874 }
875
876 /* Call string function */
877 error = ini_config_add_str_value(ini_config,
878 section,
879 key,
880 strval,
881 comments,
882 count_comment,
883 border,
884 position,
885 other_key,
886 idx,
887 flags);
888 TRACE_FLOW_RETURN(error);
889 free(strval);
890 return error;
891 }
892
893 /* Function to add binary value */
894 int ini_config_add_bin_value(struct ini_cfgobj *ini_config,
895 const char *section,
896 const char *key,
897 void *value,
898 size_t value_len,
899 const char **comments,
900 size_t count_comment,
901 int border,
902 int position,
903 const char *other_key,
904 int idx,
905 enum INI_VA flags)
906 {
907 int error = EOK;
908 size_t i;
909 char *strval = NULL;
910 char *ptr = NULL;
911
912 TRACE_FLOW_ENTRY();
913
914 /* Do we have the vo ? */
915 if ((!value) && (value_len)) {
916 TRACE_ERROR_NUMBER("Invalid argument.", EINVAL);
917 return EINVAL;
918 }
919
920 /* The value is good so we can allocate memory for it */
921 strval = malloc(value_len * 2 + 3);
922 if (strval == NULL) {
923 TRACE_ERROR_NUMBER("Failed to allocate memory.", ENOMEM);
924 return ENOMEM;
925 }
926
927 strval[0] = '\'';
928
929 /* Convert the value */
930 ptr = strval + 1;
931 for (i = 0; i < value_len; i++) {
932 sprintf(ptr, "%02x", *((unsigned char *)(value) + i));
933 ptr += 2;
934 }
935
936 strval[value_len * 2 + 1] = '\'';
937 strval[value_len * 2 + 2] = '\0';
938
939 /* Call string function */
940 error = ini_config_add_str_value(ini_config,
941 section,
942 key,
943 strval,
944 comments,
945 count_comment,
946 border,
947 position,
948 other_key,
949 idx,
950 flags);
951 free(strval);
952 TRACE_FLOW_RETURN(error);
953 return error;
954 }
955
956
957 /* Function to add string value */
958 int ini_config_add_str_value(struct ini_cfgobj *ini_config,
959 const char *section,
960 const char *key,
961 const char *value,
962 const char **comments,
963 size_t count_comment,
964 int border,
965 int position,
966 const char *other_key,
967 int idx,
968 enum INI_VA flags)
969 {
970 int error = EOK;
971 struct ini_comment *ic = NULL;
972 struct value_obj *vo = NULL;
973 struct value_obj *old_vo = NULL;
974 struct collection_item *item = NULL;
975 const char sp_key[] = INI_SECTION_KEY;
976 const char *key_ptr;
977
978 TRACE_FLOW_ENTRY();
979
980 /* Check arguments */
981 if (!ini_config) {
982 TRACE_ERROR_STRING("Invalid argument","ini_config");
983 return EINVAL;
984 }
985
986 if (!section) {
987 TRACE_ERROR_STRING("Invalid argument","section");
988 return EINVAL;
989 }
990
991 if (!key) {
992 TRACE_ERROR_STRING("Invalid argument","key");
993 return EINVAL;
994 }
995
996 if (!value) {
997 TRACE_ERROR_STRING("Invalid argument","value");
998 return EINVAL;
999 }
1000
1001 if (idx < 0) {
1002 TRACE_ERROR_STRING("Invalid argument","idx");
1003 return EINVAL;
1004 }
1005
1006
1007 switch (flags) {
1008
1009 case INI_VA_NOCHECK: /* Just fall through */
1010 break;
1011
1012 case INI_VA_MOD:
1013 case INI_VA_MOD_E:
1014 /* Find the value by index.
1015 * If value is not found return error.
1016 */
1017 error = col_get_dup_item(ini_config->cfg,
1018 section,
1019 key,
1020 COL_TYPE_ANY,
1021 idx,
1022 EXACT(flags),
1023 &item);
1024 if (error) {
1025 TRACE_ERROR_NUMBER("Error "
1026 "looking for item.",
1027 error);
1028 return error;
1029 }
1030 break;
1031
1032
1033 case INI_VA_MODADD:
1034 error = col_get_dup_item(ini_config->cfg,
1035 section,
1036 key,
1037 COL_TYPE_ANY,
1038 idx,
1039 0, /* no exact */
1040 &item);
1041 if ((error) && (error != ENOENT)) {
1042 TRACE_ERROR_NUMBER("Unexpected error "
1043 "looking for item.",
1044 error);
1045 return error;
1046 }
1047 break;
1048 case INI_VA_MODADD_E:
1049 /* Find the value by index.
1050 * If value is not found it is OK.
1051 */
1052 error = col_get_dup_item(ini_config->cfg,
1053 section,
1054 key,
1055 COL_TYPE_ANY,
1056 idx,
1057 1, /* use exact */
1058 &item);
1059 switch (error) {
1060 case EOK: /* we matched entry even with exact
1061 * just return it. */
1062 break;
1063 case ENOENT: /* Entry can be missing or index was
1064 * to big. Try one more time without
1065 * exact flag */
1066 error = col_get_dup_item(ini_config->cfg,
1067 section,
1068 key,
1069 COL_TYPE_ANY,
1070 idx,
1071 0,
1072 &item);
1073 if (error == EOK) {
1074 /* index was to big */
1075 return ENOENT;
1076 } else if (error != EOK && error != ENOENT) {
1077 TRACE_ERROR_NUMBER("Unexpected error "
1078 "looking for item.",
1079 error);
1080 return error;
1081 }
1082 break;
1083 default:
1084 TRACE_ERROR_NUMBER("Unexpected error "
1085 "looking for item.",
1086 error);
1087 return error;
1088 }
1089 break;
1090
1091
1092
1093 case INI_VA_DUPERROR: /* Find any instance */
1094 error = col_get_dup_item(ini_config->cfg,
1095 section,
1096 key,
1097 COL_TYPE_ANY,
1098 0,
1099 0,
1100 &item);
1101 if ((error) && (error != ENOENT)) {
1102 TRACE_ERROR_NUMBER("Unexpected error "
1103 "looking for item.",
1104 error);
1105 return error;
1106 }
1107 if (!error) {
1108 TRACE_ERROR_NUMBER("Key exists "
1109 "this is error.",
1110 error);
1111 return EEXIST;
1112 }
1113 break;
1114 case INI_VA_CLEAN: /* Delete all instaces of the key first */
1115 while (!error) {
1116 error = col_remove_item_with_cb(
1117 ini_config->cfg,
1118 section,
1119 COL_DSP_FIRSTDUP,
1120 key,
1121 0,
1122 COL_TYPE_ANY,
1123 cb,
1124 NULL);
1125 if (error) {
1126 if (error != ENOENT) {
1127 TRACE_ERROR_NUMBER("Failed to clean "
1128 "the section.",
1129 error);
1130 return error;
1131 }
1132 else break;
1133 }
1134 }
1135 break;
1136 default: /* The new ones should be added here */
1137 TRACE_ERROR_NUMBER("Flag is not implemented",
1138 ENOSYS);
1139 return ENOSYS;
1140 }
1141
1142 /* Start with the comment */
1143 if (comments) {
1144 error = ini_comment_construct(comments,
1145 count_comment,
1146 &ic);
1147 if (error) {
1148 TRACE_ERROR_NUMBER("Failed to construct comment", error);
1149 return error;
1150 }
1151 }
1152
1153 /* Create value object */
1154 error = value_create_new(value,
1155 strnlen(value, MAX_VALUE -1),
1156 INI_VALUE_CREATED,
1157 strnlen(key, MAX_KEY -1),
1158 border,
1159 ic,
1160 &vo);
1161 if (error) {
1162 TRACE_ERROR_NUMBER("Failed to construct value object.", error);
1163 ini_comment_destroy(ic);
1164 return error;
1165 }
1166
1167 if (item) {
1168 /* If we have selected item remove old value */
1169 old_vo = *((struct value_obj **)(col_get_item_data(item)));
1170 value_destroy(old_vo);
1171 /* Update the item with the new value */
1172 error = col_modify_binary_item(item,
1173 NULL,
1174 &vo,
1175 sizeof(struct value_obj *));
1176 if (error) {
1177 TRACE_ERROR_NUMBER("Failed to update item.", error);
1178 value_destroy(vo);
1179 return error;
1180 }
1181 }
1182 else {
1183 if (position == COL_DSP_FRONT) {
1184 key_ptr = sp_key;
1185 position = COL_DSP_AFTER;
1186 }
1187 else {
1188 key_ptr = other_key;
1189 }
1190 /* Add value to collection */
1191 error = col_insert_binary_property(ini_config->cfg,
1192 section,
1193 position,
1194 key_ptr,
1195 idx,
1196 flags,
1197 key,
1198 &vo,
1199 sizeof(struct value_obj *));
1200 }
1201 if (error) {
1202 TRACE_ERROR_NUMBER("Failed to insert value.", error);
1203 value_destroy(vo);
1204 return error;
1205 }
1206
1207 TRACE_FLOW_EXIT();
1208 return EOK;
1209 }
1210
1211 /* Function to add section */
1212 int ini_config_add_section(struct ini_cfgobj *ini_config,
1213 const char *section,
1214 const char *comments[],
1215 size_t count_comment,
1216 int position,
1217 const char *other_section,
1218 int idx)
1219 {
1220 int error = EOK;
1221 struct ini_comment *ic = NULL;
1222 struct value_obj *vo = NULL;
1223 struct collection_item *item = NULL;
1224 struct collection_item *sec = NULL;
1225
1226 TRACE_FLOW_ENTRY();
1227
1228 /* Check arguments */
1229 if (!ini_config) {
1230 TRACE_ERROR_STRING("Invalid argument","ini_config");
1231 return EINVAL;
1232 }
1233
1234 if (!section) {
1235 TRACE_ERROR_STRING("Invalid argument","section");
1236 return EINVAL;
1237 }
1238
1239 if (position > COL_DSP_INDEX) {
1240 TRACE_ERROR_STRING("Invalid argument","position");
1241 return EINVAL;
1242 }
1243
1244 if (idx < 0) {
1245 TRACE_ERROR_STRING("Invalid argument","idx");
1246 return EINVAL;
1247 }
1248
1249 /* Check if section exists */
1250 error = col_get_item(ini_config->cfg,
1251 section,
1252 COL_TYPE_COLLECTIONREF,
1253 COL_TRAVERSE_ONELEVEL,
1254 &item);
1255 if (error) {
1256 TRACE_ERROR_NUMBER("Search for section failed.", error);
1257 return error;
1258 }
1259
1260 if (item) {
1261 TRACE_ERROR_STRING("Section already exists.", section);
1262 return EEXIST;
1263 }
1264
1265 /* Create a new section */
1266 error = col_create_collection(&sec,
1267 section,
1268 COL_CLASS_INI_SECTION);
1269 if (error) {
1270 TRACE_ERROR_NUMBER("Failed to create a section", error);
1271 return error;
1272 }
1273
1274 /* Process comment */
1275 if (comments) {
1276 error = ini_comment_construct(comments,
1277 count_comment,
1278 &ic);
1279 if (error) {
1280 TRACE_ERROR_NUMBER("Failed to construct comment", error);
1281 col_destroy_collection(sec);
1282 return error;
1283 }
1284 }
1285
1286 /* Create value object */
1287 error = value_create_new(section,
1288 strnlen(section, MAX_VALUE -1),
1289 INI_VALUE_CREATED,
1290 strlen(INI_SECTION_KEY),
1291 INI_WRAP_BOUNDARY,
1292 ic,
1293 &vo);
1294 if (error) {
1295 TRACE_ERROR_NUMBER("Failed to construct value object.", error);
1296 ini_comment_destroy(ic);
1297 col_destroy_collection(sec);
1298 return error;
1299 }
1300 /* Comment is now a part of value - no need to clean it seprately. */
1301
1302 /* Add value to section collection */
1303 error = col_insert_binary_property(sec,
1304 NULL,
1305 COL_DSP_END,
1306 NULL,
1307 0,
1308 COL_INSERT_NOCHECK,
1309 INI_SECTION_KEY,
1310 &vo,
1311 sizeof(struct value_obj *));
1312 if (error) {
1313 TRACE_ERROR_NUMBER("Failed to add value object to section.", error);
1314 value_destroy(vo);
1315 col_destroy_collection(sec);
1316 return error;
1317 }
1318
1319 /* Embed section collection */
1320 /* Since there is no function to do addition of collection to
1321 * collection with disposition we will use a workaround.
1322 */
1323
1324 error = col_insert_property_with_ref(ini_config->cfg,
1325 NULL,
1326 position,
1327 other_section,
1328 idx,
1329 COL_INSERT_NOCHECK,
1330 section,
1331 COL_TYPE_COLLECTIONREF,
1332 (void *)(&sec),
1333 sizeof(struct collection_item **),
1334 NULL);
1335 if (error) {
1336 TRACE_ERROR_NUMBER("Failed to embed section", error);
1337 value_destroy(vo);
1338 col_destroy_collection(sec);
1339 return error;
1340 }
1341
1342 TRACE_FLOW_EXIT();
1343 return EOK;
1344 }
1345
1346 /* Function to add or remove comments for a section */
1347 int ini_config_comment_section(struct ini_cfgobj *ini_config,
1348 const char *section,
1349 const char *comments[],
1350 size_t count_comment)
1351 {
1352 int error = EOK;
1353 struct ini_comment *ic = NULL;
1354 struct value_obj *vo = NULL;
1355 struct collection_item *item = NULL;
1356 struct collection_item *sec = NULL;
1357
1358 TRACE_FLOW_ENTRY();
1359
1360 /* Check arguments */
1361 if (!ini_config) {
1362 TRACE_ERROR_STRING("Invalid argument","ini_config");
1363 return EINVAL;
1364 }
1365
1366 if (!section) {
1367 TRACE_ERROR_STRING("Invalid argument","section");
1368 return EINVAL;
1369 }
1370
1371 /* Get the section */
1372 error = col_get_item(ini_config->cfg,
1373 section,
1374 COL_TYPE_COLLECTIONREF,
1375 COL_TRAVERSE_ONELEVEL,
1376 &item);
1377 if (error) {
1378 TRACE_ERROR_NUMBER("Search for section failed.", error);
1379 return error;
1380 }
1381
1382 /* If item not found return error */
1383 if (!item) {
1384 TRACE_ERROR_NUMBER("Item not found.", ENOENT);
1385 return ENOENT;
1386 }
1387
1388 /* Item is actually a section reference */
1389 sec = *((struct collection_item **)col_get_item_data(item));
1390 item = NULL;
1391
1392 /* Now get the special item from the section collection */
1393 error = col_get_item(sec,
1394 INI_SECTION_KEY,
1395 COL_TYPE_ANY,
1396 COL_TRAVERSE_ONELEVEL,
1397 &item);
1398 if (error) {
1399 TRACE_ERROR_NUMBER("Search for section failed.", error);
1400 return error;
1401 }
1402
1403 /* If item not found return error */
1404 if (!item) {
1405 /* Something is really broken with the internal implementation
1406 * if we can't find the item, thus EINVAL.
1407 */
1408 TRACE_ERROR_NUMBER("Item not found.", EINVAL);
1409 return EINVAL;
1410 }
1411
1412 /* Item is actually a value object. */
1413 vo = *((struct value_obj **)(col_get_item_data(item)));
1414
1415 /* Build comment */
1416 if (comments) {
1417 error = ini_comment_construct(comments,
1418 count_comment,
1419 &ic);
1420 if (error) {
1421 TRACE_ERROR_NUMBER("Failed to construct comment", error);
1422 return error;
1423 }
1424 }
1425
1426 /* Replace comment with the new one. Old one is freed by the function */
1427 error = value_put_comment(vo, ic);
1428 if (error) {
1429 TRACE_ERROR_NUMBER("Faile to update comment.",
1430 error);
1431 ini_comment_destroy(ic);
1432 return error;
1433 }
1434
1435 TRACE_FLOW_EXIT();
1436 return EOK;
1437 }
1438
1439
1440 /* Function to rename section */
1441 int ini_config_rename_section(struct ini_cfgobj *ini_config,
1442 const char *section,
1443 const char *newname)
1444 {
1445 int error = EOK;
1446 struct collection_item *item = NULL;
1447 struct collection_item *sec = NULL;
1448
1449 TRACE_FLOW_ENTRY();
1450
1451 /* Check arguments */
1452 if (!ini_config) {
1453 TRACE_ERROR_STRING("Invalid argument","ini_config");
1454 return EINVAL;
1455 }
1456
1457 if (!section) {
1458 TRACE_ERROR_STRING("Invalid argument","section");
1459 return EINVAL;
1460 }
1461
1462 if (!newname) {
1463 TRACE_ERROR_STRING("Invalid argument","newname");
1464 return EINVAL;
1465 }
1466
1467 /* Get the section */
1468 error = col_get_item(ini_config->cfg,
1469 section,
1470 COL_TYPE_COLLECTIONREF,
1471 COL_TRAVERSE_ONELEVEL,
1472 &item);
1473 if (error) {
1474 TRACE_ERROR_NUMBER("Search for section failed.", error);
1475 return error;
1476 }
1477
1478 /* If item not found return error */
1479 if (!item) {
1480 TRACE_ERROR_NUMBER("Item not found.", ENOENT);
1481 return ENOENT;
1482 }
1483
1484 /* Item is actually a section reference */
1485 sec = *((struct collection_item **)col_get_item_data(item));
1486
1487 /* Change name only */
1488 error = col_modify_item(item,
1489 newname,
1490 0,
1491 NULL,
1492 0);
1493 if (error) {
1494 TRACE_ERROR_NUMBER("Failed to change section name.", error);
1495 return error;
1496 }
1497
1498 /* Change name of the embedded collection (for consistency) */
1499 error = col_modify_item(sec,
1500 newname,
1501 0,
1502 NULL,
1503 0);
1504 if (error) {
1505 TRACE_ERROR_NUMBER("Failed to change section name "
1506 "inside the embedded collection.", error);
1507 return error;
1508 }
1509
1510 TRACE_FLOW_EXIT();
1511 return EOK;
1512 }
1513
1514 /* Cleanup collback */
1515 static void cb(const char *property,
1516 int property_len,
1517 int type,
1518 void *data,
1519 int length,
1520 void *ext_data)
1521 {
1522 struct value_obj *vo;
1523 TRACE_FLOW_ENTRY();
1524
1525 if ((type == COL_TYPE_COLLECTIONREF) ||
1526 (type == COL_TYPE_COLLECTION)) return;
1527
1528 vo = *((struct value_obj **)(data));
1529 value_destroy(vo);
1530
1531 TRACE_FLOW_EXIT();
1532
1533 }
1534
1535 /* Function to delete section by name */
1536 int ini_config_delete_section_by_name(struct ini_cfgobj *ini_config,
1537 const char *section)
1538 {
1539 int error = EOK;
1540
1541 TRACE_FLOW_ENTRY();
1542 error = ini_config_delete_section_by_position(ini_config,
1543 COL_DSP_FIRSTDUP,
1544 section,
1545 0);
1546 if (error) {
1547 TRACE_ERROR_NUMBER("Search for section failed.", error);
1548 return error;
1549 }
1550
1551 TRACE_FLOW_EXIT();
1552 return EOK;
1553 }
1554
1555 /* Function to delete section by relative postion */
1556 int ini_config_delete_section_by_position(struct ini_cfgobj *ini_config,
1557 int position,
1558 const char *section,
1559 int idx)
1560 {
1561 int error = EOK;
1562 struct collection_item *item = NULL;
1563
1564 TRACE_FLOW_ENTRY();
1565
1566 /* Check arguments */
1567 if (!ini_config) {
1568 TRACE_ERROR_STRING("Invalid argument","ini_config");
1569 return EINVAL;
1570 }
1571
1572 if (!section) {
1573 TRACE_ERROR_STRING("Invalid argument","section");
1574 return EINVAL;
1575 }
1576
1577 if (idx < 0) {
1578 TRACE_ERROR_STRING("Invalid argument","idx");
1579 return EINVAL;
1580 }
1581
1582 /* Extract section */
1583 error = col_extract_item(ini_config->cfg,
1584 NULL,
1585 position,
1586 section,
1587 idx,
1588 COL_TYPE_ANY,
1589 &item);
1590 if (error) {
1591 TRACE_ERROR_NUMBER("Search for section failed.", error);
1592 return error;
1593 }
1594
1595 /* If item not found return error */
1596 if (!item) {
1597 TRACE_ERROR_NUMBER("Item not found.", ENOENT);
1598 return ENOENT;
1599 }
1600
1601 /* Delete item and subcollection */
1602 col_delete_item_with_cb(item, cb, NULL);
1603
1604 TRACE_FLOW_EXIT();
1605 return EOK;
1606 }