feisty meow concerns codebase  2.140
table_configurator.cpp
Go to the documentation of this file.
1 /*****************************************************************************\
2 * *
3 * Name : table_configurator *
4 * Author : Chris Koeritz *
5 * *
6 *******************************************************************************
7 * Copyright (c) 2001-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
14 
15 #include "table_configurator.h"
16 
17 #include <basis/astring.h>
18 #include <basis/functions.h>
21 
22 using namespace basis;
23 using namespace structures;
24 
25 namespace configuration {
26 
27 #undef LOG
28 #define LOG(to_print) printf("%s::%s: %s\n", static_class_name(), func, astring(to_print).s())
29 
30 class table_o_string_tables : public symbol_table<string_table>
31 {
32 public:
33 };
34 
36 
37 table_configurator::table_configurator(treatment_of_defaults behavior)
38 : configurator(behavior),
39  _real_table(new table_o_string_tables)
40 {}
41 
43 : configurator(to_copy.behavior()),
44  _real_table(new table_o_string_tables)
45 { *this = to_copy; }
46 
48 {
49  WHACK(_real_table);
50 }
51 
52 table_configurator &table_configurator::operator =
53  (const table_configurator &to_copy)
54 {
55  if (this == &to_copy) return *this;
56  reset();
57  string_array sects;
58  const_cast<table_configurator &>(to_copy).sections(sects);
59  for (int sectindy = 0; sectindy < sects.length(); sectindy++) {
60  // every entry in the current section gets added to our current config.
61  astring curr_section = sects[sectindy];
62  string_table entries;
63  const_cast<table_configurator &>(to_copy).get_section(curr_section, entries);
64  put_section(curr_section, entries);
65  }
66 
67  return *this;
68 }
69 
70 void table_configurator::reset() { _real_table->reset(); }
71 
73 { return !!_real_table->find(section); }
74 
76 {
77  to_fill.reset();
78  for (int i = 0; i < _real_table->symbols(); i++)
79  to_fill += _real_table->name(i);
80 }
81 
83 { return _real_table->whack(section) == common::OKAY; }
84 
86  const astring &ent)
87 {
88  string_table *sect = _real_table->find(section);
89  if (!sect) return false;
90  return sect->whack(ent) == common::OKAY;
91 }
92 
93 bool table_configurator::put(const astring &section,
94  const astring &entry, const astring &to_store)
95 {
96  if (!to_store.length()) return delete_entry(section, entry);
97  else if (!entry.length()) return delete_section(section);
98  string_table *sect = _real_table->find(section);
99  if (!sect) {
100  // none exists yet, so add one.
101  _real_table->add(section, string_table());
102  sect = _real_table->find(section);
103  }
104  sect->add(entry, to_store);
105  return true;
106 }
107 
108 bool table_configurator::get(const astring &section,
109  const astring &entry, astring &found)
110 {
111  found = "";
112  string_table *sect = _real_table->find(section);
113  if (!sect) return false;
114  astring *looked = sect->find(entry);
115  if (!looked) return false;
116  found = *looked;
117  return true;
118 }
119 
120 /*
121 scavenge?
122 bool is_comment(char to_check, const char *comment_list)
123 {
124  int len = int(strlen(comment_list));
125  for (int i = 0; i < len; i++) {
126  if (to_check == comment_list[i])
127  return true;
128  }
129  return false;
130 }
131 */
132 
133 /* scavenge?
134 //hmmm: could we move the commented and clean_comments methods into
135 // parser bits?
136 // yes! we should move those; they are no longer used here!
137 
138 bool table_configurator::commented(const astring &to_check,
139  const char *comment_list)
140 {
141  for (int i = 0; i < to_check.length(); i++) {
142  if (white_space(to_check[i]))
143  continue; // skip spaces.
144  if (is_comment(to_check[i], comment_list))
145  return true; // started with a comment.
146  return false; // we had our chance for a comment, but that wasn't it.
147  }
148  return false;
149 }
150 
151 astring table_configurator::clean_comments(const astring &to_clean,
152  const char *comment_list)
153 {
154  FUNCDEF("clean_comments");
155 //LOG(astring("clean in with: ") + to_clean);
156  astring to_return(' ', to_clean.length()); // make a long enough string.
157  to_return.reset(); // keep allocated buffer size, but throw out contents.
158  for (int i = 0; i < to_clean.length(); i++) {
159  if (is_comment(to_clean[i], comment_list)) {
160  // here we go; the rest is commented out.
161  break;
162  }
163  to_return += to_clean[i];
164  }
165 //LOG(astring("clean out with: ") + to_return);
166  return to_return;
167 }
168 */
169 
171  string_table &info)
172 {
173  FUNCDEF("get_section");
174  info.reset();
175  string_table *sect = _real_table->find(section);
176  if (!sect) return false;
177  for (int i = 0; i < sect->symbols(); i++)
178  info.add(sect->name(i), (*sect)[i]);
179  return true;
180 }
181 
183  const string_table &info)
184 {
185  FUNCDEF("put_section");
186  string_table *sect = _real_table->find(section);
187  if (!sect) {
188  // none exists yet, so add one.
189  _real_table->add(section, string_table());
190  sect = _real_table->find(section);
191  }
192  *sect = info;
193  return true;
194 }
195 
196 } //namespace.
197 
void reset(int number=0, const contents *initial_contents=NULL_POINTER)
Resizes this array and sets the contents from an array of contents.
Definition: array.h:349
int length() const
Returns the current reported length of the allocated C array.
Definition: array.h:115
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
int length() const
Returns the current length of the string.
Definition: astring.cpp:132
Provides a base class for configuration repositories.
Definition: configurator.h:34
Supports the configurator interface using a collection of string tables.
virtual bool delete_entry(const basis::astring &section, const basis::astring &entry)
removes the entry specified by the "section" and "entry" name.
virtual bool put(const basis::astring &section, const basis::astring &entry, const basis::astring &to_store)
implements the configurator storage function.
table_configurator(treatment_of_defaults behavior=AUTO_STORE)
Constructor just needs to know what to do for missing items.
virtual void sections(structures::string_array &list)
retrieves the section names into "list".
virtual bool delete_section(const basis::astring &section)
removes the entire "section" specified.
virtual bool get(const basis::astring &section, const basis::astring &entry, basis::astring &found)
implements the configurator retrieval function.
virtual bool section_exists(const basis::astring &section)
true if the "section" is presently in the table config.
virtual bool put_section(const basis::astring &section, const structures::string_table &info)
writes a table called "info" into the "section" held here.
virtual bool get_section(const basis::astring &section, structures::string_table &info)
reads the entire table held under "section" into a table called "info".
An array of strings with some additional helpful methods.
Definition: string_array.h:32
Provides a symbol_table that holds strings as the content.
Definition: string_table.h:32
Maintains a list of names, where each name has a type and some contents.
Definition: symbol_table.h:36
const basis::astring & name(int index) const
returns the name held at the "index".
Definition: symbol_table.h:272
contents * find(const basis::astring &name) const
returns the contents held for "name" or NULL_POINTER if it wasn't found.
Definition: symbol_table.h:313
basis::outcome add(const basis::astring &name, const contents &storage)
Enters a symbol name into the table along with some contents.
Definition: symbol_table.h:383
basis::outcome whack(const basis::astring &name)
removes a symbol from the table.
Definition: symbol_table.h:410
int symbols() const
returns the number of symbols listed in the table.
Definition: symbol_table.h:241
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition: enhance_cpp.h:57
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
Definition: functions.h:121
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55