Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added HexToInt function and unit tests for it.
[xestiacalendar/.git] / source / common / text.cpp
1 #include "text.h"
3 using namespace std;
5 multimap<string, string> ProcessTextVectors(vector<string> *TextProperties,
6         vector<string> *TextValues,
7         bool SearchMultiple,
8         string Property){
9         
10         multimap<string,string> ProcessResult;
11                 
12         // Go through each of the values.
13                 
14         int TextSeekCount = 0;
15         int TextPropertySize = 0;
16         int PropertySeekCount = 0;
17         string PropertyName = "";
18         int PropertyNameSize = 0;
19         char BufferChar = 0;
20                 
21         for (vector<string>::iterator iter = TextProperties->begin();
22                 iter != TextProperties->end(); iter++){
23                 
24                 TextPropertySize = iter->size();
25                         
26                 if (TextPropertySize == 0){
27                         
28                         // Text property size is 0. Go to the next
29                         // pair.
30                         
31                         continue;
32                         
33                 }
34                         
35                 // Get the property data up to the first semi-colon.
36                 
37                 while (TextSeekCount < TextPropertySize){
38                 
39                         BufferChar = (*iter)[TextSeekCount];
40                         
41                         if (BufferChar == ';'){
42                                 break;
43                         }
44                         
45                         PropertyName += BufferChar;
46                         
47                         TextSeekCount++;
48                         
49                 }
50                 
51                 if (*iter == Property || PropertyName == Property){
52                         
53                         ProcessResult.insert(make_pair((*TextProperties)[PropertySeekCount], 
54                                 (*TextValues)[PropertySeekCount]));
55                         
56                         // Check to continue if one is found.
57                         
58                         if (SearchMultiple == false){
59                                 
60                                 // Found one, don't search for anymore.
61                                 
62                                 break;
63                                 
64                         }
65                         
66                 }
67                         
68                 PropertySeekCount++;
69                 TextPropertySize = 0;
70                 TextSeekCount = 0;
71                 PropertyName.clear();
72                         
73         }
74                 
75         return ProcessResult;
76                 
77 }
79 map<string, string> SplitValues(string InputData){
80         
81         map<string,string> FinalSplitValues;
82         map<int,int> SplitPoints;
83         map<int,int> SplitLength;
84         size_t intPropertyLen = InputData.size();
85         int intSplitsFound = 0;
86         int intSplitSize = 0;
87         int intSplitSeek = 0;
88         int intPrevSplitFound = 0;
89         
90         // Get the split points.
91         
92         for (int i = 0; i <= intPropertyLen; i++){
94                 intSplitSize++;
95         
96                 InputData.substr(intPrevSplitFound, intSplitSize);
97                 
98                 if (InputData.substr(i, 1) == ";" &&
99                     InputData.substr((i - 1), 1) != "\\"){
101                         if (intSplitsFound > 0){
102                                 
103                                 // Split the value into two.
104                                 
105                                 PropertyNameValue NVData = SplitNameValue(InputData.substr(intPrevSplitFound, (intSplitSize - 1)));
106                                 
107                                 if (FinalSplitValues.find(NVData.Name) != FinalSplitValues.end()){
108                                         FinalSplitValues.insert(make_pair(NVData.Name, NVData.Value));
109                                 } else {
110                                         FinalSplitValues[NVData.Name] = NVData.Value;
111                                 }
112                                 
113                         }
114                         
115                         intPrevSplitFound = i + 1;
116                         intSplitSize = 0;
117                         intSplitsFound++;
118             
119                 }
121         }
123         if (intSplitsFound > 0){
124                 
125                 PropertyNameValue NVData = SplitNameValue(InputData.substr(intPrevSplitFound, (intSplitSize - 1)));
126                                 
127                 if (FinalSplitValues.find(NVData.Name) != FinalSplitValues.end()){
128                         FinalSplitValues.insert(make_pair(NVData.Name, NVData.Value));
129                 } else {
130                         FinalSplitValues[NVData.Name] = NVData.Value;
131                 }
132                 
133         }
134         
135         return FinalSplitValues;
139 PropertyNameValue SplitNameValue(string InputData){
140         
141         PropertyNameValue FinalNameValue;
142         int InputDataLength = InputData.size();
143         int SeekCount = 0;
144         bool QuoteMode = false;
145         bool DataFound = false;
146         
147         while (SeekCount < InputDataLength){
148                 
149                 if (InputData[SeekCount] == '='){
151                         FinalNameValue.Name = InputData.substr(0, SeekCount);
152                         
153                         try{
154                                 FinalNameValue.Value = InputData.substr((SeekCount + 1));
155                         }
156                         
157                         catch (const out_of_range &oor){
158                                 // Do nothing. Have an empty final value.
159                         }
160                         
161                         DataFound = true;
162                         break;
163                 }
164                 
165                 SeekCount++;
166                 
167         }
168         
169         if (DataFound == false){
170                 
171                 FinalNameValue.Name = InputData;
172                 
173         }
174         
175         // Check if the value has quotes at the start and end.
176         // Remove them if this is the case.
177         
178         if (FinalNameValue.Value.front() == '\"' && 
179                 FinalNameValue.Value.back() == '\"'){
180                 
181                 FinalNameValue.Value.erase(0, 1);
182                 FinalNameValue.Value.erase((FinalNameValue.Value.size() - 1), 1);
183                         
184         }
185         
186         return FinalNameValue;
187         
190 bool HexToInt(std::string *HexString, int *Number){
191         
192         // Check that each character in the string is a number
193         // or a letter (a-f/A-F).
194         
195         char Char = 0;
196         int CharNum = 0;
197         
198         for (int CharSeek = 0; 
199                 CharSeek < HexString->size(); CharSeek++){
200                 
201                 // Check if character is a number (0-9).
202                         
203                 Char = HexString->at(CharSeek);
204                 CharNum = Char;
205                         
206                 if (CharNum >= 48 &&
207                         CharNum <= 57){
208                 
209                         continue;
210                                 
211                 }
212                 
213                 // Check if character is a letter (A-F)
214                 
215                 if (CharNum >= 65 &&
216                         CharNum <= 70){
217                 
218                         continue;
219                                 
220                 }
221                 
222                 // Check if character is a letter (a-f).
224                 if (CharNum >= 97 &&
225                         CharNum <= 102){
226                 
227                         continue;
228                                 
229                 }
230                 
231                 // Exit from subroutine as character is invalid.
232                 
233                 return false;
234                 
235         }
236         
237         // Convert a Hex value that is in string to integer.
238         
239         try {
240         
241                 *Number = stoi((*HexString), nullptr, 16);
242         
243         }
244         
245         catch (const std::invalid_argument &err){
246                 
247                 return false;
248                 
249         }
250         
251         catch (const std::out_of_range &err){
252                 
253                 return false;
254                 
255         }
256         
257         return true;
258         
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy