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;
56 int StringDataSize = LoadStringData->size();
61 while (SeekCount < StringDataSize){
63 // Check if character is blank or a tab and is the first character
66 if (((*LoadStringData)[SeekCount] == ' ' ||
67 (*LoadStringData)[SeekCount] == '\t')
70 // Character is on a new line and it is a space or
71 // tab. Ignore this character as it is not part
76 } else if (NewLine == true){
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
83 if (ColonFound == true){
84 ObjectName.insert(ObjectName.end(), PropertyName);
85 ObjectData.insert(ObjectData.end(), PropertyValue);
91 PropertyValue.clear();
93 BufferChar = (*LoadStringData)[SeekCount];
94 PropertyName += BufferChar;
96 } else if ((*LoadStringData)[SeekCount] == '\n'){
98 // Character is the new line character so mark
99 // the NewLine boolean as true.
103 } else if ((*LoadStringData)[SeekCount] == ':'){
105 // Character is the colon. Set the colon
106 // found boolen to true.
112 // Character is not part of a new line and is not
113 // the new line character itself.
115 BufferChar = (*LoadStringData)[SeekCount];
117 if (ColonFound == false){
118 PropertyName += BufferChar;
120 PropertyValue += BufferChar;
129 // Finish off processing any data that wasn't processed
130 // when the end of the string was reached.
132 if (ColonFound == true &&
133 PropertyName.size() > 0 &&
134 PropertyValue.size() > 0){
136 ObjectName.insert(ObjectName.end(), PropertyName);
137 ObjectData.insert(ObjectData.end(), PropertyValue);
141 // Check that the object contains valid data.
143 CalendarObjectLoadResult StringProcResult = CALENDAROBJECTLOAD_UNITTESTFAIL;
144 CalendarObjectValidResult BaseDataResult = ValidBaseObject();
145 CalendarObjectValidResult EventDataResult = ValidObject();
147 return StringProcResult;
151 CalendarObjectValidResult CalendarObject::ValidBaseObject(){
153 bool ValidBegin = false;
154 bool ValidVersion = false;
155 bool ValidEnd = false;
158 // Check that the first line contains BEGIN:VCALENDAR
159 // and it only appears once.
161 for (vector<string>::iterator iter = ObjectName.begin();
162 iter != ObjectName.end(); iter++){
164 if (ObjectName[SeekCount] == "BEGIN" &&
165 ObjectData[SeekCount] == "VCALENDAR"){
167 if (ValidBegin == false){
170 return CALENDAROBJECTVALID_INVALIDFORMAT;
181 // Check that the last line contains END:VCALENDAR
182 // and it only appears once.
184 for (vector<string>::iterator iter = ObjectName.begin();
185 iter != ObjectName.end(); iter++){
187 if (ObjectName[SeekCount] == "END" &&
188 ObjectData[SeekCount] == "VCALENDAR"){
190 if (ValidEnd == false){
193 return CALENDAROBJECTVALID_INVALIDFORMAT;
204 // Check that the VERSION value contains 2.0 and that
205 // it only appears once.
207 for (vector<string>::iterator iter = ObjectName.begin();
208 iter != ObjectName.end(); iter++){
210 if (ObjectName[SeekCount] == "VERSION" &&
211 ObjectData[SeekCount] == "2.0"){
213 if (ValidVersion == false){
216 return CALENDAROBJECTVALID_INVALIDFORMAT;
225 if (ValidBegin == true &&
227 ValidVersion == true){
229 return CALENDAROBJECTVALID_OK;
233 return CALENDAROBJECTVALID_INVALIDFORMAT;