diff options
author | maximius <none@none> | 2009-10-17 15:35:07 -0700 |
---|---|---|
committer | maximius <none@none> | 2009-10-17 15:35:07 -0700 |
commit | 26b5e033ffde3d161382fc9addbfa99738379641 (patch) | |
tree | a344f369ca32945f787a02dee35c3dbe342bed7e /src/shared/Config/dotconfpp/dotconfpp.cpp | |
parent | f21f47005dcb6b76e1abc9f35fbcd03eed191bff (diff) |
*Massive cleanup (\n\n -> \n, *\n -> \n, cleanup for(...) to for (...), and some other cleanups by hand)
*Fix a possible crash in Spell::DoAllEffectOnTarget
--HG--
branch : trunk
Diffstat (limited to 'src/shared/Config/dotconfpp/dotconfpp.cpp')
-rw-r--r-- | src/shared/Config/dotconfpp/dotconfpp.cpp | 76 |
1 files changed, 0 insertions, 76 deletions
diff --git a/src/shared/Config/dotconfpp/dotconfpp.cpp b/src/shared/Config/dotconfpp/dotconfpp.cpp index e779637256e..0492c69f805 100644 --- a/src/shared/Config/dotconfpp/dotconfpp.cpp +++ b/src/shared/Config/dotconfpp/dotconfpp.cpp @@ -1,8 +1,6 @@ #include "Common.h" - #include "dotconfpp.h" - #ifdef WIN32 #define PATH_MAX _MAX_PATH #define strcasecmp stricmp @@ -14,17 +12,14 @@ #include <stdint.h> #include <strings.h> #endif - #if !defined(R_OK) #define R_OK 04 #endif - DOTCONFDocumentNode::DOTCONFDocumentNode():previousNode(NULL), nextNode(NULL), parentNode(NULL), childNode(NULL), values(NULL), valuesCount(0), name(NULL), lineNum(0), fileName(NULL), closed(true) { } - DOTCONFDocumentNode::~DOTCONFDocumentNode() { free(name); @@ -35,14 +30,12 @@ DOTCONFDocumentNode::~DOTCONFDocumentNode() free(values); } } - void DOTCONFDocumentNode::pushValue(char * _value) { ++valuesCount; values = (char**)realloc(values, valuesCount*sizeof(char*)); values[valuesCount-1] = strdup(_value); } - const char* DOTCONFDocumentNode::getValue(int index) const { if(index >= valuesCount){ @@ -50,7 +43,6 @@ const char* DOTCONFDocumentNode::getValue(int index) const } return values[index]; } - DOTCONFDocument::DOTCONFDocument(DOTCONFDocument::CaseSensitive caseSensitivity): mempool(NULL), curParent(NULL), curPrev(NULL), curLine(0), file(NULL), fileName(NULL) @@ -60,11 +52,9 @@ DOTCONFDocument::DOTCONFDocument(DOTCONFDocument::CaseSensitive caseSensitivity) } else { cmp_func = strcasecmp; } - mempool = new AsyncDNSMemPool(1024); mempool->initialize(); } - DOTCONFDocument::~DOTCONFDocument() { for(std::list<DOTCONFDocumentNode*>::iterator i = nodeTree.begin(); i != nodeTree.end(); ++i){ @@ -79,7 +69,6 @@ DOTCONFDocument::~DOTCONFDocument() free(fileName); delete mempool; } - int DOTCONFDocument::cleanupLine(char * line) { char * start = line; @@ -87,15 +76,12 @@ int DOTCONFDocument::cleanupLine(char * line) bool multiline = false; bool concat = false; char * word = NULL; - if(!words.empty() && quoted) concat = true; - while(*line){ if((*line == '#' || *line == ';') && !quoted){ *bg = 0; if(strlen(start)){ - if(concat){ word = (char*)mempool->alloc(strlen(words.back())+strlen(start)+1); strcpy(word, words.back()); @@ -112,7 +98,6 @@ int DOTCONFDocument::cleanupLine(char * line) if(*line == '=' && !quoted){ *line = ' ';continue; } - // Allowing \" in there causes problems with directory paths // like "C:\TrinIty\" //if(*line == '\\' && (*(line+1) == '"' || *(line+1) == '\'')){ @@ -131,7 +116,6 @@ int DOTCONFDocument::cleanupLine(char * line) if(*line == '\\' && (*(line+1) == '\n' || *(line+1) == '\r')){ *bg = 0; if(strlen(start)){ - if(concat){ word = (char*)mempool->alloc(strlen(words.back())+strlen(start)+1); strcpy(word, words.back()); @@ -153,7 +137,6 @@ int DOTCONFDocument::cleanupLine(char * line) if(isspace((unsigned char)*line) && !quoted){ *bg++ = 0; if(strlen(start)){ - if(concat){ word = (char*)mempool->alloc(strlen(words.back())+strlen(start)+1); strcpy(word, words.back()); @@ -167,20 +150,16 @@ int DOTCONFDocument::cleanupLine(char * line) } start = bg; while(isspace((unsigned char)*++line)) {} - continue; } *bg++ = *line++; } - if(quoted && !multiline){ error(curLine, fileName, "unterminated quote"); return -1; } - return multiline?1:0; } - int DOTCONFDocument::parseLine() { char * word = NULL; @@ -188,26 +167,21 @@ int DOTCONFDocument::parseLine() char * nodeValue = NULL; DOTCONFDocumentNode * tagNode = NULL; bool newNode = false; - for(std::list<char*>::iterator i = words.begin(); i != words.end(); ++i) { word = *i; - if(*word == '<'){ newNode = true; } - if(newNode){ nodeValue = NULL; nodeName = NULL; newNode = false; } - size_t wordLen = strlen(word); if(word[wordLen-1] == '>'){ word[wordLen-1] = 0; newNode = true; } - if(nodeName == NULL){ nodeName = word; bool closed = true; @@ -242,11 +216,9 @@ int DOTCONFDocument::parseLine() if(!nodeTree.empty()){ DOTCONFDocumentNode * prev = nodeTree.back(); if(prev->closed){ - curPrev->nextNode = tagNode; tagNode->previousNode = curPrev; tagNode->parentNode = curParent; - } else { prev->childNode = tagNode; tagNode->parentNode = prev; @@ -260,7 +232,6 @@ int DOTCONFDocument::parseLine() tagNode->pushValue(nodeValue); } } - return 0; } int DOTCONFDocument::parseFile(DOTCONFDocumentNode * _parent) @@ -269,10 +240,8 @@ int DOTCONFDocument::parseFile(DOTCONFDocumentNode * _parent) int ret = 0; curLine = 0; curParent = _parent; - quoted = false; size_t slen = 0; - while(fgets(str, 511, file)){ ++curLine; slen = strlen(str); @@ -297,14 +266,11 @@ int DOTCONFDocument::parseFile(DOTCONFDocumentNode * _parent) } } } - return ret; } - int DOTCONFDocument::checkConfig(const std::list<DOTCONFDocumentNode*>::iterator & from) { int ret = 0; - DOTCONFDocumentNode * tagNode = NULL; int vi = 0; for(std::list<DOTCONFDocumentNode*>::iterator i = from; i != nodeTree.end(); ++i){ @@ -316,7 +282,6 @@ int DOTCONFDocument::checkConfig(const std::list<DOTCONFDocumentNode*>::iterator } vi = 0; while( vi < tagNode->valuesCount ){ - if(strstr(tagNode->values[vi], "${") && strchr(tagNode->values[vi], '}') ){ ret = macroSubstitute(tagNode, vi ); mempool->free(); @@ -330,24 +295,18 @@ int DOTCONFDocument::checkConfig(const std::list<DOTCONFDocumentNode*>::iterator break; } } - return ret; } - int DOTCONFDocument::setContent(const char * _fileName) { int ret = 0; char realpathBuf[PATH_MAX]; - if(realpath(_fileName, realpathBuf) == NULL){ error(0, NULL, "realpath(%s) failed: %s", _fileName, strerror(errno)); return -1; } - fileName = strdup(realpathBuf); - processedFiles.push_back(strdup(realpathBuf)); - if(( file = fopen(fileName, "r")) == NULL){ error(0, NULL, "failed to open file '%s': %s", fileName, strerror(errno)); return -1; @@ -357,17 +316,12 @@ int DOTCONFDocument::setContent(const char * _fileName) fgets((char*)&utf8header, 4, file); // Try read header if (utf8header!=0x00BFBBEF) // If not exist fseek(file, 0, SEEK_SET); // Reset read position - ret = parseFile(); - (void) fclose(file); - if(!ret){ - if( (ret = checkConfig(nodeTree.begin())) == -1){ return -1; } - std::list<DOTCONFDocumentNode*>::iterator from; DOTCONFDocumentNode * tagNode = NULL; int vi = 0; @@ -384,7 +338,6 @@ int DOTCONFDocument::setContent(const char * _fileName) error(tagNode->lineNum, tagNode->fileName, "realpath(%s) failed: %s", tagNode->values[vi], strerror(errno)); return -1; } - bool processed = false; for(std::list<char*>::const_iterator itInode = processedFiles.begin(); itInode != processedFiles.end(); ++itInode){ if(!strcmp(*itInode, realpathBuf)){ @@ -395,18 +348,14 @@ int DOTCONFDocument::setContent(const char * _fileName) if(processed){ break; } - processedFiles.push_back(strdup(realpathBuf)); - file = fopen(tagNode->values[vi], "r"); if(file == NULL){ error(tagNode->lineNum, fileName, "failed to open file '%s': %s", tagNode->values[vi], strerror(errno)); return -1; } - fileName = strdup(realpathBuf); from = nodeTree.end(); --from; - ret = parseFile(); (void) fclose(file); if(ret == -1) @@ -419,14 +368,11 @@ int DOTCONFDocument::setContent(const char * _fileName) } } - if(!requiredOptions.empty()) ret = checkRequiredOptions(); } - return ret; } - int DOTCONFDocument::checkRequiredOptions() { for(std::list<char*>::const_iterator ci = requiredOptions.begin(); ci != requiredOptions.end(); ++ci){ @@ -444,40 +390,30 @@ int DOTCONFDocument::checkRequiredOptions() } return 0; } - void DOTCONFDocument::error(int lineNum, const char * fileName_, const char * fmt, ...) { va_list args; va_start(args, fmt); - size_t len = (lineNum!=0?strlen(fileName_):0) + strlen(fmt) + 50; char * buf = (char*)mempool->alloc(len); - if(lineNum) (void) snprintf(buf, len, "DOTCONF++: file '%s', line %d: %s\n", fileName_, lineNum, fmt); else (void) snprintf(buf, len, "DOTCONF++: %s\n", fmt); - (void) vfprintf(stderr, buf, args); - va_end(args); } - char * DOTCONFDocument::getSubstitution(char * macro, int lineNum) { char * buf = NULL; char * variable = macro+2; - char * endBr = strchr(macro, '}'); - if(!endBr){ error(lineNum, fileName, "unterminated '{'"); return NULL; } *endBr = 0; - char * defaultValue = strchr(variable, ':'); - if(defaultValue){ *defaultValue++ = 0; if(*defaultValue != '-'){ @@ -492,7 +428,6 @@ char * DOTCONFDocument::getSubstitution(char * macro, int lineNum) } else { defaultValue = NULL; } - char * subs = getenv(variable); if( subs ){ buf = mempool->strdup(subs); @@ -519,7 +454,6 @@ char * DOTCONFDocument::getSubstitution(char * macro, int lineNum) } return buf; } - int DOTCONFDocument::macroSubstitute(DOTCONFDocumentNode * tagNode, int valueIndex) { int ret = 0; @@ -528,7 +462,6 @@ int DOTCONFDocument::macroSubstitute(DOTCONFDocumentNode * tagNode, int valueInd char * value = (char*)mempool->alloc(valueLen); char * v = value; char * subs = NULL; - while(*macro){ if(*macro == '$' && *(macro+1) == '{'){ char * m = strchr(macro, '}'); @@ -548,12 +481,10 @@ int DOTCONFDocument::macroSubstitute(DOTCONFDocumentNode * tagNode, int valueInd *v++ = *macro++; } *v = 0; - free(tagNode->values[valueIndex]); tagNode->values[valueIndex] = strdup(value); return ret; } - const DOTCONFDocumentNode * DOTCONFDocument::getFirstNode() const { if ( !nodeTree.empty() ) { @@ -562,25 +493,19 @@ const DOTCONFDocumentNode * DOTCONFDocument::getFirstNode() const return NULL; } } - const DOTCONFDocumentNode * DOTCONFDocument::findNode(const char * nodeName, const DOTCONFDocumentNode * parentNode, const DOTCONFDocumentNode * startNode) const { - std::list<DOTCONFDocumentNode*>::const_iterator i = nodeTree.begin(); - if(startNode == NULL) startNode = parentNode; - if(startNode != NULL){ while( i != nodeTree.end() && (*i) != startNode ){ ++i; } if( i != nodeTree.end() ) ++i; } - for(; i!=nodeTree.end(); ++i){ - if((*i)->parentNode != parentNode){ continue; } @@ -590,7 +515,6 @@ const DOTCONFDocumentNode * DOTCONFDocument::findNode(const char * nodeName, con } return NULL; } - void DOTCONFDocument::setRequiredOptionNames(const char ** requiredOptionNames) { while(*requiredOptionNames){ |