section_manager.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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 §ions)
00049 {
00050 sections.reset();
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 §ion_name)
00058 {
00059 if (!section_name) return false;
00060 return _config.load(*_toc_title, section_name, "").t();
00061 }
00062
00063 astring section_manager::make_section_heading(const astring §ion)
00064 { return *_section_prefix + section; }
00065
00066 bool section_manager::add_section(const astring §ion_name,
00067 const string_table &to_add)
00068 {
00069 if (!section_name) return false;
00070 if (section_exists(section_name)) return false;
00071
00072 astring section_value = "t";
00073 if (!to_add.symbols())
00074 section_value = "";
00075 if (!_config.put(*_toc_title, section_name, section_value))
00076 return false;
00077
00078 astring header = make_section_heading(section_name);
00079
00080
00081 return _config.put_section(header, to_add);
00082 }
00083
00084 bool section_manager::replace_section(const astring §ion_name,
00085 const string_table &replacement)
00086 {
00087 if (!section_name) return false;
00088 if (!section_exists(section_name)) return false;
00089 if (!zap_section(section_name)) return false;
00090 if (!replacement.symbols()) return true;
00091 return add_section(section_name, replacement);
00092 }
00093
00094 bool section_manager::zap_section(const astring §ion_name)
00095 {
00096 if (!section_name) return false;
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 §ion_name,
00103 string_table &found)
00104 {
00105 if (!section_name) return false;
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 }
00112
00113