diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ini_read.c | 91 | ||||
-rw-r--r-- | src/ini_rw.h | 2 | ||||
-rw-r--r-- | src/main.c | 47 |
3 files changed, 61 insertions, 79 deletions
diff --git a/src/ini_read.c b/src/ini_read.c index ac23548..40774a3 100644 --- a/src/ini_read.c +++ b/src/ini_read.c @@ -39,15 +39,12 @@ char *strResize(char *ptr, size_t oldsize, size_t newsize){ if(tmp == NULL){ //Hiba esetén nem nyúl hozzá return ptr; } else if(ptr == NULL) { - //memset(tmp, 0, newsize); return tmp; } else if(newsize > oldsize) { // FEL - //memset(tmp, 0, newsize); strncpy(tmp, ptr, oldsize); // old < new free(ptr); return tmp; } else if(newsize < oldsize){ //LE - //memset(tmp, 0, newsize); strncpy(tmp, ptr, newsize); // new < old free(ptr); return tmp; @@ -76,8 +73,8 @@ char unescape(char c){ return '\v'; } else if(c == 'e'){ return 0x1B; -/* } else if(c < 0x20){ - return '~'; */ + /*} else if(c < 0x20){ + return '~'; */ } else { // jó az eredeti return c; } @@ -104,8 +101,8 @@ char eescape(char c){ return '\''; } else if(c == '\"'){ //Double quotation mark return '\"'; - } else if(c < 0x20){ - return '~'; + /*} else if(c < 0x20){ + return '~';*/ } else { // jó az eredeti return c; } @@ -139,7 +136,7 @@ int64_t getFileMaxLineLen(FILE *tfd){ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ - int64_t i,j; + int64_t i,j, vallen=len; enum ini_states pstate=Start, state=Start; char cc; @@ -152,13 +149,15 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ switch (state) { case Start: - 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); // strResize(ptr, oldsize, newsize) - data->valueLen += len; + data->value = strResize(data->value, data->valueLen, data->valueLen+len+1); // strResize(ptr, oldsize, newsize + vallen = data->valueLen+len+1; + data->nodeState = CONTINUE; i--; + pstate = Start; + break; } else if(in[i] == '\n' || in[i] == '\r' || in[i] == '\0'){ //sorvége state = Stop; // Sorvége i--; @@ -168,7 +167,6 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ j = -1; state = CommEndW; data->comment = strResize(data->comment, data->commentLen, len); - data->commentLen = len; memset(data->comment, 0, len); data->commentStartPos = i; data->sectionStartPos = -1; //Hogy pucoljon @@ -176,14 +174,12 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ j = -1; state = SectEndW; data->section = strResize(data->section, data->sectionLen, len); - data->sectionLen = len; memset(data->section, 0, len); data->sectionStartPos = i; //zárójeleket is veszi! } else if(isalnum(in[i]) ){ //Változó jön j = -1; state = EqW1; data->param = strResize(data->param, data->paramLen, len); - data->paramLen = len; memset(data->param, 0, len); data->paramStartPos = i; i--; @@ -204,7 +200,6 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ j = -1; state = CommEndW; data->comment = strResize(data->comment, data->commentLen, len); - data->commentLen = len; memset(data->comment, 0, len); data->commentStartPos = i; data->sectionStartPos = -1; //Hogy pucoljon @@ -212,14 +207,12 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ j = -1; state = SectEndW; data->section = strResize(data->section, data->sectionLen, len); - data->sectionLen = len; memset(data->section, 0, len); data->sectionStartPos = i; } else if (isalnum(in[i])){ //Változó lesz belőle j = -1; state = EqW1; data->param = strResize(data->param, data->paramLen, len); - data->paramLen = len; memset(data->param, 0, len); data->paramStartPos = i; i--; @@ -280,7 +273,6 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ j = -1; state = CommEndW; data->comment = strResize(data->comment, data->commentLen, len); - data->commentLen = len; memset(data->comment, 0, len); data->commentStartPos = i; data->nodeState = CONTINUE; @@ -334,7 +326,6 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ j = -1; state = CommEndW; data->comment = strResize(data->comment, data->commentLen, len); - data->commentLen = len; memset(data->comment, 0, len); data->commentStartPos = i; data->nodeState = CONTINUE; @@ -343,17 +334,15 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ j = -1; state = DqmW; data->value = strResize(data->value, data->valueLen, len); - data->valueLen = len; memset(data->value, 0, len); data->valueStartPos = i; data->nodeState = CONTINUE; } else if(isspace(in[i])){ //SPACE jött -> marad helyben state = ValPSP; - } else if(isalnum(in[i])) { //Változó macskaköröm nélkül + } else if(isalnum(in[i]) || in[i]=='-') { //Változó macskaköröm nélkül j = -1; state = ValW; data->value = strResize(data->value, data->valueLen, len); - data->valueLen = len; memset(data->value, 0, len); data->valueStartPos = i; data->nodeState = CONTINUE; @@ -380,13 +369,12 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ state = CommEndW; j = -1; data->comment = strResize(data->comment, data->commentLen, len); - data->commentLen = len; memset(data->comment, 0, len); data->commentStartPos = i; data->nodeState = CONTINUE; - } 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( in[i] == '\\' ){ //Backslash jött + // j--; //A '\' nem számít bele! + // state = Bslsh; } else if( isalnum(in[i]) || in[i]=='_' || in[i]=='-' || 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 @@ -416,12 +404,15 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ if(in[i] == '\n'){ //Workaround for UNIX line end i--; } + if( in[i] == '\r'){ //Workaround for WIN line endings + j--; + } } else { state = Error; i--; } - if(in[i] < 0x20){ //láthatatlan: pl \\n (escapelt sorvége) - j--; + if(0x00 < in[i] && in[i] < 0x20){ //láthatatlan: pl \\n (escapelt sorvége) + //j--; } pstate = Bslsh; break; @@ -435,7 +426,6 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ j = -1; state = CommEndW; data->comment = strResize(data->comment, data->commentLen, len); - data->commentLen = len; memset(data->comment, 0, len); data->commentStartPos = i; data->nodeState = CONTINUE; @@ -450,24 +440,24 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ case DqmW: //dupla idézőjelben az érték - if(/*pstate != Bslsh &&*/ in[i] == '\"'){ //újabb idézőjel -> string vége + if( in[i] == '\"'){ //újabb idézőjel -> string vége data->value[j] = '\0'; data->valueLen = j+1; data->nodeState = READY; state = ValFSP; - } else if (/*pstate != Bslsh &&*/ in[i] == '\\'){ //Backslash jött + } else if ( in[i] == '\\'){ //Backslash jött j--; state = Bslsh; } else if( in[i] == '\n' && in[i-1] == '\\'){ ///UNIX style - //j--; - data->value[j-1] = '\0'; // '\' => '\0' - data->valueLen = j+1-1; + data->value[j] = '\0'; // '\\n' => '\n\0' + data->valueLen = j+1; data->nodeState = MULTILINE; state = Stop; i--; } else if( in[i] == '\n' && in[i-1] == '\r' && in[i-2] == '\\'){ //WINDOWS style - data->value[j] = '\0'; - data->valueLen = j+1; + data->value[j] = '\n'; + data->value[j+1] = '\0'; + data->valueLen = j+2; data->nodeState = MULTILINE; state = Stop; i--; @@ -483,7 +473,6 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ case Error: data->errorMsg = strResize(data->errorMsg, data->errorMsgLen, 256); - data->errorMsgLen = 256; memset(data->errorMsg, 0, 256) ; if(pstate == SectEndW || pstate == SectEndD){ @@ -495,7 +484,7 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ } else if(pstate == DqmW){ data->errorMsgLen = snprintf(data->errorMsg, 256, "Double quotation mark needed! (line: %ld, pos: %ld)", data->lineNum, i+1); } else { //pstate == Stop - data->errorMsgLen = snprintf(data->errorMsg, 256, "Illegal character! (line: %ld, pos: %ld)", data->lineNum, i+1); + data->errorMsgLen = snprintf(data->errorMsg, 256, "Illegal character! (line: %ld, pos: %ld)\0", data->lineNum, i+1)+1; } i--; @@ -508,16 +497,16 @@ struct lci_data *iniFSM(struct lci_data *data, const char *in, int64_t len){ if(in[i] == '\n' || in[i] == '\r' || in[i] == '\0' || pstate == Error){ //Sorvége, maradunk state = Stop; if(strNullLen(data->section) == 0){data->sectionLen = 0;} - if(/*data->nodeState != MULTILINE &&*/ strNullLen(data->param) == 0){data->paramLen = 0;} + if(strNullLen(data->param) == 0){data->paramLen = 0;} 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, len, data->valueLen); + data->value = strResize(data->value, vallen, data->valueLen); data->comment = strResize(data->comment, len, data->commentLen); data->errorMsg = strResize(data->errorMsg, 256, data->errorMsgLen); - if(data->section==NULL && data->param==NULL && data->value==NULL && data->comment==NULL && data->errorMsg==NULL){ + if(data->sectionStartPos < 0 && data->param==NULL && data->value==NULL && data->comment==NULL && data->errorMsg==NULL){ data->nodeState = EMPTY; } return data; @@ -561,7 +550,7 @@ lci_data *createNode( lci_data *head, int64_t lineLen ){ memset(curr->value, 0, lineLen); curr->valueLen = lineLen; curr->valueStartPos = -1; - memset(curr->comment, 0, lineLen); + memset(curr->comment, 0, lineLen); curr->commentLen = lineLen; curr->commentStartPos = -1; memset(curr->errorMsg, 0, lineLen); @@ -572,19 +561,19 @@ lci_data *createNode( lci_data *head, int64_t lineLen ){ head->next = curr; } return curr; -} +} lci_data *destroyNodes( lci_data *head){ lci_data *tmp, *node=head; while(node != NULL){ + free(node->section); + free(node->param); + free(node->value); + free(node->comment); + free(node->errorMsg); tmp = node; - free(tmp->section); - free(tmp->param); - free(tmp->value); - free(tmp->comment); - free(tmp->errorMsg); - free(tmp); node = node->next; + free(tmp); } return NULL; } @@ -631,6 +620,8 @@ struct lci_data *iniReadOut(const char *filename){ if(prev != NULL && prev != curr && prev->section != NULL && curr != NULL && curr->section != NULL){ strncpy(curr->section, prev->section, curr->sectionLen); curr->sectionLen = prev->sectionLen; + //curr->section = strResize(curr->section, linemax, curr->sectionLen); + } if(curr != NULL){ curr->lineNum = line; @@ -662,7 +653,7 @@ struct lci_data *iniReadOut(const char *filename){ } if(curr != NULL && curr->nodeState == ERROR){ - return list; + //return list; } pos = 0; memset(buff, 0, linemax); diff --git a/src/ini_rw.h b/src/ini_rw.h index 9a84c10..b424efb 100644 --- a/src/ini_rw.h +++ b/src/ini_rw.h @@ -33,6 +33,8 @@ enum ini_states {Start, BgnSp, CommEndW, SectEndW, SectEndD, EqW1, EqW2, ValPSP, struct lci_data *iniReadOut(const char *filename); int64_t getFileMaxLineLen(FILE *tfd); +char *strResize(char *ptr, size_t oldsize, size_t newsize); +lci_data *destroyNodes( lci_data *head); @@ -6,21 +6,24 @@ #include <linux/limits.h> /* PATH_MAX*/ #include "ini_rw.h" -#define main_c +#define main_c int main() { - char filename[] = "tests/test0.ini", *buff1, *buff2, *buff3, *buff4, *buff5; + char filename[] = "tests/test.ini", *buff1, *buff2, *buff3, *buff4, *buff5; lci_data *ini=NULL, *tmp=NULL; - int len=0, elen=50, lens=0, lenp=0, lenv=0, lenc=0; + int len=0, elen=64, lens=0, lenp=0, lenv=0, lenc=0; FILE *fp; fp = fopen(filename, "rb"); len = getFileMaxLineLen(fp); fclose(fp); +//len = 24000; + + buff1 = malloc((len+3)*sizeof(char)); buff2 = malloc((len+3)*sizeof(char)); buff3 = malloc((len+3)*sizeof(char)); @@ -32,34 +35,15 @@ memset(buff3, 0, (len+3)*sizeof(char)); memset(buff4, 0, (len+3)*sizeof(char)); memset(buff5, 0, (elen+3)*sizeof(char)); - char *aa, *bb, *cc; - aa = (char *) malloc(20*sizeof(char)); - aa[0] = 'q'; - aa[1] = 'w'; - aa[2] = 'e'; - aa[3] = '\0'; - bb = (char *) malloc(30*sizeof(char)); - - cc=aa; - strncpy(bb,aa,20); - free(aa); - free(NULL); - - printf("\n%s\n", bb); - - - - - printf("\nLineMax: %d\n\n",len); ini = iniReadOut(filename); - lens=18; - lenp=18; - lenv=18; + lens=16; + lenp=16; + lenv=16; lenc=44; @@ -70,16 +54,21 @@ memset(buff5, 0, (elen+3)*sizeof(char)); snprintf(buff2, len+3, "'%s' %3ld",tmp->param, tmp->paramLen); snprintf(buff3, len+3, "'%s' %3ld",tmp->value, tmp->valueLen); snprintf(buff4, len+3, "'%s' %3ld",tmp->comment, tmp->commentLen); - snprintf(buff5, elen, "'%s' %3ld", tmp->errorMsg, tmp->errorMsgLen); + snprintf(buff5, elen, "'%s' %3ld",tmp->errorMsg, tmp->errorMsgLen); - 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); + //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; } - +free(buff1); +free(buff2); +free(buff3); +free(buff4); +free(buff5); + destroyNodes( ini); return 0; |