Debian Codegraph @

ini/ini_comment.c

raw symbols SCIP
1 /*
2 INI LIBRARY
3
4 Object to handle comments
5
6 Copyright (C) Dmitri Pal <[email protected]> 2010
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 <ctype.h>
26 #include "trace.h"
27 #include "ref_array.h"
28 #include "ini_configobj.h"
29 #include "ini_config_priv.h"
30 #include "simplebuffer.h"
31 #include "ini_comment.h"
32 #include "ini_defines.h"
33
34 /* The lines will increment in this number */
35 #define INI_COMMENT_BLOCK 10
36 /* Default comment length */
37 #define INI_COMMENT_LEN 100
38
39
40 /***************************/
41 /* Internal comment states */
42 /***************************/
43 /* Empty - initial */
44 #define INI_COMMENT_EMPTY 0
45 /* Read - read from file */
46 #define INI_COMMENT_READ 1
47 /* Comment was altered */
48 #define INI_COMMENT_CHANGED 2
49
50
51 /*********************************/
52 /* Modes to wrap ref array calls */
53 /*********************************/
54 #define INI_COMMENT_MODE_BUILD 1
55 #define INI_COMMENT_MODE_APPEND 2
56 #define INI_COMMENT_MODE_INSERT 3
57 #define INI_COMMENT_MODE_REPLACE 4
58 #define INI_COMMENT_MODE_REMOVE 5
59 #define INI_COMMENT_MODE_CLEAR 6
60
61 /****************************************/
62 /* Internal structure to hold a comment */
63 /****************************************/
64 struct ini_comment {
65 struct ref_array *ra;
66 uint32_t state;
67 };
68
69
70 /****************************************/
71
72 /* Destroy the comment object */
73 void ini_comment_destroy(struct ini_comment *ic)
74 {
75
76 TRACE_FLOW_ENTRY();
77 if (ic) {
78 /* Function will check for NULL */
79 ref_array_destroy(ic->ra);
80 free(ic);
81 }
82 TRACE_FLOW_EXIT();
83 }
84
85
86 /* Cleanup callback */
87 static void ini_comment_cb(void *elem,
88 ref_array_del_enum type,
89 void *data)
90 {
91
92 TRACE_FLOW_ENTRY();
93 simplebuffer_free(*((struct simplebuffer **)elem));
94 TRACE_FLOW_EXIT();
95 }
96
97
98 /* Create a comment object */
99 int ini_comment_create(struct ini_comment **ic)
100 {
101 int error = EOK;
102 struct ref_array *ra = NULL;
103 struct ini_comment *ic_new = NULL;
104
105 TRACE_FLOW_ENTRY();
106
107 error = ref_array_create(&ra,
108 sizeof(struct simplebuffer *),
109 INI_COMMENT_BLOCK,
110 ini_comment_cb,
111 NULL);
112 if (error) {
113 TRACE_ERROR_NUMBER("Error creating ref array", error);
114 return error;
115 }
116
117 ic_new = malloc(sizeof(struct ini_comment));
118 if (!ic_new) {
119 TRACE_ERROR_NUMBER("Memory allocation error", ENOMEM);
120 ref_array_destroy(ra);
121 return ENOMEM;
122 }
123
124 /* Initialize members here */
125 ic_new->ra = ra;
126 ic_new->state = INI_COMMENT_EMPTY;
127
128 *ic = ic_new;
129
130 TRACE_FLOW_EXIT();
131 return error;
132 }
133
134 /* Callback to copy comment */
135 static int ini_comment_copy_cb(void *elem,
136 void *new_elem)
137 {
138 int error = EOK;
139 struct simplebuffer *sb = NULL;
140 struct simplebuffer *sb_new = NULL;
141
142 TRACE_FLOW_ENTRY();
143
144 error = simplebuffer_alloc(&sb_new);
145 if (error) {
146 TRACE_ERROR_NUMBER("Failed to allocate buffer", error);
147 return error;
148 }
149
150 sb = *((struct simplebuffer **)elem);
151 error = simplebuffer_add_str(sb_new,
152 (const char *)simplebuffer_get_buf(sb),
153 simplebuffer_get_len(sb),
154 INI_COMMENT_LEN);
155 if (error) {
156 TRACE_ERROR_NUMBER("Failed to allocate buffer", error);
157 simplebuffer_free(sb_new);
158 return error;
159 }
160
161 *((struct simplebuffer **)new_elem) = sb_new;
162
163 TRACE_FLOW_EXIT();
164 return error;
165 }
166
167
168 /* Create a copy of the comment object */
169 int ini_comment_copy(struct ini_comment *ic,
170 struct ini_comment **ic_copy)
171 {
172 int error = EOK;
173 struct ref_array *ra = NULL;
174 struct ini_comment *ic_new = NULL;
175
176 TRACE_FLOW_ENTRY();
177
178 error = ref_array_copy(ic->ra,
179 ini_comment_copy_cb,
180 ini_comment_cb,
181 NULL,
182 &ra);
183 if (error) {
184 TRACE_ERROR_NUMBER("Error creating a copy of ref array", error);
185 return error;
186 }
187
188 ic_new = malloc(sizeof(struct ini_comment));
189 if (!ic_new) {
190 TRACE_ERROR_NUMBER("Memory allocation error", ENOMEM);
191 ref_array_destroy(ra);
192 return ENOMEM;
193 }
194
195 /* Initialize members here */
196 ic_new->ra = ra;
197 ic_new->state = ic->state;
198
199 *ic_copy = ic_new;
200
201 TRACE_FLOW_EXIT();
202 return error;
203 }
204
205 /*
206 * Modify the comment object
207 */
208 static int ini_comment_modify(struct ini_comment *ic,
209 int mode,
210 uint32_t idx,
211 const char *line,
212 uint32_t length)
213 {
214 int error = EOK;
215 struct simplebuffer *elem = NULL;
216 struct simplebuffer *empty = NULL;
217 char *input = NULL;
218
219 uint32_t i, len = 0;
220 uint32_t input_len = 0;
221
222 TRACE_FLOW_ENTRY();
223
224 if (!ic) {
225 TRACE_ERROR_NUMBER("Invalid comment object", EINVAL);
226 return EINVAL;
227 }
228
229
230 if (mode == INI_COMMENT_MODE_BUILD) {
231 /*
232 * Can use this function only if object is empty or
233 * reading from the file.
234 */
235 if ((ic->state != INI_COMMENT_EMPTY) &&
236 (ic->state != INI_COMMENT_READ)) {
237 TRACE_ERROR_NUMBER("Invalid use of the function", EINVAL);
238 return EINVAL;
239 }
240 }
241
242 /* Make sure that we ignore "line" in reset case */
243 if (mode != INI_COMMENT_MODE_CLEAR)
244 memcpy(&input, &line, sizeof(char *));
245
246 if (mode != INI_COMMENT_MODE_REMOVE) {
247
248 error = simplebuffer_alloc(&elem);
249 if (error) {
250 TRACE_ERROR_NUMBER("Allocate buffer for the comment", error);
251 return error;
252 }
253
254 if (input) {
255 if (length == 0) input_len = strlen(input);
256 else input_len = length;
257
258 error = simplebuffer_add_str(elem,
259 input,
260 input_len,
261 INI_COMMENT_LEN);
262 }
263 else {
264 error = simplebuffer_add_str(elem,
265 "",
266 0,
267 INI_COMMENT_LEN);
268 }
269
270 if (error) {
271 TRACE_ERROR_NUMBER("Allocate buffer for the comment", error);
272 simplebuffer_free(elem);
273 return error;
274 }
275 }
276
277 /* Do action depending on mode */
278 switch (mode) {
279 case INI_COMMENT_MODE_BUILD:
280
281 TRACE_INFO_STRING("BUILD mode", "");
282 error = ref_array_append(ic->ra, (void *)&elem);
283 if (error) {
284 TRACE_ERROR_NUMBER("Failed to append line to an array", error);
285 simplebuffer_free(elem);
286 return error;
287 }
288
289 break;
290
291 case INI_COMMENT_MODE_APPEND:
292
293 TRACE_INFO_STRING("Append mode", "");
294 error = ref_array_append(ic->ra, (void *)&elem);
295 if (error) {
296 TRACE_ERROR_NUMBER("Failed to append line to an array", error);
297 simplebuffer_free(elem);
298 return error;
299 }
300
301 break;
302
303 case INI_COMMENT_MODE_INSERT:
304
305 TRACE_INFO_STRING("Insert mode", "");
306 len = ref_array_len(ic->ra);
307 if (idx > len) {
308 /* Fill in empty lines */
309 for (i = 0; i < (idx-len); i++) {
310 error = simplebuffer_alloc(&empty);
311 if (error) {
312 TRACE_ERROR_NUMBER("Allocate buffer for the comment", error);
313 simplebuffer_free(elem);
314 return error;
315 }
316 error = simplebuffer_add_str(elem,
317 NULL,
318 0,
319 INI_COMMENT_LEN);
320 if (error) {
321 TRACE_ERROR_NUMBER("Make comment empty", error);
322 simplebuffer_free(empty);
323 simplebuffer_free(elem);
324 return error;
325 }
326 error = ref_array_append(ic->ra, (void *)&empty);
327 if (error) {
328 TRACE_ERROR_NUMBER("Append problem", error);
329 simplebuffer_free(empty);
330 simplebuffer_free(elem);
331 return error;
332 }
333 }
334 /* Append last line */
335 error = ref_array_append(ic->ra, (void *)&elem);
336 if (error) {
337 TRACE_ERROR_NUMBER("Failed to append last line", error);
338 simplebuffer_free(elem);
339 return error;
340 }
341 }
342 else {
343 /* Insert inside the array */
344 error = ref_array_insert(ic->ra, idx, (void *)&elem);
345 if (error) {
346 TRACE_ERROR_NUMBER("Failed to append last line", error);
347 simplebuffer_free(elem);
348 return error;
349 }
350
351 }
352 break;
353
354
355 case INI_COMMENT_MODE_REPLACE:
356
357 TRACE_INFO_STRING("Replace mode", "");
358 error = ref_array_replace(ic->ra, idx, (void *)&elem);
359 if (error) {
360 TRACE_ERROR_NUMBER("Failed to replace", error);
361 simplebuffer_free(elem);
362 return error;
363 }
364 break;
365
366 case INI_COMMENT_MODE_REMOVE:
367
368 TRACE_INFO_STRING("Remove mode", "");
369 error = ref_array_remove(ic->ra, idx);
370 if (error) {
371 TRACE_ERROR_NUMBER("Failed to remove", error);
372 return error;
373 }
374
375 break;
376
377 case INI_COMMENT_MODE_CLEAR:
378
379 TRACE_INFO_STRING("Clear mode", "");
380 error = ref_array_replace(ic->ra, idx, (void *)&elem);
381 if (error) {
382 TRACE_ERROR_NUMBER("Failed to replace", error);
383 simplebuffer_free(elem);
384 return error;
385 }
386 break;
387
388 default :
389
390 TRACE_ERROR_STRING("Coding error", "");
391 simplebuffer_free(elem);
392 return EINVAL;
393
394 }
395
396
397 /* Change state */
398 if (INI_COMMENT_MODE_BUILD) ic->state = INI_COMMENT_READ;
399 else ic->state = INI_COMMENT_CHANGED;
400
401
402 TRACE_FLOW_EXIT();
403 return error;
404 }
405
406 /*
407 * Build up a comment object - use this when reading
408 * comments from a file.
409 */
410 int ini_comment_build(struct ini_comment *ic, const char *line)
411 {
412 int error = EOK;
413
414 TRACE_FLOW_ENTRY();
415
416 error = ini_comment_modify(ic, INI_COMMENT_MODE_BUILD, 0, line, 0);
417
418 TRACE_FLOW_NUMBER("ini_comment_build - Returning", error);
419 return error;
420 }
421
422 /*
423 * Build up a comment object - use this when reading
424 * comments from a file.
425 */
426 int ini_comment_build_wl(struct ini_comment *ic,
427 const char *line,
428 uint32_t length)
429 {
430 int error = EOK;
431
432 TRACE_FLOW_ENTRY();
433
434 error = ini_comment_modify(ic, INI_COMMENT_MODE_BUILD, 0, line, length);
435
436 TRACE_FLOW_NUMBER("ini_comment_build - Returning", error);
437 return error;
438 }
439
440 /*
441 * Modify comment by instering a line.
442 */
443 int ini_comment_insert(struct ini_comment *ic,
444 uint32_t idx,
445 const char *line)
446 {
447 int error = EOK;
448
449 TRACE_FLOW_ENTRY();
450
451 error = ini_comment_modify(ic, INI_COMMENT_MODE_INSERT, idx, line, 0);
452
453 TRACE_FLOW_NUMBER("ini_comment_insert - Returning", error);
454 return error;
455 }
456
457 /* Modify comment by appending a line. */
458 int ini_comment_append(struct ini_comment *ic, const char *line)
459 {
460 int error = EOK;
461
462 TRACE_FLOW_ENTRY();
463
464 error = ini_comment_modify(ic, INI_COMMENT_MODE_APPEND, 0, line, 0);
465
466 TRACE_FLOW_NUMBER("ini_comment_append - Returning", error);
467 return error;
468 }
469
470 /* Remove line from the comment.*/
471 int ini_comment_remove(struct ini_comment *ic, uint32_t idx)
472 {
473 int error = EOK;
474
475 TRACE_FLOW_ENTRY();
476
477 error = ini_comment_modify(ic, INI_COMMENT_MODE_REMOVE, idx, NULL, 0);
478
479 TRACE_FLOW_NUMBER("ini_comment_remove - Returning", error);
480 return error;
481 }
482
483 /* Clear line in the comment. Line is replaced with an empty line */
484 int ini_comment_clear(struct ini_comment *ic, uint32_t idx)
485 {
486 int error = EOK;
487
488 TRACE_FLOW_ENTRY();
489
490 error = ini_comment_modify(ic, INI_COMMENT_MODE_CLEAR, idx, NULL, 0);
491
492 TRACE_FLOW_NUMBER("ini_comment_clear - Returning", error);
493 return error;
494
495 }
496
497 /* Replace a line in the comment */
498 int ini_comment_replace(struct ini_comment *ic,
499 uint32_t idx,
500 const char *line)
501 {
502 int error = EOK;
503
504 TRACE_FLOW_ENTRY();
505
506 error = ini_comment_modify(ic, INI_COMMENT_MODE_REPLACE, idx, line, 0);
507
508 TRACE_FLOW_NUMBER("ini_comment_replace - Returning", error);
509 return error;
510 }
511
512
513 /* Reset the comment - clean all lines.*/
514 int ini_comment_reset(struct ini_comment *ic)
515 {
516 int error = EOK;
517
518 TRACE_FLOW_ENTRY();
519
520 if (!ic) {
521 TRACE_ERROR_NUMBER("Invalid comment object", EINVAL);
522 return EINVAL;
523 }
524
525 /* Reset comment if it is not empty */
526 if (ic->state != INI_COMMENT_EMPTY) {
527 ref_array_reset(ic->ra);
528 ic->state = INI_COMMENT_CHANGED;
529 }
530
531 TRACE_FLOW_EXIT();
532 return error;
533 }
534
535 /* Get number of lines */
536 int ini_comment_get_numlines(struct ini_comment *ic, uint32_t *num)
537 {
538 int error = EOK;
539
540 TRACE_FLOW_ENTRY();
541
542 if ((!ic) || (!num)) {
543 TRACE_ERROR_NUMBER("Invalid argument", EINVAL);
544 return EINVAL;
545 }
546
547 error = ref_array_getlen(ic->ra, num);
548
549 TRACE_FLOW_NUMBER("ini_comment_get_numlines - Returning", error);
550 return error;
551
552 }
553
554 /* Get line */
555 int ini_comment_get_line(struct ini_comment *ic, uint32_t idx,
556 char **line, uint32_t *line_len)
557 {
558 int error = EOK;
559 void *res = NULL;
560 struct simplebuffer *sb = NULL;
561 const unsigned char *ln;
562
563 TRACE_FLOW_ENTRY();
564
565 if ((!ic) || (!line)) {
566 TRACE_ERROR_NUMBER("Invalid argument", EINVAL);
567 return EINVAL;
568 }
569
570 res = ref_array_get(ic->ra, idx, (void *)&sb);
571 if (!res) {
572 error = EINVAL;
573 *line = NULL;
574 if (line_len) line_len = 0;
575 }
576 else {
577 ln = simplebuffer_get_buf(sb);
578 memcpy(line, &ln, sizeof(char *));
579 if (line_len) *line_len = simplebuffer_get_len(sb);
580 }
581
582 TRACE_FLOW_NUMBER("ini_comment_get_line - Returning", error);
583 return error;
584 }
585
586 /* Swap lines */
587 int ini_comment_swap(struct ini_comment *ic,
588 uint32_t idx1,
589 uint32_t idx2)
590 {
591 int error = EOK;
592
593 TRACE_FLOW_ENTRY();
594
595 if (!ic) {
596 TRACE_ERROR_NUMBER("Invalid argument", EINVAL);
597 return EINVAL;
598 }
599
600 if (idx1 != idx2) {
601 if ((error = ref_array_swap(ic->ra, idx1, idx2))) {
602 TRACE_ERROR_NUMBER("Failed to swap", error);
603 return error;
604 }
605 ic->state = INI_COMMENT_CHANGED;
606 }
607
608 TRACE_FLOW_EXIT();
609 return error;
610 }
611
612 /* Add one comment to another */
613 int ini_comment_add(struct ini_comment *ic_to_add,
614 struct ini_comment *ic)
615 {
616 int error = EOK;
617 struct simplebuffer *sb = NULL;
618 struct simplebuffer *sb_new = NULL;
619 void *res = NULL;
620 uint32_t len = 0;
621 int i;
622
623 TRACE_FLOW_ENTRY();
624
625 len = ref_array_len(ic_to_add->ra);
626
627 for (i = 0; i< len; i++) {
628
629 /* Get data element */
630 res = ref_array_get(ic_to_add->ra, i, (void *)(&sb));
631 if (!res) {
632 TRACE_ERROR_NUMBER("Failed to get comment element", error);
633 return error;
634 }
635
636 /* Create a storage a for a copy */
637 error = simplebuffer_alloc(&sb_new);
638 if (error) {
639 TRACE_ERROR_NUMBER("Allocate buffer for the comment", error);
640 return error;
641 }
642
643 /* Copy actual data */
644 error = simplebuffer_add_str(sb_new,
645 (const char *)simplebuffer_get_buf(sb),
646 simplebuffer_get_len(sb),
647 INI_COMMENT_LEN);
648 if (error) {
649 TRACE_ERROR_NUMBER("Failed to append line to an array", error);
650 simplebuffer_free(sb_new);
651 return error;
652 }
653
654 /* Append it to the array */
655 error = ref_array_append(ic->ra, (void *)&sb_new);
656 if (error) {
657 TRACE_ERROR_NUMBER("Failed to append element to an array", error);
658 simplebuffer_free(sb_new);
659 return error;
660 }
661 }
662
663 TRACE_FLOW_EXIT();
664 return error;
665 }
666
667 /* Serialize comment */
668 int ini_comment_serialize (struct ini_comment *ic,
669 struct simplebuffer *sbobj)
670 {
671 int error = EOK;
672 uint32_t num = 0;
673 uint32_t i = 0;
674 uint32_t len = 0;
675 char *commentline = NULL;
676
677 TRACE_FLOW_ENTRY();
678
679 /* Get number of lines in the comment */
680 error = ini_comment_get_numlines(ic, &num);
681 if (error) {
682 TRACE_ERROR_NUMBER("Failed to get number of lines", error);
683 return error;
684 }
685
686 for (i = 0; i < num; i++) {
687
688 len = 0;
689 commentline = NULL;
690
691 error = ini_comment_get_line(ic, i, &commentline, &len);
692 if (error) {
693 TRACE_ERROR_NUMBER("Failed to get line", error);
694 return error;
695 }
696
697 error = simplebuffer_add_raw(sbobj,
698 commentline,
699 len,
700 INI_VALUE_BLOCK);
701 if (error) {
702 TRACE_ERROR_NUMBER("Failed to add comment", error);
703 return error;
704 }
705
706 error = simplebuffer_add_cr(sbobj);
707 if (error) {
708 TRACE_ERROR_NUMBER("Failed to add CR", error);
709 return error;
710 }
711 }
712
713 TRACE_FLOW_EXIT();
714 return error;
715 }
716
717 /* Internal function to print comment */
718 void ini_comment_print(struct ini_comment *ic, FILE *file)
719 {
720 int len;
721 int i;
722 struct simplebuffer *sb = NULL;
723
724 TRACE_FLOW_ENTRY();
725
726 if (!file) {
727 TRACE_ERROR_NUMBER("Invalid file argument", EINVAL);
728 return;
729 }
730
731 if (ic) {
732 len = ref_array_len(ic->ra);
733 for (i = 0; i < len; i++) {
734 ref_array_get(ic->ra, i, (void *)(&sb));
735 fprintf(file, "%s\n", simplebuffer_get_buf(sb));
736 }
737 }
738
739 TRACE_FLOW_EXIT();
740 }
741
742 /* Construct a comment out of array or strings. */
743 int ini_comment_construct(const char *comments[],
744 size_t count_comment,
745 struct ini_comment **ic)
746 {
747 int error = EOK;
748 size_t cnt = 0;
749 struct ini_comment *new_ic = NULL;
750
751 TRACE_FLOW_ENTRY();
752 if (!ic) {
753 TRACE_ERROR_STRING("Invalid argument","comment object");
754 return EINVAL;
755 }
756
757 if (comments) {
758 error = ini_comment_create(&new_ic);
759 if (error) {
760 TRACE_ERROR_NUMBER("Failed to create comment object",
761 error);
762 return error;
763 }
764
765 if (count_comment) {
766 /* Base the number of lines on count */
767 for (cnt = 0; cnt < count_comment; cnt++) {
768 error = ini_comment_append(new_ic, comments[cnt]);
769 if (error) {
770 TRACE_ERROR_NUMBER("Failed to append comment in for loop.",
771 error);
772 ini_comment_destroy(new_ic);
773 return error;
774 }
775 }
776 }
777 else {
778 /* Assume that the list is NULL terminated */
779 while (comments[cnt]) {
780 error = ini_comment_append(new_ic, comments[cnt]);
781 if (error) {
782 TRACE_ERROR_NUMBER("Failed to append comment in for loop.",
783 error);
784 ini_comment_destroy(new_ic);
785 return error;
786 }
787 cnt++;
788 }
789 }
790 *ic = new_ic;
791 }
792 else {
793 /* No comments! */
794 *ic = NULL;
795 }
796
797 TRACE_FLOW_EXIT();
798 return EOK;
799 }