Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Run ProcessData after loading the data in LoadString.
[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         char BufferChar = 0;
56         int StringDataSize = LoadStringData->size();
57         int SeekCount = 0;
58         string PropertyName;
59         string PropertyValue;
61         while (SeekCount < StringDataSize){
62                 
63                 // Check if character is blank or a tab and is the first character
64                 // on a new line.
65                 
66                 if (((*LoadStringData)[SeekCount] == ' ' || 
67                         (*LoadStringData)[SeekCount] == '\t')
68                         && NewLine == true){
69                         
70                         // Character is on a new line and it is a space or
71                         // tab. Ignore this character as it is not part
72                         // of the value.
74                         NewLine = false;
75                                 
76                 } else if (NewLine == true){
77                 
78                         // Character is on a new line but not a space or
79                         // tab so check if the colon has been found
80                         // and add the property name and value to
81                         // the lists.
83                         if (ColonFound == true){
84                                 ObjectName.insert(ObjectName.end(), PropertyName);
85                                 ObjectData.insert(ObjectData.end(), PropertyValue);
86                         }
87                         
88                         ColonFound = false;
89                         NewLine = false;
90                         PropertyName.clear();
91                         PropertyValue.clear();
92                         
93                         BufferChar = (*LoadStringData)[SeekCount];
94                         PropertyName += BufferChar;                     
95                         
96                 } else if ((*LoadStringData)[SeekCount] == '\n'){
97                 
98                         // Character is the new line character so mark
99                         // the NewLine boolean as true.
100                         
101                         NewLine = true;
102                         
103                 } else if ((*LoadStringData)[SeekCount] == ':'){
104                 
105                         // Character is the colon. Set the colon
106                         // found boolen to true.
107                         
108                         ColonFound = true;
109                         
110                 } else {
111                         
112                         // Character is not part of a new line and is not
113                         // the new line character itself.
114                         
115                         BufferChar = (*LoadStringData)[SeekCount];
116                         
117                         if (ColonFound == false){
118                                 PropertyName += BufferChar;
119                         } else {
120                                 PropertyValue += BufferChar;
121                         }
122                         
123                 }
124                 
125                 SeekCount++;
126                 
127         }
128         
129         // Finish off processing any data that wasn't processed
130         // when the end of the string was reached.
131         
132         if (ColonFound == true && 
133                 PropertyName.size() > 0 &&
134                 PropertyValue.size() > 0){
135                 
136                 ObjectName.insert(ObjectName.end(), PropertyName);
137                 ObjectData.insert(ObjectData.end(), PropertyValue);
138                         
139         }
140         
141         // Check that the object contains valid data.
142         
143         CalendarObjectLoadResult StringProcResult = CALENDAROBJECTLOAD_UNITTESTFAIL;
144         CalendarObjectValidResult BaseDataResult = ValidBaseObject();
145         CalendarObjectValidResult EventDataResult = ValidObject();
146         
147         if (BaseDataResult != CALENDAROBJECTVALID_OK || 
148                 EventDataResult != CALENDAROBJECTVALID_OK){
149                         
150                 StringProcResult = CALENDAROBJECTLOAD_INVALIDFORMAT;
151                         
152         } else {
153                 
154                 StringProcResult = CALENDAROBJECTLOAD_OK;
155                 
156         }
157         
158         ProcessData();
159         
160         return StringProcResult;
161         
164 CalendarObjectValidResult CalendarObject::ValidBaseObject(){
165         
166         bool ValidBegin = false;
167         bool ValidVersion = false;
168         bool ValidEnd = false;
169         int SeekCount = 0;
170         
171         // Check that the first line contains BEGIN:VCALENDAR
172         // and it only appears once.
173         
174         for (vector<string>::iterator iter = ObjectName.begin();
175                 iter != ObjectName.end(); iter++){
177                 if (ObjectName[SeekCount] == "BEGIN" &&
178                         ObjectData[SeekCount] == "VCALENDAR"){
179                         
180                         if (ValidBegin == false){
181                                 ValidBegin = true;
182                         } else {
183                                 return CALENDAROBJECTVALID_INVALIDFORMAT;
184                         }
185                                 
186                 }
187                 
188                 if (ObjectName[SeekCount] == "END" &&
189                         ObjectData[SeekCount] == "VCALENDAR" &&
190                         ValidBegin == false){
191                 
192                         return CALENDAROBJECTVALID_INVALIDFORMAT;
193                                 
194                 }
195                 
196                 SeekCount++;
197                         
198         }
199         
200         SeekCount = 0;
201         
202         // Check that the last line contains END:VCALENDAR
203         // and it only appears once.
204         
205         for (vector<string>::iterator iter = ObjectName.begin();
206                 iter != ObjectName.end(); iter++){
207                 
208                 if (ObjectName[SeekCount] == "END" &&
209                         ObjectData[SeekCount] == "VCALENDAR"){
210                         
211                         if (ValidEnd == false){
212                                 ValidEnd = true;
213                         } else {
214                                 return CALENDAROBJECTVALID_INVALIDFORMAT;
215                         }
216                                 
217                 }
218                         
219                 SeekCount++;
220                         
221         }
222         
223         SeekCount = 0;
224         
225         // Check that the VERSION value contains 2.0 and that
226         // it only appears once.
227         
228         for (vector<string>::iterator iter = ObjectName.begin();
229                 iter != ObjectName.end(); iter++){
230                 
231                 if (ObjectName[SeekCount] == "VERSION" &&
232                         ObjectData[SeekCount] == "2.0"){
233                         
234                         if (ValidVersion == false){
235                                 ValidVersion = true;
236                         } else {
237                                 return CALENDAROBJECTVALID_INVALIDFORMAT;
238                         }
239                                 
240                 }
241                 
242                 SeekCount++;
243                         
244         }
246         if (ValidBegin == true && 
247                 ValidEnd == true && 
248                 ValidVersion == true){
249                 
250                 return CALENDAROBJECTVALID_OK;
251                         
252         } else {
253                 
254                 return CALENDAROBJECTVALID_INVALIDFORMAT;
255                 
256         }
257         
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