]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspircd_io.cpp
Some pointless changes to this in the course of bugfinding
[user/henk/code/inspircd.git] / src / inspircd_io.cpp
index 65597704f917395c68c6565eb7a099dca4aea7b3..b9e7fa40b64cdb55cfa405b778edad59c9997fad 100644 (file)
@@ -105,8 +105,12 @@ int DaemonSeed (void)
 
 
 /* Make Sure Modules Are Avaliable!
- * (BugFix By Craig.. See? I do work! :p) */
-int FileExists (char* file)
+ * (BugFix By Craig.. See? I do work! :p)
+ * Modified by brain, requires const char*
+ * to work with other API functions
+ */
+
+bool FileExists (const char* file)
 {
   FILE *input;
   
@@ -115,9 +119,13 @@ int FileExists (char* file)
 }
 
 
-void LoadConf(const char* filename, std::stringstream *target)
+bool LoadConf(const char* filename, std::stringstream *target)
 {
        FILE* conf = fopen(filename,"r");
+       if (!FileExists(filename))
+       {
+               return false;
+       }
        char buffer[MAXBUF];
        if (conf)
        {
@@ -138,6 +146,7 @@ void LoadConf(const char* filename, std::stringstream *target)
                fclose(conf);
        }
        target->seekg(0);
+       return true;
 }
 
 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
@@ -211,11 +220,97 @@ int EnumConf(std::stringstream *config, const char* tag)
        return idx;
 }
 
+/* Counts the number of values within a certain tag */
+
+int EnumValues(std::stringstream *config, const char* tag, int index)
+{
+       int ptr = 0;
+       char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
+       int in_token, in_quotes, tptr, j, idx = 0;
+       char* key;
+       
+       bool correct_tag = false;
+       int num_items = 0;
+
+       const char* buf = config->str().c_str();
+       long bptr = 0;
+       long len = strlen(buf);
+       
+       ptr = 0;
+       in_token = 0;
+       in_quotes = 0;
+       lastc = '\0';
+       while (bptr<len)
+       {
+               lastc = c;
+               c = buf[bptr++];
+               if ((c == '#') && (lastc == '\n'))
+               {
+                       while ((c != '\n') && (bptr<len))
+                       {
+                               lastc = c;
+                               c = buf[bptr++];
+                       }
+               }
+               if ((c == '<') && (!in_quotes))
+               {
+                       tptr = 0;
+                       in_token = 1;
+                       do {
+                               c = buf[bptr++];
+                               if (c != ' ')
+                               {
+                                       c_tag[tptr++] = c;
+                                       c_tag[tptr] = '\0';
+                                       
+                                       if ((!strcmp(c_tag,tag)) && (idx == index))
+                                       {
+                                               correct_tag = true;
+                                       }
+                               }
+                       } while (c != ' ');
+               }
+               if (c == '"')
+               {
+                       in_quotes = (!in_quotes);
+               }
+               
+               if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
+               {
+                       num_items++;
+               }
+               if ((c == '>') && (!in_quotes))
+               {
+                       in_token = 0;
+                       if (correct_tag)
+                               correct_tag = false;
+                       if (!strcmp(c_tag,tag))
+                       {
+                               /* correct tag, but wrong index */
+                               idx++;
+                       }
+                       c_tag[0] = '\0';
+                       buffer[0] = '\0';
+                       ptr = 0;
+                       tptr = 0;
+               }
+               if (c != '>')
+               {
+                       if ((in_token) && (c != '\n') && (c != '\r'))
+                       {
+                               buffer[ptr++] = c;
+                               buffer[ptr] = '\0';
+                       }
+               }
+       }
+       return num_items+1;
+}
+
 
 
 int ConfValueEnum(char* tag, std::stringstream* config)
 {
-       EnumConf(config,tag);
+       return EnumConf(config,tag);
 }
 
 
@@ -229,30 +324,28 @@ int ConfValueEnum(char* tag, std::stringstream* config)
 int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
 {
        int ptr = 0;
-       char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
+       char buffer[65535], c_tag[MAXBUF], c, lastc;
        int in_token, in_quotes, tptr, j, idx = 0;
        char* key;
 
        const char* buf = config->str().c_str();
        long bptr = 0;
        long len = strlen(buf);
+       log(DEBUG,"Data length: %d",len);
        
        ptr = 0;
        in_token = 0;
        in_quotes = 0;
        lastc = '\0';
+       c_tag[0] = '\0';
+       buffer[0] = '\0';
        while (bptr<len)
        {
                lastc = c;
                c = buf[bptr++];
-               if ((c == '#') && (lastc == '\n'))
-               {
-                       while ((c != '\n') && (bptr<len))
-                       {
-                               lastc = c;
-                               c = buf[bptr++];
-                       }
-               }
+               // FIX: Treat tabs as spaces
+               if (c == 9)
+                       c = 32;
                if ((c == '<') && (!in_quotes))
                {
                        tptr = 0;
@@ -264,7 +357,8 @@ int ReadConf(std::stringstream *config, const char* tag, const char* var, int in
                                        c_tag[tptr++] = c;
                                        c_tag[tptr] = '\0';
                                }
-                       } while (c != ' ');
+                       // FIX: Tab can follow a tagname as well as space.
+                       } while ((c != ' ') && (c != 9));
                }
                if (c == '"')
                {
@@ -283,6 +377,8 @@ int ReadConf(std::stringstream *config, const char* tag, const char* var, int in
                                                if (!key)
                                                {
                                                        /* value not found in tag */
+                                                       strcpy(result,"");
+                                                       log(DEBUG,"ReadConf: value '%s' was not found in tag",var);
                                                        return 0;
                                                }
                                                else
@@ -293,6 +389,8 @@ int ReadConf(std::stringstream *config, const char* tag, const char* var, int in
                                                                if (!strlen(key))
                                                                {
                                                                        /* missing quote */
+                                                                       strcpy(result,"");
+                                                                       log(DEBUG,"ReadConf: possible missing quote!");
                                                                        return 0;
                                                                }
                                                                key++;
@@ -306,6 +404,7 @@ int ReadConf(std::stringstream *config, const char* tag, const char* var, int in
                                                                }
                                                        }
                                                        strcpy(result,key);
+                                                       log(DEBUG,"ReadConf: Got value '%s'",result);
                                                        return 1;
                                                }
                                        }
@@ -313,6 +412,7 @@ int ReadConf(std::stringstream *config, const char* tag, const char* var, int in
                        }
                        if (!strcmp(c_tag,tag))
                        {
+                               log(DEBUG,"Tag name correct but index value incorrect");
                                /* correct tag, but wrong index */
                                idx++;
                        }
@@ -330,6 +430,8 @@ int ReadConf(std::stringstream *config, const char* tag, const char* var, int in
                        }
                }
        }
+       log(DEBUG,"ReadConf: neither value '%s' or tag '%s' were found at all!",var,tag);
+       strcpy(result,""); // value or its tag not found at all
        return 0;
 }
 
@@ -338,6 +440,7 @@ int ReadConf(std::stringstream *config, const char* tag, const char* var, int in
 int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
 {
        ReadConf(config, tag, var, index, result);
+       return 0;
 }