|
Fast Research Interface Library
Manual and Documentation
|
00001 // ---------------------- Doxygen info ---------------------- 00050 // ---------------------------------------------------------- 00051 // For a convenient reading of this file's source code, 00052 // please use a tab width of four characters. 00053 // ---------------------------------------------------------- 00054 00055 00056 #include <InitializationFileEntry.h> 00057 #include <string.h> 00058 #include <stdio.h> 00059 00060 00061 #define LINELEN (NAMELEN + VALUELEN) 00062 00063 00064 // **************************************************************** 00065 // Constructor 00066 // 00067 InitializationFileEntry::InitializationFileEntry(const char *FileName) 00068 { 00069 this->Next = NULL; 00070 this->Prev = NULL; 00071 this->CurrentEntry = NULL; 00072 00073 memset(this->Name, 0x0, NAMELEN); 00074 memset(this->Value, 0x0, VALUELEN); 00075 memset(this->EntryName, 0x0, NAMELEN); 00076 memset(this->SectionName, 0x0, NAMELEN); 00077 memset(this->CurrentSectionName, 0x0, NAMELEN); 00078 00079 // Now read the file if desired 00080 if (FileName != NULL) 00081 { 00082 this->ReadFile(FileName); 00083 } 00084 } 00085 00086 00087 // **************************************************************** 00088 // Destructor 00089 // 00090 InitializationFileEntry::~InitializationFileEntry(void) 00091 { 00092 // This destructor deletes the complete list regardless of the member it was called for. 00093 // For the currently unordered list, it is not necessary to delete predecessors 00094 // since they won't exist, but if the implementation supports an ordered list 00095 // it might be useful to have this destructor. 00096 InitializationFileEntry *current = this; 00097 00098 // delete the successor(s) 00099 if (current->Next != NULL) 00100 { 00101 current->Next->Prev = NULL; 00102 delete (current->Next); 00103 current->Next = NULL; 00104 } 00105 // delete the predecessors 00106 if (current->Prev != NULL) 00107 { 00108 delete (current->Prev); 00109 current->Prev = NULL; 00110 } 00111 } 00112 00113 00114 // **************************************************************** 00115 // Add 00116 // 00117 bool InitializationFileEntry::Add(const char *Line) 00118 { 00119 bool Result = false; 00120 00121 char *Start = NULL 00122 , *End = NULL; 00123 00124 InitializationFileEntry *NewEntry = NULL 00125 , *EntryPtr = NULL; 00126 00127 if (((Start = strchr ((char*)Line, '[')) != NULL) && ((End = strchr ((char*)Line, ']')) != NULL)) 00128 { 00129 // (new) section name found 00130 strcpy (this->CurrentSectionName, Start + 1); 00131 this->CurrentSectionName[End - Start - 1] = '\0'; 00132 Result = true; 00133 } 00134 else 00135 { if ((Start = strchr ((char*)Line, '=')) != NULL) 00136 { 00137 // entry = value found 00138 if ((this->EntryName[0] == '\0') && (this->SectionName[0] == '\0') && (this->Value[0] == '\0') && (this->CurrentSectionName[0] == '\0')) 00139 { 00140 // use current (empty) entry 00141 NewEntry = this; 00142 } 00143 else 00144 { 00145 // reserve space for a new entry 00146 NewEntry = new (InitializationFileEntry); 00147 } 00148 if (NewEntry != NULL) 00149 { 00150 // free space is available 00151 strcpy (NewEntry->Value, Start+1); 00152 // cut line at '=' 00153 *Start = '\0'; 00154 // copy the entry name 00155 strcpy (NewEntry->EntryName, Line); 00156 // restore line contents 00157 *Start = '='; 00158 // set the section name 00159 strcpy (NewEntry->SectionName, this->CurrentSectionName); 00160 Result = true; 00161 } 00162 } 00163 else 00164 { 00165 // entry line found 00166 if ((this->EntryName[0] == '\0') && (this->SectionName[0] == '\0') && (this->Value[0] == '\0') && (this->CurrentSectionName[0] == '\0')) 00167 { 00168 // use current (empty) entry 00169 NewEntry = this; 00170 } 00171 else 00172 { 00173 // reserve space for a new entry 00174 NewEntry = new (InitializationFileEntry); 00175 } 00176 if (NewEntry != NULL) 00177 { 00178 // free space is available 00179 // copy the entry and the section name 00180 strcpy (NewEntry->SectionName, this->CurrentSectionName); 00181 strcpy (NewEntry->EntryName, Line); 00182 Result = true; 00183 } 00184 } 00185 } 00186 00187 // if line contained a new entry it will be appended to the list 00188 if ((NewEntry != NULL) && (NewEntry != this)) 00189 { 00190 // Start with this 00191 EntryPtr = this; 00192 // and move the pointer to the end of the list 00193 while (EntryPtr->Next != NULL) 00194 { 00195 EntryPtr = EntryPtr->Next; 00196 } 00197 // append the new entry 00198 NewEntry->Prev = EntryPtr; 00199 EntryPtr->Next = NewEntry; 00200 } 00201 00202 return(Result); 00203 } 00204 00205 00206 // **************************************************************** 00207 // NextEntry 00208 // 00209 bool InitializationFileEntry::NextEntry(void) 00210 { 00211 bool Result; 00212 00213 if (this->CurrentEntry != NULL) 00214 { 00215 this->CurrentEntry = this->CurrentEntry->Next; 00216 } 00217 else 00218 { 00219 this->CurrentEntry = this; 00220 } 00221 Result = (this->CurrentEntry != NULL); 00222 return(Result); 00223 } 00224 00225 00226 // **************************************************************** 00227 // FindNextsection 00228 // 00229 bool InitializationFileEntry::FindNextSection(void) 00230 { 00231 InitializationFileEntry *EntryPtr = this->CurrentEntry; // Start with the current entry 00232 00233 bool Found = false 00234 , Result = false; 00235 00236 char NameOfSection[NAMELEN]; 00237 00238 memset(NameOfSection, 0x0, NAMELEN); 00239 00240 if (EntryPtr != NULL) 00241 { 00242 // cycle through the list until a new section name is found or the end 00243 // of the list is reached 00244 00245 strcpy(NameOfSection, EntryPtr->SectionName); 00246 00247 while ((EntryPtr != NULL) && !Found) 00248 { 00249 if (strcmp (NameOfSection, EntryPtr->SectionName)) 00250 { 00251 // set Found if the section name does not match 00252 Found = true; 00253 } 00254 else 00255 { 00256 // else check the next entry 00257 EntryPtr = EntryPtr->Next; 00258 } 00259 } 00260 if (Found) 00261 { 00262 this->CurrentEntry = EntryPtr; 00263 Result = true; 00264 } 00265 else 00266 // if nothing could be found set the current pointer to the 00267 // first entry -> the next call will again start cycling through 00268 // the list 00269 { 00270 this->CurrentEntry = this; 00271 } 00272 } 00273 return(Result); 00274 } 00275 00276 00277 // **************************************************************** 00278 // GetSection 00279 // 00280 char *InitializationFileEntry::GetSection(void) 00281 { 00282 if (this->CurrentEntry == NULL) 00283 { 00284 this->CurrentEntry = this; 00285 } 00286 return(this->CurrentEntry->SectionName); 00287 } 00288 00289 00290 // **************************************************************** 00291 // GetAll 00292 // 00293 char *InitializationFileEntry::GetAll(void) 00294 { 00295 if (this->CurrentEntry == NULL) 00296 { 00297 this->CurrentEntry = this; 00298 } 00299 return(this->CurrentEntry->EntryName); 00300 } 00301 00302 00303 // **************************************************************** 00304 // GetName 00305 // 00306 char *InitializationFileEntry::GetName(void) 00307 { 00308 int Index = 0; 00309 00310 if (this->CurrentEntry == NULL) 00311 { 00312 this->CurrentEntry = this; 00313 } 00314 00315 // copy the name to the result array 00316 while ( (this->CurrentEntry->EntryName[Index] != ' ' ) 00317 && (this->CurrentEntry->EntryName[Index] != 9 ) 00318 && (this->CurrentEntry->EntryName[Index] != 0 )) 00319 { 00320 this->Name[Index] = this->CurrentEntry->EntryName[Index]; 00321 Index++; 00322 } 00323 this->Name[Index] = 0; 00324 00325 return(Name); 00326 } 00327 00328 00329 // **************************************************************** 00330 // GetValue 00331 // 00332 char *InitializationFileEntry::GetValue(void) 00333 { 00334 if (this->CurrentEntry == NULL) 00335 { 00336 this->CurrentEntry = this; 00337 } 00338 return(this->CurrentEntry->Value); 00339 } 00340 00341 00342 // **************************************************************** 00343 // FindEntry 00344 // 00345 bool InitializationFileEntry::FindEntry(const char *Name) 00346 { 00347 InitializationFileEntry *EntryPtr = this; // start with this 00348 00349 bool Found = false 00350 , Result = false; 00351 00352 // Cycle through the list until the name is found or the end of the list is reached 00353 while ((EntryPtr != NULL) && !Found) 00354 { 00355 if (!strcmp (Name, EntryPtr->EntryName)) 00356 { 00357 // set Found if the name matches 00358 Found = true; 00359 } 00360 else 00361 { 00362 // else check the next entry 00363 EntryPtr = EntryPtr->Next; 00364 } 00365 } 00366 00367 if (Found) 00368 // If the name could be found set the current entry to it 00369 // and set the result to true. 00370 { 00371 this->CurrentEntry = EntryPtr; 00372 Result = true; 00373 } 00374 00375 return(Result); 00376 } 00377 00378 00379 // **************************************************************** 00380 // ReadFile 00381 // 00382 void InitializationFileEntry::ReadFile(const char *FileName) 00383 { 00384 bool Comment = false; 00385 00386 char Line[LINELEN]; 00387 00388 int Input = 0 00389 , Counter = 0; 00390 00391 FILE *FileHandler = NULL; 00392 00393 memset(Line, 0x0, LINELEN); 00394 00395 if ((FileHandler = fopen (FileName, "rt")) != NULL) 00396 { 00397 while (!feof (FileHandler)) 00398 { 00399 Counter = 0; // new line 00400 Comment = false; 00401 while (((Input = fgetc (FileHandler)) != '\n') && !feof(FileHandler) && !ferror(FileHandler)) 00402 { 00403 if (Counter == 0 && Input == '#') // Comment lines start with a '#'. 00404 { 00405 Comment = true; 00406 } 00407 if (Input != '\r') 00408 { 00409 Line [Counter++] = (char)Input; 00410 } 00411 } 00412 if (!Comment && Counter > 0) 00413 { 00414 Line[Counter] = 0; 00415 this->Add(Line); 00416 } 00417 } 00418 fclose(FileHandler); 00419 } 00420 else 00421 { 00422 fprintf(stderr, "InitializationFileEntry: ERROR! File \"%s\" cannot be read !\n", FileName); 00423 } 00424 }