Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

swconfig.cpp

00001 /******************************************************************************
00002  *  swconfig.cpp   - implementation of Class SWConfig used for saving and
00003  *                      retrieval of configuration information
00004  *
00005  * $Id: swconfig_8cpp-source.html,v 1.3 2002/06/20 20:23:10 mgruner Exp $
00006  *
00007  * Copyright 1998 CrossWire Bible Society (http://www.crosswire.org)
00008  *      CrossWire Bible Society
00009  *      P. O. Box 2528
00010  *      Tempe, AZ  85280-2528
00011  *
00012  * This program is free software; you can redistribute it and/or modify it
00013  * under the terms of the GNU General Public License as published by the
00014  * Free Software Foundation version 2.
00015  *
00016  * This program is distributed in the hope that it will be useful, but
00017  * WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * General Public License for more details.
00020  *
00021  */
00022 
00023 #include <swconfig.h>
00024 #include <utilfuns.h>
00025 
00026 
00027 SWConfig::SWConfig(const char * ifilename) {
00028         filename = ifilename;
00029         Load();
00030 }
00031 
00032 
00033 SWConfig::~SWConfig() {
00034 }
00035 
00036 
00037 char SWConfig::getline(FILE *fp, string &line)
00038 {
00039         char retval = 0;
00040         char buf[255];
00041 
00042         line = "";
00043         
00044         while (fgets(buf, 254, fp)) {
00045                 while (buf[strlen(buf)-1] == '\n' || buf[strlen(buf)-1] == '\r')
00046                         buf[strlen(buf)-1] = 0;
00047 
00048                 if (buf[strlen(buf)-1] == '\\') {
00049                         buf[strlen(buf)-1] = 0;
00050                         line += buf;
00051                         continue;
00052                 }
00053                 line += buf;
00054 
00055                 if (strlen(buf) < 253) {
00056                         retval = 1;
00057                         break;
00058                 }
00059         }
00060         return retval;
00061 }
00062 
00063 
00064 void SWConfig::Load() {
00065         FILE *cfile;
00066         char *buf, *data;
00067         string line;
00068         ConfigEntMap cursect;
00069         string sectname;
00070         bool first = true;
00071         
00072         Sections.erase(Sections.begin(), Sections.end());
00073         
00074         if ((cfile = fopen(filename.c_str(), "r"))) {
00075                 while (getline(cfile, line)) {
00076                         buf = new char [ line.length() + 1 ];
00077                         strcpy(buf, line.c_str());
00078                         if (*strstrip(buf) == '[') {
00079                                 if (!first)
00080                                         Sections.insert(SectionMap::value_type(sectname, cursect));
00081                                 else first = false;
00082                                 
00083                                 cursect.erase(cursect.begin(), cursect.end());
00084 
00085                                 strtok(buf, "]");
00086                                 sectname = buf+1;
00087                         }
00088                         else {
00089                                 strtok(buf, "=");
00090                                 if ((*buf) && (*buf != '=')) {
00091                                         if ((data = strtok(NULL, "")))
00092                                                 cursect.insert(ConfigEntMap::value_type(buf, strstrip(data)));
00093                                         else cursect.insert(ConfigEntMap::value_type(buf, ""));
00094                                 }
00095                         }
00096                         delete [] buf;
00097                 }
00098                 if (!first)
00099                         Sections.insert(SectionMap::value_type(sectname, cursect));
00100 
00101                 fclose(cfile);
00102         }
00103 }
00104 
00105 
00106 void SWConfig::Save() {
00107         FILE *cfile;
00108         string buf;
00109         SectionMap::iterator sit;
00110         ConfigEntMap::iterator entry;
00111         string sectname;
00112         
00113         if ((cfile = fopen(filename.c_str(), "w"))) {
00114                 
00115                 for (sit = Sections.begin(); sit != Sections.end(); sit++) {
00116                         buf =  "\n[";
00117                         buf += (*sit).first.c_str();
00118                         buf += "]\n";
00119                         fputs(buf.c_str(), cfile);
00120                         for (entry = (*sit).second.begin(); entry != (*sit).second.end(); entry++) {
00121                                 buf = (*entry).first.c_str();
00122                                 buf += "=";
00123                                 buf += (*entry).second.c_str();
00124                                 buf += "\n";
00125                                 fputs(buf.c_str(), cfile);
00126                         }
00127                 }
00128                 fputs("\n", cfile);     // so getline will find last line
00129                 fclose(cfile);
00130         }
00131 }
00132 
00133 
00134 SWConfig &SWConfig::operator +=(SWConfig &addFrom)
00135 {
00136 
00137         SectionMap::iterator section;
00138         ConfigEntMap::iterator entry, start, end;
00139 
00140         for (section = addFrom.Sections.begin(); section != addFrom.Sections.end(); section++) {
00141                 for (entry = (*section).second.begin(); entry != (*section).second.end(); entry++) {
00142                         start = Sections[section->first].lower_bound(entry->first);
00143                         end   = Sections[section->first].upper_bound(entry->first);
00144                         if (start != end) {
00145                                 if (((++start) != end)
00146                                                 || ((++(addFrom.Sections[section->first].lower_bound(entry->first))) != addFrom.Sections[section->first].upper_bound(entry->first))) {
00147                                         for (--start; start != end; start++) {
00148                                                 if (!strcmp(start->second.c_str(), entry->second.c_str()))
00149                                                         break;
00150                                         }
00151                                         if (start == end)
00152                                                 Sections[(*section).first].insert(ConfigEntMap::value_type((*entry).first, (*entry).second));
00153                                 }
00154                                 else    Sections[section->first][entry->first.c_str()] = entry->second.c_str();
00155                         }               
00156                         else    Sections[section->first][entry->first.c_str()] = entry->second.c_str();
00157                 }
00158         }
00159         return *this;
00160 }
00161 
00162 
00163 ConfigEntMap & SWConfig::operator [] (const char *section) {
00164     return Sections[section];
00165 }

Generated on Thu Jun 20 22:13:00 2002 for The Sword Project by doxygen1.2.15