Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Replace insert with push_back for ObjectName and ObjectData in CalendarObject.
[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.push_back(PropertyName);
102                                 ObjectData.push_back(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                         BufferChar = (*LoadStringData)[SeekCount];
127                                         
128                         if (ColonFound == true){
129                                 PropertyValue += BufferChar;
130                         } else {
131                                 ColonFound = true;
132                         }
133                         
134                 } else {
135                         
136                         // Character is not part of a new line and is not
137                         // the new line character itself.
138                         
139                         BufferChar = (*LoadStringData)[SeekCount];
140                         
141                         if (ColonFound == false){
142                                 PropertyName += BufferChar;
143                         } else {
144                                 PropertyValue += BufferChar;
145                         }
146                         
147                 }
148                 
149                 SeekCount++;
150                 
151         }
152         
153         // Finish off processing any data that wasn't processed
154         // when the end of the string was reached.
155         
156         if (ColonFound == true && 
157                 PropertyName.size() > 0 &&
158                 PropertyValue.size() > 0){
159                 
160                 ObjectName.push_back(PropertyName);
161                 ObjectData.push_back(PropertyValue);
162                         
163         }
164         
165         // Check that the object contains valid data.
166         
167         CalendarObjectLoadResult StringProcResult = CALENDAROBJECTLOAD_UNITTESTFAIL;
168         CalendarObjectValidResult BaseDataResult = ValidBaseObject();
169         CalendarObjectValidResult EventDataResult = ValidObject();
170         
171         if (BaseDataResult != CALENDAROBJECTVALID_OK || 
172                 EventDataResult != CALENDAROBJECTVALID_OK){
173                         
174                 StringProcResult = CALENDAROBJECTLOAD_INVALIDFORMAT;
175                         
176         } else {
177                 
178                 StringProcResult = CALENDAROBJECTLOAD_OK;
179                 
180         }
181         
182         ProcessBaseData();
183         ProcessData();
184         
185         return StringProcResult;
186         
189 CalendarObjectValidResult CalendarObject::ValidBaseObject(){
190         
191         bool ValidBegin = false;
192         bool ValidVersion = false;
193         bool ValidEnd = false;
194         int SeekCount = 0;
195         
196         // Check that the first line contains BEGIN:VCALENDAR
197         // and it only appears once.
198         
199         for (vector<string>::iterator iter = ObjectName.begin();
200                 iter != ObjectName.end(); iter++){
202                 if (ObjectName[SeekCount] == "BEGIN" &&
203                         ObjectData[SeekCount] == "VCALENDAR"){
204                         
205                         if (ValidBegin == false){
206                                 ValidBegin = true;
207                         } else {
208                                 return CALENDAROBJECTVALID_INVALIDFORMAT;
209                         }
210                                 
211                 }
212                 
213                 if (ObjectName[SeekCount] == "END" &&
214                         ObjectData[SeekCount] == "VCALENDAR" &&
215                         ValidBegin == false){
216                 
217                         return CALENDAROBJECTVALID_INVALIDFORMAT;
218                                 
219                 }
220                 
221                 SeekCount++;
222                         
223         }
224         
225         SeekCount = 0;
226         
227         // Check that the last line contains END:VCALENDAR
228         // and it only appears once.
229         
230         for (vector<string>::iterator iter = ObjectName.begin();
231                 iter != ObjectName.end(); iter++){
232                 
233                 if (ObjectName[SeekCount] == "END" &&
234                         ObjectData[SeekCount] == "VCALENDAR"){
235                         
236                         if (ValidEnd == false){
237                                 ValidEnd = true;
238                         } else {
239                                 return CALENDAROBJECTVALID_INVALIDFORMAT;
240                         }
241                                 
242                 }
243                         
244                 SeekCount++;
245                         
246         }
247         
248         SeekCount = 0;
249         
250         // Check that the VERSION value contains 2.0 and that
251         // it only appears once.
252         
253         for (vector<string>::iterator iter = ObjectName.begin();
254                 iter != ObjectName.end(); iter++){
255                 
256                 if (ObjectName[SeekCount] == "VERSION" &&
257                         ObjectData[SeekCount] == "2.0"){
258                         
259                         if (ValidVersion == false){
260                                 ValidVersion = true;
261                         } else {
262                                 return CALENDAROBJECTVALID_INVALIDFORMAT;
263                         }
264                                 
265                 }
266                 
267                 SeekCount++;
268                         
269         }
271         if (ValidBegin == true && 
272                 ValidEnd == true && 
273                 ValidVersion == true){
274                 
275                 return CALENDAROBJECTVALID_OK;
276                         
277         } else {
278                 
279                 return CALENDAROBJECTVALID_INVALIDFORMAT;
280                 
281         }
282         
285 void CalendarObject::ProcessBaseData(){
286         
287         // Process the base object data.
288         
289         multimap<string,string> DataReceived;
290         
291         // Get the method (METHOD).
292         
293         DataReceived = ProcessTextVectors(&ObjectName, &ObjectData, false, "METHOD");
294         
295         if (DataReceived.begin() != DataReceived.end()){
296                 
297                 try {
298                         MethodTokens = DataReceived.begin()->first.substr(7);
299                 }
300                 
301                 catch(const out_of_range &oor){
302                         // Do nothing as there is no data.
303                 }               
304                 
305                 MethodData = DataReceived.begin()->second;
306                 
307         }
308         
309         // Get the calendar scale (CALSCALE).
310         
311         DataReceived = ProcessTextVectors(&ObjectName, &ObjectData, false, "CALSCALE");
312         
313         if (DataReceived.begin() != DataReceived.end()){
314                 
315                 try {
316                         CalendarScaleTokens = DataReceived.begin()->first.substr(9);
317                 }
318                 
319                 catch(const out_of_range &oor){
320                         // Do nothing as there is no data.
321                 }               
322                 
323                 CalendarScaleData = DataReceived.begin()->second;
324                 
325         }
326         
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