aboutsummaryrefslogtreecommitdiffstats
path: root/src/ini_read.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ini_read.c')
-rw-r--r--src/ini_read.c91
1 files changed, 41 insertions, 50 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);