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; } // Try read utf8 header and skip it if exist uint32 utf8header = 0; 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; for(std::list<DOTCONFDocumentNode*>::iterator i = nodeTree.begin(); i!=nodeTree.end(); ++i){ tagNode = *i; if(!cmp_func("DOTCONFPPIncludeFile", tagNode->name)){ vi = 0; while( vi < tagNode->valuesCount ){ if(access(tagNode->values[vi], R_OK) == -1){ error(tagNode->lineNum, tagNode->fileName, "%s: %s", tagNode->values[vi], strerror(errno)); return -1; } if(realpath(tagNode->values[vi], realpathBuf) == NULL){ 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)){ processed = true; break; } } 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) return -1; if(checkConfig(++from) == -1){ return -1; } ++vi; } } } if(!requiredOptions.empty()) ret = checkRequiredOptions(); } 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; } 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; for(std::list<DOTCONFDocumentNode*>::iterator i = nodeTree.begin(); i!=nodeTree.end(); i++) { tagNode = *i; if(!cmp_func("DOTCONFPPIncludeFile", tagNode->name)) { vi = 0; while( vi < tagNode->valuesCount ) { if(access(tagNode->values[vi], R_OK) == -1) { error(tagNode->lineNum, tagNode->fileName, "%s: %s", tagNode->values[vi], strerror(errno)); return -1; } if(realpath(tagNode->values[vi], realpathBuf) == NULL) { 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)) { processed = true; break; } } 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) return -1; if(checkConfig(++from) == -1) { return -1; } vi++; } } } if(!requiredOptions.empty()) ret = checkRequiredOptions(); } return ret; }