bool IniFile::readIntArrayU32(const AnsiStr& section, const AnsiStr& variable, int ctExpected, std::vector<U32>& arrayInt) { AnsiStr strVal; if(readValue(section, variable, strVal)) { int pos; int iComp = 0; AnsiStr strTemp; if(strVal.firstChar() == '(') strVal = strVal.substr(1); else return false; while(strVal.lfind(',', pos)) { strTemp = strVal.substr(0, pos); strVal = strVal.substr(pos + 1); strVal.removeStartEndSpaces(); arrayInt.push_back(atoi(strTemp.ptr())); iComp++; } if(strVal.length() >= 1) { if(strVal.lastChar() == ')') { strTemp = strVal.substr(0, strVal.length() - 1); strTemp.removeStartEndSpaces(); if(strTemp.length() > 0) arrayInt.push_back(atoi(strTemp.ptr())); } } } return ((int)arrayInt.size() == ctExpected); }
//================================================================== AnsiStr ExtractOneLevelUp(const AnsiStr& strFilePath) { AnsiStr strOutput = strFilePath; bool bHasLastSlash = false; char lastSlash; //Remove last forward or backward slash while((strOutput.lastChar() == '/') || (strOutput.lastChar() == '\\')) { lastSlash = strOutput.lastChar(); strOutput = strOutput.substr(0, strOutput.length() - 1); bHasLastSlash = true; } int pos = 0; //Up one level if(strOutput.rfind('/', pos)) strOutput = strOutput.substr(0, pos); else if(strOutput.rfind('\\', pos)) strOutput = strOutput.substr(0, pos); if(bHasLastSlash) strOutput += lastSlash; return strOutput; }
//==================================================================================== bool IniFile::readValue(const AnsiStr& section, const AnsiStr& variable, AnsiStr& strValue) { AnsiStr strLine = ""; if(readLine(section, variable, strLine)) { int len = strLine.length(); for(int i=0; i < len; i++) { if(strLine[i] == L'=') { strValue = strLine.substr(i+1); return true; } } } else { AnsiStr strFileTitle = ExtractFileName(m_strFileName); vlogerror("SettingsScript tried to read [file: %s; section: %s; variable: %s] which is not found.", strFileTitle.cptr(), section.cptr(), variable.cptr()); } return false; }
//==================================================================================== int IniFile::extractSection(const AnsiStr& title, std::vector<AnsiStr>& lines) { lines.resize(0); AnsiStr str; AnsiStr titleUpperCased = title; titleUpperCased.toUpper(); int idxStart = -1; for(int i=0; i < (int)m_content.size(); i++) { str = m_content[i]; if((str.firstChar() == '[')&&(str.lastChar() == ']')) { //[ + ] = 2 str = str.substr(1, str.length() - 2); if(str.toUpper() == titleUpperCased) { idxStart = i; break; } } } //Process Content for(int i=idxStart+1; i < (int)m_content.size(); i++) { str = m_content[i]; if((str.firstChar() == '[')&&(str.lastChar() == ']')) break; else lines.push_back(str); } return (int)lines.size(); }
//================================================================== AnsiStr ExtractFileTitleOnly(const AnsiStr& strFilePath) { int pos = 0; AnsiStr strTemp = ExtractFileName(strFilePath); if(strTemp.lfind('.', pos)) strTemp = strTemp.substr(0, pos); return strTemp; }
//================================================================== AnsiStr ExtractFileExt(const AnsiStr& strPathFileName) { int npos; AnsiStr strOut; if(strPathFileName.rfind('.', npos)) { strOut = strPathFileName.substr(npos+1); } return strOut; }
//================================================================== AnsiStr ExtractFilePath(const AnsiStr& fileName) { int npos; AnsiStr strOutput; if(fileName.rfind(L'/', npos) || fileName.rfind(L'\\', npos)) { strOutput = fileName.substr(0, npos+1); } return strOutput; }
//================================================================== AnsiStr ExtractFileName(const AnsiStr& strPathFileName) { int npos; AnsiStr strOutput = strPathFileName; if(strPathFileName.rfind(L'/', npos) || strPathFileName.rfind(L'\\', npos)) { strOutput = strPathFileName.substr(npos+1); } return strOutput; }
//================================================================== AnsiStr CreateNewFileAtRoot(const char* pExtWithDot) { AnsiStr strOutput = ps::dir::GetExePath(); int posDot; if(strOutput.rfind(L'.', posDot)) { AnsiStr temp = strOutput.substr(0, posDot); temp.appendFromT(pExtWithDot); return temp; } else return strOutput; }
//==================================================================================== vec4f IniFile::readVec4f(const AnsiStr& section, const AnsiStr& variable) { AnsiStr strVal; vec4f res; if(readValue(section, variable, strVal)) { float f[4]; int pos; int iComp = 0; AnsiStr strTemp; if(strVal.firstChar() == '(') strVal = strVal.substr(1); else return res; while(strVal.lfind(',', pos)) { strTemp = strVal.substr(0, pos); strVal = strVal.substr(pos + 1); strVal.removeStartEndSpaces(); f[iComp] = static_cast<float>(atof(strTemp.ptr())); iComp++; } if(strVal.length() >= 1 && iComp < 4) { if(strVal.lastChar() == ')') { strTemp = strVal.substr(0, strVal.length() - 1); strTemp.removeStartEndSpaces(); f[iComp] = static_cast<float>(atof(strTemp.ptr())); } } res = vec4f(f[0], f[1], f[2], f[3]); } return res; }
//================================================================== AnsiStr ChangeFileExt(const AnsiStr& strFilePath, const AnsiStr& strExtWithDot) { AnsiStr strOut; int npos; if(strFilePath.rfind('.', npos)) { strOut = strFilePath.substr(0, npos); strOut += strExtWithDot; } else strOut = strFilePath + strExtWithDot; return strOut; }
//==================================================================================== bool IniFile::readLine(const AnsiStr& section, const AnsiStr& variable, AnsiStr& strLine) { int startPos = hasSection(section); if(startPos < 0) return false; //Record file pos for(int i=startPos+1; i < (int)m_content.size(); i++) { strLine = m_content[i]; //If it is a comment then ignore if(strLine.firstChar() == '#') continue; //Is section? if((strLine.firstChar() == L'[')&&(strLine.lastChar() == L']')) { return false; } else { for(int iChar=0; iChar < strLine.length(); iChar++) { if(strLine[iChar] == L'=') { AnsiStr str = strLine.substr(0, iChar); AnsiStr varUpperCased = variable; varUpperCased.toUpper(); if( str.toUpper() == varUpperCased ) { return true; } else break; } } } } return false; }
//==================================================================================== int IniFile::hasSection(const AnsiStr& strSection) { AnsiStr str; for(int i=0; i < (int)m_content.size(); i++) { str = m_content[i]; char ch = str.firstChar(); ch = str.lastChar(); if((str.firstChar() == '[')&&(str.lastChar() == ']')) { //[ + ] = 2 str = str.substr(1, str.length() - 2); AnsiStr strSecUpperCased = strSection; strSecUpperCased.toUpper(); if(str.toUpper() == strSecUpperCased) return i; } } return -1; }