From 4cd4beaf0a6d8032fe88efbbc6cb42303766e9f6 Mon Sep 17 00:00:00 2001 From: M.Gergő Date: Thu, 7 May 2020 00:22:25 +0200 Subject: README + LICENCE --- README.md | 12 ++++--- src/ini_read.c | 103 ++++++++++++++++++++++++++------------------------------- src/main.c | 10 ++++-- tests/test.ini | 26 +++++++-------- 4 files changed, 76 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index b602787..cc51c09 100644 --- a/README.md +++ b/README.md @@ -8,12 +8,16 @@ - Converting readed values to int/float/string. - Building linked list from parameters. - Multiline values are supported. -- Reading comments. +- Reading comments. +- UNICODE/UTF8 support. +- Escape sequence support. -Automatically switch between UNIX ('\n') and Windows ('\r\n') line endings. MAC ('\r' only) is not supported currently. -Comments can be started with ';' or '#' signs. -Multiline values are supported is they are surrounded with Double-Quotation-Mark (") signs. +**Automatically switch** between **UNIX ('\n')** and **Windows ('\r\n')** line endings. MAC ('\r' only) is not supported currently. +**Comments** can be started with **';'** or **'#'** signs. +**Multiline** values are supported if they are surrounded **with Double-Quotation-Mark (")** signs. Uses Finite State Machine technique for file processing. +**Sections, Parameter names** can be **ASCII** alphabetical or numerical characters. +**Values** in alone also ASCII, or between Double-Quotation-Marks(") can be **any UNICODE/UTF8** characters. ## Future Plans diff --git a/src/ini_read.c b/src/ini_read.c index a33189b..214e445 100644 --- a/src/ini_read.c +++ b/src/ini_read.c @@ -8,11 +8,10 @@ #include #include /* int64_t*/ - - #define ini_read_c #include "ini_rw.h" + enum ini_states state = Start; struct lci_data fsmdata; @@ -60,24 +59,6 @@ char *strResize(char *ptr, size_t oldsize, size_t newsize){ } } -int checkspace(int in){ - switch (in){ - case ' ': - return 0x20; - case '\t': - return 0x09; - case '\v': - return 0x0b; - case '\f': - return 0x0c; - case '\r': - return 0x0d; - default: - return 0; - } - -} - char unescape(char c){ if(c == 'n'){ //Newline return '\n'; @@ -108,6 +89,34 @@ char unescape(char c){ } } +char eescape(char 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 == '\\'){ //Backslash + return '\\'; + } else if(c == '\''){ //Apostrophe + return '\''; + } else if(c == '\"'){ //Double quotation mark + return '\"'; + } else if(c < 0x20){ + return '~'; + } else { // jó az eredeti + return c; + } +} + int64_t getFileMaxLineLen(FILE *tfd){ @@ -134,7 +143,6 @@ int64_t getFileMaxLineLen(FILE *tfd){ } } - struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ int64_t i,j; @@ -150,14 +158,13 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ switch (state) { case Start: - if(data->nodeState == MULTILINE){ //Elküldjükgyűjteni + if(data->nodeState == 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); i--; - //j--; } else if(in[i] == '\n' || in[i] == '\r' || in[i] == '\0'){ //sorvége - state = Stop; // marad helyben + state = Stop; // Sorvége i--; } else if(isspace(in[i])){ //ISSPACE, de nem sorvége state = BgnSp; @@ -181,17 +188,16 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ memset(data->param, 0, len); data->paramStartPos = i; i--; - } else { + } else { // control karakterek az 1. pos-ban state = Error; i--; - // control karakterek az 1. pos-ban } data->nodeState = EMPTY; pstate = Start; break; - case BgnSp: + case BgnSp: //Space() at Begining if (in[i] == '\n' || in[i] == '\r' ){ state = Stop; i--; @@ -225,7 +231,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ break; - case CommEndW: + case CommEndW: //Innentől comment, a sor végéig if(in[i] == '\n' || in[i] == '\r'){ state = Stop; data->nodeState = READY; @@ -239,26 +245,26 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ break; - case SectEndW: // Sectiont gyűjti + case SectEndW: // Section-t gyűjti if(in[i] == ']'){ state = SectEndD; data->section[j] = '\0'; data->sectionLen = j+1; - } else if(isalnum(in[i])){ + } else if(isalnum(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 + } 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 { + } /*else { data->section[j] = in[i]; - } + }*/ pstate = SectEndW; break; - case SectEndD: //Section begyűjtve, utána + case SectEndD: //Section begyűjtve, utána: SP(), sorvég, vagy komment if(in[i] == '\n' || in[i] == '\r'){ state = Stop; data->nodeState = READY; @@ -275,7 +281,6 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ } else { state = Error; //hibás sor i--; - //memset(data->section, 0, len); // legyen-e érvényes a section? } pstate = SectEndD; break; @@ -286,9 +291,9 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ state = ValPSP; data->param[j] = '\0'; data->paramLen = j + 1; - } else if(isalnum(in[i]) || in[i] == '[' || in[i] == ']'){ //A paraméter neve (PHP támogatással) + } else if(isalnum(in[i]) || in[i] == '[' || in[i] == ']'){ //A paraméter neve vagy tömb támogatása (Mint PHP-ben) data->param[j] = in[i]; - } else if(isspace(in[i]) && in[i] != '\r' && in[i] != '\n'){ //Végén csak space lehet + } else if(isspace(in[i]) && in[i] != '\r' && in[i] != '\n'){ //Végén csak space lehet, egyenlő nélkül sorvég nem state = EqW2; data->param[j] = '\0'; data->paramLen = j + 1; @@ -369,24 +374,15 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ memset(data->comment, 0, len); data->commentStartPos = i; data->nodeState = CONTINUE; - /* } else if( pstate != Bslsh && in[i] == '\"' ){ //Macskaköröm jött - j = -1; - state = DqmW; - data->nodeState = CONTINUE; - */ - } else if( /* pstate != DqmW &&*/ /*pstate != Bslsh &&*/ in[i] == '\\' ){ //Backslash jött //egymás után több backslash is lehet?? + } else if( in[i] == '\\' ){ //Backslash jött //egymás után több backslash is lehet?? j--; //A '\' nem számít bele! state = Bslsh; - //} else if( pstate == Bslsh ){ //Backslash-ból jöttünk vissza - //data->value[j] = eescape(c); - } else if( /*pstate != DqmW &&*/ isalnum(in[i])){ //Normál betűk, space() csak idézőjelben + } else if( isalnum(in[i]) ){ //Normál betűk, space() csak idézőjelben jöhet! data->value[j] = in[i]; } else if(isspace(in[i])){ //Aposztróf nélüli space -> sorvég - //if(pstate != DqmW){ - data->value[j] = '\0'; - data->valueLen = j+1; - data->nodeState = READY; - //} + data->value[j] = '\0'; + data->valueLen = j+1; + data->nodeState = READY; state = ValFSP; i--; } else { @@ -447,7 +443,6 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ data->value[j] = '\0'; data->valueLen = j+1; data->nodeState = READY; - // state = ValW; //Ha több, egymás melletti idézőjeles részt is akarunk state = ValFSP; } else if (/*pstate != Bslsh &&*/ in[i] == '\\'){ //Backslash jött j--; @@ -500,10 +495,6 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ case Stop: if(in[i] == '\n' || in[i] == '\r' || in[i] == '\0' || pstate == Error){ //Sorvége, maradunk state = Stop; - //} else if(isgraph(c)) { //Látható karakter -> újabb adag - // i--; - // state = Start; - //} else if(in[i] == '\0'){ if(strNullLen(data->section) == 0){data->sectionLen = 0;} if(strNullLen(data->param) == 0){data->paramLen = 0;} if(strNullLen(data->value) == 0){data->valueLen = 0;} diff --git a/src/main.c b/src/main.c index 1e6246f..728c6da 100644 --- a/src/main.c +++ b/src/main.c @@ -14,7 +14,7 @@ int main() { char filename[] = "tests/test.ini", *buff1, *buff2, *buff3, *buff4, *buff5; lci_data *ini=NULL, *tmp=NULL; - int len=0, elen=50; + int len=0, elen=50, lens=0, lenp=0, lenv=0, lenc=0; FILE *fp; fp = fopen(filename, "rb"); @@ -33,6 +33,11 @@ int main() printf("\n"); + lens=18; + lenp=18; + lenv=18; + lenc=44; + tmp = ini; while(tmp != NULL){ @@ -44,7 +49,8 @@ int main() snprintf(buff5, elen, "'%s' %3ld", tmp->errorMsg, tmp->errorMsgLen); - printf("LN: %ld,\tLL: %ld,\tSE: %*s, P: %*s, V: %*s, C: %*s, ER: %*s \n", tmp->lineNum, tmp->lineLen,len+6, buff1, len+6, buff2, len+6, buff3, len+6, buff4, elen, buff5); + printf("LN: %ld,\tLL: %ld,\tSE: %*s,%2ld P: %*s,%2ld V: %*s,%2ld C: %*s,%2ld ER: %*s \n", tmp->lineNum, tmp->lineLen, lens, buff1,tmp->sectionStartPos, + lenp, buff2, tmp->paramStartPos, lenv, buff3, tmp->valueStartPos, lenc, buff4, tmp->commentStartPos, elen, buff5); tmp=tmp->next; } diff --git a/tests/test.ini b/tests/test.ini index faa06fb..5c218d0 100644 --- a/tests/test.ini +++ b/tests/test.ini @@ -1,21 +1,21 @@ - [ABéBCD] -ad = "123456\ - aléásdasd #;--Ł$-¶¶-Ä-€-- \ -wqeweq \ -aabcd=efgh " -ghijkl = mnopq ; - -aaaa = asd ;come - -bbb = ddd #comen + [A BBCD] +ghijkl=val1 ; komment +ghijkl = val2 ;kom1 +ghijkl = val3;kom3 + ghijkl=val14;kom14 + ghijkl = val15;kom15 + ghijkl = val4;kom11 eee = fff= - +aa= #commt ;#//sdfsdf// -[test2] ;#coment + [test2];#coment ggg = aaa #commnta - +;ad = "123456\ +; aléásdasd #;--Ł$ +;wqeweq \ +;aabcd=efgh " -- cgit v1.2.3