// SetValue // Given a key, a value and a section, this function will attempt to locate the // Key within the given section, and if it finds it, change the keys value to // the new value. If it does not locate the key, it will create a new key with // the proper value and place it in the section requested. bool CDataFile::SetValue(t_Str szKey, t_Str szValue, t_Str szComment, t_Str szSection) { t_Key* pKey = GetKey(szKey, szSection); t_Section* pSection = GetSection(szSection); if (pSection == NULL) { if ( !(m_Flags & AUTOCREATE_SECTIONS) || !CreateSection(szSection,"")) return false; pSection = GetSection(szSection); } // Sanity check... if ( pSection == NULL ) return false; // if the key does not exist in that section, and the value passed // is not t_Str("") then add the new key. if ( pKey == NULL && szValue.size() > 0 && (m_Flags & AUTOCREATE_KEYS)) { t_Key Key; Key.szKey = szKey; Key.szValue = szValue; Key.szComment = szComment; m_bDirty = true; pSection->Keys.PushBack(Key); pKey = GetKey(szKey, szSection); return true; } if ( pKey != NULL ) { pKey->szValue = szValue; pKey->szComment = szComment; m_bDirty = true; return true; } return false; }
t_Str CDataFile::CommentStr(t_Str szComment) { t_Str szNewStr = t_Str(""); Trim(szComment); if ( szComment.size() == 0 ) return szComment; if ( szComment.find_first_of(CommentIndicators) != 0 ) { szNewStr = CommentIndicators[0]; szNewStr += " "; } szNewStr += szComment; return szNewStr; }
t_Str CDataFile::CommentStr(t_Str szComment) { t_Str szNewStr = t_Str(""); Trim(szComment); if ( szComment.size() == 0 ) return szComment; if ( szComment.find(CommentIndicators[0]) == gedString::npos && szComment.find(CommentIndicators[1]) == gedString::npos) { szNewStr = (char)CommentIndicators[0]; szNewStr += " "; } szNewStr += szComment; return szNewStr; }
// Trim // Trims whitespace from both sides of a string. void Trim(t_Str& szStr) { t_Str szTrimChars = WhiteSpace; szTrimChars += EqualIndicators; int nPos, rPos; // trim left nPos = szStr.find_first_not_of(szTrimChars); if ( nPos > 0 ) szStr.erase(0, nPos); // trim right and return nPos = szStr.find_last_not_of(szTrimChars); rPos = szStr.find_last_of(szTrimChars); if ( rPos > nPos && rPos > -1) szStr.erase(rPos, szStr.size()-rPos); }