From cd28b1ec07c8ad615078a1ec55f4f9dfc6b92504 Mon Sep 17 00:00:00 2001 From: M.Gergő Date: Sun, 17 May 2020 06:39:32 +0200 Subject: New Read function (Own) --- engine.html | 18 ++++ src/ini_read.c | 254 +++++++++++++++++++++++++++++++-------------------- src/inirw_internal.h | 34 ++++--- src/lightconfini.h | 72 +++++++++++++++ src/main.c | 45 +++++---- tests/bom.ini | 2 +- tests/test.ini | 3 +- 7 files changed, 291 insertions(+), 137 deletions(-) create mode 100644 engine.html create mode 100644 src/lightconfini.h diff --git a/engine.html b/engine.html new file mode 100644 index 0000000..095071f --- /dev/null +++ b/engine.html @@ -0,0 +1,18 @@ + + + + + + + Welcome file + + + + +

Engine is using a Finite State Machine

+

(markdown graph)

+
\n,\r
comment_sign
any_space
utf8_BOM
square_bracket
alpha_num
other
any_character
\n, \r
\n, \r
comment_sign
square_bracket
alpha_num
other
square_bracket
alpha_num
other
\n, \r
any_space
comment_sign
other
equal_sign
alpha_num
any_space
other
equal_sign
any_space
other
\n, \r
comment_sign
double_quotation_mark
any_space
alpha_num
other
\n, \r
comment_sign
alpha_num
any_space
other
prev==quoted_val
\n \r
comment_sign
any_space
other
double_quotation_mark
backslash
\r \n \0
any_other
any_other
\n \r \0 prev==ERROR
other
Start
Stop
Comment
begin_space
section
label
ERROR
sect_end
value_pre
label_end
quoted_val
value
value_end
BS
+
+ + + diff --git a/src/ini_read.c b/src/ini_read.c index bbfb68e..9ac834c 100644 --- a/src/ini_read.c +++ b/src/ini_read.c @@ -9,8 +9,13 @@ #define ini_read_c #include "inirw_internal.h" +#include "lightconfini.h" +/* +extern void (mylciniReadOutFunct)(int line, int linelen, char *section, int sectionlen, char *param, int paramlen, char *value, int valuelen, char *comment, int commentlen, char *error, int errorlen ); +*/ + size_t strNullLen(const char *str){ if(str == NULL){ @@ -20,7 +25,7 @@ size_t strNullLen(const char *str){ } } -char *strResize(char *ptr, size_t oldsize, size_t newsize){ +char *lciniStrResize(char *ptr, size_t oldsize, size_t newsize){ char *tmp; if(newsize <= 0){ /* deleting */ @@ -29,25 +34,18 @@ char *strResize(char *ptr, size_t oldsize, size_t newsize){ } else if(newsize != oldsize){ /* If any changes needed */ tmp = (char *) malloc(newsize*sizeof(char)); - memset(tmp, 0, newsize); + memset(tmp, 0, newsize*sizeof(char)); if(tmp == NULL){ /* String is not changed at malloc error */ return ptr; } else if(ptr == NULL) { return tmp; - /*} else if(newsize > oldsize) { //FEL - strncpy(tmp, ptr, oldsize); // old < new - //snprintf(tmp, newsize, "%s", ptr); - free(ptr); - return tmp; - */ - } else /*if(newsize < oldsize)*/ { + + } else { strncpy(tmp, ptr, newsize); /* c89 */ /*snprintf(tmp, newsize, "%s", ptr);*/ /* c99 */ free(ptr); return tmp; - } /*else { //Ide sosem jutunk - return ptr; - }*/ + } } else { return ptr; } @@ -92,10 +90,6 @@ int eescape(int c){ return 't'; } else if(c == '\v'){ /* Vertical tab */ return 'v'; - /*} else if(c == '\''){ //Apostrophe debug - // return '\''; - //} else if(c == '"'){ //Double quotation mark debug - // return '\"'; */ /*} else if(c < 0x20){ //debug return '~';*/ } else { /* Original is OK */ @@ -139,7 +133,7 @@ int checkspace(int c){ /* Only for ASCII characters */ -size_t getFileMaxLineLen(FILE *tfd){ +size_t lciniFileMaxLineLen(FILE *tfd){ size_t c=0; size_t i=0, max=0; @@ -164,7 +158,7 @@ size_t getFileMaxLineLen(FILE *tfd){ } } -struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t len){ +struct lcini_data *iniFSM(struct lcini_data *data, const char *in, int32_t len){ int32_t i,j, vallen=len; enum ini_states pstate=Start, state=Start; @@ -175,30 +169,30 @@ struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t } else { for(i=0, j=0; inodeState == lci_MULTILINE){ /* Bypass to the DQM collection */ + if(data->nodeState == lcini_MULTILINE){ /* Bypass to the DQM collection */ state = DqmW; j = data->valueLen-2; /* (x-1) => \0; (x-2) => j--; */ - data->value = strResize(data->value, data->valueLen, data->valueLen+len+1); /* strResize(ptr, oldsize, newsize */ + data->value = lciniStrResize(data->value, data->valueLen, data->valueLen+len+1); /* lciniStrResize(ptr, oldsize, newsize */ vallen = data->valueLen+len+1; - data->nodeState = lci_CONTINUE; + 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 */ + }else if((unsigned char)in[i] == 0xEF || (unsigned char)in[i] == 0xBB || (unsigned char)in[i] == 0xBF || (unsigned char)in[i] == 0xFF || (unsigned char)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 = strResize(data->comment, data->commentLen, len); + data->comment = lciniStrResize(data->comment, data->commentLen, len); memset(data->comment, 0, len); data->commentStartPos = i; data->sectionStartPos = -1; /* Comment only line */ @@ -206,13 +200,13 @@ struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t } else if(in[i] == '['){ /* Section start */ j = -1; state = SectEndW; - data->section = strResize(data->section, data->sectionLen, len); + 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 = strResize(data->param, data->paramLen, len); + data->param = lciniStrResize(data->param, data->paramLen, len); memset(data->param, 0, len); data->paramStartPos = i; i--; @@ -220,8 +214,8 @@ struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t state = Error; i--; } - data->nodeState = lci_EMPTY; - data->valueDraw = lci_EMPTYVAL; + data->nodeState = lcini_EMPTY; + data->valueDraw = lcini_EMPTYVAL; pstate = Start; break; @@ -233,7 +227,7 @@ struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t } else if (in[i] == ';' || in[i] == '#'){ /* Comment is coming */ j = -1; state = CommEndW; - data->comment = strResize(data->comment, data->commentLen, len); + 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 */ @@ -241,13 +235,13 @@ struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t } else if(in[i] == '['){ /* Section is starting */ j = -1; state = SectEndW; - data->section = strResize(data->section, data->sectionLen, len); + 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 = strResize(data->param, data->paramLen, len); + data->param = lciniStrResize(data->param, data->paramLen, len); memset(data->param, 0, len); data->paramStartPos = i; i--; @@ -264,7 +258,7 @@ struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t case CommEndW: /* Till comment to the line end */ if(in[i] == '\n' || in[i] == '\r'){ state = Stop; - data->nodeState = lci_READY; + data->nodeState = lcini_READY; data->comment[j] = '\0'; data->commentLen = j+1; i--; @@ -300,18 +294,18 @@ struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t case SectEndD: /* Section collected, then: SP(), line_end, or comment */ if(in[i] == '\n' || in[i] == '\r'){ state = Stop; - data->nodeState = lci_READY; + 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 = strResize(data->comment, data->commentLen, len); + data->comment = lciniStrResize(data->comment, data->commentLen, len); memset(data->comment, 0, len); data->commentStartPos = i; data->commentSign = in[i]; - data->nodeState = lci_CONTINUE; + data->nodeState = lcini_CONTINUE; } else { state = Error; /* wrong character in line */ i--; @@ -356,37 +350,37 @@ struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t 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 = lci_READY; - data->valueDraw = lci_SIMPLEVAL; + data->nodeState = lcini_READY; + data->valueDraw = lcini_SIMPLEVAL; i--; } else if(in[i] == ';' || in[i] == '#'){ /* Comment */ j = -1; state = CommEndW; - data->comment = strResize(data->comment, data->commentLen, len); + data->comment = lciniStrResize(data->comment, data->commentLen, len); memset(data->comment, 0, len); data->commentStartPos = i; - data->nodeState = lci_CONTINUE; - data->valueDraw = lci_SIMPLEVAL; + 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 = strResize(data->value, data->valueLen, len); + data->value = lciniStrResize(data->value, data->valueLen, len); memset(data->value, 0, len); data->valueStartPos = i; - data->nodeState = lci_CONTINUE; - data->valueDraw = lci_DQUOTEDVAL; + 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 = strResize(data->value, data->valueLen, len); + data->value = lciniStrResize(data->value, data->valueLen, len); memset(data->value, 0, len); data->valueStartPos = i; - data->nodeState = lci_CONTINUE; - data->valueDraw = lci_SIMPLEVAL; + data->nodeState = lcini_CONTINUE; + data->valueDraw = lcini_SIMPLEVAL; i--; } else { state = Error; @@ -399,21 +393,21 @@ struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t case ValW: /* Normal_Value collector */ if(in[i] == '\n' || in[i] == '\r'){ /* end -> new_line */ state = Stop; - data->nodeState = lci_READY; + data->nodeState = lcini_READY; data->value[j] = '\0'; data->valueLen = j + 1; i--; } else if(in[i] == ';' || in[i] == '#'){ /* comment */ - data->nodeState = lci_CONTINUE; + data->nodeState = lcini_CONTINUE; data->value[j] = '\0'; data->valueLen = j+1; state = CommEndW; j = -1; - data->comment = strResize(data->comment, data->commentLen, len); + data->comment = lciniStrResize(data->comment, data->commentLen, len); memset(data->comment, 0, len); data->commentStartPos = i; data->commentSign = in[i]; - data->nodeState = lci_CONTINUE; + data->nodeState = lcini_CONTINUE; /* } else if( in[i] == '\\' ){ // Backslash support // j--; //A '\' nem számít bele! // state = Bslsh; */ @@ -422,7 +416,7 @@ struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t } else if(checkspace(in[i])){ /* SPACE arrived -> line_end */ data->value[j] = '\0'; data->valueLen = j+1; - data->nodeState = lci_READY; + data->nodeState = lcini_READY; state = ValFSP; i--; } else { @@ -467,11 +461,11 @@ struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t } else if( in[i] == ';' || in[i] == '#'){ j = -1; state = CommEndW; - data->comment = strResize(data->comment, data->commentLen, len); + data->comment = lciniStrResize(data->comment, data->commentLen, len); memset(data->comment, 0, len); data->commentStartPos = i; data->commentSign = in[i]; - data->nodeState = lci_CONTINUE; + data->nodeState = lcini_CONTINUE; } else if(checkspace(in[i])){ state = ValFSP; /* SP() -> ermain here */ } else { @@ -486,7 +480,7 @@ struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t if( in[i] == '\"'){ /* Second DQM => Sring_end */ data->value[j] = '\0'; data->valueLen = j+1; - data->nodeState = lci_READY; + data->nodeState = lcini_READY; state = ValFSP; } else if ( in[i] == '\\'){ /* Backslash */ j--; @@ -494,16 +488,16 @@ struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t } 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 = lci_MULTILINE; - data->valueDraw = lci_MULTILINEVAL; + 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 = lci_MULTILINE; - data->valueDraw = lci_MULTILINEVAL; + 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 */ @@ -517,7 +511,7 @@ struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t case Error: - data->errorMsg = strResize(data->errorMsg, data->errorMsgLen, 256); /* Returns zero-filled string */ + data->errorMsg = lciniStrResize(data->errorMsg, data->errorMsgLen, 256); /* Returns zero-filled string */ if(pstate == SectEndW || pstate == SectEndD){ data->errorMsgLen = sprintf(data->errorMsg, "Illegal character or EMPTY SECTION! (line: %d, pos: %d)", data->lineNum, i+1) +1; @@ -534,7 +528,7 @@ struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t i--; state = Stop; - data->nodeState = lci_ERROR; + data->nodeState = lcini_ERROR; pstate = Error; break; @@ -546,14 +540,14 @@ struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t 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 = strResize(data->section, len, data->sectionLen); - data->param = strResize(data->param, len, data->paramLen); - data->value = strResize(data->value, vallen, data->valueLen); - data->comment = strResize(data->comment, len, data->commentLen); - data->errorMsg = strResize(data->errorMsg, 256, data->errorMsgLen); + 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 = lci_EMPTY; - data->valueDraw = lci_EMPTYVAL; + data->nodeState = lcini_EMPTY; + data->valueDraw = lcini_EMPTYVAL; } return data; } else { /* Everything else -> ERROR */ @@ -574,26 +568,30 @@ struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t -lci_data *createNode( lci_data *head, int64_t lineLen ){ /* Creates one Node */ - lci_data *curr; +lcini_data *lciniCreateNode( lcini_data *head, int64_t lineLen ){ /* Creates one Node */ + lcini_data *curr; - curr = (lci_data *) calloc(1, sizeof(lci_data)); - curr->nodeState = lci_EMPTY; + curr = (lcini_data *) calloc(1, sizeof(lcini_data)); + curr->nodeState = lcini_EMPTY; curr->lineNum = 0; curr->lineLen = lineLen; - curr->section = (char *) calloc(lineLen, sizeof(char)); - curr->param = (char *) calloc(lineLen, sizeof(char)); - curr->value = (char *) calloc(lineLen, sizeof(char)); - curr->comment = (char *) calloc(lineLen, sizeof(char)); - curr->errorMsg = (char *) calloc(lineLen, sizeof(char)); + if(lineLen > 0){ + curr->section = (char *) calloc(lineLen, sizeof(char)); + curr->param = (char *) calloc(lineLen, sizeof(char)); + curr->value = (char *) calloc(lineLen, sizeof(char)); + curr->comment = (char *) calloc(lineLen, sizeof(char)); + curr->errorMsg = (char *) calloc(lineLen, sizeof(char)); + } else { + lineLen = 0; + } curr->sectionLen = lineLen; curr->sectionStartPos = -1; curr->paramLen = lineLen; curr->paramStartPos = -1; curr->valueLen = lineLen; curr->valueStartPos = -1; - curr->valueDraw = lci_EMPTYVAL; + curr->valueDraw = lcini_EMPTYVAL; curr->commentLen = lineLen; curr->commentStartPos = -1; curr->commentSign = '\0'; @@ -606,8 +604,8 @@ lci_data *createNode( lci_data *head, int64_t lineLen ){ /* Creates one Node */ return curr; } -lci_data *destroyNodes( lci_data *head){ /* Destroys Nodes from HEAD to the end */ - lci_data *tmp, *node=head; +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); @@ -623,26 +621,26 @@ lci_data *destroyNodes( lci_data *head){ /* Destroys Nodes from HEAD to the end -struct lci_data *iniReadOut(const char *filename){ +struct lcini_data *lciniReadOut(const char *filename){ /* Reads the entire file to a linked-list */ int c=0; - unsigned char *buff; + char *buff; FILE *fp=NULL; int64_t linemax, line=0, pos=0; /*char cc;*/ - struct lci_data *prev=NULL, *curr=NULL, *list = NULL; + struct lcini_data *prev=NULL, *curr=NULL, *list = NULL; fp = fopen(filename, "rb"); if(!fp ){ /* fp == NULL */ - list = createNode(NULL, 256); - list->errorMsg = strResize(list->errorMsg, list->errorMsgLen, 256); + list = lciniCreateNode(NULL, 256); + list->errorMsg = lciniStrResize(list->errorMsg, list->errorMsgLen, 256); list->errorMsgLen = sprintf(list->errorMsg, "File opening error. Errno: %d (%s)", errno, strerror(errno) ); - list->nodeState = lci_ERROR; + list->nodeState = lcini_ERROR; } else { - linemax = getFileMaxLineLen(fp) +1; - buff = (unsigned char *) malloc(linemax*sizeof(char)); - memset(buff, 0, linemax); + linemax = lciniFileMaxLineLen(fp) +1; + buff = (char *) malloc(linemax*sizeof(char)); + memset(buff, 0, linemax*sizeof(char)); while( c != EOF){ c = fgetc(fp); @@ -652,14 +650,14 @@ struct lci_data *iniReadOut(const char *filename){ line++; buff[pos] = '\n'; - if(curr == NULL || curr->nodeState != lci_MULTILINE ){ - curr = createNode(NULL, linemax); + 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 = strResize(curr->section, curr->sectionLen, prev->sectionLen); + curr->section = lciniStrResize(curr->section, curr->sectionLen, prev->sectionLen); memcpy(curr->section, prev->section, prev->sectionLen); curr->sectionLen = prev->sectionLen; } @@ -668,29 +666,28 @@ struct lci_data *iniReadOut(const char *filename){ curr->lineLen = pos + 1; curr = iniFSM(curr, buff, linemax); } - if(curr->nodeState == lci_EMPTY){ /* Dropping empty lines */ + if(curr->nodeState == lcini_EMPTY){ /* Dropping empty lines */ if(list == curr || prev == curr ){ /* File contains empty lines */ list = NULL; prev = NULL; } - curr = destroyNodes(curr); + curr = lciniDestroyNodes(curr); } - if(!prev){ /* Prew is null */ prev = curr; } else { /* Register current for next step */ prev->next = curr; - if(prev->next && curr->nodeState != lci_MULTILINE){ /* Step only, when current is not multiline */ + if(prev->next && curr->nodeState != lcini_MULTILINE){ /* Step only, when current is not multiline */ prev = prev->next; } } - if(curr && curr->nodeState == lci_ERROR){ /* Stop on first ERROR */ + if(curr && curr->nodeState == lcini_ERROR){ /* Stop on first ERROR */ /* return list; */ } pos = 0; - memset(buff, 0, linemax); + memset(buff, 0, linemax*sizeof(char)); } else { buff[pos] = c; pos++; @@ -707,4 +704,67 @@ struct lci_data *iniReadOut(const char *filename){ +int lciniReadOutOwn(const char *filename){ /* Reads the entire file to a linked-list */ + + int c=0; + char *buff=NULL; + FILE *fp=NULL; + int64_t linemax, line=0, pos=0; + struct lcini_data curr; + /* char cc;*/ + + curr.section = NULL; + curr.param = NULL; + curr.value = NULL; + curr.comment = NULL; + curr.errorMsg = NULL; + buff = (char *) malloc(256*sizeof(char)); + memset(buff, 0, 256*sizeof(char)); + fp = fopen(filename, "rb"); + + if(!fp && mylciniReadOutFunct != NULL){ /* fp == NULL */ + + sprintf(buff, "File opening error. Errno: %d (%s)", errno, strerror(errno) ); + mylciniReadOutFunct(0,0, NULL,0, NULL,0, NULL,0, NULL,0, buff, 256); + + } else { + linemax = lciniFileMaxLineLen(fp) +1; + buff = lciniStrResize(buff, 256, linemax); + + while( c != EOF){ + c = fgetc(fp); + /*cc = c;*/ /* debug */ + + if( c == '\n' || c == EOF){ + line++; + buff[pos] = '\n'; + + + if(1){ /* Call the Finite-State-Machine processor */ + curr.lineNum = line; + curr.lineLen = pos + 1; + iniFSM(&curr, buff, linemax); + } + if(curr.nodeState != lcini_EMPTY && mylciniReadOutFunct != NULL ){ /* Dropping empty lines */ + /* mylciniReadOutFunct(line, pos+1, curr.section, curr.sectionLen, curr.param, curr.paramLen, curr.value, curr.valueLen, curr.comment, curr.commentLen, curr.errorMsg, curr.errorMsgLen);*/ + (*mylciniReadOutFunct)(line, pos+1, curr.section, curr.sectionLen, curr.param, curr.paramLen, curr.value, curr.valueLen, curr.comment, curr.commentLen, curr.errorMsg, curr.errorMsgLen); + } + + pos = 0; + memset(buff, 0, linemax*sizeof(char)); + } else { + buff[pos] = c; + pos++; + } + } + } + + if(fp){ + fclose(fp); + } + free(buff); + return line; +} + + diff --git a/src/inirw_internal.h b/src/inirw_internal.h index a75a70b..c569fd7 100644 --- a/src/inirw_internal.h +++ b/src/inirw_internal.h @@ -1,13 +1,13 @@ #ifndef INI_READ_H_INCLUDED #define INI_READ_H_INCLUDED -/* #include "deftypes.h" */ #include /* int64_t*/ +#include "lightconfini.h" +/* +typedef struct lcini_data { -typedef struct lci_data { - - enum nodeState {lci_EMPTY, lci_READY, lci_CONTINUE, lci_MULTILINE, lci_ERROR } nodeState; + enum nodeState {lcini_EMPTY, lcini_READY, lcini_CONTINUE, lcini_MULTILINE, lcini_ERROR } nodeState; int32_t lineNum; int32_t lineLen; @@ -20,7 +20,7 @@ typedef struct lci_data { char *value; int32_t valueLen; int32_t valueStartPos; - enum valueDraw {lci_EMPTYVAL, lci_SIMPLEVAL, lci_MULTILINEVAL, lci_DQUOTEDVAL} valueDraw; + enum valueDraw {lcini_EMPTYVAL, lcini_SIMPLEVAL, lcini_MULTILINEVAL, lcini_DQUOTEDVAL} valueDraw; char *comment; int32_t commentLen; int32_t commentStartPos; @@ -28,23 +28,22 @@ typedef struct lci_data { char *errorMsg; int32_t errorMsgLen; - struct lci_data *next; -} lci_data; - - + struct lcini_data *next; +} lcini_data; +*/ -struct lci_data *iniReadOut(const char *filename); -/* int64_t getFileMaxLineLen(FILE *tfd); */ +/* +struct lcini_data *iniReadOut(const char *filename); char *strResize(char *ptr, size_t oldsize, size_t newsize); -lci_data *destroyNodes( lci_data *head); -lci_data *createNode( lci_data *head, int64_t lineLen ); +lcini_data *destroyNodes( lcini_data *head); +lcini_data *createNode( lcini_data *head, int64_t lineLen ); size_t getFileMaxLineLen(FILE *tfd); - +*/ #if defined(ini_read_c) || defined(ini_write_c) enum ini_states {Start, BgnSp, CommEndW, SectEndW, SectEndD, EqW1, EqW2, ValPSP, ValW, ValFSP, DqmW, Bslsh, Error, Stop }; size_t strNullLen(const char *str); -struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t len); +struct lcini_data *iniFSM(struct lcini_data *data, const char *in, int32_t len); int eescape(int c); int isascalnum(int c); /* Check if input is ASCII Alpha-numeric */ int checkspace(int c); /* Only for ASCII characters */ @@ -60,9 +59,8 @@ int unescape(int c); #endif /* ini_read_c, ini_write_c*/ - +/* char *lciGETtoStr( const char *section, const char *param, char *dest, size_t dstlen ); -/*int lciGETtoStrlen(const char *section, const char *param, ...); */ int8_t lciGETtoInt8( const char *filename, const char *section, const char *param); int16_t lciGETtoInt16(const char *filename, const char *section, const char *param); int32_t lciGETtoInt32(const char *filename, const char *section, const char *param); @@ -71,7 +69,7 @@ int64_t lciGETtoInt64(const char *filename, const char *section, const char * double lciGETtoDlb(const char *filename, const char *section, const char *param); float lciGETtoFlt(const char *filename, const char *section, const char *param); long int lciGETtoLng(const char *filename, const char *section, const char *param); - +*/ diff --git a/src/lightconfini.h b/src/lightconfini.h new file mode 100644 index 0000000..ee5a7c3 --- /dev/null +++ b/src/lightconfini.h @@ -0,0 +1,72 @@ +#ifndef LIGHTCONFINI_H_INCLUDED +#define LIGHTCONFINI_H_INCLUDED +#include /* int64_t*/ + +typedef struct lcini_data { + enum nodeState {lcini_EMPTY, lcini_READY, lcini_CONTINUE, lcini_MULTILINE, lcini_ERROR } nodeState; + int32_t lineNum; + int32_t lineLen; + + char *section; + int32_t sectionLen; + int32_t sectionStartPos; + char *param; + int32_t paramLen; + int32_t paramStartPos; + char *value; + int32_t valueLen; + int32_t valueStartPos; + enum valueDraw {lcini_EMPTYVAL, lcini_SIMPLEVAL, lcini_MULTILINEVAL, lcini_DQUOTEDVAL} valueDraw; + char *comment; + int32_t commentLen; + int32_t commentStartPos; + char commentSign; + char *errorMsg; + int32_t errorMsgLen; + + struct lcini_data *next; +} lcini_data; + + +typedef struct lcini_retdata{ + char *value; + int32_t vallen; + char *error; + int32_t errorlen; +} lcini_retdata; + +/* Ha maga függvény van átpakolva, nevestül, testestül */ +/* extern void (mylciniReadOutFunct)(int line, int linelen, char *section, int sectionlen, char *param, int paramlen, char *value, int valuelen, char *comment, int commentlen, char *error, int errorlen ) ; */ +/* Ha csak egy fggvényre mutató ptr */ +typedef void (*lcinimyReadFunc)(int line, int linelen, char *section, int sectionlen, char *param, int paramlen, char *value, int valuelen, char *comment, int commentlen, char *error, int errorlen ) ; +extern lcinimyReadFunc mylciniReadOutFunct; + + +struct lcini_data *lciniReadOut(const char *filename); +int lciniReadOutOwn(const char *filename); + +char *lciniGet(const char *filename, const char *section, const char *parameter, int32_t bufflen); +lcini_retdata *lciniGet2(const char *filename, const char *section, const char *parameter); + + + + + +char *lciGETtoStr( const char *section, const char *param, char *dest, size_t dstlen ); +/*int lciGETtoStrlen(const char *section, const char *param, ...); */ +int8_t lciGETtoInt8( const char *filename, const char *section, const char *param); +int16_t lciGETtoInt16(const char *filename, const char *section, const char *param); +int32_t lciGETtoInt32(const char *filename, const char *section, const char *param); +int64_t lciGETtoInt64(const char *filename, const char *section, const char *param); + +double lciGETtoDlb(const char *filename, const char *section, const char *param); +float lciGETtoFlt(const char *filename, const char *section, const char *param); +long int lciGETtoLng(const char *filename, const char *section, const char *param); + +char *lciniStrResize(char *ptr, size_t oldsize, size_t newsize); +lcini_data *lciniDestroyNodes( lcini_data *head); +lcini_data *lciniCreateNode( lcini_data *head, int64_t lineLen ); +size_t lciniFileMaxLineLen(FILE *tfd); + +#endif /* LIGHTCONFINI_H_INCLUDED */ + diff --git a/src/main.c b/src/main.c index 37ed4f5..3f7af09 100644 --- a/src/main.c +++ b/src/main.c @@ -4,17 +4,28 @@ #include #include -#include "inirw_internal.h" +/* #include "inirw_internal.h" */ +#include "lightconfini.h" #define main_c - + +int lens=16, lenp=16, lenv=16, lenc=24, elen=55; + + +void myfunct(int line, int linelen, char *section, int sectionlen, char *param, int paramlen, char *value, int valuelen, char *comment, int commentlen, char *error, int errorlen ){ + + printf("LN: %d,\tLL: %d,\tSC: %*s,%2d P: %*s,%2d V: %*s,%2d C: %*s,%2d ER: %*s \n", line, linelen, lens, section,sectionlen, + lenp, param, paramlen, lenv, value, valuelen, lenc, comment, commentlen, elen, error); +} +/*lcinimyReadFunc mylciniReadOutFunct=myfunct;*/ +lcinimyReadFunc mylciniReadOutFunct=NULL; + int main(int argc, char* argv[]){ - + int len; char filename[4096] = "tests/test.ini", *buff1, *buff2, *buff3, *buff4, *buff5; - lci_data *ini=NULL, *tmp=NULL; - int len=0, elen=54, lens=0, lenp=0, lenv=0, lenc=0; + lcini_data *ini=NULL, *tmp=NULL; FILE *fp; if(argc > 1){ @@ -22,18 +33,11 @@ int main(int argc, char* argv[]){ /* snprintf(filename, 4096, "%s", argv[1]); */ sprintf(filename, "%s", argv[1]); } - - fp = fopen(filename, "rb"); - len = getFileMaxLineLen(fp)+1; + len = lciniFileMaxLineLen(fp)+1; if(fp != NULL) { fclose(fp);} /* len = 24000; */ /*Debug*/ - lens=16; - lenp=16; - lenv=16; - lenc=44; - buff1 = calloc((len+100), sizeof(char)); buff2 = calloc((len+100), sizeof(char)); @@ -44,13 +48,9 @@ int main(int argc, char* argv[]){ printf("\nLineMax: %d\n\n",len); - ini = iniReadOut(filename); - - + ini = lciniReadOut(filename); tmp = ini; while(tmp != NULL){ - - /* snprintf(buff1, len+3, "'%s' %3ld",tmp->section, tmp->sectionLen); snprintf(buff2, len+3, "'%s' %3ld",tmp->param, tmp->paramLen); snprintf(buff3, len+3, "'%s' %3ld",tmp->value, tmp->valueLen); @@ -69,14 +69,19 @@ int main(int argc, char* argv[]){ tmp=tmp->next; } - free(buff1); free(buff2); free(buff3); free(buff4); free(buff5); + lciniDestroyNodes( ini); + + + + + + lciniReadOutOwn(filename); - destroyNodes( ini); return 0; diff --git a/tests/bom.ini b/tests/bom.ini index f527b00..291abd8 100644 --- a/tests/bom.ini +++ b/tests/bom.ini @@ -1,5 +1,5 @@ [bom_section] ;OK bom_name=bom_value ;OK -key“ = value“ ;ERROR +key“ = value“ ;ER key="value" ;OK 10= 10 \ No newline at end of file diff --git a/tests/test.ini b/tests/test.ini index a69444f..fcac46c 100644 --- a/tests/test.ini +++ b/tests/test.ini @@ -84,4 +84,5 @@ server_ = server.example.com ;OK _server_= server.example.com ;ERROR ip = 127.0.0.1 ;OK ip = "127.0.0.1" ;OK - +1024 = xxxxx ;OK +1024 = 2048 ;OK (but only syntactically :) -- cgit v1.2.3