diff options
author | brain <brain@e03df62e-2008-0410-955e-edbf42e46eb7> | 2004-04-07 13:22:17 +0000 |
---|---|---|
committer | brain <brain@e03df62e-2008-0410-955e-edbf42e46eb7> | 2004-04-07 13:22:17 +0000 |
commit | 6ec52cab73c5e77337ddd364617d8c4ad4065d80 (patch) | |
tree | 91ada68c6f7780a202133a4402b7e013af33e679 /src/inspircd_io.cpp | |
parent | 3717c045aabf40d8280d1f8a7a0f33383ecafae4 (diff) |
Fixed config reader to cache its files instead of re-reading them for each access
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@413 e03df62e-2008-0410-955e-edbf42e46eb7
Diffstat (limited to 'src/inspircd_io.cpp')
-rw-r--r-- | src/inspircd_io.cpp | 297 |
1 files changed, 158 insertions, 139 deletions
diff --git a/src/inspircd_io.cpp b/src/inspircd_io.cpp index 11471538d..65597704f 100644 --- a/src/inspircd_io.cpp +++ b/src/inspircd_io.cpp @@ -19,11 +19,17 @@ #endif #include <sys/types.h> +#include <string> #include <unistd.h> +#include <sstream> +#include <iostream> +#include <fstream> #include "inspircd.h" #include "inspircd_io.h" #include "inspircd_util.h" +using namespace std; + extern FILE *log_file; void WriteOpers(char* text, ...); @@ -109,87 +115,107 @@ int FileExists (char* file) } +void LoadConf(const char* filename, std::stringstream *target) +{ + FILE* conf = fopen(filename,"r"); + char buffer[MAXBUF]; + if (conf) + { + target->clear(); + while (!feof(conf)) + { + if (fgets(buffer, MAXBUF, conf)) + { + if ((!feof(conf)) && (buffer) && (strlen(buffer))) + { + if (buffer[0] != '#') + { + *target << std::string(buffer); + } + } + } + } + fclose(conf); + } + target->seekg(0); +} + /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */ -int EnumConf(const char* filename, const char* tag) +int EnumConf(std::stringstream *config, const char* tag) { - FILE *config; int ptr = 0; char buffer[MAXBUF], c_tag[MAXBUF], c, lastc; int in_token, in_quotes, tptr, j, idx = 0; char* key; - if ((config = fopen (filename, "r")) == NULL) + 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) { - return 0; - } - else - { - ptr = 0; - in_token = 0; - in_quotes = 0; - lastc = '\0'; - while (!feof(config)) + lastc = c; + c = buf[bptr++]; + if ((c == '#') && (lastc == '\n')) { - lastc = c; - c = fgetc(config); - if ((c == '#') && (lastc == '\n')) + while ((c != '\n') && (bptr<len)) { - while ((c != '\n') && (!feof(config))) + lastc = c; + c = buf[bptr++]; + } + } + if ((c == '<') && (!in_quotes)) + { + tptr = 0; + in_token = 1; + do { + c = buf[bptr++]; + if (c != ' ') { - lastc = c; - c = fgetc(config); + c_tag[tptr++] = c; + c_tag[tptr] = '\0'; } - } - if ((c == '<') && (!in_quotes)) - { - tptr = 0; - in_token = 1; - do { - c = fgetc(config); - if (c != ' ') - { - c_tag[tptr++] = c; - c_tag[tptr] = '\0'; - } - } while (c != ' '); - } - if (c == '"') + } while (c != ' '); + } + if (c == '"') + { + in_quotes = (!in_quotes); + } + if ((c == '>') && (!in_quotes)) + { + in_token = 0; + if (!strcmp(c_tag,tag)) { - in_quotes = (!in_quotes); + /* correct tag, but wrong index */ + idx++; } - if ((c == '>') && (!in_quotes)) + c_tag[0] = '\0'; + buffer[0] = '\0'; + ptr = 0; + tptr = 0; + } + if (c != '>') + { + if ((in_token) && (c != '\n') && (c != '\r')) { - in_token = 0; - 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'; - } + buffer[ptr++] = c; + buffer[ptr] = '\0'; } } } - fclose(config); return idx; } -int ConfValueEnum(char* tag) +int ConfValueEnum(char* tag, std::stringstream* config) { - EnumConf(CONFIG_FILE,tag); + EnumConf(config,tag); } @@ -200,125 +226,118 @@ int ConfValueEnum(char* tag) * ConfValue("oper","name",2,result); */ -int ReadConf(const char* filename, const char* tag, const char* var, int index, char *result) +int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result) { - FILE *config; int ptr = 0; char buffer[MAXBUF], c_tag[MAXBUF], c, lastc; int in_token, in_quotes, tptr, j, idx = 0; char* key; - if ((config = fopen (filename, "r")) == NULL) + 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) { - return 0; - } - else - { - ptr = 0; - in_token = 0; - in_quotes = 0; - lastc = '\0'; - while (!feof(config)) + lastc = c; + c = buf[bptr++]; + if ((c == '#') && (lastc == '\n')) { - lastc = c; - c = fgetc(config); - if ((c == '#') && (lastc == '\n')) + while ((c != '\n') && (bptr<len)) { - while ((c != '\n') && (!feof(config))) + lastc = c; + c = buf[bptr++]; + } + } + if ((c == '<') && (!in_quotes)) + { + tptr = 0; + in_token = 1; + do { + c = buf[bptr++]; + if (c != ' ') { - lastc = c; - c = fgetc(config); + c_tag[tptr++] = c; + c_tag[tptr] = '\0'; } - } - if ((c == '<') && (!in_quotes)) - { - tptr = 0; - in_token = 1; - do { - c = fgetc(config); - if (c != ' ') - { - c_tag[tptr++] = c; - c_tag[tptr] = '\0'; - } - } while (c != ' '); - } - if (c == '"') - { - in_quotes = (!in_quotes); - } - if ((c == '>') && (!in_quotes)) + } while (c != ' '); + } + if (c == '"') + { + in_quotes = (!in_quotes); + } + if ((c == '>') && (!in_quotes)) + { + in_token = 0; + if (idx == index) { - in_token = 0; - if (idx == index) + if (!strcmp(c_tag,tag)) { - if (!strcmp(c_tag,tag)) + if ((buffer) && (c_tag) && (var)) { - if ((buffer) && (c_tag) && (var)) + key = strstr(buffer,var); + if (!key) { - key = strstr(buffer,var); - if (!key) - { - /* value not found in tag */ - fclose(config); - return 0; - } - else + /* value not found in tag */ + return 0; + } + else + { + key+=strlen(var); + while (key[0] !='"') { - key+=strlen(var); - while (key[0] !='"') + if (!strlen(key)) { - if (!strlen(key)) - { - /* missing quote */ - return 0; - } - key++; + /* missing quote */ + return 0; } key++; - for (j = 0; j < strlen(key); j++) + } + key++; + for (j = 0; j < strlen(key); j++) + { + if (key[j] == '"') { - if (key[j] == '"') - { - key[j] = '\0'; - } + key[j] = '\0'; } - fclose(config); - strcpy(result,key); - return 1; } + strcpy(result,key); + return 1; } } } - 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 (!strcmp(c_tag,tag)) { - if ((in_token) && (c != '\n') && (c != '\r')) - { - buffer[ptr++] = c; - buffer[ptr] = '\0'; - } + /* 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'; } } } - fclose(config); return 0; } -int ConfValue(char* tag, char* var, int index, char *result) +int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config) { - ReadConf(CONFIG_FILE, tag, var, index, result); + ReadConf(config, tag, var, index, result); } |