4 /*****************************************************************************\
6 * Name : section_manager *
7 * Author : Chris Koeritz *
9 *******************************************************************************
10 * Copyright (c) 2000-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
18 #include "section_manager.h"
20 #include <basis/functions.h>
21 #include <basis/astring.h>
22 #include <structures/string_array.h>
23 #include <structures/string_table.h>
25 using namespace basis;
26 using namespace configuration;
27 using namespace structures;
29 namespace configuration {
31 section_manager::section_manager(configurator &config,
32 const astring &toc_title, const astring &header_prefix)
34 _toc_title(new astring(toc_title)),
35 _section_prefix(new astring(header_prefix))
39 section_manager::~section_manager()
42 WHACK(_section_prefix);
45 bool section_manager::get_toc(string_table &toc)
46 { return _config.get_section(*_toc_title, toc); }
48 bool section_manager::get_section_names(string_array §ions)
50 sections.reset(); // clean up the array they gave us.
52 if (!get_toc(toc)) return false;
53 for (int i = 0; i < toc.symbols(); i++) sections += toc.name(i);
57 bool section_manager::section_exists(const astring §ion_name)
59 if (!section_name) return false; // empty names are invalid.
60 return _config.load(*_toc_title, section_name, "").t();
63 astring section_manager::make_section_heading(const astring §ion)
64 { return *_section_prefix + section; }
66 bool section_manager::add_section(const astring §ion_name,
67 const string_table &to_add)
69 if (!section_name) return false; // empty names are invalid.
70 if (section_exists(section_name)) return false;
71 // write the name into the table of contents.
72 astring section_value = "t"; // place-holder for section entries.
73 if (!to_add.symbols())
74 section_value = ""; // nothing to write; delete the section entry.
75 if (!_config.put(*_toc_title, section_name, section_value))
77 // create the appropriate header for the new section.
78 astring header = make_section_heading(section_name);
79 // if there aren't any elements, the put_section should just wipe out
80 // the entire section.
81 return _config.put_section(header, to_add);
84 bool section_manager::replace_section(const astring §ion_name,
85 const string_table &replacement)
87 if (!section_name) return false; // empty names are invalid.
88 if (!section_exists(section_name)) return false;
89 if (!zap_section(section_name)) return false;
90 if (!replacement.symbols()) return true; // nothing to write.
91 return add_section(section_name, replacement);
94 bool section_manager::zap_section(const astring §ion_name)
96 if (!section_name) return false; // empty names are invalid.
97 astring header = make_section_heading(section_name);
98 if (!_config.delete_section(header)) return false;
99 return _config.delete_entry(*_toc_title, section_name);
102 bool section_manager::find_section(const astring §ion_name,
105 if (!section_name) return false; // empty names are invalid.
106 if (!section_exists(section_name)) return false;
107 astring header = make_section_heading(section_name);
108 return _config.get_section(header, found);