Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
camelCase: Converted remaining code that was missed
[xestiacalendar/.git] / source / common / text.cpp
1 // text.cpp - Text functions.
2 //
3 // (c) 2016-2017 Xestia Software Development.
4 //
5 // This file is part of Xestia Calendar.
6 //
7 // Xestia Calendar is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
10 //
11 // Xestia Calendar is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Calendar. If not, see <http://www.gnu.org/licenses/>
19 #include "text.h"
21 using namespace std;
23 multimap<string, string> ProcessTextVectors(vector<string> *textProperties,
24         vector<string> *textValues,
25         bool searchMultiple,
26         string property){
27         
28         multimap<string,string> processResult;
29                 
30         // Go through each of the values.
31                 
32         int textSeekCount = 0;
33         int textPropertySize = 0;
34         int propertySeekCount = 0;
35         string propertyName = "";
36         int propertyNameSize = 0;
37         char bufferChar = 0;
38                 
39         for (vector<string>::iterator iter = textProperties->begin();
40                 iter != textProperties->end(); iter++){
41                 
42                 textPropertySize = iter->size();
43                         
44                 if (textPropertySize == 0){
45                         
46                         // Text property size is 0. Go to the next
47                         // pair.
48                         
49                         continue;
50                         
51                 }
52                         
53                 // Get the property data up to the first semi-colon.
54                 
55                 while (textSeekCount < textPropertySize){
56                 
57                         bufferChar = (*iter)[textSeekCount];
58                         
59                         if (bufferChar == ';'){
60                                 break;
61                         }
62                         
63                         propertyName += bufferChar;
64                         
65                         textSeekCount++;
66                         
67                 }
68                 
69                 if (*iter == property || propertyName == property){
70                         
71                         processResult.insert(make_pair((*textProperties)[propertySeekCount], 
72                                 (*textValues)[propertySeekCount]));
73                         
74                         // Check to continue if one is found.
75                         
76                         if (searchMultiple == false){
77                                 
78                                 // Found one, don't search for anymore.
79                                 
80                                 break;
81                                 
82                         }
83                         
84                 }
85                         
86                 propertySeekCount++;
87                 textPropertySize = 0;
88                 textSeekCount = 0;
89                 propertyName.clear();
90                         
91         }
92                 
93         return processResult;
94                 
95 }
97 map<string, string> SplitValues(string inputData){
98         
99         map<string,string> finalSplitValues;
100         map<int,int> splitPoints;
101         map<int,int> splitLength;
102         size_t intPropertyLen = inputData.size();
103         int intSplitsFound = 0;
104         int intSplitSize = 0;
105         int intSplitSeek = 0;
106         int intPrevSplitFound = 0;
107         
108         // Get the split points.
109         
110         for (int i = 0; i <= intPropertyLen; i++){
112                 intSplitSize++;
113         
114                 inputData.substr(intPrevSplitFound, intSplitSize);
115                 
116                 if (inputData.substr(i, 1) == ";" &&
117                     inputData.substr((i - 1), 1) != "\\"){
119                         if (intSplitsFound > 0){
120                                 
121                                 // Split the value into two.
122                                 
123                                 PropertyNameValue nVData = SplitNameValue(inputData.substr(intPrevSplitFound, (intSplitSize - 1)));
124                                 
125                                 if (finalSplitValues.find(nVData.name) != finalSplitValues.end()){
126                                         finalSplitValues.insert(make_pair(nVData.name, nVData.value));
127                                 } else {
128                                         finalSplitValues[nVData.name] = nVData.value;
129                                 }
130                                 
131                         }
132                         
133                         intPrevSplitFound = i + 1;
134                         intSplitSize = 0;
135                         intSplitsFound++;
136             
137                 }
139         }
141         if (intSplitsFound > 0){
142                 
143                 PropertyNameValue nVData = SplitNameValue(inputData.substr(intPrevSplitFound, (intSplitSize - 1)));
144                                 
145                 if (finalSplitValues.find(nVData.name) != finalSplitValues.end()){
146                         finalSplitValues.insert(make_pair(nVData.name, nVData.value));
147                 } else {
148                         finalSplitValues[nVData.name] = nVData.value;
149                 }
150                 
151         }
152         
153         return finalSplitValues;
157 PropertyNameValue SplitNameValue(string inputData){
158         
159         PropertyNameValue finalNameValue;
160         int inputDataLength = inputData.size();
161         int seekCount = 0;
162         bool quoteMode = false;
163         bool dataFound = false;
164         
165         while (seekCount < inputDataLength){
166                 
167                 if (inputData[seekCount] == '='){
169                         finalNameValue.name = inputData.substr(0, seekCount);
170                         
171                         try{
172                                 finalNameValue.value = inputData.substr((seekCount + 1));
173                         }
174                         
175                         catch (const out_of_range &oor){
176                                 // Do nothing. Have an empty final value.
177                         }
178                         
179                         dataFound = true;
180                         break;
181                 }
182                 
183                 seekCount++;
184                 
185         }
186         
187         if (dataFound == false){
188                 
189                 finalNameValue.name = inputData;
190                 
191         }
192         
193         // Check if the value has quotes at the start and end.
194         // Remove them if this is the case.
195         
196         if (finalNameValue.value.front() == '\"' && 
197                 finalNameValue.value.back() == '\"'){
198                 
199                 finalNameValue.value.erase(0, 1);
200                 finalNameValue.value.erase((finalNameValue.value.size() - 1), 1);
201                         
202         }
203         
204         return finalNameValue;
205         
208 bool HexToInt(std::string *hexString, int *number){
209         
210         // Check that each character in the string is a number
211         // or a letter (a-f/A-F).
212         
213         char charLetter = 0;
214         int charNum = 0;
215         
216         for (int charSeek = 0; 
217                 charSeek < hexString->size(); charSeek++){
218                 
219                 // Check if character is a number (0-9).
220                         
221                 charLetter = hexString->at(charSeek);
222                 charNum = charLetter;
223                         
224                 if (charNum >= 48 &&
225                         charNum <= 57){
226                 
227                         continue;
228                                 
229                 }
230                 
231                 // Check if character is a letter (A-F)
232                 
233                 if (charNum >= 65 &&
234                         charNum <= 70){
235                 
236                         continue;
237                                 
238                 }
239                 
240                 // Check if character is a letter (a-f).
242                 if (charNum >= 97 &&
243                         charNum <= 102){
244                 
245                         continue;
246                                 
247                 }
248                 
249                 // Exit from subroutine as character is invalid.
250                 
251                 return false;
252                 
253         }
254         
255         // Convert a Hex value that is in string to integer.
256         
257         try {
258         
259                 *number = stoi((*hexString), nullptr, 16);
260         
261         }
262         
263         catch (const std::invalid_argument &err){
264                 
265                 return false;
266                 
267         }
268         
269         catch (const std::out_of_range &err){
270                 
271                 return false;
272                 
273         }
274         
275         return true;
276         
279 bool IntToHex(int *number, std::string *hexString, int hexFill){
280         
281         stringstream stringData;
282         stringData << setfill('0') << hex << setw(hexFill) << (*number);
283         (*hexString) = stringData.str();
284         
285         return true;
286         
289 void SplitPathFilename(string *calendarEntryHREF, string *entryURIPath, 
290         string *entryFilename){
291         
292         // Look for the last forward slash.
293                 
294         int lastForwardSlash = -1;
295         int charSeek = 0;
296         string stringIterChar = "";
297                 
298         for (string::iterator stringIter = calendarEntryHREF->begin();
299                 stringIter != calendarEntryHREF->end(); stringIter++){
300                 
301                 stringIterChar = *stringIter;
302                         
303                 if (stringIterChar == "/"){
304                         lastForwardSlash = charSeek;
305                 }
306                 
307                 charSeek++;
308                         
309         }
310         
311         if (lastForwardSlash == -1){
312                 
313                 return;
314                 
315         }
316         
317         // Get the string before the last hash for the path.
318         
319         (*entryURIPath) = calendarEntryHREF->substr(0, (lastForwardSlash + 1));
320         
321         // Get the string after the last hash for the filename.
322         
323         (*entryFilename) = calendarEntryHREF->substr((lastForwardSlash + 1));
324                 
327 string OutputText(string *textInput){
328         
329         string outputTextData;
330         string outputLine;
331         int charSeek = 0;
332         int lineSeek = 0;
333         int maxLineSeek = 77;
334         
335         for (charSeek = 0; charSeek < textInput->size(); charSeek++){
336                 
337                 lineSeek++;
338                 
339                 if (lineSeek == maxLineSeek){
341                         if (textInput->substr(charSeek, 1) != "\n"){
342                                 outputLine += textInput->substr(charSeek, 1);
343                         }
344                         outputLine += "\n";
345                         outputTextData += outputLine;
346                         outputLine = " ";
347                         lineSeek = 0;
348                         maxLineSeek = 76;
349                         continue;
350                         
351                 }
352                 
353                 outputLine += textInput->substr(charSeek, 1);
354                 
355         }
357         if (outputLine != " "){
358         
359                 outputTextData += outputLine;
360                 
361         }
362         
363         return outputTextData;
364         
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