00001 #ifndef INI_PARSER_IMPLEMENTATION_FILE
00002 #define INI_PARSER_IMPLEMENTATION_FILE
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "ini_parser.h"
00019
00020 #include <basis/function.h>
00021 #include <basis/istring.h>
00022 #include <basis/log_base.h>
00023 #include <basis/string_array.h>
00024 #include <data_struct/amorph.h>
00025 #include <data_struct/table_configurator.h>
00026 #include <data_struct/string_table.h>
00027 #include <textual/parser_bits.h>
00028 #include <textual/tokenizer.h>
00029
00030
00031
00032
00033 #undef LOG
00034 #ifdef DEBUG_INI_PARSER
00035 #define LOG(to_print) program_wide_logger().log(to_print);
00036 #else
00037 #define LOG(a) {}
00038 #endif
00039
00041
00042
00043
00044
00045
00046
00047
00048 ini_parser::ini_parser(const istring &to_parse, treatment_of_defaults behavior)
00049 : table_configurator(behavior),
00050 _well_formed(false),
00051 _preface(new istring)
00052 {
00053 reset(to_parse);
00054 }
00055
00056 ini_parser::~ini_parser()
00057 {
00058 WHACK(_preface);
00059 }
00060
00061 void ini_parser::chow_through_eol(istring &to_chow)
00062 {
00063 while (to_chow.length()) {
00064 if (parser_bits::is_eol(to_chow[0])) {
00065
00066 while (to_chow.length() && parser_bits::is_eol(to_chow[0])) {
00067 *_preface += to_chow[0];
00068 to_chow.zap(0, 0);
00069 }
00070 return;
00071 }
00072 *_preface += to_chow[0];
00073 to_chow.zap(0, 0);
00074 }
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 void ini_parser::reset(const istring &to_parse)
00108 {
00109 _well_formed = false;
00110 table_configurator::reset();
00111 _preface->reset();
00112 add(to_parse);
00113 }
00114
00115 void ini_parser::add(const istring &to_parse)
00116 {
00117 istring parsing = to_parse;
00118
00119 _preface->reset();
00120 while (parsing.length()) {
00121 istring section_name;
00122 bool found_sect = parse_section(parsing, section_name);
00123 if (!found_sect) {
00124
00125 chow_through_eol(parsing);
00126 continue;
00127 }
00128
00129 int next_sect = 0;
00130 for (next_sect = 0; next_sect < parsing.length(); next_sect++) {
00131
00132 if (parser_bits::is_eol(parsing[next_sect])) {
00133
00134
00135
00136 if (parsing[next_sect + 1] == '[') {
00137
00138 break;
00139 }
00140 }
00141 }
00142
00143 if (next_sect >= parsing.length()) next_sect--;
00144
00145 LOG(isprintf("bounds are %d to %d, string len is %d.", 0, next_sect,
00146 parsing.length()));
00147 istring sect_parsing = parsing.substring(0, next_sect);
00148 LOG(istring("going to parse: >>") + sect_parsing + "<<");
00149 parsing.zap(0, next_sect);
00150 tokenizer section_reader("\n", "=");
00151 section_reader.set_comment_chars(";#");
00152 section_reader.parse(sect_parsing);
00153 LOG(istring("read: ") + section_reader.text_form());
00154 merge_section(section_name, section_reader.table());
00155 }
00156 _well_formed = true;
00157 }
00158
00159 void ini_parser::merge_section(const istring §ion_name,
00160 const string_table &to_merge)
00161 {
00162 if (!section_exists(section_name)) {
00163
00164 put_section(section_name, to_merge);
00165 return;
00166 }
00167
00168
00169
00170 for (int i = 0; i < to_merge.symbols(); i++)
00171 put(section_name, to_merge.name(i), to_merge[i]);
00172 }
00173
00174 bool ini_parser::parse_section(istring &to_parse, istring §ion_name)
00175 {
00176 section_name = "";
00177
00178
00179 enum states {
00180 SEEKING_OPENING_BRACKET,
00181 EATING_SECTION_NAME
00182 };
00183 states state = SEEKING_OPENING_BRACKET;
00184
00185
00186 for (int i = 0; i < to_parse.length(); i++) {
00187 char curr = to_parse[i];
00188 LOG(istring("<") + istring(curr, 1) + ">");
00189 switch (state) {
00190 case SEEKING_OPENING_BRACKET:
00191
00192 if (parser_bits::white_space(curr)) continue;
00193 if (curr != '[') return false;
00194 state = EATING_SECTION_NAME;
00195 break;
00196 case EATING_SECTION_NAME:
00197
00198 if (curr == ']') {
00199
00200 to_parse.zap(0, i);
00201
00202
00203 return true;
00204 }
00205 section_name += curr;
00206 break;
00207 default:
00208
00209 return false;
00210 }
00211 }
00212
00213
00214 return false;
00215 }
00216
00217 bool ini_parser::restate(istring &new_ini, bool add_spaces)
00218 {
00219 new_ini = *_preface;
00220 string_array sects;
00221 sections(sects);
00222 for (int i = 0; i < sects.length(); i++) {
00223 new_ini += istring("[") + sects[i] + "]" + log_base::platform_ending();
00224 string_table tab;
00225 if (!get_section(sects[i], tab)) continue;
00226 tab.add_spaces(add_spaces);
00227 new_ini += tab.text_form();
00228 }
00229 return true;
00230 }
00231
00232
00233 #endif //INI_PARSER_IMPLEMENTATION_FILE
00234