<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>swconfig.cpp Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.2.15 --> <center> <a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="namespaces.html">Namespace List</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="classes.html">Alphabetical List</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Compound Members</a> </center> <hr><h1>swconfig.cpp</h1><div class="fragment"><pre>00001 <font class="comment">/******************************************************************************</font> 00002 <font class="comment"> * swconfig.cpp - implementation of Class SWConfig used for saving and</font> 00003 <font class="comment"> * retrieval of configuration information</font> 00004 <font class="comment"> *</font> 00005 <font class="comment"> * $Id: swconfig_8cpp-source.html,v 1.3 2002/06/20 20:23:10 mgruner Exp $</font> 00006 <font class="comment"> *</font> 00007 <font class="comment"> * Copyright 1998 CrossWire Bible Society (http://www.crosswire.org)</font> 00008 <font class="comment"> * CrossWire Bible Society</font> 00009 <font class="comment"> * P. O. Box 2528</font> 00010 <font class="comment"> * Tempe, AZ 85280-2528</font> 00011 <font class="comment"> *</font> 00012 <font class="comment"> * This program is free software; you can redistribute it and/or modify it</font> 00013 <font class="comment"> * under the terms of the GNU General Public License as published by the</font> 00014 <font class="comment"> * Free Software Foundation version 2.</font> 00015 <font class="comment"> *</font> 00016 <font class="comment"> * This program is distributed in the hope that it will be useful, but</font> 00017 <font class="comment"> * WITHOUT ANY WARRANTY; without even the implied warranty of</font> 00018 <font class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</font> 00019 <font class="comment"> * General Public License for more details.</font> 00020 <font class="comment"> *</font> 00021 <font class="comment"> */</font> 00022 00023 <font class="preprocessor">#include <swconfig.h></font> 00024 <font class="preprocessor">#include <utilfuns.h></font> 00025 00026 <a name="l00027"></a><a class="code" href="class_s_w_config.html#a0">00027</a> <a class="code" href="class_s_w_config.html#a0">SWConfig::SWConfig</a>(<font class="keyword">const</font> <font class="keywordtype">char</font> * ifilename) { 00028 <a class="code" href="class_s_w_config.html#m0">filename</a> = ifilename; 00029 <a class="code" href="class_s_w_config.html#a2">Load</a>(); 00030 } 00031 00032 00033 SWConfig::~SWConfig() { 00034 } 00035 00036 00037 <font class="keywordtype">char</font> SWConfig::getline(FILE *fp, string &line) 00038 { 00039 <font class="keywordtype">char</font> retval = 0; 00040 <font class="keywordtype">char</font> buf[255]; 00041 00042 line = <font class="stringliteral">""</font>; 00043 00044 <font class="keywordflow">while</font> (fgets(buf, 254, fp)) { 00045 <font class="keywordflow">while</font> (buf[strlen(buf)-1] == <font class="charliteral">'\n'</font> || buf[strlen(buf)-1] == <font class="charliteral">'\r'</font>) 00046 buf[strlen(buf)-1] = 0; 00047 00048 <font class="keywordflow">if</font> (buf[strlen(buf)-1] == <font class="charliteral">'\\'</font>) { 00049 buf[strlen(buf)-1] = 0; 00050 line += buf; 00051 <font class="keywordflow">continue</font>; 00052 } 00053 line += buf; 00054 00055 <font class="keywordflow">if</font> (strlen(buf) < 253) { 00056 retval = 1; 00057 <font class="keywordflow">break</font>; 00058 } 00059 } 00060 <font class="keywordflow">return</font> retval; 00061 } 00062 00063 <a name="l00064"></a><a class="code" href="class_s_w_config.html#a2">00064</a> <font class="keywordtype">void</font> <a class="code" href="class_s_w_config.html#a2">SWConfig::Load</a>() { 00065 FILE *cfile; 00066 <font class="keywordtype">char</font> *buf, *data; 00067 string line; 00068 ConfigEntMap cursect; 00069 string sectname; 00070 <font class="keywordtype">bool</font> first = <font class="keyword">true</font>; 00071 00072 <a class="code" href="class_s_w_config.html#m1">Sections</a>.erase(<a class="code" href="class_s_w_config.html#m1">Sections</a>.begin(), <a class="code" href="class_s_w_config.html#m1">Sections</a>.end()); 00073 00074 <font class="keywordflow">if</font> ((cfile = fopen(<a class="code" href="class_s_w_config.html#m0">filename</a>.c_str(), <font class="stringliteral">"r"</font>))) { 00075 <font class="keywordflow">while</font> (getline(cfile, line)) { 00076 buf = <font class="keyword">new</font> <font class="keywordtype">char</font> [ line.length() + 1 ]; 00077 strcpy(buf, line.c_str()); 00078 <font class="keywordflow">if</font> (*strstrip(buf) == <font class="charliteral">'['</font>) { 00079 <font class="keywordflow">if</font> (!first) 00080 <a class="code" href="class_s_w_config.html#m1">Sections</a>.insert(SectionMap::value_type(sectname, cursect)); 00081 <font class="keywordflow">else</font> first = <font class="keyword">false</font>; 00082 00083 cursect.erase(cursect.begin(), cursect.end()); 00084 00085 strtok(buf, <font class="stringliteral">"]"</font>); 00086 sectname = buf+1; 00087 } 00088 <font class="keywordflow">else</font> { 00089 strtok(buf, <font class="stringliteral">"="</font>); 00090 <font class="keywordflow">if</font> ((*buf) && (*buf != <font class="charliteral">'='</font>)) { 00091 <font class="keywordflow">if</font> ((data = strtok(NULL, <font class="stringliteral">""</font>))) 00092 cursect.insert(ConfigEntMap::value_type(buf, strstrip(data))); 00093 <font class="keywordflow">else</font> cursect.insert(ConfigEntMap::value_type(buf, <font class="stringliteral">""</font>)); 00094 } 00095 } 00096 <font class="keyword">delete</font> [] buf; 00097 } 00098 <font class="keywordflow">if</font> (!first) 00099 <a class="code" href="class_s_w_config.html#m1">Sections</a>.insert(SectionMap::value_type(sectname, cursect)); 00100 00101 fclose(cfile); 00102 } 00103 } 00104 00105 <a name="l00106"></a><a class="code" href="class_s_w_config.html#a3">00106</a> <font class="keywordtype">void</font> <a class="code" href="class_s_w_config.html#a3">SWConfig::Save</a>() { 00107 FILE *cfile; 00108 string buf; 00109 SectionMap::iterator sit; 00110 ConfigEntMap::iterator entry; 00111 string sectname; 00112 00113 <font class="keywordflow">if</font> ((cfile = fopen(<a class="code" href="class_s_w_config.html#m0">filename</a>.c_str(), <font class="stringliteral">"w"</font>))) { 00114 00115 <font class="keywordflow">for</font> (sit = <a class="code" href="class_s_w_config.html#m1">Sections</a>.begin(); sit != <a class="code" href="class_s_w_config.html#m1">Sections</a>.end(); sit++) { 00116 buf = <font class="stringliteral">"\n["</font>; 00117 buf += (*sit).first.c_str(); 00118 buf += <font class="stringliteral">"]\n"</font>; 00119 fputs(buf.c_str(), cfile); 00120 <font class="keywordflow">for</font> (entry = (*sit).second.begin(); entry != (*sit).second.end(); entry++) { 00121 buf = (*entry).first.c_str(); 00122 buf += <font class="stringliteral">"="</font>; 00123 buf += (*entry).second.c_str(); 00124 buf += <font class="stringliteral">"\n"</font>; 00125 fputs(buf.c_str(), cfile); 00126 } 00127 } 00128 fputs(<font class="stringliteral">"\n"</font>, cfile); <font class="comment">// so getline will find last line</font> 00129 fclose(cfile); 00130 } 00131 } 00132 00133 <a name="l00134"></a><a class="code" href="class_s_w_config.html#a4">00134</a> <a class="code" href="class_s_w_config.html">SWConfig</a> &<a class="code" href="class_s_w_config.html#a4">SWConfig::operator +=</a>(<a class="code" href="class_s_w_config.html">SWConfig</a> &addFrom) 00135 { 00136 00137 SectionMap::iterator section; 00138 ConfigEntMap::iterator entry, start, end; 00139 00140 <font class="keywordflow">for</font> (section = addFrom.<a class="code" href="class_s_w_config.html#m1">Sections</a>.begin(); section != addFrom.<a class="code" href="class_s_w_config.html#m1">Sections</a>.end(); section++) { 00141 <font class="keywordflow">for</font> (entry = (*section).second.begin(); entry != (*section).second.end(); entry++) { 00142 start = <a class="code" href="class_s_w_config.html#m1">Sections</a>[section->first].lower_bound(entry->first); 00143 end = <a class="code" href="class_s_w_config.html#m1">Sections</a>[section->first].upper_bound(entry->first); 00144 <font class="keywordflow">if</font> (start != end) { 00145 <font class="keywordflow">if</font> (((++start) != end) 00146 || ((++(addFrom.<a class="code" href="class_s_w_config.html#m1">Sections</a>[section->first].lower_bound(entry->first))) != addFrom.<a class="code" href="class_s_w_config.html#m1">Sections</a>[section->first].upper_bound(entry->first))) { 00147 <font class="keywordflow">for</font> (--start; start != end; start++) { 00148 <font class="keywordflow">if</font> (!strcmp(start->second.c_str(), entry->second.c_str())) 00149 <font class="keywordflow">break</font>; 00150 } 00151 <font class="keywordflow">if</font> (start == end) 00152 <a class="code" href="class_s_w_config.html#m1">Sections</a>[(*section).first].insert(ConfigEntMap::value_type((*entry).first, (*entry).second)); 00153 } 00154 <font class="keywordflow">else</font> <a class="code" href="class_s_w_config.html#m1">Sections</a>[section->first][entry->first.c_str()] = entry->second.c_str(); 00155 } 00156 <font class="keywordflow">else</font> <a class="code" href="class_s_w_config.html#m1">Sections</a>[section->first][entry->first.c_str()] = entry->second.c_str(); 00157 } 00158 } 00159 <font class="keywordflow">return</font> *<font class="keyword">this</font>; 00160 } 00161 00162 <a name="l00163"></a><a class="code" href="class_s_w_config.html#a5">00163</a> ConfigEntMap & <a class="code" href="class_s_w_config.html#a5">SWConfig::operator [] </a>(<font class="keyword">const</font> <font class="keywordtype">char</font> *section) { 00164 <font class="keywordflow">return</font> <a class="code" href="class_s_w_config.html#m1">Sections</a>[section]; 00165 } </pre></div><hr><address align="right"><small>Generated on Thu Jun 20 22:13:00 2002 for The Sword Project by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border=0 width=110 height=53></a>1.2.15 </small></address> </body> </html>