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.push_back(PropertyName);
102 ObjectData.push_back(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.
126 BufferChar = (*LoadStringData)[SeekCount];
128 if (ColonFound == true){
129 PropertyValue += BufferChar;
136 // Character is not part of a new line and is not
137 // the new line character itself.
139 BufferChar = (*LoadStringData)[SeekCount];
141 if (ColonFound == false){
142 PropertyName += BufferChar;
144 PropertyValue += BufferChar;
153 // Finish off processing any data that wasn't processed
154 // when the end of the string was reached.
156 if (ColonFound == true &&
157 PropertyName.size() > 0 &&
158 PropertyValue.size() > 0){
160 ObjectName.push_back(PropertyName);
161 ObjectData.push_back(PropertyValue);
165 // Check that the object contains valid data.
167 CalendarObjectLoadResult StringProcResult = CALENDAROBJECTLOAD_UNITTESTFAIL;
168 CalendarObjectValidResult BaseDataResult = ValidBaseObject();
169 CalendarObjectValidResult EventDataResult = ValidObject();
171 if (BaseDataResult != CALENDAROBJECTVALID_OK ||
172 EventDataResult != CALENDAROBJECTVALID_OK){
174 StringProcResult = CALENDAROBJECTLOAD_INVALIDFORMAT;
178 StringProcResult = CALENDAROBJECTLOAD_OK;
185 return StringProcResult;
189 CalendarObjectValidResult CalendarObject::ValidBaseObject(){
191 bool ValidBegin = false;
192 bool ValidVersion = false;
193 bool ValidEnd = false;
196 // Check that the first line contains BEGIN:VCALENDAR
197 // and it only appears once.
199 for (vector<string>::iterator iter = ObjectName.begin();
200 iter != ObjectName.end(); iter++){
202 if (ObjectName[SeekCount] == "BEGIN" &&
203 ObjectData[SeekCount] == "VCALENDAR"){
205 if (ValidBegin == false){
208 return CALENDAROBJECTVALID_INVALIDFORMAT;
213 if (ObjectName[SeekCount] == "END" &&
214 ObjectData[SeekCount] == "VCALENDAR" &&
215 ValidBegin == false){
217 return CALENDAROBJECTVALID_INVALIDFORMAT;
227 // Check that the last line contains END:VCALENDAR
228 // and it only appears once.
230 for (vector<string>::iterator iter = ObjectName.begin();
231 iter != ObjectName.end(); iter++){
233 if (ObjectName[SeekCount] == "END" &&
234 ObjectData[SeekCount] == "VCALENDAR"){
236 if (ValidEnd == false){
239 return CALENDAROBJECTVALID_INVALIDFORMAT;
250 // Check that the VERSION value contains 2.0 and that
251 // it only appears once.
253 for (vector<string>::iterator iter = ObjectName.begin();
254 iter != ObjectName.end(); iter++){
256 if (ObjectName[SeekCount] == "VERSION" &&
257 ObjectData[SeekCount] == "2.0"){
259 if (ValidVersion == false){
262 return CALENDAROBJECTVALID_INVALIDFORMAT;
271 if (ValidBegin == true &&
273 ValidVersion == true){
275 return CALENDAROBJECTVALID_OK;
279 return CALENDAROBJECTVALID_INVALIDFORMAT;
285 void CalendarObject::ProcessBaseData(){
287 // Process the base object data.
289 multimap<string,string> DataReceived;
291 // Get the method (METHOD).
293 DataReceived = ProcessTextVectors(&ObjectName, &ObjectData, false, "METHOD");
295 if (DataReceived.begin() != DataReceived.end()){
298 MethodTokens = DataReceived.begin()->first.substr(7);
301 catch(const out_of_range &oor){
302 // Do nothing as there is no data.
305 MethodData = DataReceived.begin()->second;
309 // Get the calendar scale (CALSCALE).
311 DataReceived = ProcessTextVectors(&ObjectName, &ObjectData, false, "CALSCALE");
313 if (DataReceived.begin() != DataReceived.end()){
316 CalendarScaleTokens = DataReceived.begin()->first.substr(9);
319 catch(const out_of_range &oor){
320 // Do nothing as there is no data.
323 CalendarScaleData = DataReceived.begin()->second;