ConfigList.h

Go to the documentation of this file.
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   void set(const char *newdata) {  // take a string, and copy it
00060     delete [] data;
00061     data = new char[strlen(newdata)+1];
00062     if ( data == NULL )
00063     {
00064       NAMD_die("new failed in struct StringList");
00065     }
00066     strcpy( data, newdata);
00067   }
00068   ~StringList( void) {  // just clear out my info
00069     delete [] data;
00070     data = NULL;
00071     next = NULL;
00072   }
00073 };
00074 
00075 class ConfigList {
00076   public:
00077     class ConfigListNode 
00078     {
00079     private:
00080     public:
00081       char *name;
00082       StringList *data;
00083       ConfigListNode *next;
00084       ConfigListNode( ConfigListNode *newnext, char *newname, 
00085                                           StringList *newdata) {
00086         name = new char[strlen(newname)+1];  // create space for the name
00087         if ( name == NULL )
00088         {
00089           NAMD_die("new failed in ConfigListNode::ConfigListNode");
00090         }
00091         strcpy((char *) name, newname);      // and copy the new name
00092         data = newdata;
00093         next = newnext;
00094       }
00095       ~ConfigListNode( void) 
00096       {
00097         delete [] name;                  // delete the new'ed name
00098         name = NULL;
00099         next = NULL;
00100         StringList *curr=data, *next_local=NULL;  // go through the string list
00101 
00102         while ( curr!=NULL ) 
00103         {
00104           next_local = curr->next;           // and delete each element
00105           delete curr;
00106           curr = next_local;
00107         }
00108       }
00109     };
00110  private:
00111     ConfigListNode *theList;
00112        // copy the information into a String, as appropriate
00113        // this is really a "push"
00114     ConfigListNode *find_key_word( const char *keyword) const;
00115     Bool isokay;
00116   public:
00117     ConfigList(void);
00118     void add_element( const char *s1, int len1, const char *s2, int len2);
00119     ConfigList( const char *filename);
00120     Bool okay( void) { return isokay; }
00121     ~ConfigList( void);
00122     StringList *find( const char *name) const;   //search for values by name
00123        // NOTE: do not change or delete this information.  It is not new'ed
00124        //   and any changed you make will be permanent.
00125        
00126     ConfigListNode *head( void) const { return theList;  } // return everything
00127        // NOTE:  you _REALLY_ not not want to change the information
00128        //   you get from this (unless you really want to)
00129 };
00130 
00131 
00132 #endif // CONFIGLIST_H
00133 

Generated on Tue Sep 19 01:17:12 2017 for NAMD by  doxygen 1.4.7