diff options
author | M.Gergő | 2020-05-14 03:24:24 +0200 |
---|---|---|
committer | M.Gergő | 2020-05-14 03:24:24 +0200 |
commit | 286199e71f65b44d16e3684501935ec5b9556e9a (patch) | |
tree | b904e4a8263c1aa7154081dd25ff60e359b58e75 /src | |
parent | 0026bdb684cd610be6a7f8499db8920ee1579c1b (diff) | |
download | lightconfini-286199e71f65b44d16e3684501935ec5b9556e9a.tar.gz lightconfini-286199e71f65b44d16e3684501935ec5b9556e9a.zip |
some refactoring
Diffstat (limited to 'src')
-rw-r--r-- | src/ini_read.c | 73 | ||||
-rw-r--r-- | src/ini_rw.h | 25 | ||||
-rw-r--r-- | src/main.c | 2 |
3 files changed, 59 insertions, 41 deletions
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; i<len; i++, j++){ - cc = in[i]; + //cc = in[i]; switch (state) { case Start: - if(data->nodeState == 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 @@ -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 |