section_manager.cpp

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