1 #include "CalendarObject.h"
2 #include "../../common/file.h"
6 CalendarObjectLoadResult CalendarObject::LoadFile(std::string LoadFilename){
8 // Check if the file exists and return
9 // CALENDAROBJECTLOAD_CANNOTOPEN if not.
11 if (!FileExists(LoadFilename)){
12 return CALENDAROBJECTLOAD_MISSING;
16 string ReceivedStringData = "";
18 FileStream.open(LoadFilename, ifstream::in);
20 if (FileStream.rdstate() & ifstream::failbit){
21 return CALENDAROBJECTLOAD_CANNOTOPEN;
24 if (FileStream.rdstate() & ifstream::badbit){
25 return CALENDAROBJECTLOAD_CANNOTOPEN;
28 // Read the data into a string.
30 char *BufferRead = new char[256];
32 while (!FileStream.eof()){
34 FileStream.getline(BufferRead, 256);
35 ReceivedStringData.append(BufferRead);
36 ReceivedStringData.append("\n");
42 CalendarObjectLoadResult StringProcResult = CALENDAROBJECTLOAD_UNITTESTFAIL;
44 StringProcResult = LoadString(&ReceivedStringData);
46 return StringProcResult;
50 CalendarObjectLoadResult CalendarObject::LoadString(std::string *LoadStringData){
53 bool SkipMode = false;
54 bool ColonFound = false;
55 bool QuoteMode = false;
57 int StringDataSize = LoadStringData->size();
62 while (SeekCount < StringDataSize){
64 // Check if character is blank or a tab and is the first character
67 if (((*LoadStringData)[SeekCount] == ' ' ||
68 (*LoadStringData)[SeekCount] == '\t')
71 // Character is on a new line and it is a space or
72 // tab. Ignore this character as it is not part
77 } else if ((*LoadStringData)[SeekCount] == '\"'){
79 if (QuoteMode == false){
85 BufferChar = (*LoadStringData)[SeekCount];
87 if (ColonFound == false){
88 PropertyName += BufferChar;
90 PropertyValue += BufferChar;
93 } else if (NewLine == true){
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
100 if (ColonFound == true){
101 ObjectName.insert(ObjectName.end(), PropertyName);
102 ObjectData.insert(ObjectData.end(), PropertyValue);
107 PropertyName.clear();
108 PropertyValue.clear();
110 BufferChar = (*LoadStringData)[SeekCount];
111 PropertyName += BufferChar;
113 } else if ((*LoadStringData)[SeekCount] == '\n'){
115 // Character is the new line character so mark
116 // the NewLine boolean as true.
120 } else if ((*LoadStringData)[SeekCount] == ':' &&
123 // Character is the colon. Set the colon
124 // found boolen to true.
130 // Character is not part of a new line and is not
131 // the new line character itself.
133 BufferChar = (*LoadStringData)[SeekCount];
135 if (ColonFound == false){
136 PropertyName += BufferChar;
138 PropertyValue += BufferChar;
147 // Finish off processing any data that wasn't processed
148 // when the end of the string was reached.
150 if (ColonFound == true &&
151 PropertyName.size() > 0 &&
152 PropertyValue.size() > 0){
154 ObjectName.insert(ObjectName.end(), PropertyName);
155 ObjectData.insert(ObjectData.end(), PropertyValue);
159 // Check that the object contains valid data.
161 CalendarObjectLoadResult StringProcResult = CALENDAROBJECTLOAD_UNITTESTFAIL;
162 CalendarObjectValidResult BaseDataResult = ValidBaseObject();
163 CalendarObjectValidResult EventDataResult = ValidObject();
165 if (BaseDataResult != CALENDAROBJECTVALID_OK ||
166 EventDataResult != CALENDAROBJECTVALID_OK){
168 StringProcResult = CALENDAROBJECTLOAD_INVALIDFORMAT;
172 StringProcResult = CALENDAROBJECTLOAD_OK;
179 return StringProcResult;
183 CalendarObjectValidResult CalendarObject::ValidBaseObject(){
185 bool ValidBegin = false;
186 bool ValidVersion = false;
187 bool ValidEnd = false;
190 // Check that the first line contains BEGIN:VCALENDAR
191 // and it only appears once.
193 for (vector<string>::iterator iter = ObjectName.begin();
194 iter != ObjectName.end(); iter++){
196 if (ObjectName[SeekCount] == "BEGIN" &&
197 ObjectData[SeekCount] == "VCALENDAR"){
199 if (ValidBegin == false){
202 return CALENDAROBJECTVALID_INVALIDFORMAT;
207 if (ObjectName[SeekCount] == "END" &&
208 ObjectData[SeekCount] == "VCALENDAR" &&
209 ValidBegin == false){
211 return CALENDAROBJECTVALID_INVALIDFORMAT;
221 // Check that the last line contains END:VCALENDAR
222 // and it only appears once.
224 for (vector<string>::iterator iter = ObjectName.begin();
225 iter != ObjectName.end(); iter++){
227 if (ObjectName[SeekCount] == "END" &&
228 ObjectData[SeekCount] == "VCALENDAR"){
230 if (ValidEnd == false){
233 return CALENDAROBJECTVALID_INVALIDFORMAT;
244 // Check that the VERSION value contains 2.0 and that
245 // it only appears once.
247 for (vector<string>::iterator iter = ObjectName.begin();
248 iter != ObjectName.end(); iter++){
250 if (ObjectName[SeekCount] == "VERSION" &&
251 ObjectData[SeekCount] == "2.0"){
253 if (ValidVersion == false){
256 return CALENDAROBJECTVALID_INVALIDFORMAT;
265 if (ValidBegin == true &&
267 ValidVersion == true){
269 return CALENDAROBJECTVALID_OK;
273 return CALENDAROBJECTVALID_INVALIDFORMAT;
279 void CalendarObject::ProcessBaseData(){
281 // Process the base object data.
283 multimap<string,string> DataReceived;
285 // Get the method (METHOD).
287 DataReceived = ProcessTextVectors(&ObjectName, &ObjectData, false, "METHOD");
289 if (DataReceived.begin() != DataReceived.end()){
292 MethodTokens = DataReceived.begin()->first.substr(7);
295 catch(const out_of_range &oor){
296 // Do nothing as there is no data.
299 MethodData = DataReceived.begin()->second;
303 // Get the calendar scale (CALSCALE).
305 DataReceived = ProcessTextVectors(&ObjectName, &ObjectData, false, "CALSCALE");
307 if (DataReceived.begin() != DataReceived.end()){
310 CalendarScaleTokens = DataReceived.begin()->first.substr(9);
313 catch(const out_of_range &oor){
314 // Do nothing as there is no data.
317 CalendarScaleData = DataReceived.begin()->second;