michael@0: // Common/TextConfig.cpp michael@0: michael@0: #include "StdAfx.h" michael@0: michael@0: #include "Common/TextConfig.h" michael@0: michael@0: #include "Defs.h" michael@0: #include "Common/UTFConvert.h" michael@0: michael@0: static bool IsDelimitChar(char c) michael@0: { michael@0: return (c == ' ' || c == 0x0A || c == 0x0D || michael@0: c == '\0' || c == '\t'); michael@0: } michael@0: michael@0: static AString GetIDString(const char *string, int &finishPos) michael@0: { michael@0: AString result; michael@0: for (finishPos = 0; true; finishPos++) michael@0: { michael@0: char c = string[finishPos]; michael@0: if (IsDelimitChar(c) || c == '=') michael@0: return result; michael@0: result += c; michael@0: } michael@0: } michael@0: michael@0: static bool WaitNextLine(const AString &string, int &pos) michael@0: { michael@0: for (;pos < string.Length(); pos++) michael@0: if (string[pos] == 0x0A) michael@0: return true; michael@0: return false; michael@0: } michael@0: michael@0: static bool SkipSpaces(const AString &string, int &pos) michael@0: { michael@0: for (;pos < string.Length(); pos++) michael@0: { michael@0: char c = string[pos]; michael@0: if (!IsDelimitChar(c)) michael@0: { michael@0: if (c != ';') michael@0: return true; michael@0: if (!WaitNextLine(string, pos)) michael@0: return false; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool GetTextConfig(const AString &string, CObjectVector &pairs) michael@0: { michael@0: pairs.Clear(); michael@0: int pos = 0; michael@0: michael@0: ///////////////////// michael@0: // read strings michael@0: michael@0: while (true) michael@0: { michael@0: if (!SkipSpaces(string, pos)) michael@0: break; michael@0: CTextConfigPair pair; michael@0: int finishPos; michael@0: AString temp = GetIDString(((const char *)string) + pos, finishPos); michael@0: if (!ConvertUTF8ToUnicode(temp, pair.ID)) michael@0: return false; michael@0: if (finishPos == 0) michael@0: return false; michael@0: pos += finishPos; michael@0: if (!SkipSpaces(string, pos)) michael@0: return false; michael@0: if (string[pos] != '=') michael@0: return false; michael@0: pos++; michael@0: if (!SkipSpaces(string, pos)) michael@0: return false; michael@0: if (string[pos] != '\"') michael@0: return false; michael@0: pos++; michael@0: AString message; michael@0: while(true) michael@0: { michael@0: if (pos >= string.Length()) michael@0: return false; michael@0: char c = string[pos++]; michael@0: if (c == '\"') michael@0: break; michael@0: if (c == '\\') michael@0: { michael@0: char c = string[pos++]; michael@0: switch(c) michael@0: { michael@0: case 'n': michael@0: message += '\n'; michael@0: break; michael@0: case 't': michael@0: message += '\t'; michael@0: break; michael@0: case '\\': michael@0: message += '\\'; michael@0: break; michael@0: case '\"': michael@0: message += '\"'; michael@0: break; michael@0: default: michael@0: message += '\\'; michael@0: message += c; michael@0: break; michael@0: } michael@0: } michael@0: else michael@0: message += c; michael@0: } michael@0: if (!ConvertUTF8ToUnicode(message, pair.String)) michael@0: return false; michael@0: pairs.Add(pair); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: int FindTextConfigItem(const CObjectVector &pairs, const UString &id) michael@0: { michael@0: for (int i = 0; i < pairs.Size(); i++) michael@0: if (pairs[i].ID.Compare(id) == 0) michael@0: return i; michael@0: return -1; michael@0: } michael@0: michael@0: UString GetTextConfigValue(const CObjectVector &pairs, const UString &id) michael@0: { michael@0: int index = FindTextConfigItem(pairs, id); michael@0: if (index < 0) michael@0: return UString(); michael@0: return pairs[index].String; michael@0: }