00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <swconfig.h>
00024 #include <utilstr.h>
00025 #include <filemgr.h>
00026 #include <fcntl.h>
00027
00028
00029 SWORD_NAMESPACE_START
00030
00031 SWConfig::SWConfig() {
00032 }
00033
00034 SWConfig::SWConfig(const char * ifilename) {
00035 filename = ifilename;
00036 Load();
00037 }
00038
00039
00040 SWConfig::~SWConfig() {
00041 }
00042
00043 void SWConfig::Load() {
00044
00045 if (!filename.size()) return;
00046
00047 FileDesc *cfile;
00048 char *buf, *data;
00049 SWBuf line;
00050 ConfigEntMap cursect;
00051 SWBuf sectname;
00052 bool first = true;
00053
00054 Sections.erase(Sections.begin(), Sections.end());
00055
00056 cfile = FileMgr::getSystemFileMgr()->open(filename.c_str(), FileMgr::RDONLY);
00057 if (cfile->getFd() > 0) {
00058 bool goodLine = FileMgr::getLine(cfile, line);
00059
00060
00061 while (goodLine && line.length() &&
00062 ((((unsigned char)line[0]) == 0xEF) ||
00063 (((unsigned char)line[0]) == 0xBB) ||
00064 (((unsigned char)line[0]) == 0xBF))) {
00065 line << 1;
00066 }
00067
00068 while (goodLine) {
00069
00070 if (!line.startsWith("#")) {
00071 buf = new char [ line.length() + 1 ];
00072 strcpy(buf, line.c_str());
00073 if (*strstrip(buf) == '[') {
00074 if (!first)
00075 Sections.insert(SectionMap::value_type(sectname, cursect));
00076 else first = false;
00077
00078 cursect.erase(cursect.begin(), cursect.end());
00079
00080 strtok(buf, "]");
00081 sectname = buf+1;
00082 }
00083 else {
00084 strtok(buf, "=");
00085 if ((*buf) && (*buf != '=')) {
00086 if ((data = strtok(NULL, "")))
00087 cursect.insert(ConfigEntMap::value_type(buf, strstrip(data)));
00088 else cursect.insert(ConfigEntMap::value_type(buf, ""));
00089 }
00090 }
00091 delete [] buf;
00092 }
00093 goodLine = FileMgr::getLine(cfile, line);
00094 }
00095 if (!first)
00096 Sections.insert(SectionMap::value_type(sectname, cursect));
00097
00098 FileMgr::getSystemFileMgr()->close(cfile);
00099 }
00100 }
00101
00102
00103 void SWConfig::Save() {
00104
00105 if (!filename.size()) return;
00106
00107 FileDesc *cfile;
00108 SWBuf buf;
00109 SectionMap::iterator sit;
00110 ConfigEntMap::iterator entry;
00111 SWBuf sectname;
00112
00113 cfile = FileMgr::getSystemFileMgr()->open(filename.c_str(), FileMgr::RDWR|FileMgr::CREAT|FileMgr::TRUNC);
00114 if (cfile->getFd() > 0) {
00115
00116 for (sit = Sections.begin(); sit != Sections.end(); sit++) {
00117 buf = "\n[";
00118 buf += (*sit).first.c_str();
00119 buf += "]\n";
00120 cfile->write(buf.c_str(), buf.length());
00121 for (entry = (*sit).second.begin(); entry != (*sit).second.end(); entry++) {
00122 buf = (*entry).first.c_str();
00123 buf += "=";
00124 buf += (*entry).second.c_str();
00125 buf += "\n";
00126 cfile->write(buf.c_str(), buf.length());
00127 }
00128 }
00129 buf = "\n";
00130 cfile->write(buf.c_str(), buf.length());
00131 FileMgr::getSystemFileMgr()->close(cfile);
00132 }
00133 }
00134
00135
00136 void SWConfig::augment(SWConfig &addFrom) {
00137
00138 SectionMap::iterator section;
00139 ConfigEntMap::iterator entry, start, end;
00140
00141 for (section = addFrom.Sections.begin(); section != addFrom.Sections.end(); section++) {
00142 for (entry = (*section).second.begin(); entry != (*section).second.end(); entry++) {
00143 start = Sections[section->first].lower_bound(entry->first);
00144 end = Sections[section->first].upper_bound(entry->first);
00145 if (start != end) {
00146 if (((++start) != end)
00147 || ((++(addFrom.Sections[section->first].lower_bound(entry->first))) != addFrom.Sections[section->first].upper_bound(entry->first))) {
00148 for (--start; start != end; start++) {
00149 if (!strcmp(start->second.c_str(), entry->second.c_str()))
00150 break;
00151 }
00152 if (start == end)
00153 Sections[(*section).first].insert(ConfigEntMap::value_type((*entry).first, (*entry).second));
00154 }
00155 else Sections[section->first][entry->first.c_str()] = entry->second.c_str();
00156 }
00157 else Sections[section->first][entry->first.c_str()] = entry->second.c_str();
00158 }
00159 }
00160 }
00161
00162
00163 ConfigEntMap & SWConfig::operator [] (const char *section) {
00164 return Sections[section];
00165 }
00166
00167 SWORD_NAMESPACE_END