bool Config::parseSection(const std::string& section, std::ifstream& file, int& lineNb) { std::string line; while(std::getline(file, line)) { lineNb++; if(isSection(line)) { lineNb--; putbackIntoStream(file, line); break; //end of section } //skip comments & empty lines else if(isComment(line) || isEmpty(line)) { continue; } //parse line else if(parseLine(section, line, file, lineNb)) { continue; } else { Log(LOG_TYPE::ERROR) << "Config: invalid line at '" << lineNb << "' : " << line; return false; } } return true; }
void setView(struct imageInfo *image, char *flakyClone, char *flakyStage, char *flakyView) /* Set image->view from view name if it looks good. */ { if (startsWith("2_", flakyView)) flakyView += 2; if (isAllish(flakyView)) image->view = "mixed"; else if (isAnimal(flakyView)) image->view = "animal"; else if (isAnterior(flakyView)) image->view = "anterior"; else if (isBody(flakyView)) image->view = "whole"; else if (isDorsal(flakyView)) image->view = "dorsal"; else if (isTail(flakyView)) image->view = "tail"; else if (isLateral(flakyView)) image->view = "lateral"; else if (isPosterior(flakyView)) image->view = "posterior"; else if (isSection(flakyView)) image->view = "section"; else if (isVentral(flakyView)) image->view = "ventral"; else if (isVegetal(flakyView)) image->view = "vegetal"; else if (isHead(flakyView)) image->view = "head"; }
void Config::extractArray(const std::string& section, const std::string& line, std::ifstream& file, int& lineNb) { std::string array = line; std::string lineTmp; while(std::getline(file, lineTmp)) { lineNb++; if(isSection(lineTmp)) { lineNb--; putbackIntoStream(file, lineTmp); break; //end of section } //skip comments & empty lines else if(isComment(lineTmp) || isEmpty(lineTmp)) { continue; } //parse array else { array += ":" + lineTmp; } } std::string key = section.substr(1, section.length()-2) + "=" + array; extractLine(section, key, file, lineNb); }
bool Config::parse(const char* filePath) { std::ifstream file; file.open(filePath); if(!file) { Log(LOG_TYPE::ERROR) << "Can't open config file '" << filePath << "'"; return false; } std::string line; int lineNb = 0; while(std::getline(file, line)) { lineNb++; //find section if(isSection(line)) { if(!parseSection(line, file, lineNb)) { return false; } } //skip comments & empty lines else if(!isComment(line) && !isEmpty(line)) { //otherwise flag line Log(LOG_TYPE::ERROR) << "Config: invalid line at '" << lineNb << "' : " << line; return false; } } file.close(); _parsed = true; _filePath = filePath; return true; }
void IniFile::addSection(const std::string& Name) { if(!isSection(Name)) { insert(std::make_pair(Name, new Section(Name))); } }
bool INIParser::isValue(const char * section, const char * name) { if ( isSection(section) > 0 ) if ( sections[section].count(name) > 0 ) return true; return false; }
IConfigSection* IXMLConfigSection::nextSection() { TiXmlElement* foundSection = NULL; TiXmlElement* iElement = mPElement->NextSiblingElement(); while (iElement != NULL) { if (isSection(iElement)) { foundSection = iElement; break; } iElement = iElement->NextSiblingElement(); } if (foundSection) return (IXMLConfigSection*)registerPendingInterface(new IXMLConfigSection(foundSection)); else return NULL; }
IConfigSection* IXMLConfigSection::firstSection() { // Search for a section, which is a TiXmlElement in the first place. TiXmlElement* foundSection = NULL; TiXmlElement* iElement = mPElement->FirstChildElement(); while (iElement != NULL) { if (isSection(iElement)) { foundSection = iElement; break; } iElement = iElement->NextSiblingElement(); } if (foundSection) return (IXMLConfigSection*)registerPendingInterface(new IXMLConfigSection(foundSection)); else return NULL; }
Dxf* dxfProcessDocument(FILE* dxfFile) { Dxf* root = makeDxf(); StackItem stackItem = {root, Dxf_OBJ}; stackPush(stackItem); Section* pCurrentSection = NULL; while (1) { CodeData codeData = readCodeData(dxfFile, false); if ((codeData.code == 0) && (!strcmp(codeData.data, "EOF"))) { assert(SAFE_CAST_TO(Dxf, stackTop())); stackPop(); assert(stackEmpty()); break; } if ((codeData.code == 0) && (!strcmp(codeData.data, "SECTION"))) { /* Check Context */ Dxf* pDxf = SAFE_CAST_TO(Dxf, stackTop()); assert(pDxf); /* Create an new section and push into stack */ Section* newSection = makeSection(codeData.counter); pCurrentSection = newSection; StackItem newItem = {newSection, Section_OBJ}; stackPush(newItem); /* Add the new section to parent */ if (pDxf->pSectionHead) { pDxf->pSectionTail->next = newSection; pDxf->pSectionTail = newSection; } else { pDxf->pSectionHead = pDxf->pSectionTail = newSection; } continue; } if ((codeData.code == 0) && (!strcmp(codeData.data, "ENDSEC"))) { if (pCurrentSection && (!strcmp(pCurrentSection->type, "ENTITIES"))) { Entity* pEntity = SAFE_CAST_TO(Entity, stackTop()); if (pEntity) { pEntity->endCounter = codeData.counter - 1; } stackPop(); } Section* pSection = SAFE_CAST_TO(Section, stackTop()); assert(pSection); pSection->endCounter = codeData.counter; stackPop(); continue; } /* Store Section Type */ if (isSection(&codeData)) { Section* pSection = SAFE_CAST_TO(Section, stackTop()); assert(pSection); strcpy(pSection->type, codeData.data); continue; } /* Process Code, Data under Entities Section */ if (pCurrentSection && (!strcmp(pCurrentSection->type, "ENTITIES"))) { dxfProcessEntities(&codeData); continue; } /* TODO: process other sections */ } return root; }
/* See documentation in header file. */ int ini_parse_file (FILE * file, const struct IniConfig * config, void * user) { /* Uses a fair bit of stack (use heap instead if you need to) */ char * line; char section[MAX_SECTION] = ""; char prev_name[MAX_NAME] = ""; char * start; char * end; char * name; char * value; char delim = config->delim; int lineno = 0; int error = 0; line = (char *)malloc (INI_MAX_LINE); ELEKTRA_LOG_DEBUG ("Allocated memory for line"); if (!line) { return -2; } /* Scan through file line by line */ while (fgets (line, INI_MAX_LINE, file) != NULL) { lineno++; ELEKTRA_LOG_DEBUG ("Read line %d with content “%s”", lineno, line); start = line; #if INI_ALLOW_BOM if (lineno == 1 && (unsigned char)start[0] == 0xEF && (unsigned char)start[1] == 0xBB && (unsigned char)start[2] == 0xBF) { start += 3; config->bomHandler (user, 1); } else { config->bomHandler (user, 0); } #endif if (*start == '\n') { if (!config->commentHandler (user, "") && !error) error = lineno; continue; } start = lskip (line); if (*start == '\0') { if (!config->commentHandler (user, "") && !error) error = lineno; continue; } if (isContinuation (line, config) && config->supportMultiline && *prev_name) { start = line + strlen (config->continuationString); if (*start == '"') ++start; end = line + (strlen (line) - 1); while ((*end != '"') && (!isprint (*end)) && (end > start)) { if (*end == '\n') *end = '\0'; --end; } if (*end == '"') *end = '\0'; if (!config->keyHandler (user, section, prev_name, start, 1) && !error) error = lineno; } else if (isSection (line)) { ELEKTRA_LOG_DEBUG ("Line contains a section"); end = line + (strlen (line) - 1); while (end > start) { if (*end == ']') break; --end; } ++start; if (*end == ']') { *end = '\0'; strncpy0 (section, start, sizeof (section)); *prev_name = '\0'; ELEKTRA_LOG_DEBUG ("Found section “%s”", section); size_t numberBackslashes = 0; for (char * endSection = section + strlen (section) - 1; endSection >= section && *endSection == '\\'; endSection--) { numberBackslashes++; } if (numberBackslashes % 2 != 0) { ELEKTRA_LOG_WARNING ("Found uneven number of backlashes at end of section"); error = lineno; break; } if (!config->sectionHandler (user, section) && !error) error = lineno; } else { end = line + (strlen (line) - 1); if (*end == '\n') { strncpy0 (section, start, sizeof (section)); while (fgets (line, INI_MAX_LINE, file)) { end = line + (strlen (line) - 1); while ((end > line) && *end != ']') --end; if (*end == ']') { *end = '\0'; strncpy0 (section + strlen (section), line, sizeof (section) - strlen (section)); *prev_name = '\0'; if (!config->sectionHandler (user, section) && !error) error = lineno; break; } else { strncpy0 (section + strlen (section), line, sizeof (section) - strlen (section)); } } } else { error = lineno; } } } else if (isComment (line)) { start = line; end = line + (strlen (line) - 1); if (*end == '\n') *end = '\0'; if (!config->commentHandler (user, start) && !error) error = lineno; } else { ELEKTRA_LOG_DEBUG ("Line contains a key"); char * ptr = start; unsigned int assign = 0; ELEKTRA_LOG_DEBUG ("Search for delimiter “%c”", delim); while (*ptr) { if (*ptr == delim) { ++assign; } ++ptr; } if (assign == 1) { ELEKTRA_LOG_DEBUG ("Found exactly one delimiter"); name = start; end = strchr (start, delim); if (*name == '"') { ELEKTRA_LOG_DEBUG ("Name starts with double quote character"); ++name; if (*(end - 2) == '"') { *(end - 2) = '\0'; } else if (*(end - 1) == '"') { *(end - 1) = '\0'; } else { ELEKTRA_LOG_DEBUG ("Did not find closing double quote characters in current line"); strncpy0 (prev_name, name, sizeof (prev_name)); while (fgets (line, INI_MAX_LINE, file)) { ELEKTRA_LOG_DEBUG ("Read continuation line with content “%s”", line); end = line + (strlen (line) - 1); while (end > line && *end != '"') --end; if (*end == '"') { ELEKTRA_LOG_DEBUG ("Found closing double quote character"); *(end++) = '\0'; strncpy0 (prev_name + strlen (prev_name), line, sizeof (prev_name) - strlen (prev_name)); break; } else { ELEKTRA_LOG_DEBUG ("Found name continuation"); strncpy (prev_name + strlen (prev_name), line, sizeof (prev_name) - strlen (prev_name)); } ELEKTRA_LOG_DEBUG ("New extended name is “%s”", prev_name); } name = prev_name; ELEKTRA_LOG_DEBUG ("Name of key is “%s”", name); } } if (*end != delim) { ELEKTRA_LOG_DEBUG ("Search for delimiter in “%s”", end); ptr = lskip (end + 1); end = strchr (ptr, delim); if (end && *end == delim) { *end = '\0'; ELEKTRA_LOG_DEBUG ("Found delimiter – New name is “%s”", end); } else { ELEKTRA_LOG_WARNING ("Unable to find delimiter"); error = lineno; break; } } else { *end = '\0'; } if (name != prev_name && end > line) { rstrip (end - 1); } value = lskip (end + 1); end = find_char_or_comment (value, '\0'); if (*end == ';') *end = '\0'; rstrip (value); if (*value == '"') { *(value++) = '\0'; while ((*end != '"') && !isprint (*end) && end > value) --end; if (*end == '"') *end = '\0'; } if (prev_name != name) strncpy0 (prev_name, name, sizeof (prev_name)); if (!config->keyHandler (user, section, name, value, 0) && !error) error = lineno; } else if (assign == 0) { ELEKTRA_LOG_DEBUG ("Found no delimiter"); if (*start == '"') { ELEKTRA_LOG_DEBUG ("Found initial double quote character"); ++start; end = line + (strlen (line) - 1); while (end > start && *end != '"') --end; if (*end == '"' && end != start) { *end = '\0'; if (!config->keyHandler (user, section, start, NULL, 0) && !error) error = lineno; } else { ELEKTRA_LOG_DEBUG ("Did not find closing double quote character"); strncpy0 (prev_name, start, sizeof (prev_name)); while (fgets (line, INI_MAX_LINE, file)) { end = line + (strlen (line) - 1); ELEKTRA_LOG_DEBUG ("Read continuation line with content “%s”", line); while (end > line && *end != '"') --end; if (*end == '"') { ELEKTRA_LOG_DEBUG ("Found closing double quote character"); *end = '\0'; strncpy0 (prev_name + strlen (prev_name), line, sizeof (prev_name) - strlen (prev_name)); break; } else { ELEKTRA_LOG_DEBUG ("Found name continuation"); strncpy (prev_name + strlen (prev_name), line, sizeof (prev_name) - strlen (prev_name)); } ELEKTRA_LOG_DEBUG ("New extended name is “%s”", prev_name); } name = prev_name; ptr = end + 1; end = strchr (ptr, '='); if (!end) end = strchr (ptr, ':'); if (!end) { if (!config->keyHandler (user, section, name, NULL, 0) && !error) error = lineno; } else { *end = '\0'; value = lskip (end + 1); if (*value == '"') end = find_char_or_comment (value, '\0'); if (*end == ';') *end = '\0'; rstrip (value); if (*value == '"' || *(value + 1) == '"') { if (*value == '"') *(value++) = '\0'; else if (*(value + 1) == '"') { *(value + 1) = '\0'; value += 2; } while ((*end != '"') && !isprint (*end) && end > value) --end; if (*end == '"') *end = '\0'; } if (prev_name != name) strncpy0 (prev_name, name, sizeof (prev_name)); if (!config->keyHandler (user, section, name, value, 0) && !error) error = lineno; } } } else { name = rstrip (start); strncpy0 (prev_name, name, sizeof (prev_name)); if (!config->keyHandler (user, section, name, NULL, 0) && !error) error = lineno; } } else { ELEKTRA_LOG_DEBUG ("Found multiple delimiters"); ptr = start + 1; while (*ptr) { if (*ptr == delim) { if (*(ptr + 1) == '"' || *(ptr + 2) == '"' || *(ptr - 1) == '"' || *(ptr - 2) == '"') break; } ++ptr; } if (*ptr) { ELEKTRA_LOG_DEBUG ("Found double quote character"); char tmpDel[4] = { ' ', delim, ' ', '\0' }; end = strstr (ptr, tmpDel); name = NULL; if (end) { // keyname == "=" or " = " where '=' is the delimiter if (*(ptr + 1) == '"') { *(ptr + 1) = '\0'; } else if (*(ptr + 2) == '"') { *(ptr + 2) = '\0'; } if (*(ptr - 1) == '"') *(ptr - 1) = '\0'; else if (*(ptr - 2) == '"') *(ptr - 2) = '\0'; name = ptr; } else if (*ptr == delim) { *ptr = '\0'; rstrip (start); if (*start == '"') ++start; if (*(ptr - 1) == '"') *(ptr - 1) = '\0'; else if (*(ptr - 2) == '"') *(ptr - 2) = '\0'; name = start; } else { if (!end) end = strrstr (start + 1, tmpDel); *end = '\0'; ptr = end + 2; rstrip (start); name = start; } value = ptr + 1; end = find_char_or_comment (value, '\0'); if (*end == ';') *end = '\0'; rstrip (value); if (*value == '"' || *(value + 1) == '"') { if (*value == '"') *(value++) = '\0'; else if (*(value + 1) == '"') { *(value + 1) = '\0'; value += 2; } while ((*end != '"') && !isprint (*end) && end > value) --end; if (*end == '"') *end = '\0'; } } else { ELEKTRA_LOG_DEBUG ("Found no double quote character"); rstrip (start); name = start; end = strchr (start, delim); if (!end) { ELEKTRA_LOG_DEBUG ("Found no delimiter"); value = NULL; } else { ELEKTRA_LOG_DEBUG ("Found delimiter"); if (*end == delim) *end = '\0'; rstrip (end - 1); value = lskip (end + 1); rstrip (value); if (*value == '"') { *(value++) = '\0'; while ((*end != '"') && !isprint (*end) && end > value) --end; if (*end == '"') *end = '\0'; } } } strncpy0 (prev_name, name, sizeof (prev_name)); if (!config->keyHandler (user, section, name, value, 0) && !error) error = lineno; } } #if INI_STOP_ON_FIRST_ERROR if (error) break; #endif } free (line); return error; }
bool IniFile::isKey(const std::string& Name, const std::string& Key) { return isSection(Name) && (*this)[Name]->count(Key); }