1 // ------------------------------------------------
6 // .INI file reading/writing class
8 // (c) 2002 peercast.org
9 // ------------------------------------------------
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 // ------------------------------------------------
25 #include "chkMemoryLeak.h"
26 #define DEBUG_NEW new(__FILE__, __LINE__)
31 void openReadOnly(const char *);
32 void openWriteReplace(const char *);
33 // -----------------------------------------
34 bool IniFile::openReadOnly(const char *fn)
38 fStream.openReadOnly(fn);
39 }catch(StreamException &)
45 // -----------------------------------------
46 bool IniFile::openWriteReplace(const char *fn)
50 fStream.openWriteReplace(fn);
51 #if defined(_LINUX) || defined(__APPLE__)
52 fStream.writeCRLF = false;
55 }catch(StreamException &)
61 // -----------------------------------------
68 // -----------------------------------------
69 bool IniFile::readNext()
76 fStream.readLine(currLine,256);
77 }catch(StreamException &)
83 // find end of value name and null terminate
84 char *nend = strstr(currLine,"=");
89 valueStr = trimstr(nend+1);
93 nameStr = trimstr(currLine);
97 // -----------------------------------------
98 bool IniFile::isName(const char *str)
100 return stricmp(getName(),str)==0;
103 // -----------------------------------------
104 char * IniFile::getName()
108 // -----------------------------------------
109 int IniFile::getIntValue()
112 return atoi(valueStr);
116 // -----------------------------------------
117 char * IniFile::getStrValue()
124 // -----------------------------------------
125 bool IniFile::getBoolValue()
131 if ( (stricmp(valueStr,"yes")==0) ||
132 (stricmp(valueStr,"y")==0) ||
133 (stricmp(valueStr,"1")==0) )
139 // -----------------------------------------
140 void IniFile::writeIntValue(const char *name, int iv)
142 sprintf(currLine,"%s = %d",name,iv);
143 fStream.writeLine(currLine);
145 // -----------------------------------------
146 void IniFile::writeStrValue(const char *name, const char *sv)
148 sprintf(currLine,"%s = %s",name,sv);
149 fStream.writeLine(currLine);
151 // -----------------------------------------
152 void IniFile::writeSection(const char *name)
154 fStream.writeLine("");
155 sprintf(currLine,"[%s]",name);
156 fStream.writeLine(currLine);
158 // -----------------------------------------
159 void IniFile::writeBoolValue(const char *name, int v)
161 sprintf(currLine,"%s = %s",name,(v!=0)?"Yes":"No");
162 fStream.writeLine(currLine);
164 // -----------------------------------------
165 void IniFile::writeLine(const char *str)
167 fStream.writeLine(str);