00001 00007 /* 00008 Read in a configuration file of the form: 00009 keyword = information\n 00010 -or- keyword information\n 00011 -or- keyword = {\n line 0\n line 1\n ... line n\n} 00012 and produces a database which can return a linked list of strings (char *) 00013 to all the information fields associated with that keyword. 00014 00015 A "word" is a seqeunce of characters that are not white space (see 00016 isspace(3C) ). The equals sign ('=') is optional (though if there is more 00017 more than one equals sign, then the 2nd is not ignored). The "information" 00018 field may contain more than one word. White space is ignored except that 00019 white space between multiple words in the information field is maintained. 00020 Everything on the line at and beyond a pound sign ('#') is ignored. Hence 00021 a data file can be: 00022 fullname = George Washington # the first president of the U.S. 00023 fullname = Martha Washington # his second wife 00024 Once that is read in, all data associated with "name" can be retreived as 00025 StringList *strList = configFile.find("fullname"); 00026 for (StringList *tmp=strList; tmp!=NULL; tmp = tmp -> next) 00027 std::cout << tmp->data << '\n'; 00028 Note: 00029 The returned StringList * is NOT new'ed. Do NOT free it. 00030 Keywords are case INsensitive 00031 */ 00032 00033 // This header introduces two names to the global name space 00034 // They are: 00035 // StringList -- a linked list of char *. It has an initilizer and 00036 // destructor to handle the char * by alloc'ing new space 00037 // ConfigList -- its constructor opens a file containing lines of the 00038 // format "keyword = data" and creates a listing of all the data 00039 // for each keyword. This data can be retreived with "find(char *)" 00040 00041 #ifndef CONFIGLIST_H 00042 #define CONFIGLIST_H 00043 #include "common.h" 00044 #include <string.h> 00045 00046 class StringList { 00047 public: 00048 char *data; 00049 StringList *next; 00050 StringList(char *newdata) { // take a string, and copy it 00051 data = new char[strlen(newdata)+1]; 00052 if ( data == NULL ) 00053 { 00054 NAMD_die("new failed in struct StringList"); 00055 } 00056 strcpy( data, newdata); 00057 next = NULL; 00058 } 00059 ~StringList( void) { // just clear out my info 00060 delete [] data; 00061 data = NULL; 00062 next = NULL; 00063 } 00064 }; 00065 00066 class ConfigList { 00067 public: 00068 class ConfigListNode 00069 { 00070 private: 00071 public: 00072 char *name; 00073 StringList *data; 00074 ConfigListNode *next; 00075 ConfigListNode( ConfigListNode *newnext, char *newname, 00076 StringList *newdata) { 00077 name = new char[strlen(newname)+1]; // create space for the name 00078 if ( name == NULL ) 00079 { 00080 NAMD_die("new failed in ConfigListNode::ConfigListNode"); 00081 } 00082 strcpy((char *) name, newname); // and copy the new name 00083 data = newdata; 00084 next = newnext; 00085 } 00086 ~ConfigListNode( void) 00087 { 00088 delete [] name; // delete the new'ed name 00089 name = NULL; 00090 next = NULL; 00091 StringList *curr=data, *next_local=NULL; // go through the string list 00092 00093 while ( curr!=NULL ) 00094 { 00095 next_local = curr->next; // and delete each element 00096 delete curr; 00097 curr = next_local; 00098 } 00099 } 00100 }; 00101 private: 00102 ConfigListNode *theList; 00103 // copy the information into a String, as appropriate 00104 // this is really a "push" 00105 ConfigListNode *find_key_word( const char *keyword) const; 00106 Bool isokay; 00107 public: 00108 ConfigList(void); 00109 void add_element( const char *s1, int len1, const char *s2, int len2); 00110 ConfigList( const char *filename); 00111 Bool okay( void) { return isokay; } 00112 ~ConfigList( void); 00113 StringList *find( const char *name) const; //search for values by name 00114 // NOTE: do not change or delete this information. It is not new'ed 00115 // and any changed you make will be permanent. 00116 00117 ConfigListNode *head( void) const { return theList; } // return everything 00118 // NOTE: you _REALLY_ not not want to change the information 00119 // you get from this (unless you really want to) 00120 }; 00121 00122 00123 #endif // CONFIGLIST_H 00124
1.3.9.1