feisty meow concerns codebase 2.140
section_manager.cpp
Go to the documentation of this file.
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>
24
25using namespace basis;
26using namespace configuration;
27using namespace structures;
28
29namespace configuration {
30
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
40{
41 WHACK(_toc_title);
42 WHACK(_section_prefix);
43}
44
46{ return _config.get_section(*_toc_title, toc); }
47
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
58{
59 if (!section_name) return false; // empty names are invalid.
60 return _config.load(*_toc_title, section_name, "").t();
61}
62
64{ return *_section_prefix + section; }
65
66bool 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
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
94bool 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
102bool 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
void reset(int number=0, const contents *initial_contents=NULL_POINTER)
Resizes this array and sets the contents from an array of contents.
Definition array.h:349
Provides a dynamically resizable ASCII character string.
Definition astring.h:35
bool t() const
t() is a shortcut for the string being "true", as in non-empty.
Definition astring.h:97
Provides a base class for configuration repositories.
virtual bool put(const basis::astring &section, const basis::astring &entry, const basis::astring &to_store)=0
Places an item into the configuration store.
virtual bool get_section(const basis::astring &formal(section), structures::string_table &formal(found))
retrieves an entire "section", if supported by the derived object.
virtual bool delete_section(const basis::astring &formal(section))
whacks the entire "section" specified.
virtual bool delete_entry(const basis::astring &formal(section), const basis::astring &formal(entry))
eliminates the entry specified by the "section" and "entry" name.
basis::astring load(const basis::astring &section, const basis::astring &entry, const basis::astring &default_value)
a synonym for get that implements the auto-store behavior.
virtual bool put_section(const basis::astring &formal(section), const structures::string_table &formal(to_store))
stores an entire "section" from the table in "to_store", if supported.
basis::astring make_section_heading(const basis::astring &section)
provides the appropriate heading string for the "section" name.
bool get_section_names(structures::string_array &sections)
loads the "sections" array with all section names.
section_manager(configurator &config, const basis::astring &toc_title, const basis::astring &header_prefix)
creates a section_manager that uses the "config" for storage.
bool add_section(const basis::astring &section_name, const structures::string_table &to_add)
stores a new section for "section_name" using the table "to_add".
bool get_toc(structures::string_table &toc)
reads the table of contents into "toc".
bool zap_section(const basis::astring &section_name)
removes the data for "section_name" from both the config and TOC.
bool section_exists(const basis::astring &section_name)
returns true if the section called "section_name" exists in the config.
bool replace_section(const basis::astring &section, const structures::string_table &replacement)
replaces the contents of "section" with the "replacement" table.
bool find_section(const basis::astring &section_name, structures::string_table &found)
loads the data from "section_name" into the table "found".
An array of strings with some additional helpful methods.
Provides a symbol_table that holds strings as the content.
const basis::astring & name(int index) const
returns the name held at the "index".
int symbols() const
returns the number of symbols listed in the table.
The guards collection helps in testing preconditions and reporting errors.
Definition array.h:30
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
Definition functions.h:121
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55