section_manager.cpp

Go to the documentation of this file.
00001 #ifndef SECTION_MANAGER_IMPLEMENTATION_FILE
00002 #define SECTION_MANAGER_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : section_manager                                                   *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 2000-$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 "configurator.h"
00019 #include "section_manager.h"
00020 #include "string_table.h"
00021 
00022 #include <basis/function.h>
00023 #include <basis/istring.h>
00024 #include <basis/string_array.h>
00025 
00026 section_manager::section_manager(configurator &config,
00027     const istring &toc_title, const istring &header_prefix)
00028 : _config(config),
00029   _toc_title(new istring(toc_title)),
00030   _section_prefix(new istring(header_prefix))
00031 {
00032 }
00033 
00034 section_manager::~section_manager()
00035 {
00036   WHACK(_toc_title);
00037   WHACK(_section_prefix);
00038 }
00039 
00040 bool section_manager::get_toc(string_table &toc)
00041 { return _config.get_section(*_toc_title, toc); }
00042 
00043 bool section_manager::get_section_names(string_array &sections)
00044 {
00045   sections.reset();  // clean up the array they gave us.
00046   string_table toc;
00047   if (!get_toc(toc)) return false;
00048   for (int i = 0; i < toc.symbols(); i++) sections += toc.name(i);
00049   return true;
00050 }
00051 
00052 bool section_manager::section_exists(const istring &section_name)
00053 {
00054   if (!section_name) return false;  // empty names are invalid.
00055   return _config.load(*_toc_title, section_name, "").t();
00056 }
00057 
00058 istring section_manager::make_section_heading(const istring &section)
00059 { return *_section_prefix + section; }
00060 
00061 bool section_manager::add_section(const istring &section_name,
00062     const string_table &to_add)
00063 {
00064   if (!section_name) return false;  // empty names are invalid.
00065   if (section_exists(section_name)) return false;
00066   // write the name into the table of contents.
00067   istring section_value = "t";  // place-holder for section entries.
00068   if (!to_add.symbols())
00069     section_value = "";  // nothing to write; delete the section entry.
00070   if (!_config.put(*_toc_title, section_name, section_value))
00071     return false;
00072   // create the appropriate header for the new section.
00073   istring header = make_section_heading(section_name);
00074   // if there aren't any elements, the put_section should just wipe out
00075   // the entire section.
00076   return _config.put_section(header, to_add);
00077 }
00078 
00079 bool section_manager::replace_section(const istring &section_name,
00080     const string_table &replacement)
00081 {
00082   if (!section_name) return false;  // empty names are invalid.
00083   if (!section_exists(section_name)) return false;
00084   if (!zap_section(section_name)) return false;
00085   if (!replacement.symbols()) return true;  // nothing to write.
00086   return add_section(section_name, replacement);
00087 }
00088 
00089 bool section_manager::zap_section(const istring &section_name)
00090 {
00091   if (!section_name) return false;  // empty names are invalid.
00092   istring header = make_section_heading(section_name);
00093   if (!_config.delete_section(header)) return false;
00094   return _config.delete_entry(*_toc_title, section_name);
00095 }
00096 
00097 bool section_manager::find_section(const istring &section_name,
00098     string_table &found)
00099 {
00100   if (!section_name) return false;  // empty names are invalid.
00101   if (!section_exists(section_name)) return false;
00102   istring header = make_section_heading(section_name);
00103   return _config.get_section(header, found);
00104 }
00105 
00106 
00107 #endif //SECTION_MANAGER_IMPLEMENTATION_FILE
00108 

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