Add forgotten files 1.4.70b
[polyglot.git] / ini.c
diff --git a/ini.c b/ini.c
index 5509547..af4ab79 100644 (file)
--- 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,10 +126,10 @@ line_type_t ini_line_parse(const char *line,
                 goto next;
                 break;    // we don't get here
             case VALUE_SPACE:
-                if(c==' '){
+                if(!quoted && c==' '){
                     spaces++;
-                }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{
@@ -133,38 +137,13 @@ line_type_t ini_line_parse(const char *line,
                         value[value_index++]=' ';
                     }
                     spaces=0;
-                    if(c!='"'){
-                        value[value_index++]=c;
-                        state=VALUE;
-                    }else{
-                        state=QUOTE_SPACE;
-                    }
-                }
-                goto next;
-                break;    // we don't get here
-            case QUOTE_SPACE:
-                if(c==' '){
-                    spaces++;
-                }else if(c=='\r' || c=='\n' || c==';' || c=='#'||(c=='\0')){
-                    type=NAME_VALUE;
-                    state=FINISHED;
-                }else{
-                    value[value_index++]='"';
-                    for(i=0;i<spaces;i++){
-                        value[value_index++]=' ';
-                    }
-                    spaces=0;
-                    if(c!='"'){
-                        value[value_index++]=c;
-                        state=VALUE;
-                    }else{
-                        state=QUOTE_SPACE;
-                    }
+                   value[value_index++]=c;
+                   state=VALUE;
                 }
                 goto next;
                 break;    // we don't get here
             case SECTION_NAME:
-                if(c==']'){
+                if(!quoted && c==']'){
                     type=SECTION;
                     state=FINISHED;
                 }else{
@@ -209,6 +188,19 @@ void ini_clear(ini_t *ini){
     ini->index=0;
 }
 
+// ini_copy()
+
+void ini_copy(ini_t *dst, ini_t *src){
+  int i;
+  dst->index=src->index;
+  dst->iter=src->iter;
+  for(i=0;i<src->index;i++){
+    my_string_set(&dst->entries[i].section,src->entries[i].section);
+    my_string_set(&dst->entries[i].name,src->entries[i].name);
+    my_string_set(&dst->entries[i].value,src->entries[i].value);
+  }
+}
+
 // ini_find()
 
 ini_entry_t *ini_find(ini_t *ini, const char *section, const char* name){
@@ -309,7 +301,6 @@ int ini_parse(ini_t *ini, const char *filename){
 
 void ini_disp(ini_t *ini){
     int i;
-    my_log("POLYGLOT Current options\n");
     for(i=0;i<ini->index;i++){
         my_log("POLYGLOT [%s] %s=\"%s\"\n",
                (ini->entries)[i].section,
@@ -334,5 +325,4 @@ ini_entry_t * ini_next(ini_t *ini){
     return &ini->entries[ini->iter++];
 }
 
-// ini_create_pg()