NAMD
ConfigList.h
Go to the documentation of this file.
1 
7 /*
8  Read in a configuration file of the form:
9  keyword = information\n
10  -or- keyword information\n
11  -or- keyword = {\n line 0\n line 1\n ... line n\n}
12  and produces a database which can return a linked list of strings (char *)
13  to all the information fields associated with that keyword.
14 
15  A "word" is a seqeunce of characters that are not white space (see
16  isspace(3C) ). The equals sign ('=') is optional (though if there is more
17  more than one equals sign, then the 2nd is not ignored). The "information"
18  field may contain more than one word. White space is ignored except that
19  white space between multiple words in the information field is maintained.
20  Everything on the line at and beyond a pound sign ('#') is ignored. Hence
21  a data file can be:
22  fullname = George Washington # the first president of the U.S.
23  fullname = Martha Washington # his second wife
24  Once that is read in, all data associated with "name" can be retreived as
25  StringList *strList = configFile.find("fullname");
26  for (StringList *tmp=strList; tmp!=NULL; tmp = tmp -> next)
27  std::cout << tmp->data << '\n';
28  Note:
29  The returned StringList * is NOT new'ed. Do NOT free it.
30  Keywords are case INsensitive
31 */
32 
33 // This header introduces two names to the global name space
34 // They are:
35 // StringList -- a linked list of char *. It has an initilizer and
36 // destructor to handle the char * by alloc'ing new space
37 // ConfigList -- its constructor opens a file containing lines of the
38 // format "keyword = data" and creates a listing of all the data
39 // for each keyword. This data can be retreived with "find(char *)"
40 
41 #ifndef CONFIGLIST_H
42 #define CONFIGLIST_H
43 #include "common.h"
44 #include <string.h>
45 
46 class StringList {
47  public:
48  char *data;
50  StringList(char *newdata) { // take a string, and copy it
51  data = new char[strlen(newdata)+1];
52  if ( data == NULL )
53  {
54  NAMD_die("new failed in struct StringList");
55  }
56  strcpy( data, newdata);
57  next = NULL;
58  }
59  void set(const char *newdata) { // take a string, and copy it
60  delete [] data;
61  data = new char[strlen(newdata)+1];
62  if ( data == NULL )
63  {
64  NAMD_die("new failed in struct StringList");
65  }
66  strcpy( data, newdata);
67  }
68  ~StringList( void) { // just clear out my info
69  delete [] data;
70  data = NULL;
71  next = NULL;
72  }
73 };
74 
75 class ConfigList {
76  public:
78  {
79  private:
80  public:
81  char *name;
84  ConfigListNode( ConfigListNode *newnext, char *newname,
85  StringList *newdata) {
86  name = new char[strlen(newname)+1]; // create space for the name
87  if ( name == NULL )
88  {
89  NAMD_die("new failed in ConfigListNode::ConfigListNode");
90  }
91  strcpy((char *) name, newname); // and copy the new name
92  data = newdata;
93  next = newnext;
94  }
96  {
97  delete [] name; // delete the new'ed name
98  name = NULL;
99  next = NULL;
100  StringList *curr=data, *next_local=NULL; // go through the string list
101 
102  while ( curr!=NULL )
103  {
104  next_local = curr->next; // and delete each element
105  delete curr;
106  curr = next_local;
107  }
108  }
109  };
110  private:
111  ConfigListNode *theList;
112  // copy the information into a String, as appropriate
113  // this is really a "push"
114  ConfigListNode *find_key_word( const char *keyword) const;
115  Bool isokay;
116  public:
117  ConfigList(void);
118  void add_element( const char *s1, int len1, const char *s2, int len2);
119  ConfigList( const char *filename);
120  Bool okay( void) { return isokay; }
121  ~ConfigList( void);
122  StringList *find( const char *name) const; //search for values by name
123  // NOTE: do not change or delete this information. It is not new'ed
124  // and any changed you make will be permanent.
125 
126  ConfigListNode *head( void) const { return theList; } // return everything
127  // NOTE: you _REALLY_ not not want to change the information
128  // you get from this (unless you really want to)
129 };
130 
131 
132 #endif // CONFIGLIST_H
133 
void set(const char *newdata)
Definition: ConfigList.h:59
void add_element(const char *s1, int len1, const char *s2, int len2)
Definition: ConfigList.C:58
~StringList(void)
Definition: ConfigList.h:68
~ConfigList(void)
Definition: ConfigList.C:322
ConfigListNode * head(void) const
Definition: ConfigList.h:126
int Bool
Definition: common.h:131
StringList(char *newdata)
Definition: ConfigList.h:50
void NAMD_die(const char *err_msg)
Definition: common.C:83
StringList * next
Definition: ConfigList.h:49
char * data
Definition: ConfigList.h:48
StringList * find(const char *name) const
Definition: ConfigList.C:341
ConfigList(void)
Definition: ConfigList.C:116
ConfigListNode * next
Definition: ConfigList.h:83
Bool okay(void)
Definition: ConfigList.h:120
ConfigListNode(ConfigListNode *newnext, char *newname, StringList *newdata)
Definition: ConfigList.h:84