summaryrefslogtreecommitdiffstats
path: root/common/ini/ini_comment.c
blob: 19ee80e2b050e860f40c22fd36fb0ad5058937fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/*
    INI LIBRARY

    Object to handle comments

    Copyright (C) Dmitri Pal <dpal@redhat.com> 2010

    INI Library is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    INI Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with INI Library.  If not, see <http://www.gnu.org/licenses/>.
*/

#define _GNU_SOURCE
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include "config.h"
#include "trace.h"
#include "ref_array.h"
#include "simplebuffer.h"
#include "ini_comment.h"

/* The lines will increment in this number */
#define INI_COMMENT_BLOCK 10
/* Default comment length */
#define INI_COMMENT_LEN 100


/***************************/
/* Internal comment states */
/***************************/
/* Empty - initial */
#define INI_COMMENT_EMPTY   0
/* Read - read from file */
#define INI_COMMENT_READ    1
/* Comment was altered */
#define INI_COMMENT_CHANGED 2


/*********************************/
/* Modes to wrap ref array calls */
/*********************************/
#define INI_COMMENT_MODE_BUILD      1
#define INI_COMMENT_MODE_APPEND     2
#define INI_COMMENT_MODE_INSERT     3
#define INI_COMMENT_MODE_REPLACE    4
#define INI_COMMENT_MODE_REMOVE     5
#define INI_COMMENT_MODE_CLEAR      6

/****************************************/
/* Internal structure to hold a comment */
/****************************************/
struct ini_comment {
    struct ref_array *ra;
    uint32_t state;
};


/****************************************/

/* Destroy the comment object */
void ini_comment_destroy(struct ini_comment *ic)
{

    TRACE_FLOW_ENTRY();
    if (ic) {
        /* Function will check for NULL */
        ref_array_destroy(ic->ra);
        free(ic);
    }
    TRACE_FLOW_EXIT();
}


/* Cleanup callback */
void ini_comment_cb(void *elem,
                    ref_array_del_enum type,
                    void *data)
{

    TRACE_FLOW_ENTRY();
    simplebuffer_free(*((struct simplebuffer **)elem));
    TRACE_FLOW_EXIT();
}


/* Create a comment object */
int ini_comment_create(struct ini_comment **ic)
{
    int error = EOK;
    struct ref_array *ra = NULL;
    struct ini_comment *ic_new = NULL;

    TRACE_FLOW_ENTRY();

    error = ref_array_create(&ra,
                             sizeof(struct simplebuffer *),
                             INI_COMMENT_BLOCK,
                             ini_comment_cb,
                             NULL);
    if (error) {
        TRACE_ERROR_NUMBER("Error creating ref array", error);
        return error;
    }

    ic_new = malloc(sizeof(struct ini_comment));
    if (!ic_new) {
        TRACE_ERROR_NUMBER("Memory allocation error", ENOMEM);
        ref_array_destroy(ra);
        return ENOMEM;
    }

    /* Initialize members here */
    ic_new->ra = ra;
    ic_new->state = INI_COMMENT_EMPTY;

    *ic = ic_new;

    TRACE_FLOW_EXIT();
    return error;
}

/* Callback to copy comment */
static int ini_comment_copy_cb(void *elem,
                               void *new_elem)
{
    int error = EOK;
    struct simplebuffer *sb = NULL;
    struct simplebuffer *sb_new = NULL;
    struct simplebuffer **sb_acpt = NULL;

    TRACE_FLOW_ENTRY();

    error = simplebuffer_alloc(&sb_new);
    if (error) {
        TRACE_ERROR_NUMBER("Failed to allocate buffer", error);
        return error;
    }

    sb = (struct simplebuffer *)elem;
    error = simplebuffer_add_str(sb_new,
                                 simplebuffer_get_buf(sb),
                                 simplebuffer_get_len(sb),
                                 INI_COMMENT_LEN);
    if (error) {
        TRACE_ERROR_NUMBER("Failed to allocate buffer", error);
        simplebuffer_free(sb_new);
        return error;
    }

    sb_acpt = (struct simplebuffer **)new_elem;
    *sb_acpt = sb_new;

    TRACE_FLOW_EXIT();
    return error;
}


/* Create a comment object */
int ini_comment_copy(struct ini_comment *ic,
                     struct ini_comment **copy_ic)
{
    int error = EOK;
    struct ref_array *ra = NULL;
    struct ini_comment *ic_new = NULL;

    TRACE_FLOW_ENTRY();

    error = ref_array_copy(ic->ra,
                           ini_comment_copy_cb,
                           &ra);
    if (error) {
        TRACE_ERROR_NUMBER("Error creating a copy of ref array", error);
        return error;
    }

    ic_new = malloc(sizeof(struct ini_comment));
    if (!ic_new) {
        TRACE_ERROR_NUMBER("Memory allocation error", ENOMEM);
        ref_array_destroy(ra);
        return ENOMEM;
    }

    /* Initialize members here */
    ic_new->ra = ra;
    ic_new->state = ic->state;

    *ic_copy = ic_new;

    TRACE_FLOW_EXIT();
    return error;
}

/* Is the comment valid? */
static int ini_comment_is_valid(const char *line)
{
    int i;

    TRACE_FLOW_ENTRY();

    /* Null is ok */
    if (!line) {
        TRACE_FLOW_STRING("ini_comment_is_valid", "Exit - NULL str");
        return 1;
    }

    /* Empty is Ok or starts with a special symbol */
    if ((line[0] == '\0') ||
        (line[0] == ';') ||
        (line[0] == '#')) {
        TRACE_FLOW_STRING("ini_comment_is_valid", "Exit - empty or comment");
        return 1;
    }

    /* All spaces is Ok too */
    TRACE_INFO_STRING("Line to eval", line);

    i = 0;
    while (line[i] != '\0') {
        if (!isspace(line[i])) {
            TRACE_ERROR_STRING("ini_comment_is_valid", "Invalid comment");
            return 0;
        }
        i++;
    }

    TRACE_FLOW_STRING("ini_comment_is_valid", "Exit - empty str");
    return 1;

}


/*
 * Modify the comment object
 */
static int ini_comment_modify(struct ini_comment *ic,
                              int mode,
                              uint32_t idx,
                              const char *line,
                              uint32_t length)
{
    int error = EOK;
    struct simplebuffer *elem = NULL;
    struct simplebuffer *empty = NULL;
    char *input = NULL;
    uint32_t i, len = 0;
    uint32_t input_len = 0;

    TRACE_FLOW_ENTRY();

    if (!ic) {
        TRACE_ERROR_NUMBER("Invalid comment object", EINVAL);
        return EINVAL;
    }


    if (mode == INI_COMMENT_MODE_BUILD) {
        /*
         * Can use this function only if object is empty or
         * reading from the file.
         */
        if ((ic->state != INI_COMMENT_EMPTY) &&
            (ic->state != INI_COMMENT_READ)) {
            TRACE_ERROR_NUMBER("Invalid use of the function", EINVAL);
            return EINVAL;
        }
    }

    /* Make sure that we ignore "line" in reset case */
    if (mode != INI_COMMENT_MODE_CLEAR)
        memcpy(&input, &line, sizeof(char *));

    if (mode != INI_COMMENT_MODE_REMOVE) {
        /*
         * Check that provided line is a comment or an empty line.
         * Can be NULL too.
         */
        if (!ini_comment_is_valid(input)) {
            TRACE_ERROR_NUMBER("Invalid comment", EINVAL);
            return EINVAL;
        }

        error = simplebuffer_alloc(&elem);
        if (error) {
            TRACE_ERROR_NUMBER("Allocate buffer for the comment", error);
            return error;
        }

        /* Dup it */
        if (input) {

            if (length == 0) input_len = strlen(input);
            else input_len = length;
            error = simplebuffer_add_str(elem,
                                         input,
                                         input_len,
                                         INI_COMMENT_LEN);
            if (error) {
                TRACE_ERROR_NUMBER("Allocate buffer for the comment", error);
                simplebuffer_free(elem);
                return error;
            }
        }

    }

    /* Do action depending on mode */
    switch (mode) {
    case INI_COMMENT_MODE_BUILD:

        TRACE_INFO_STRING("BUILD mode", "");
        error = ref_array_append(ic->ra, (void *)&elem);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to append line to an array", error);
            simplebuffer_free(elem);
            return error;
        }

        break;

    case INI_COMMENT_MODE_APPEND:

        TRACE_INFO_STRING("Append mode", "");
        error = ref_array_append(ic->ra, (void *)&elem);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to append line to an array", error);
            free(elem);
            return error;
        }

        break;

    case INI_COMMENT_MODE_INSERT:

        TRACE_INFO_STRING("Insert mode", "");
        len = ref_array_len(ic->ra);
        if (idx > len) {
            /* Fill in empty lines */
            for (i = 0; i < (idx-len); i++) {
                error = simplebuffer_alloc(&empty);
                if (error) {
                    TRACE_ERROR_NUMBER("Allocate buffer for the comment", error);
                    simplebuffer_free(elem);
                    return error;
                }
                error = ref_array_append(ic->ra, (void *)&empty);
                if (error) {
                    TRACE_ERROR_NUMBER("Append problem", error);
                    simplebuffer_free(empty);
                    simplebuffer_free(elem);
                    return error;
                }
            }
            /* Append last line */
            error = ref_array_append(ic->ra, (void *)&elem);
            if (error) {
                TRACE_ERROR_NUMBER("Failed to append last line", error);
                simplebuffer_free(elem);
                return error;
            }
        }
        else {
            /* Insert inside the array */
            error = ref_array_insert(ic->ra, idx, (void *)&elem);
            if (error) {
                TRACE_ERROR_NUMBER("Failed to append last line", error);
                simplebuffer_free(elem);
                return error;
            }

        }
        break;


    case INI_COMMENT_MODE_REPLACE:

        TRACE_INFO_STRING("Replace mode", "");
        error = ref_array_replace(ic->ra, idx, (void *)&elem);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to replace", error);
            simplebuffer_free(elem);
            return error;
        }
        break;

    case INI_COMMENT_MODE_REMOVE:

        TRACE_INFO_STRING("Remove mode", "");
        error = ref_array_remove(ic->ra, idx);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to remove", error);
            return error;
        }

        break;

    case INI_COMMENT_MODE_CLEAR:

        TRACE_INFO_STRING("Clear mode", "");
        error = ref_array_replace(ic->ra, idx, (void *)&elem);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to replace", error);
            simplebuffer_free(elem);
            return error;
        }
        break;

    default :

        TRACE_ERROR_STRING("Coding error", "");
        return EINVAL;

    }


    /* Change state */
    if (INI_COMMENT_MODE_BUILD) ic->state = INI_COMMENT_READ;
    else ic->state = INI_COMMENT_CHANGED;


    TRACE_FLOW_EXIT();
    return error;
}

/*
 * Build up a comment object - use this when reading
 * comments from a file.
 */
int ini_comment_build(struct ini_comment *ic, const char *line)
{
    int error = EOK;

    TRACE_FLOW_ENTRY();

    error = ini_comment_modify(ic, INI_COMMENT_MODE_BUILD, 0, line, 0);

    TRACE_FLOW_NUMBER("ini_comment_build - Returning", error);
    return error;
}

/*
 * Build up a comment object - use this when reading
 * comments from a file.
 */
int ini_comment_build_wl(struct ini_comment *ic,
                         const char *line,
                         uint32_t length)
{
    int error = EOK;

    TRACE_FLOW_ENTRY();

    error = ini_comment_modify(ic, INI_COMMENT_MODE_BUILD, 0, line, length);

    TRACE_FLOW_NUMBER("ini_comment_build - Returning", error);
    return error;
}

/*
 * Modify comment by instering a line.
 */
int ini_comment_insert(struct ini_comment *ic,
                       uint32_t idx,
                       const char *line)
{
    int error = EOK;

    TRACE_FLOW_ENTRY();

    error = ini_comment_modify(ic, INI_COMMENT_MODE_INSERT, idx, line, 0);

    TRACE_FLOW_NUMBER("ini_comment_insert - Returning", error);
    return error;
}

/* Modify comment by appending a line. */
int ini_comment_append(struct ini_comment *ic, const char *line)
{
    int error = EOK;

    TRACE_FLOW_ENTRY();

    error = ini_comment_modify(ic, INI_COMMENT_MODE_APPEND, 0, line, 0);

    TRACE_FLOW_NUMBER("ini_comment_append - Returning", error);
    return error;
}

/* Remove line from the comment.*/
int ini_comment_remove(struct ini_comment *ic, uint32_t idx)
{
    int error = EOK;

    TRACE_FLOW_ENTRY();

    error = ini_comment_modify(ic, INI_COMMENT_MODE_REMOVE, idx, NULL, 0);

    TRACE_FLOW_NUMBER("ini_comment_remove - Returning", error);
    return error;
}

/* Clear line in the comment. Line is replaced with an empty line */
int ini_comment_clear(struct ini_comment *ic, uint32_t idx)
{
    int error = EOK;

    TRACE_FLOW_ENTRY();

    error = ini_comment_modify(ic, INI_COMMENT_MODE_CLEAR, idx, NULL, 0);

    TRACE_FLOW_NUMBER("ini_comment_clear - Returning", error);
    return error;

}

/* Replace a line in the comment */
int ini_comment_replace(struct ini_comment *ic,
                        uint32_t idx,
                        const char *line)
{
    int error = EOK;

    TRACE_FLOW_ENTRY();

    error = ini_comment_modify(ic, INI_COMMENT_MODE_REPLACE, idx, line, 0);

    TRACE_FLOW_NUMBER("ini_comment_replace - Returning", error);
    return error;
}


/* Reset the comment - clean all lines.*/
int ini_comment_reset(struct ini_comment *ic)
{
    int error = EOK;

    TRACE_FLOW_ENTRY();

    if (!ic) {
        TRACE_ERROR_NUMBER("Invalid comment object", EINVAL);
        return EINVAL;
    }

    /* Reset comment if it is not empty */
    if (ic->state != INI_COMMENT_EMPTY) {
        ref_array_reset(ic->ra);
        ic->state = INI_COMMENT_CHANGED;
    }

    TRACE_FLOW_EXIT();
    return error;
}

/* Get number of lines */
int ini_comment_get_numlines(struct ini_comment *ic, uint32_t *num)
{
    int error = EOK;

    TRACE_FLOW_ENTRY();

    if ((!ic) || (!num)) {
        TRACE_ERROR_NUMBER("Invalid argument", EINVAL);
        return EINVAL;
    }

    error = ref_array_getlen(ic->ra, num);

    TRACE_FLOW_NUMBER("ini_comment_get_numlines - Returning", error);
    return error;

}

/* Get line */
int ini_comment_get_line(struct ini_comment *ic, uint32_t idx,
                         char **line, uint32_t *line_len)
{
    int error = EOK;
    void *res = NULL;
    struct simplebuffer *sb = NULL;

    TRACE_FLOW_ENTRY();

    if ((!ic) || (!line)) {
        TRACE_ERROR_NUMBER("Invalid argument", EINVAL);
        return EINVAL;
    }

    res = ref_array_get(ic->ra, idx, (void *)&sb);
    if (!res) {
        error = EINVAL;
        *line = NULL;
        if (line_len) line_len = 0;
    }
    else {
        *line = simplebuffer_get_buf(sb);
        if (line_len) *line_len = simplebuffer_get_len(sb);
    }

    TRACE_FLOW_NUMBER("ini_comment_get_line - Returning", error);
    return error;
}

/* Swap lines */
int ini_comment_swap(struct ini_comment *ic,
                     uint32_t idx1,
                     uint32_t idx2)
{
    int error = EOK;

    TRACE_FLOW_ENTRY();

    if (!ic) {
        TRACE_ERROR_NUMBER("Invalid argument", EINVAL);
        return EINVAL;
    }

    if (idx1 != idx2) {
        if ((error = ref_array_swap(ic->ra, idx1, idx2)) ||
            (error = ref_array_swap(ic->ra_len, idx1, idx2))) {
            TRACE_ERROR_NUMBER("Failed to swap", error);
            return error;
        }
        ic->state = INI_COMMENT_CHANGED;
    }

    TRACE_FLOW_EXIT();
    return error;
}


/* Internal function to print comment */
void ini_comment_print(struct ini_comment *ic, FILE *file)
{
    int len;
    int i;
    struct simplebuffer *sb = NULL;

    TRACE_FLOW_ENTRY();

    if (!file) {
        TRACE_ERROR_NUMBER("Invalid file argument", EINVAL);
        return;
    }

    if (ic) {
        len = ref_array_len(ic->ra);
        for (i = 0; i < len; i++) {
            ref_array_get(ic->ra, i, (void *)(&sb));
            fprintf(file, "%s\n", simplebuffer_get_buf(sb));
        }
    }

    TRACE_FLOW_EXIT();
}