first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / configuration / section_manager.cpp
1
2
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : section_manager                                                   *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
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 \*****************************************************************************/
17
18 #include "section_manager.h"
19
20 #include <basis/functions.h>
21 #include <basis/astring.h>
22 #include <structures/string_array.h>
23 #include <structures/string_table.h>
24
25 using namespace basis;
26 using namespace configuration;
27 using namespace structures;
28
29 namespace configuration {
30
31 section_manager::section_manager(configurator &config,
32     const astring &toc_title, const astring &header_prefix)
33 : _config(config),
34   _toc_title(new astring(toc_title)),
35   _section_prefix(new astring(header_prefix))
36 {
37 }
38
39 section_manager::~section_manager()
40 {
41   WHACK(_toc_title);
42   WHACK(_section_prefix);
43 }
44
45 bool section_manager::get_toc(string_table &toc)
46 { return _config.get_section(*_toc_title, toc); }
47
48 bool section_manager::get_section_names(string_array &sections)
49 {
50   sections.reset();  // clean up the array they gave us.
51   string_table toc;
52   if (!get_toc(toc)) return false;
53   for (int i = 0; i < toc.symbols(); i++) sections += toc.name(i);
54   return true;
55 }
56
57 bool section_manager::section_exists(const astring &section_name)
58 {
59   if (!section_name) return false;  // empty names are invalid.
60   return _config.load(*_toc_title, section_name, "").t();
61 }
62
63 astring section_manager::make_section_heading(const astring &section)
64 { return *_section_prefix + section; }
65
66 bool section_manager::add_section(const astring &section_name,
67     const string_table &to_add)
68 {
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))
76     return false;
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);
82 }
83
84 bool section_manager::replace_section(const astring &section_name,
85     const string_table &replacement)
86 {
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);
92 }
93
94 bool section_manager::zap_section(const astring &section_name)
95 {
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);
100 }
101
102 bool section_manager::find_section(const astring &section_name,
103     string_table &found)
104 {
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);
109 }
110
111 } //namespace.
112
113