table_configurator.cpp

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

Generated on Fri Nov 21 04:29:42 2008 for HOOPLE Libraries by  doxygen 1.5.1