1
/******************************************************************************
2
* swconfig.cpp - implementation of Class SWConfig used for saving and
3
* retrieval of configuration information
5
* $Id: swconfig.cpp 2218 2008-12-23 09:33:38Z scribe $
7
* Copyright 1998 CrossWire Bible Society (http://www.crosswire.org)
8
* CrossWire Bible Society
10
* Tempe, AZ 85280-2528
12
* This program is free software; you can redistribute it and/or modify it
13
* under the terms of the GNU General Public License as published by the
14
* Free Software Foundation version 2.
16
* This program is distributed in the hope that it will be useful, but
17
* WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
* General Public License for more details.
31
SWConfig::SWConfig() {
34
SWConfig::SWConfig(const char * ifilename) {
40
SWConfig::~SWConfig() {
43
void SWConfig::Load() {
45
if (!filename.size()) return; // assert we have a filename
54
Sections.erase(Sections.begin(), Sections.end());
56
cfile = FileMgr::getSystemFileMgr()->open(filename.c_str(), FileMgr::RDONLY);
57
if (cfile->getFd() > 0) {
58
bool goodLine = FileMgr::getLine(cfile, line);
60
// clean UTF encoding tags at start of file
61
while (goodLine && line.length() &&
62
((((unsigned char)line[0]) == 0xEF) ||
63
(((unsigned char)line[0]) == 0xBB) ||
64
(((unsigned char)line[0]) == 0xBF))) {
69
// ignore commented lines
70
if (!line.startsWith("#")) {
71
buf = new char [ line.length() + 1 ];
72
strcpy(buf, line.c_str());
73
if (*strstrip(buf) == '[') {
75
Sections.insert(SectionMap::value_type(sectname, cursect));
78
cursect.erase(cursect.begin(), cursect.end());
85
if ((*buf) && (*buf != '=')) {
86
if ((data = strtok(NULL, "")))
87
cursect.insert(ConfigEntMap::value_type(buf, strstrip(data)));
88
else cursect.insert(ConfigEntMap::value_type(buf, ""));
93
goodLine = FileMgr::getLine(cfile, line);
96
Sections.insert(SectionMap::value_type(sectname, cursect));
98
FileMgr::getSystemFileMgr()->close(cfile);
103
void SWConfig::Save() {
105
if (!filename.size()) return; // assert we have a filename
109
SectionMap::iterator sit;
110
ConfigEntMap::iterator entry;
113
cfile = FileMgr::getSystemFileMgr()->open(filename.c_str(), FileMgr::RDWR|FileMgr::CREAT|FileMgr::TRUNC);
114
if (cfile->getFd() > 0) {
116
for (sit = Sections.begin(); sit != Sections.end(); sit++) {
118
buf += (*sit).first.c_str();
120
cfile->write(buf.c_str(), buf.length());
121
for (entry = (*sit).second.begin(); entry != (*sit).second.end(); entry++) {
122
buf = (*entry).first.c_str();
124
buf += (*entry).second.c_str();
126
cfile->write(buf.c_str(), buf.length());
130
cfile->write(buf.c_str(), buf.length());
131
FileMgr::getSystemFileMgr()->close(cfile);
136
void SWConfig::augment(SWConfig &addFrom) {
138
SectionMap::iterator section;
139
ConfigEntMap::iterator entry, start, end;
141
for (section = addFrom.Sections.begin(); section != addFrom.Sections.end(); section++) {
142
for (entry = (*section).second.begin(); entry != (*section).second.end(); entry++) {
143
start = Sections[section->first].lower_bound(entry->first);
144
end = Sections[section->first].upper_bound(entry->first);
146
if (((++start) != end)
147
|| ((++(addFrom.Sections[section->first].lower_bound(entry->first))) != addFrom.Sections[section->first].upper_bound(entry->first))) {
148
for (--start; start != end; start++) {
149
if (!strcmp(start->second.c_str(), entry->second.c_str()))
153
Sections[(*section).first].insert(ConfigEntMap::value_type((*entry).first, (*entry).second));
155
else Sections[section->first][entry->first.c_str()] = entry->second.c_str();
157
else Sections[section->first][entry->first.c_str()] = entry->second.c_str();
163
ConfigEntMap & SWConfig::operator [] (const char *section) {
164
return Sections[section];