other-licenses/7zstub/src/Common/TextConfig.cpp

branch
TOR_BUG_9701
changeset 14
925c144e1f1f
equal deleted inserted replaced
-1:000000000000 0:7ac045404344
1 // Common/TextConfig.cpp
2
3 #include "StdAfx.h"
4
5 #include "Common/TextConfig.h"
6
7 #include "Defs.h"
8 #include "Common/UTFConvert.h"
9
10 static bool IsDelimitChar(char c)
11 {
12 return (c == ' ' || c == 0x0A || c == 0x0D ||
13 c == '\0' || c == '\t');
14 }
15
16 static AString GetIDString(const char *string, int &finishPos)
17 {
18 AString result;
19 for (finishPos = 0; true; finishPos++)
20 {
21 char c = string[finishPos];
22 if (IsDelimitChar(c) || c == '=')
23 return result;
24 result += c;
25 }
26 }
27
28 static bool WaitNextLine(const AString &string, int &pos)
29 {
30 for (;pos < string.Length(); pos++)
31 if (string[pos] == 0x0A)
32 return true;
33 return false;
34 }
35
36 static bool SkipSpaces(const AString &string, int &pos)
37 {
38 for (;pos < string.Length(); pos++)
39 {
40 char c = string[pos];
41 if (!IsDelimitChar(c))
42 {
43 if (c != ';')
44 return true;
45 if (!WaitNextLine(string, pos))
46 return false;
47 }
48 }
49 return false;
50 }
51
52 bool GetTextConfig(const AString &string, CObjectVector<CTextConfigPair> &pairs)
53 {
54 pairs.Clear();
55 int pos = 0;
56
57 /////////////////////
58 // read strings
59
60 while (true)
61 {
62 if (!SkipSpaces(string, pos))
63 break;
64 CTextConfigPair pair;
65 int finishPos;
66 AString temp = GetIDString(((const char *)string) + pos, finishPos);
67 if (!ConvertUTF8ToUnicode(temp, pair.ID))
68 return false;
69 if (finishPos == 0)
70 return false;
71 pos += finishPos;
72 if (!SkipSpaces(string, pos))
73 return false;
74 if (string[pos] != '=')
75 return false;
76 pos++;
77 if (!SkipSpaces(string, pos))
78 return false;
79 if (string[pos] != '\"')
80 return false;
81 pos++;
82 AString message;
83 while(true)
84 {
85 if (pos >= string.Length())
86 return false;
87 char c = string[pos++];
88 if (c == '\"')
89 break;
90 if (c == '\\')
91 {
92 char c = string[pos++];
93 switch(c)
94 {
95 case 'n':
96 message += '\n';
97 break;
98 case 't':
99 message += '\t';
100 break;
101 case '\\':
102 message += '\\';
103 break;
104 case '\"':
105 message += '\"';
106 break;
107 default:
108 message += '\\';
109 message += c;
110 break;
111 }
112 }
113 else
114 message += c;
115 }
116 if (!ConvertUTF8ToUnicode(message, pair.String))
117 return false;
118 pairs.Add(pair);
119 }
120 return true;
121 }
122
123 int FindTextConfigItem(const CObjectVector<CTextConfigPair> &pairs, const UString &id)
124 {
125 for (int i = 0; i < pairs.Size(); i++)
126 if (pairs[i].ID.Compare(id) == 0)
127 return i;
128 return -1;
129 }
130
131 UString GetTextConfigValue(const CObjectVector<CTextConfigPair> &pairs, const UString &id)
132 {
133 int index = FindTextConfigItem(pairs, id);
134 if (index < 0)
135 return UString();
136 return pairs[index].String;
137 }

mercurial