X-Git-Url: http://winboard.nl/cgi-bin?p=polyglot.git;a=blobdiff_plain;f=ini.c;h=af4ab7912c06bdcd4c072c5717e7bb346c54e797;hp=5b02ef800c6465378e6036ea4516dd879fd94e66;hb=HEAD;hpb=cd81270f2b1723e0798f4d6dcaee134f0b4aca7f diff --git a/ini.c b/ini.c index 5b02ef8..af4ab79 100644 --- a/ini.c +++ b/ini.c @@ -7,10 +7,6 @@ #include "util.h" #include "errno.h" -// macros - -#define StringSize ((int) 4096) - // types typedef enum { @@ -25,6 +21,10 @@ typedef enum { FINISHED =8, } parse_state_t; +// variables + +const char *ini_specials=";\\#[]="; + // functions // ini_line_parse() @@ -42,30 +42,39 @@ line_type_t ini_line_parse(const char *line, char c; int type=SYNTAX_ERROR; int spaces=0; - bool outer_quote=FALSE; + bool quoted; while(state!=FINISHED){ c=line[index++]; -// printf("STATE=%d c=[%c]\n",state,c); + quoted=FALSE; + if(c=='\\'){ + if(strchr(ini_specials,line[index])){ + quoted=TRUE; + c=line[index++]; + } + } + // printf("STATE=%d quoted=%d c=[%c]\n",state,quoted,c); switch(state){ case START: - if((c==';')||(c=='#')||(c=='\r')||(c=='\n')||(c=='\0')){ + if(!quoted && ((c==';')||(c=='#')||(c=='\r')|| + (c=='\n')||(c=='\0'))){ type=EMPTY_LINE; state=FINISHED; - }else if(c=='['){ + }else if(!quoted && c=='['){ state=SECTION_NAME; - }else if(c!=' '){ + }else if(quoted || c!=' '){ name[name_index++]=c; state=NAME; } goto next; break; case NAME: - if(c=='='){ + if(!quoted && c=='='){ state=START_VALUE; - }else if(c==' '){ + }else if(!quoted && c==' '){ state=NAME_SPACE; spaces=1; - }else if((c==';')||(c=='#')||(c=='\r')||(c=='\n')||(c=='\0')){ + }else if(!quoted && ((c==';')||(c=='#')||(c=='\r') + ||(c=='\n')||(c=='\0'))){ type=SYNTAX_ERROR; state=FINISHED; }else{ @@ -74,11 +83,12 @@ line_type_t ini_line_parse(const char *line, goto next; break; // we don't get here case NAME_SPACE: - if(c==' '){ + if(!quoted && c==' '){ spaces++; - }else if(c=='='){ + }else if(!quoted && c=='='){ state=START_VALUE; - }else if((c==';')||(c=='#')||(c=='\r')||(c=='\n')||(c=='\0')){ + }else if(!quoted && ((c==';')||(c=='#')||(c=='\r')|| + (c=='\n')||(c=='\0'))){ type=SYNTAX_ERROR; state=FINISHED; }else{ @@ -92,28 +102,22 @@ line_type_t ini_line_parse(const char *line, goto next; break; // we don't get here case START_VALUE: - if(c=='"'){ - outer_quote=TRUE; - spaces=0; - state=VALUE_SPACE; - }else if((c==';')||(c=='#')||(c=='\r')||(c=='\n')||(c=='\0')){ + if(!quoted && ((c==';')||(c=='#')||(c=='\r')|| + (c=='\n')||(c=='\0'))){ type=EMPTY_VALUE; state=FINISHED; - }else if(c!=' '){ + }else if(quoted || c!=' '){ value[value_index++]=c; state=VALUE; } goto next; break; // we don't get here case VALUE: - if(c=='"'){ - state=QUOTE_SPACE; - spaces=0; - }else if(c==' '){ + if(!quoted && c==' '){ state=VALUE_SPACE; spaces=1; - }else if((c=='\r' || c=='\n' || c==';' || c=='#'||(c=='\0'))&& - !outer_quote){ + }else if(!quoted && ((c=='\r' || c=='\n' || c==';' || + c=='#'||(c=='\0')))){ type=NAME_VALUE; state=FINISHED; }else{ @@ -122,49 +126,24 @@ line_type_t ini_line_parse(const char *line, goto next; break; // we don't get here case VALUE_SPACE: - if(c==' '){ - spaces++; - }else if((c=='\r' || c=='\n' || c==';' || c=='#'||(c=='\0'))&& - !outer_quote){ - type=NAME_VALUE; - state=FINISHED; - }else{ - for(i=0;i