Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Check for quote and go in and out of Quote mode as needed.
[xestiacalendar/.git] / source / objects / calendarobject / CalendarObject.cpp
1 #include "CalendarObject.h"
2 #include "../../common/file.h"
4 using namespace std;
6 CalendarObjectLoadResult CalendarObject::LoadFile(std::string LoadFilename){
8         // Check if the file exists and return 
9         // CALENDAROBJECTLOAD_CANNOTOPEN if not.
10         
11         if (!FileExists(LoadFilename)){
12                 return CALENDAROBJECTLOAD_MISSING;
13         }
14         
15         ifstream FileStream;
16         string ReceivedStringData = "";
17         
18         FileStream.open(LoadFilename, ifstream::in);
19         
20         if (FileStream.rdstate() & ifstream::failbit){
21                 return CALENDAROBJECTLOAD_CANNOTOPEN;
22         }
24         if (FileStream.rdstate() & ifstream::badbit){
25                 return CALENDAROBJECTLOAD_CANNOTOPEN;
26         }
27         
28         // Read the data into a string.
29         
30         char *BufferRead = new char[256];
31         
32         while (!FileStream.eof()){
33                 
34                 FileStream.getline(BufferRead, 256);
35                 ReceivedStringData.append(BufferRead);
36                 ReceivedStringData.append("\n");
37                 
38         }
39         
40         delete[] BufferRead;
41         
42         CalendarObjectLoadResult StringProcResult = CALENDAROBJECTLOAD_UNITTESTFAIL;
43         
44         StringProcResult = LoadString(&ReceivedStringData);
45         
46         return StringProcResult;
47         
48 }
50 CalendarObjectLoadResult CalendarObject::LoadString(std::string *LoadStringData){
52         bool NewLine = false;
53         bool SkipMode = false;
54         bool ColonFound = false;
55         bool QuoteMode = false;
56         char BufferChar = 0;
57         int StringDataSize = LoadStringData->size();
58         int SeekCount = 0;
59         string PropertyName;
60         string PropertyValue;
62         while (SeekCount < StringDataSize){
63                 
64                 // Check if character is blank or a tab and is the first character
65                 // on a new line.
66                 
67                 if (((*LoadStringData)[SeekCount] == ' ' || 
68                         (*LoadStringData)[SeekCount] == '\t')
69                         && NewLine == true){
70                         
71                         // Character is on a new line and it is a space or
72                         // tab. Ignore this character as it is not part
73                         // of the value.
75                         NewLine = false;
76                                 
77                 } else if ((*LoadStringData)[SeekCount] == '\"'){
78                         
79                         if (QuoteMode == false){
80                                 QuoteMode = true;
81                         } else {
82                                 QuoteMode = false;
83                         }
84                         
85                         BufferChar = (*LoadStringData)[SeekCount];
86                         
87                         if (ColonFound == false){
88                                 PropertyName += BufferChar;
89                         } else {
90                                 PropertyValue += BufferChar;
91                         }
92                         
93                 } else if (NewLine == true){
94                 
95                         // Character is on a new line but not a space or
96                         // tab so check if the colon has been found
97                         // and add the property name and value to
98                         // the lists.
100                         if (ColonFound == true){
101                                 ObjectName.insert(ObjectName.end(), PropertyName);
102                                 ObjectData.insert(ObjectData.end(), PropertyValue);
103                         }
104                         
105                         ColonFound = false;
106                         NewLine = false;
107                         PropertyName.clear();
108                         PropertyValue.clear();
109                         
110                         BufferChar = (*LoadStringData)[SeekCount];
111                         PropertyName += BufferChar;                     
112                         
113                 } else if ((*LoadStringData)[SeekCount] == '\n'){
114                 
115                         // Character is the new line character so mark
116                         // the NewLine boolean as true.
117                         
118                         NewLine = true;
119                         
120                 } else if ((*LoadStringData)[SeekCount] == ':' &&
121                                 QuoteMode == false){
122                 
123                         // Character is the colon. Set the colon
124                         // found boolen to true.
125                         
126                         ColonFound = true;
127                         
128                 } else {
129                         
130                         // Character is not part of a new line and is not
131                         // the new line character itself.
132                         
133                         BufferChar = (*LoadStringData)[SeekCount];
134                         
135                         if (ColonFound == false){
136                                 PropertyName += BufferChar;
137                         } else {
138                                 PropertyValue += BufferChar;
139                         }
140                         
141                 }
142                 
143                 SeekCount++;
144                 
145         }
146         
147         // Finish off processing any data that wasn't processed
148         // when the end of the string was reached.
149         
150         if (ColonFound == true && 
151                 PropertyName.size() > 0 &&
152                 PropertyValue.size() > 0){
153                 
154                 ObjectName.insert(ObjectName.end(), PropertyName);
155                 ObjectData.insert(ObjectData.end(), PropertyValue);
156                         
157         }
158         
159         // Check that the object contains valid data.
160         
161         CalendarObjectLoadResult StringProcResult = CALENDAROBJECTLOAD_UNITTESTFAIL;
162         CalendarObjectValidResult BaseDataResult = ValidBaseObject();
163         CalendarObjectValidResult EventDataResult = ValidObject();
164         
165         if (BaseDataResult != CALENDAROBJECTVALID_OK || 
166                 EventDataResult != CALENDAROBJECTVALID_OK){
167                         
168                 StringProcResult = CALENDAROBJECTLOAD_INVALIDFORMAT;
169                         
170         } else {
171                 
172                 StringProcResult = CALENDAROBJECTLOAD_OK;
173                 
174         }
175         
176         ProcessBaseData();
177         ProcessData();
178         
179         return StringProcResult;
180         
183 CalendarObjectValidResult CalendarObject::ValidBaseObject(){
184         
185         bool ValidBegin = false;
186         bool ValidVersion = false;
187         bool ValidEnd = false;
188         int SeekCount = 0;
189         
190         // Check that the first line contains BEGIN:VCALENDAR
191         // and it only appears once.
192         
193         for (vector<string>::iterator iter = ObjectName.begin();
194                 iter != ObjectName.end(); iter++){
196                 if (ObjectName[SeekCount] == "BEGIN" &&
197                         ObjectData[SeekCount] == "VCALENDAR"){
198                         
199                         if (ValidBegin == false){
200                                 ValidBegin = true;
201                         } else {
202                                 return CALENDAROBJECTVALID_INVALIDFORMAT;
203                         }
204                                 
205                 }
206                 
207                 if (ObjectName[SeekCount] == "END" &&
208                         ObjectData[SeekCount] == "VCALENDAR" &&
209                         ValidBegin == false){
210                 
211                         return CALENDAROBJECTVALID_INVALIDFORMAT;
212                                 
213                 }
214                 
215                 SeekCount++;
216                         
217         }
218         
219         SeekCount = 0;
220         
221         // Check that the last line contains END:VCALENDAR
222         // and it only appears once.
223         
224         for (vector<string>::iterator iter = ObjectName.begin();
225                 iter != ObjectName.end(); iter++){
226                 
227                 if (ObjectName[SeekCount] == "END" &&
228                         ObjectData[SeekCount] == "VCALENDAR"){
229                         
230                         if (ValidEnd == false){
231                                 ValidEnd = true;
232                         } else {
233                                 return CALENDAROBJECTVALID_INVALIDFORMAT;
234                         }
235                                 
236                 }
237                         
238                 SeekCount++;
239                         
240         }
241         
242         SeekCount = 0;
243         
244         // Check that the VERSION value contains 2.0 and that
245         // it only appears once.
246         
247         for (vector<string>::iterator iter = ObjectName.begin();
248                 iter != ObjectName.end(); iter++){
249                 
250                 if (ObjectName[SeekCount] == "VERSION" &&
251                         ObjectData[SeekCount] == "2.0"){
252                         
253                         if (ValidVersion == false){
254                                 ValidVersion = true;
255                         } else {
256                                 return CALENDAROBJECTVALID_INVALIDFORMAT;
257                         }
258                                 
259                 }
260                 
261                 SeekCount++;
262                         
263         }
265         if (ValidBegin == true && 
266                 ValidEnd == true && 
267                 ValidVersion == true){
268                 
269                 return CALENDAROBJECTVALID_OK;
270                         
271         } else {
272                 
273                 return CALENDAROBJECTVALID_INVALIDFORMAT;
274                 
275         }
276         
279 void CalendarObject::ProcessBaseData(){
280         
281         // Process the base object data.
282         
283         multimap<string,string> DataReceived;
284         
285         // Get the method (METHOD).
286         
287         DataReceived = ProcessTextVectors(&ObjectName, &ObjectData, false, "METHOD");
288         
289         if (DataReceived.begin() != DataReceived.end()){
290                 
291                 try {
292                         MethodTokens = DataReceived.begin()->first.substr(7);
293                 }
294                 
295                 catch(const out_of_range &oor){
296                         // Do nothing as there is no data.
297                 }               
298                 
299                 MethodData = DataReceived.begin()->second;
300                 
301         }
302         
303         // Get the calendar scale (CALSCALE).
304         
305         DataReceived = ProcessTextVectors(&ObjectName, &ObjectData, false, "CALSCALE");
306         
307         if (DataReceived.begin() != DataReceived.end()){
308                 
309                 try {
310                         CalendarScaleTokens = DataReceived.begin()->first.substr(9);
311                 }
312                 
313                 catch(const out_of_range &oor){
314                         // Do nothing as there is no data.
315                 }               
316                 
317                 CalendarScaleData = DataReceived.begin()->second;
318                 
319         }
320         
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