00001 #ifndef SECTION_MANAGER_IMPLEMENTATION_FILE
00002 #define SECTION_MANAGER_IMPLEMENTATION_FILE
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "configurator.h"
00019 #include "section_manager.h"
00020 #include "string_table.h"
00021
00022 #include <basis/function.h>
00023 #include <basis/istring.h>
00024 #include <basis/string_array.h>
00025
00026 section_manager::section_manager(configurator &config,
00027 const istring &toc_title, const istring &header_prefix)
00028 : _config(config),
00029 _toc_title(new istring(toc_title)),
00030 _section_prefix(new istring(header_prefix))
00031 {
00032 }
00033
00034 section_manager::~section_manager()
00035 {
00036 WHACK(_toc_title);
00037 WHACK(_section_prefix);
00038 }
00039
00040 bool section_manager::get_toc(string_table &toc)
00041 { return _config.get_section(*_toc_title, toc); }
00042
00043 bool section_manager::get_section_names(string_array §ions)
00044 {
00045 sections.reset();
00046 string_table toc;
00047 if (!get_toc(toc)) return false;
00048 for (int i = 0; i < toc.symbols(); i++) sections += toc.name(i);
00049 return true;
00050 }
00051
00052 bool section_manager::section_exists(const istring §ion_name)
00053 {
00054 if (!section_name) return false;
00055 return _config.load(*_toc_title, section_name, "").t();
00056 }
00057
00058 istring section_manager::make_section_heading(const istring §ion)
00059 { return *_section_prefix + section; }
00060
00061 bool section_manager::add_section(const istring §ion_name,
00062 const string_table &to_add)
00063 {
00064 if (!section_name) return false;
00065 if (section_exists(section_name)) return false;
00066
00067 istring section_value = "t";
00068 if (!to_add.symbols())
00069 section_value = "";
00070 if (!_config.put(*_toc_title, section_name, section_value))
00071 return false;
00072
00073 istring header = make_section_heading(section_name);
00074
00075
00076 return _config.put_section(header, to_add);
00077 }
00078
00079 bool section_manager::replace_section(const istring §ion_name,
00080 const string_table &replacement)
00081 {
00082 if (!section_name) return false;
00083 if (!section_exists(section_name)) return false;
00084 if (!zap_section(section_name)) return false;
00085 if (!replacement.symbols()) return true;
00086 return add_section(section_name, replacement);
00087 }
00088
00089 bool section_manager::zap_section(const istring §ion_name)
00090 {
00091 if (!section_name) return false;
00092 istring header = make_section_heading(section_name);
00093 if (!_config.delete_section(header)) return false;
00094 return _config.delete_entry(*_toc_title, section_name);
00095 }
00096
00097 bool section_manager::find_section(const istring §ion_name,
00098 string_table &found)
00099 {
00100 if (!section_name) return false;
00101 if (!section_exists(section_name)) return false;
00102 istring header = make_section_heading(section_name);
00103 return _config.get_section(header, found);
00104 }
00105
00106
00107 #endif //SECTION_MANAGER_IMPLEMENTATION_FILE
00108