table_configurator.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : table_configurator                                                *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 2001-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include "table_configurator.h"
00016 
00017 #include <basis/astring.h>
00018 #include <basis/functions.h>
00019 #include <structures/string_array.h>
00020 #include <structures/string_table.h>
00021 
00022 using namespace basis;
00023 using namespace structures;
00024 
00025 namespace configuration {
00026 
00027 #undef LOG
00028 #define LOG(to_print) printf("%s::%s: %s\n", static_class_name(), func, astring(to_print).s())
00029 
00030 class table_o_string_tables : public symbol_table<string_table>
00031 {
00032 public:
00033 };
00034 
00036 
00037 table_configurator::table_configurator(treatment_of_defaults behavior)
00038 : configurator(behavior),
00039   _real_table(new table_o_string_tables)
00040 {}
00041 
00042 table_configurator::table_configurator(const table_configurator &to_copy)
00043 : configurator(to_copy.behavior()),
00044   _real_table(new table_o_string_tables)
00045 { *this = to_copy; }
00046 
00047 table_configurator::~table_configurator()
00048 {
00049   WHACK(_real_table);
00050 }
00051 
00052 table_configurator &table_configurator::operator =
00053     (const table_configurator &to_copy)
00054 {
00055   if (this == &to_copy) return *this;
00056   reset();
00057   string_array sects;
00058   const_cast<table_configurator &>(to_copy).sections(sects);
00059   for (int sectindy = 0; sectindy < sects.length(); sectindy++) {
00060     // every entry in the current section gets added to our current config.
00061     astring curr_section = sects[sectindy];
00062     string_table entries;
00063   const_cast<table_configurator &>(to_copy).get_section(curr_section, entries);
00064     put_section(curr_section, entries);
00065   }
00066 
00067   return *this;
00068 }
00069 
00070 void table_configurator::reset() { _real_table->reset(); }
00071 
00072 bool table_configurator::section_exists(const astring &section)
00073 { return !!_real_table->find(section); }
00074 
00075 void table_configurator::sections(string_array &to_fill)
00076 {
00077   to_fill.reset();
00078   for (int i = 0; i < _real_table->symbols(); i++)
00079     to_fill += _real_table->name(i);
00080 }
00081 
00082 bool table_configurator::delete_section(const astring &section)
00083 { return _real_table->whack(section) == common::OKAY; }
00084 
00085 bool table_configurator::delete_entry(const astring &section,
00086     const astring &ent)
00087 {
00088   string_table *sect = _real_table->find(section);
00089   if (!sect) return false;
00090   return sect->whack(ent) == common::OKAY;
00091 }
00092 
00093 bool table_configurator::put(const astring &section,
00094     const astring &entry, const astring &to_store)
00095 {
00096   if (!to_store.length()) return delete_entry(section, entry);
00097   else if (!entry.length()) return delete_section(section);
00098   string_table *sect = _real_table->find(section);
00099   if (!sect) {
00100     // none exists yet, so add one.
00101     _real_table->add(section, string_table());
00102     sect = _real_table->find(section);
00103   }
00104   sect->add(entry, to_store);
00105   return true;
00106 }
00107 
00108 bool table_configurator::get(const astring &section,
00109     const astring &entry, astring &found)
00110 {
00111   found = "";
00112   string_table *sect = _real_table->find(section);
00113   if (!sect) return false;
00114   astring *looked = sect->find(entry);
00115   if (!looked) return false;
00116   found = *looked;
00117   return true;
00118 }
00119 
00120 /*
00121 scavenge?
00122 bool is_comment(char to_check, const char *comment_list)
00123 {
00124   int len = int(strlen(comment_list));
00125   for (int i = 0; i < len; i++) {
00126     if (to_check == comment_list[i])
00127       return true;
00128   }
00129   return false;
00130 }
00131 */
00132 
00133 /* scavenge?
00134 //hmmm: could we move the commented and clean_comments methods into
00135 //      parser bits?
00136 //      yes!  we should move those; they are no longer used here!
00137 
00138 bool table_configurator::commented(const astring &to_check,
00139     const char *comment_list)
00140 {
00141   for (int i = 0; i < to_check.length(); i++) {
00142     if (white_space(to_check[i]))
00143       continue;  // skip spaces.
00144     if (is_comment(to_check[i], comment_list))
00145       return true;  // started with a comment.
00146     return false;  // we had our chance for a comment, but that wasn't it.
00147   }
00148   return false;
00149 }
00150 
00151 astring table_configurator::clean_comments(const astring &to_clean,
00152     const char *comment_list)
00153 {
00154   FUNCDEF("clean_comments");
00155 //LOG(astring("clean in with: ") + to_clean);
00156   astring to_return(' ', to_clean.length());  // make a long enough string.
00157   to_return.reset();  // keep allocated buffer size, but throw out contents.
00158   for (int i = 0; i < to_clean.length(); i++) {
00159     if (is_comment(to_clean[i], comment_list)) {
00160       // here we go; the rest is commented out.
00161       break;
00162     }
00163     to_return += to_clean[i];
00164   }
00165 //LOG(astring("clean out with: ") + to_return);
00166   return to_return;
00167 }
00168 */
00169 
00170 bool table_configurator::get_section(const astring &section,
00171     string_table &info)
00172 {
00174   info.reset();
00175   string_table *sect = _real_table->find(section);
00176   if (!sect) return false;
00177   for (int i = 0; i < sect->symbols(); i++)
00178     info.add(sect->name(i), (*sect)[i]);
00179   return true;
00180 }
00181 
00182 bool table_configurator::put_section(const astring &section,
00183     const string_table &info)
00184 {
00186   string_table *sect = _real_table->find(section);
00187   if (!sect) {
00188     // none exists yet, so add one.
00189     _real_table->add(section, string_table());
00190     sect = _real_table->find(section);
00191   }
00192   *sect = info;
00193   return true;
00194 }
00195 
00196 } //namespace.
00197 
Generated on Sat Jan 28 04:22:18 2012 for hoople2 project by  doxygen 1.6.3