diff options
Diffstat (limited to 'src/ini_read.c')
-rw-r--r-- | src/ini_read.c | 254 |
1 files changed, 157 insertions, 97 deletions
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; i<len; i++, j++){ - /*cc = in[i]; */ /*debug*/ + /*cc = in[i];*/ /*debug*/ switch (state) { case Start: - if(data->nodeState == 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; +} + + |