From 286199e71f65b44d16e3684501935ec5b9556e9a Mon Sep 17 00:00:00 2001 From: M.Gergő Date: Thu, 14 May 2020 03:24:24 +0200 Subject: some refactoring --- README.md | 2 -- src/ini_read.c | 73 +++++++++++++++++++++++++++++----------------------------- src/ini_rw.h | 25 ++++++++++++++++---- src/main.c | 2 +- tests/test.ini | 2 +- 5 files changed, 60 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index be9ea9e..d5570ef 100644 --- a/README.md +++ b/README.md @@ -20,10 +20,8 @@ Uses Finite State Machine technique for file processing. **Values** in alone also ASCII, or between Double-Quotation-Marks(") can be **any UNICODE/UTF8** characters. Maximum line length can be 2^64/2 = 2^63 characters. - ## Future Plans - Writing and Rewriting support for ini files. - Writing support for comments. - Modifying one or more values/parameters/sections in ini files. - diff --git a/src/ini_read.c b/src/ini_read.c index 7f7a252..d285f9b 100644 --- a/src/ini_read.c +++ b/src/ini_read.c @@ -139,23 +139,23 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ int64_t i,j, vallen=len; enum ini_states pstate=Start, state=Start; - char cc; + //char cc; if(data == NULL){ return NULL; } else { for(i=0, j=0; inodeState == MULTILINE){ //Elküldjük gyűjteni + if(data->nodeState == lci_MULTILINE){ //Elküldjük gyűjteni state = DqmW; // Vagy ValW ?? 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 vallen = data->valueLen+len+1; - data->nodeState = CONTINUE; + data->nodeState = lci_CONTINUE; i--; pstate = Start; break; @@ -188,8 +188,8 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ state = Error; i--; } - data->nodeState = EMPTY; - data->valueDraw = EMPTYVAL; + data->nodeState = lci_EMPTY; + data->valueDraw = lci_EMPTYVAL; pstate = Start; break; @@ -231,7 +231,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ case CommEndW: //Innentől comment, a sor végéig if(in[i] == '\n' || in[i] == '\r'){ state = Stop; - data->nodeState = READY; + data->nodeState = lci_READY; data->comment[j] = '\0'; data->commentLen = j+1; i--; @@ -267,7 +267,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ case SectEndD: //Section begyűjtve, utána: SP(), sorvég, vagy komment if(in[i] == '\n' || in[i] == '\r'){ state = Stop; - data->nodeState = READY; + data->nodeState = lci_READY; i--; }else if(isspace(in[i])){ state = SectEndD; //marad helyben @@ -277,7 +277,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ data->comment = strResize(data->comment, data->commentLen, len); memset(data->comment, 0, len); data->commentStartPos = i; - data->nodeState = CONTINUE; + data->nodeState = lci_CONTINUE; } else { state = Error; //hibás sor i--; @@ -322,8 +322,8 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ case ValPSP: //EqD //megvolt az egyenlő, érték előtti space if(in[i] == '\n' || in[i] == '\r' /*|| in[i] == '\0'*/ ){ //ekkor üres változó state = Stop; - data->nodeState = READY; - data->valueDraw = SIMPLEVAL; + data->nodeState = lci_READY; + data->valueDraw = lci_SIMPLEVAL; i--; } else if(in[i] == ';' || in[i] == '#'){ //komment jön j = -1; @@ -331,8 +331,8 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ data->comment = strResize(data->comment, data->commentLen, len); memset(data->comment, 0, len); data->commentStartPos = i; - data->nodeState = CONTINUE; - data->valueDraw = SIMPLEVAL; + data->nodeState = lci_CONTINUE; + data->valueDraw = lci_SIMPLEVAL; } else if( pstate != Bslsh && in[i] == '\"' ){ //Macskaköröm jött j = -1; @@ -340,8 +340,8 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ data->value = strResize(data->value, data->valueLen, len); memset(data->value, 0, len); data->valueStartPos = i; - data->nodeState = CONTINUE; - data->valueDraw = DQUOTEDVAL; + data->nodeState = lci_CONTINUE; + data->valueDraw = lci_DQUOTEDVAL; } else if(isspace(in[i])){ //SPACE jött -> marad helyben state = ValPSP; } else if(isalnum(in[i]) || in[i]=='-') { //Változó macskaköröm nélkül @@ -350,8 +350,8 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ data->value = strResize(data->value, data->valueLen, len); memset(data->value, 0, len); data->valueStartPos = i; - data->nodeState = CONTINUE; - data->valueDraw = SIMPLEVAL; + data->nodeState = lci_CONTINUE; + data->valueDraw = lci_SIMPLEVAL; i--; } else { state = Error; @@ -364,12 +364,12 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ case ValW: //Érték kigyűjtése if(in[i] == '\n' || in[i] == '\r'){ //vége -> új sor state = Stop; - data->nodeState = READY; + data->nodeState = lci_READY; data->value[j] = '\0'; data->valueLen = j + 1; i--; } else if(in[i] == ';' || in[i] == '#'){ //comment - data->nodeState = CONTINUE; + data->nodeState = lci_CONTINUE; data->value[j] = '\0'; data->valueLen = j+1; state = CommEndW; @@ -377,7 +377,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ data->comment = strResize(data->comment, data->commentLen, len); memset(data->comment, 0, len); data->commentStartPos = i; - data->nodeState = CONTINUE; + data->nodeState = lci_CONTINUE; //} else if( in[i] == '\\' ){ //Backslash jött // j--; //A '\' nem számít bele! // state = Bslsh; @@ -386,7 +386,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ } else if(isspace(in[i])){ //Aposztróf nélüli space -> sorvég data->value[j] = '\0'; data->valueLen = j+1; - data->nodeState = READY; + data->nodeState = lci_READY; state = ValFSP; i--; } else { @@ -434,7 +434,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ data->comment = strResize(data->comment, data->commentLen, len); memset(data->comment, 0, len); data->commentStartPos = i; - data->nodeState = CONTINUE; + data->nodeState = lci_CONTINUE; } else if(isspace(in[i])){ state = ValFSP; //marad helyben } else { @@ -449,7 +449,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ if( in[i] == '\"'){ //újabb idézőjel -> string vége data->value[j] = '\0'; data->valueLen = j+1; - data->nodeState = READY; + data->nodeState = lci_READY; state = ValFSP; } else if ( in[i] == '\\'){ //Backslash jött j--; @@ -457,16 +457,16 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ } else if( in[i] == '\n' && in[i-1] == '\\'){ ///UNIX style data->value[j] = '\0'; // '\\n' => '\n\0' data->valueLen = j+1; - data->nodeState = MULTILINE; - data->valueDraw = MULTILINE; + data->nodeState = lci_MULTILINE; + data->valueDraw = lci_MULTILINEVAL; state = Stop; i--; } else if( 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 = MULTILINE; - data->valueDraw = MULTILINE; + data->nodeState = lci_MULTILINE; + data->valueDraw = lci_MULTILINEVAL; state = Stop; i--; } else if( pstate != Bslsh && ( in[i] == '\r' || in[i] == '\n' || in[i] == '\0')){ //túl korai sorvége @@ -497,7 +497,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ i--; state = Stop; - data->nodeState = ERROR; + data->nodeState = lci_ERROR; pstate = Error; break; @@ -515,7 +515,8 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ data->comment = strResize(data->comment, len, data->commentLen); data->errorMsg = strResize(data->errorMsg, 256, data->errorMsgLen); if(data->sectionStartPos < 0 && data->param==NULL && data->value==NULL && data->comment==NULL && data->errorMsg==NULL){ - data->nodeState = EMPTY; + data->nodeState = lci_EMPTY; + data->valueDraw = lci_EMPTYVAL; } return data; } else { //minden más-> hiba @@ -540,7 +541,7 @@ lci_data *createNode( lci_data *head, int64_t lineLen ){ lci_data *curr; curr = (lci_data *) calloc(1, sizeof(lci_data)); - curr->nodeState = EMPTY; + curr->nodeState = lci_EMPTY; curr->lineNum = 0; curr->lineLen = lineLen; curr->section = (char *) calloc(lineLen, sizeof(char)); @@ -555,7 +556,7 @@ lci_data *createNode( lci_data *head, int64_t lineLen ){ curr->paramStartPos = -1; curr->valueLen = lineLen; curr->valueStartPos = -1; - curr->valueDraw = EMPTYVAL; + curr->valueDraw = lci_EMPTYVAL; curr->commentLen = lineLen; curr->commentStartPos = -1; curr->errorMsgLen=lineLen; @@ -596,7 +597,7 @@ struct lci_data *iniReadOut(const char *filename){ if(fp == NULL){ list = createNode(NULL, 120); list->errorMsgLen = snprintf(list->errorMsg, 120, "File opening error. Errno: %d (%s)", errno, strerror(errno) ); - list->nodeState = ERROR; + list->nodeState = lci_ERROR; //return list; } else { linemax = getFileMaxLineLen(fp) +1; @@ -617,7 +618,7 @@ struct lci_data *iniReadOut(const char *filename){ buff[pos] = cc; } - if(curr == NULL || curr->nodeState != MULTILINE ){ + if(curr == NULL || curr->nodeState != lci_MULTILINE ){ curr = createNode(NULL, linemax); } if(list == NULL){ @@ -634,7 +635,7 @@ struct lci_data *iniReadOut(const char *filename){ curr->lineLen = pos + 1; curr = iniFSM(curr, buff, linemax); } - if(curr->nodeState == EMPTY){ //üres sorokat eldobjuk + if(curr->nodeState == lci_EMPTY){ //üres sorokat eldobjuk if(prev == curr || list == curr){ //Ha csak az az egy van a listában list = NULL; prev = NULL; @@ -653,12 +654,12 @@ struct lci_data *iniReadOut(const char *filename){ prev = curr; } else { //Az előzőhöz hozzávesszük a mostanit prev->next = curr; - if(prev->next != NULL && curr->nodeState != MULTILINE){ //Csak, ha lett új felvéve + if(prev->next != NULL && curr->nodeState != lci_MULTILINE){ //Csak, ha lett új felvéve prev = prev->next; } } - if(curr != NULL && curr->nodeState == ERROR){ + if(curr != NULL && curr->nodeState == lci_ERROR){ //return list; } pos = 0; diff --git a/src/ini_rw.h b/src/ini_rw.h index a3808bc..0a594c7 100644 --- a/src/ini_rw.h +++ b/src/ini_rw.h @@ -7,7 +7,7 @@ typedef struct lci_data { - enum nodeState {EMPTY, READY, CONTINUE, MULTILINE, ERROR } nodeState; + enum nodeState {lci_EMPTY, lci_READY, lci_CONTINUE, lci_MULTILINE, lci_ERROR } nodeState; int64_t lineNum; int64_t lineLen; @@ -20,7 +20,7 @@ typedef struct lci_data { char *value; int64_t valueLen; int64_t valueStartPos; - enum valueDraw {EMPTYVAL, SIMPLEVAL, MULTILINEVAL, DQUOTEDVAL} valueDraw; + enum valueDraw {lci_EMPTYVAL, lci_SIMPLEVAL, lci_MULTILINEVAL, lci_DQUOTEDVAL} valueDraw; char *comment; int64_t commentLen; int64_t commentStartPos; @@ -30,12 +30,29 @@ typedef struct lci_data { struct lci_data *next; } lci_data; -enum ini_states {Start, BgnSp, CommEndW, SectEndW, SectEndD, EqW1, EqW2, ValPSP, ValW, ValFSP, DqmW, Bslsh, Error, Stop }; + struct lci_data *iniReadOut(const char *filename); -int64_t getFileMaxLineLen(FILE *tfd); +//int64_t getFileMaxLineLen(FILE *tfd); 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 ); +int64_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 char *in, int64_t len); +char eescape(char c); + +#ifdef ini_read_c +char unescape(char c); +#endif // ini_read_c +#ifdef ini_write_c +//static const char* komment = ";#"; +#endif //ini_write_c +#endif // ini_read_c, ini_write_c diff --git a/src/main.c b/src/main.c index a003c66..0617d91 100644 --- a/src/main.c +++ b/src/main.c @@ -24,7 +24,7 @@ int main(int argc, char* argv[]){ fp = fopen(filename, "rb"); - len = getFileMaxLineLen(fp)+0; + len = getFileMaxLineLen(fp)+1; if(fp != NULL) { fclose(fp);} len = 24000; //Debug diff --git a/tests/test.ini b/tests/test.ini index af1da51..a69444f 100644 --- a/tests/test.ini +++ b/tests/test.ini @@ -32,7 +32,7 @@ parameter2 = "I like \t emojis 😍 but, don't like mosquitoes カ ! :)" ; Even! Multiline Parameters are supported: parameter3 = "With double quotation mark \ -I can write multiline values, but I shoud \ +I can write multiline values, but I should \ escapse the trailing newline with '\' sign!" -- cgit v1.2.3