aboutsummaryrefslogtreecommitdiffstats
path: root/src/ini_read.c
blob: 817aa157e4eebd02fdfb4ab6e170fd7a47f1bd7f (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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
/* INI fájl olvasás */

#include <stdio.h>
#include <string.h> /* strncpy + strerror */
#include <stdlib.h> /* malloc(), atoi(), exit(EXIT_FALIURE) */
#include <errno.h>  /* errno */
#include <stdarg.h>
#include <stdint.h> /* int64_t */

#define ini_read_c
#include "inirw_internal.h"
#include "lightconfini.h"



static size_t strNullLen(const uint8_t *str){
    if(str == NULL){
        return 0;
    } else { 
        return strlen((const char*)str);
    } 
}


static size_t strLcpy(uint8_t *dst, size_t dstlen, const uint8_t *src, size_t srclen){  /* Safe strncpy() */
    uint8_t *tdst=dst; 
    const uint8_t *tsrc=src;
    size_t i=dstlen, j=srclen;

    if(dst == NULL || src == NULL || dstlen == 0){
        return 0;
    } else {
        while(--i && j-- && *tsrc != '\0'){ /* [ --i => Strats from dstlen=2] [ j-- => Stops on n=srclen] */
            *(tdst++) = *(tsrc++);  /* dst[n] = src[n]; n++; */
        }
        *tdst = '\0'; /* Applies from dstlen=n=1 */
        return tdst-dst+1;   /* full buffer (incl. '\0') */
    }
}


static uint8_t *lciniStrResize(uint8_t *ptr, size_t oldsize, size_t newsize){
    uint8_t *tmp=NULL;

    if(newsize <= 0){           /* deleting */
        free(ptr);
        return NULL;

    } else if(newsize != oldsize){       /* If any changes needed */
        tmp = (uint8_t *) malloc(newsize*sizeof(uint8_t));
        memset(tmp, 0, newsize*sizeof(uint8_t));
        if(tmp == NULL){            /* String is not changed at malloc error */
            return ptr;
        } else if(ptr == NULL) {
            return tmp;

        } else {   
            /*strncpy(tmp, ptr, newsize);*/ /* c89 */
            /*snprintf(tmp, newsize, "%s", ptr);*/ /* c99 */
            strLcpy(tmp, newsize, ptr, oldsize);
            free(ptr);
            return tmp;
        } 
    } else {
        return ptr;
    }
}


static uint8_t unescape(uint8_t c){
    if(c == 'n'){            /* Newline */
        return '\n';
    } else if(c == 'a'){     /* Bell */
        return '\a';
    } else if(c == 'b'){     /* Backspace */
        return '\b';
    } else if(c == 'f'){     /* Formfeed Page Break */
        return '\f';
    } else if(c == 'r'){     /* Carrige return */
        return '\r';
    } else if(c == 't'){     /* Horizontal tab */
        return '\t';
    } else if(c == 'v'){     /* Vertical tab */
        return '\v';
    } else if(c == 'e'){
        return 0x1B;
    /*} else if(c < 0x20){  // debug
        return '~';    */
    } else {                /* Original is OK */
        return c;  
    }
}

#ifdef ini_write_c
static uint8_t eescape(uint8_t c){
    if(c == '\n'){          /* Newline */
        return 'n';
    } else if(c == '\a'){   /* Bell */
        return 'a';
    } else if(c == '\b'){   /* Backspace */
        return 'b';
    } else if(c == '\f'){   /* Formfeed Page Break */
        return 'f';
    } else if(c == '\r'){   /* Carrige return */
        return 'r';
    } else if(c == '\t'){   /* Horizontal tab */
        return 't';
    } else if(c == '\v'){   /* Vertical tab */
        return 'v';
    /*} else if(c < 0x20){  //debug
        return '~'; */
    } else {                /* Original is OK */
        return c;  
    }
}
#endif

static uint8_t isascalnum(uint8_t c){ /* Check if input is ASCII Alpha-numeric */
    if( 0x30 <= c && c <= 0x39){    /* Numeric */
        return 1;
    } else if (0x41 <= c && c <= 0x5a){ /* UPPER */
        return 1;
    } else if( 0x61 <= c && c <= 0x7a){ /* lower */
        return 1;
    } else {
        return 0;
    }
}

static uint8_t checkspace(uint8_t c){  /* Only for ASCII characters */
    switch (c) {
    case 0x20:      /* space (SPC) */
        return 1;
    case 0x09:      /* horizontal tab (TAB) */
        return 1;
    case 0x0A:      /* newline (LF) */
        return 1;
    case 0x0B:      /* vertical tab (VT) */
        return 1;
    case 0x0C:      /* feed (FF) */
        return 1;
    case 0x0d:      /* carrige return (CR) */
        return 1;
    default:
        return 0;
    }
}



size_t lciniFileMaxLineLen(FILE *tfd){
    int c=0;
    size_t i=0, max=0;
    if (tfd != NULL){
        while( c != EOF){
            c = fgetc(tfd);
            if (c == '\n' || c == EOF) {    /* Line End */
                i++;                        /* The "\n" is a character also */ 
                if(max < i){
                    max = i;
                }
                i = 0;                      /* Recount on every newline */
            } else {
                i++;
            }
        }
        fseek(tfd, 0, SEEK_SET);            /* Pos. to the start */
        return max;
    } else {                                /* File open error */
        return -1;
    }       
}

static struct lcini_data *iniFSM(struct lcini_data *data, const uint8_t *in, int32_t len){

    int32_t i,j, vallen=len; 
    enum lcini_states pstate=Start, state=Start;
    /*char cc; */
    if(data == NULL){
        return NULL;
    } else {

        for(i=0, j=0; i<len; i++, j++){
            /*cc = in[i];*/  /*debug*/
            switch (state) { 
                case Start: 
                    if(data->nodeState == lcini_MULTILINE){                                               /* Bypass to the DQM collection */
                        state = DqmW;   
                        j = data->valueLen-2;       /* (x-1) => \0; (x-2) => j--; */
                        data->value = lciniStrResize(data->value, data->valueLen, data->valueLen+len+1);   /* lciniStrResize(ptr, oldsize, newsize */
                        vallen = data->valueLen+len+1;
                        data->nodeState = lcini_CONTINUE;
                        i--;                                    /* first char is collected also */
                        pstate = Start;
                        break;
                    } else if(in[i] == '\n' || in[i] == '\r' /*|| in[i] == '\0'*/){   /* Line End */
                        state = Stop;                   
                        i--;
                    }else if(in[i]==0xEF || in[i]==0xBB || in[i]==0xBF || in[i]==0xFF || in[i]==0x00 ){ /* UTF8, UTF16, UTF32 BOM */
                        state = Start;
                    } else if(checkspace(in[i])){                  /* ISSPACE, but not line end */
                        state = BgnSp;
                    } else if(in[i] == ';' || in[i] == '#' ){   /* Comment sign first */
                        j = -1;
                        state = CommEndW;
                        data->comment = lciniStrResize(data->comment, data->commentLen, len);
                        memset(data->comment, 0, len);
                        data->commentStartPos = i;
                        data->sectionStartPos = -1;             /* Comment only line */
                        data->commentSign = in[i];
                    } else if(in[i] == '['){                    /* Section start */
                        j = -1;
                        state = SectEndW;
                        data->section = lciniStrResize(data->section, data->sectionLen, len);
                        memset(data->section, 0, len);
                        data->sectionStartPos = i;              /* Brackets [] are counted! */
                    } else if(isascalnum(in[i]) ){                 /* Parameter is starting */
                        j = -1;
                        state = EqW1;    
                        data->param = lciniStrResize(data->param, data->paramLen, len);
                        memset(data->param, 0, len);
                        data->paramStartPos = i;
                        i--;
                    } else {                                    /* eg: Control characters in pos 1. */
                        state = Error; 
                        i--;
                    }
                    data->nodeState = lcini_EMPTY;
                    data->valueDraw = lcini_EMPTYVAL;
                    pstate = Start;
                    break;


                case BgnSp:                                     /* Space() at Begining */
                    if (in[i] == '\n' || in[i] == '\r' ){
                        state = Stop;
                        i--;
                    } else if (in[i] == ';' || in[i] == '#'){   /* Comment is coming */
                        j = -1;
                        state = CommEndW;
                        data->comment = lciniStrResize(data->comment, data->commentLen, len);
                        memset(data->comment, 0, len);
                        data->commentStartPos = i;
                        data->sectionStartPos = -1;             /* No Section started in this line */
                        data->commentSign = in[i];
                    } else if(in[i] == '['){                    /* Section is starting */
                        j = -1;
                        state = SectEndW;
                        data->section = lciniStrResize(data->section, data->sectionLen, len);
                        memset(data->section, 0, len);
                        data->sectionStartPos = i;
                    } else if (isascalnum(in[i])){                 /* Parameter will be */
                        j = -1;
                        state = EqW1;
                        data->param = lciniStrResize(data->param, data->paramLen, len);
                        memset(data->param, 0, len);
                        data->paramStartPos = i;
                        i--;
                    } else if(checkspace(in[i]) /*|| in[i] == '\0' */ ){
                        state = BgnSp;
                    } else {
                        state = Error;
                        i--;
                    }
                    pstate = BgnSp;
                    break;
 

                case CommEndW:      /* Till comment to the line end */
                    if(in[i] == '\n' || in[i] == '\r'){
                        state = Stop;
                        data->nodeState = lcini_READY;
                        data->comment[j] = '\0';
                        data->commentLen = j+1;
                        i--;
                    } else {
                        data->comment[j] = in[i];
                    }
                    pstate = CommEndW;
                    break;        


                case SectEndW:              /* Section Collector */
                    if(in[i] == ']'){
                        state = SectEndD;
                        data->section[j] = '\0';
                        data->sectionLen = j+1;
                        if(j == 0){         /* empty section */
                            state = Error;
                        }
                    } else if(isascalnum(in[i]) || in[i]=='/' || in[i]==' ' || in[i]=='_' || in[i]=='-' || in[i]=='.'){   
                        data->section[j] = in[i];
                    } else /*if((in[i] >= 0x00 && in[i] < 0x20) || in[i] == ';' || in[i] == '#' )*/ {       /* túl korai sorvég, SP(), vagy komment */
                        state = Error;
                        data->section[j] = '\0';
                        data->sectionLen = j+1;
                        i--;
                    } /*else {
                        data->section[j] = in[i];
                    }*/
                    pstate = SectEndW;
                    break;


                case SectEndD:          /* Section collected, then: SP(), line_end, or comment */
                    if(in[i]=='\n' || in[i]=='\r'){
                        state = Stop;
                        data->nodeState = lcini_READY;
                        i--;
                    }else if(checkspace(in[i])){
                        state = SectEndD;             /* remain here */
                    } else if (in[i] == ';' || in[i] == '#'){
                        j = -1;
                        state = CommEndW;
                        data->comment = lciniStrResize(data->comment, data->commentLen, len);
                        memset(data->comment, 0, len);
                        data->commentStartPos = i;
                        data->commentSign = in[i];
                        data->nodeState = lcini_CONTINUE;
                    } else {
                        state = Error;      /* wrong character in line */
                        i--;
                    }
                    pstate = SectEndD;
                    break;


                case EqW1:              /* Parameter name, befora EQ_sign(=) */
                    if(in[i] == '='){    /* EQ sign arrived */
                        state = ValPSP;
                        data->param[j] = '\0';
                        data->paramLen = j + 1;
                    } else if(isascalnum(in[i]) || in[i]=='[' || in[i]==']' || in[i]=='_' || in[i]=='-' || in[i]=='.'){   /* Arrays are supported ins parameter name, like in PHP */
                        data->param[j] = in[i];
                    } else if(checkspace(in[i]) && in[i] != '\r' && in[i] != '\n'){   /* Only SPACE allowed between Param_name and EQ_sign */
                        state = EqW2;
                        data->param[j] = '\0';
                        data->paramLen = j + 1;
                    } else {
                        state = Error;
                        i--;
                    }
                    pstate = EqW1;
                    break;


                case EqW2:              /* SPACE before EQ_sign */
                    if(in[i] == '='){    /* EQ_sign arrived */
                        j = -1;
                        state = ValPSP;
                    } else if(checkspace(in[i])){   /* Another space, remain here */
                        state = EqW2;
                    } else {
                        state = Error;
                        i--;
                    }
                    pstate = EqW2;
                    break;


                case ValPSP:    /* After EQ_sign,  (SPACE, or value can arrive) */
                    if(in[i] == '\n' || in[i] == '\r' /*|| in[i] == '\0'*/ ){   /* Empty value */
                        state = Stop;
                        data->nodeState = lcini_READY;
                        data->valueDraw = lcini_SIMPLEVAL;
                        i--;
                    } else if(in[i] == ';' || in[i] == '#'){  /* Comment */
                        j = -1;
                        state = CommEndW;
                        data->comment = lciniStrResize(data->comment, data->commentLen, len);
                        memset(data->comment, 0, len);
                        data->commentStartPos = i;
                        data->nodeState = lcini_CONTINUE;
                        data->valueDraw = lcini_SIMPLEVAL;
                        data->commentSign = in[i];

                    } else if( pstate != Bslsh && in[i] == '\"' ){   /* DQM arrived => DQM collector */
                        j = -1;
                        state = DqmW;
                        data->value = lciniStrResize(data->value, data->valueLen, len);
                        memset(data->value, 0, len);
                        data->valueStartPos = i;
                        data->nodeState = lcini_CONTINUE;
                        data->valueDraw = lcini_DQUOTEDVAL;
                    } else if(checkspace(in[i])){                      /* Another SP(), remain here */
                        state = ValPSP;              
                    } else if(isascalnum(in[i]) || in[i]=='-') {       /* Normal_Value collector */
                        j = -1;
                        state = ValW;
                        data->value = lciniStrResize(data->value, data->valueLen, len);
                        memset(data->value, 0, len);
                        data->valueStartPos = i;
                        data->nodeState = lcini_CONTINUE;
                        data->valueDraw = lcini_SIMPLEVAL;
                        i--;
                    } else {
                        state = Error;
                        i--;
                    }
                    pstate = ValPSP;
                    break;


                case ValW:   /* Normal_Value collector */
                    if(in[i] == '\n' || in[i] == '\r'){   /* end -> new_line */
                        state = Stop;
                        data->nodeState = lcini_READY;
                        data->value[j] = '\0';
                        data->valueLen = j + 1;
                        i--;
                    } else if(in[i] == ';' || in[i] == '#'){   /* comment */
                        data->nodeState = lcini_CONTINUE;
                        data->value[j] = '\0';
                        data->valueLen = j+1;
                        state = CommEndW;
                        j = -1;
                        data->comment = lciniStrResize(data->comment, data->commentLen, len);
                        memset(data->comment, 0, len);
                        data->commentStartPos = i;
                        data->commentSign = in[i];
                        data->nodeState = lcini_CONTINUE;
                    /*
                        } else if( in[i] == '\\' ){  // Backslash support 
                            j--;            //A '\' nem számít bele!
                            state = Bslsh;   
                    */
                    } else if(isascalnum(in[i]) || in[i]=='_' || in[i]=='-' || in[i]=='.' ){ /* Regular characters without SP() */
                        data->value[j] = in[i];
                    } else if(checkspace(in[i])){            /* SPACE arrived -> line_end */
                        data->value[j] = '\0';
                        data->valueLen = j+1;
                        data->nodeState = lcini_READY;
                        state = ValFSP;
                        i--;
                    } else {
                        state = Error;
                        i--;
                    }
                    pstate = ValW;
                    break;


                case Bslsh: /* Backslash -> Escaping needed */
                    if(pstate == ValW){
                        state = ValW;
                        data->value[j] = unescape(in[i]);
                    } else if(pstate == SectEndW){
                        state = SectEndW;
                        data->section[j] = unescape(in[i]);
                    }else if(pstate == DqmW){
                        state = DqmW;
                        data->value[j] = unescape(in[i]);
                        if(in[i] == '\n'){      /* Workaround for UNIX line end */
                            i--;
                        }
                        if( in[i] == '\r'){     /* Workaround for WIN line endings */
                            j--;
                        }
                    } else {
                        state = Error;
                        i--;
                    }
                    /* if(0x00 < in[i] && in[i] < 0x20){   //láthatatlan: pl \\n (escapelt sorvége) //Vagy inkább mégis kell?
                        j--;
                    }     */
                    pstate = Bslsh;
                    break;


                case ValFSP:    /* Space after value */
                    if( in[i] == '\n' || in[i] == '\r'){
                        state = Stop;
                        i--;
                    } else if( in[i] == ';' || in[i] == '#'){
                        j = -1;
                        state = CommEndW;
                        data->comment = lciniStrResize(data->comment, data->commentLen, len);
                        memset(data->comment, 0, len);
                        data->commentStartPos = i;
                        data->commentSign = in[i];
                        data->nodeState = lcini_CONTINUE;
                    } else if(checkspace(in[i])){
                        state = ValFSP;         /* SP() -> ermain here */ 
                    } else {
                        state = Error;
                        i--;
                    }
                    pstate = ValFSP;
                    break;


                case DqmW:   /* DQM Collector (Double Quotation MArk) */
                    if( in[i] == '\"'){        /* Second DQM => Sring_end */
                        data->value[j] = '\0';
                        data->valueLen = j+1;
                        data->nodeState = lcini_READY;
                        state = ValFSP;
                    } else if ( in[i] == '\\'){    /* Backslash */
                        j--;
                        state = Bslsh;
                    } else if(len > 1 && in[i] == '\n' && in[i-1] == '\\'){       /* UNIX style line endings*/
                        data->value[j] = '\0';                              /* '\\n' => '\n\0' */
                        data->valueLen = j+1;
                        data->nodeState = lcini_MULTILINE;
                        data->valueDraw = lcini_MULTILINEVAL;
                        state = Stop;
                        i--;
                    } else if(len > 2 && in[i] == '\n' && in[i-1] == '\r' && in[i-2] == '\\'){ /* WINDOWS style */
                        data->value[j]   = '\n';
                        data->value[j+1] = '\0';
                        data->valueLen = j+2;
                        data->nodeState = lcini_MULTILINE;
                        data->valueDraw = lcini_MULTILINEVAL;
                        state = Stop;
                        i--;
                    } else if( pstate != Bslsh && (in[i] == '\r' || in[i] == '\n' || in[i] == '\0')){ /* Too early Line_end */
                        state = Error;
                        i--;
                    } else {
                        data->value[j] = in[i];
                    }
                    pstate = DqmW;
                    break; 


                case Error:
                    data->errorMsg = lciniStrResize(data->errorMsg, data->errorMsgLen, 256);  /* Returns zero-filled string */

                    if(pstate == SectEndW || pstate == SectEndD){
                        data->errorMsgLen = sprintf((char*)data->errorMsg, "Illegal character or EMPTY SECTION! (line: %d, pos: %d)", data->lineNum, i+1) +1;
                    } else if(pstate == EqW1 || pstate == EqW2){
                        data->errorMsgLen = sprintf((char*)data->errorMsg, "Illegal character at PARAMETER! (line: %d, pos: %d)", data->lineNum, i+1) +1;
                    } else if(pstate == ValPSP || pstate == ValW || pstate == ValFSP ){
                        data->errorMsgLen = sprintf((char*)data->errorMsg, "Illegal character at VALUE! (line: %d, pos: %d)", data->lineNum, i+1) +1;
                    } else if(pstate == DqmW){
                        data->errorMsgLen = sprintf((char*)data->errorMsg, "Double quotation mark needed! (line: %d, pos: %d)", data->lineNum, i+1) +1;
                    } else {    
                        data->errorMsgLen = sprintf((char*)data->errorMsg, "Illegal character! (line: %d, pos: %d)", data->lineNum, i+1) +1;
                    }

                    i--;
                    state = Stop;    
                    data->nodeState = lcini_ERROR;
                    pstate = Error;
                    break;
 
                case Stop:
                    if(in[i] == '\n' || in[i] == '\r' || in[i] == '\0' || pstate == Error){   /* Line Ended */
                        state = Stop;
                        if(strNullLen(data->section) == 0){data->sectionLen = 0;}
                        if(strNullLen(data->param) == 0){data->paramLen = 0;}
                        if(strNullLen(data->value) == 0){data->valueLen = 0;}
                        if(strNullLen(data->comment) == 0){data->commentLen = 0;}
                        if(strNullLen(data->errorMsg) == 0){data->errorMsgLen = 0;}
                        data->section = lciniStrResize(data->section, len, data->sectionLen);
                        data->param = lciniStrResize(data->param, len, data->paramLen);
                        data->value = lciniStrResize(data->value, vallen, data->valueLen);
                        data->comment = lciniStrResize(data->comment, len, data->commentLen);
                        data->errorMsg = lciniStrResize(data->errorMsg, 256, data->errorMsgLen);
                        if(data->sectionStartPos < 0 && data->param==NULL && data->value==NULL && data->comment==NULL && data->errorMsg==NULL){
                            data->nodeState = lcini_EMPTY;
                            data->valueDraw = lcini_EMPTYVAL;
                        }
                        return data;
                    } else {    /* Everything else -> ERROR */
                        state = Error;
                    }
                    pstate = Stop;
                    break;

                default:
                    state = Start;
                    break;
            }
        }
        return data;
    }
}




lcini_data *lciniCreateNode( lcini_data *head, int lineLen ){ /* Creates one Node */
    lcini_data *curr;

    curr = (lcini_data *) calloc(1, sizeof(lcini_data));
    curr->nodeState = lcini_EMPTY; 
    curr->lineNum = 0;
    curr->lineLen = lineLen;

    if(lineLen > 0){
        curr->section  = (uint8_t *) calloc(lineLen, sizeof(uint8_t));
        curr->param    = (uint8_t *) calloc(lineLen, sizeof(uint8_t));
        curr->value    = (uint8_t *) calloc(lineLen, sizeof(uint8_t));
        curr->comment  = (uint8_t *) calloc(lineLen, sizeof(uint8_t));
        curr->errorMsg = (uint8_t *) calloc(lineLen, sizeof(uint8_t));
    } else {
        lineLen = 0;
    }
    curr->sectionLen = lineLen;
    curr->sectionStartPos = -1;
    curr->paramLen = lineLen;
    curr->paramStartPos = -1;
    curr->valueLen = lineLen;
    curr->valueStartPos = -1;
    curr->valueDraw = lcini_EMPTYVAL;
    curr->commentLen = lineLen;
    curr->commentStartPos = -1;
    curr->commentSign = '\0';
    curr->errorMsgLen=lineLen;
    curr->next = NULL;

    if(head != NULL){
        head->next = curr;
    }
    return curr;
}       

lcini_data *lciniDestroyNodes( lcini_data *head){ /* Destroys Nodes from HEAD to the end */
    lcini_data *tmp, *node=head;
        while(node != NULL){
            free(node->section);
            free(node->param);
            free(node->value);
            free(node->comment);
            free(node->errorMsg);
            tmp = node;
            node = node->next;
            free(tmp);
        }
    return NULL;
}



struct lcini_data *lciniReadOut(const char *filename){      /* Reads the entire file to a linked-list */

    int  c=0; 
    uint8_t *buff; 
    FILE *fp=NULL;
    int32_t linemax, line=0, pos=0;
    /*char cc;*/
    struct lcini_data *prev=NULL, *curr=NULL, *list=NULL;

    fp = fopen(filename, "rb"); 
    if(!fp ){   /* fp == NULL */
        list = lciniCreateNode(NULL, 256);
        list->errorMsg = lciniStrResize(list->errorMsg, list->errorMsgLen, 256);
        list->errorMsgLen = sprintf((char*)list->errorMsg, "File opening error. Errno: %d (%s)", errno, strerror(errno) );
        list->nodeState = lcini_ERROR;
    
    } else {
        linemax = lciniFileMaxLineLen(fp) +1; 
        buff = (uint8_t*)malloc(linemax*sizeof(uint8_t));
        memset(buff, 0, linemax*sizeof(uint8_t));

        while( c != EOF){
            c = fgetc(fp);
            /*cc = c;*/     /* debug */

            if( c == '\n' || c == EOF){   
                line++;
                buff[pos] = '\n';

                if(curr == NULL || curr->nodeState != lcini_MULTILINE ){
                    curr = lciniCreateNode(NULL, linemax);
                }
                if(list == NULL){   /* First node */
                    list = curr;
                }
                if(prev && curr && prev != curr && prev->section ){ /* Copy SECTION string from previous node to current */
                    curr->section = lciniStrResize(curr->section, curr->sectionLen, prev->sectionLen);
                    memcpy(curr->section, prev->section, prev->sectionLen);    
                    curr->sectionLen = prev->sectionLen;
                }
                if(curr){      /* Call the Finite-State-Machine processor */
                    curr->lineNum = line;
                    curr->lineLen = pos + 1;
                    curr = iniFSM(curr, buff, linemax);
                }
                if(curr->nodeState == lcini_EMPTY){   /* Dropping empty lines */
                    if(list == curr || prev == curr ){   /* File contains empty lines */
                        list = NULL;
                        prev = NULL;
                    }
                    curr = lciniDestroyNodes(curr);
                }

                if(!prev){ /* Prew is null */
                    prev = curr;
                } else {    /* Register current for next step */
                    prev->next = curr;
                    if(prev->next && curr->nodeState != lcini_MULTILINE){  /* Step only, when current is not multiline */
                        prev = prev->next;
                    }
                }
                
                if(curr && curr->nodeState == lcini_ERROR){   /* Stop on first ERROR */
                    /* return list; */
                }
                pos = 0;
                memset(buff, 0, linemax*sizeof(uint8_t));
            } else {
                buff[pos] = c;
                pos++;
            }
        }
        free(buff);
    }

    if(fp){
        fclose(fp);
    }
    return list;
}



int lciniReadOutOwn(const char *filename){      /* Reads the entire file to a linked-list */

    int  c=0; 
    uint8_t *buff=NULL; 
    FILE *fp=NULL;
    int64_t linemax, line=0, pos=0;
    struct lcini_data *curr;

    curr = lciniCreateNode(NULL,256);
    buff = (uint8_t*) malloc(256*sizeof(uint8_t));
    memset(buff, 0, 256*sizeof(uint8_t));
    fp = fopen(filename, "rb"); 
    
    if(!fp){   /* fp == NULL */
        if( mylciniReadOutFunct != NULL){
            sprintf((char*)buff, "File opening error. Errno: %d (%s)", errno, strerror(errno) );
            (*mylciniReadOutFunct)(0,0, NULL,0, NULL,0, NULL,0, NULL,0, (char*)buff, 256);
        }
    
    } else {
        linemax = lciniFileMaxLineLen(fp) +1; 
        buff = lciniStrResize(buff, 256, linemax);

        while( c != EOF){
            c = fgetc(fp);

            if( c == '\n' || c == EOF){   
                line++;
                buff[pos] = '\n';

                if(curr){      /* Call the Finite-State-Machine processor */
                    curr->lineNum = line;
                    curr->lineLen = pos + 1;
                    iniFSM(curr, buff, linemax);
                } 
                if(curr->nodeState != lcini_EMPTY && mylciniReadOutFunct != NULL ){   /* Call with function ptr */
                    (*mylciniReadOutFunct)(line, pos+1, (char*)curr->section, curr->sectionLen, (char*)curr->param, curr->paramLen, (char*)curr->value, curr->valueLen, (char*)curr->comment, curr->commentLen, (char*)curr->errorMsg, curr->errorMsgLen);
                }
                curr->paramLen = 0;
                curr->valueLen = 0;
                curr->commentLen = 0;
                curr->errorMsgLen = 0;
                
                pos = 0;
                memset(buff, 0, linemax*sizeof(uint8_t));
            } else {
                buff[pos] = c;
                pos++;
            }
        }
    }

    if(fp){
        fclose(fp);
    }
    lciniDestroyNodes(curr);
    free(buff);
    return line;
}



/*
char *lciniGetFromFileStr(const char *filename, const char *section, const char *parameter, char *buff, int len){

    int  c=0; 
    uint8_t *buff=NULL;
    char *value=NULL; 
    FILE *fp=NULL;
    int64_t linemax, line=0, pos=0;
    struct lcini_data *curr;

    fp = fopen(filename, "rb"); 
    
    if(!fp){   
        return NULL;
    } else {
        linemax = lciniFileMaxLineLen(fp) +1; 
        curr = lciniCreateNode(NULL,linemax);
        buff = (uint8_t *) malloc(linemax*sizeof(uint8_t));
        memset(buff, 0, linemax*sizeof(uint8_t));
        value = malloc(1*sizeof(char));

        while( c != EOF){
            c = fgetc(fp);

            if( c == '\n' || c == EOF){   
                line++;
                buff[pos] = '\n';

                if(curr){      
                    curr->lineNum = line;
                    curr->lineLen = pos + 1;
                    iniFSM(curr, buff, linemax);
                } 
                if(curr->nodeState != lcini_EMPTY && value && section && param){   
                    if(strcmp(curr->section, section) == 0  && strcmp(curr->param, param) == 0){
                        value = lciniStrResize(value, 1, curr->valueLen);
                    }
                }
                curr->paramLen = 0;
                curr->valueLen = 0;
                curr->commentLen = 0;
                curr->errorMsgLen = 0;
                
                pos = 0;
                memset(buff, 0, linemax*sizeof(uint8_t));
            } else {
                buff[pos] = c;
                pos++;
            }
        }
        lciniDestroyNodes(curr);
        free(buff);
    }

    if(fp){
        fclose(fp);
    }
    return value; 
}
*/