OSDN Git Service

WebGUIのレスポンスが悪かったのを修正
[peercast-im/PeerCastIM.git] / c: / Git / PeerCast.root / PeerCast / core / common / inifile.cpp
1 // ------------------------------------------------
2 // File : inifile.cpp
3 // Date: 4-apr-2002
4 // Author: giles
5 // Desc: 
6 //              .INI file reading/writing class
7 //
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.
14
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 // ------------------------------------------------
20
21 #include <stdlib.h>
22 #include "inifile.h"
23 #include "sys.h"
24 #ifdef _DEBUG
25 #include "chkMemoryLeak.h"
26 #define DEBUG_NEW new(__FILE__, __LINE__)
27 #define new DEBUG_NEW
28 #endif
29
30
31         void    openReadOnly(const char *);
32         void    openWriteReplace(const char *);
33 // -----------------------------------------
34 bool IniFile::openReadOnly(const char *fn)
35 {
36         try 
37         {
38                 fStream.openReadOnly(fn);
39         }catch(StreamException &)
40         {
41                 return false;
42         }
43         return true;
44 }
45 // -----------------------------------------
46 bool IniFile::openWriteReplace(const char *fn)
47 {
48         try 
49         {
50                 fStream.openWriteReplace(fn);
51 #if defined(_LINUX) || defined(__APPLE__)
52                 fStream.writeCRLF = false;
53 #endif
54
55         }catch(StreamException &)
56         {
57                 return false;
58         }
59         return true;
60 }
61 // -----------------------------------------
62 void IniFile::close()
63 {
64         fStream.close();
65 }
66
67
68 // -----------------------------------------
69 bool    IniFile::readNext()
70 {
71         if (fStream.eof())
72                 return false;
73
74         try
75         {
76                 fStream.readLine(currLine,256);
77         }catch(StreamException &)
78         {
79                 return false;
80         }
81
82
83         // find end of value name and null terminate
84         char *nend = strstr(currLine,"=");
85
86         if (nend)
87         {
88                 *nend = 0;
89                 valueStr = trimstr(nend+1);
90         }else
91                 valueStr = NULL;
92
93         nameStr = trimstr(currLine);
94
95         return true;
96 }
97 // -----------------------------------------
98 bool IniFile::isName(const char *str)
99 {
100         return stricmp(getName(),str)==0;
101 }
102
103 // -----------------------------------------
104 char *  IniFile::getName()
105 {
106         return nameStr;
107 }
108 // -----------------------------------------
109 int             IniFile::getIntValue()
110 {
111         if (valueStr)
112                 return atoi(valueStr);
113         else
114                 return 0;
115 }
116 // -----------------------------------------
117 char *  IniFile::getStrValue()
118 {
119         if (valueStr)
120                 return valueStr;
121         else
122                 return "";
123 }
124 // -----------------------------------------
125 bool    IniFile::getBoolValue()
126 {
127         if (!valueStr)
128                 return false;
129
130
131         if ( (stricmp(valueStr,"yes")==0) ||
132                  (stricmp(valueStr,"y")==0) ||
133                  (stricmp(valueStr,"1")==0) )
134                 return true;
135
136         return false;
137 }
138
139 // -----------------------------------------
140 void    IniFile::writeIntValue(const char *name, int iv)
141 {
142         sprintf(currLine,"%s = %d",name,iv);
143         fStream.writeLine(currLine);
144 }
145 // -----------------------------------------
146 void    IniFile::writeStrValue(const char *name, const char *sv)
147 {
148         sprintf(currLine,"%s = %s",name,sv);
149         fStream.writeLine(currLine);
150 }
151 // -----------------------------------------
152 void    IniFile::writeSection(const char *name)
153 {
154         fStream.writeLine("");
155         sprintf(currLine,"[%s]",name);
156         fStream.writeLine(currLine);
157 }
158 // -----------------------------------------
159 void    IniFile::writeBoolValue(const char *name, int v)
160 {
161         sprintf(currLine,"%s = %s",name,(v!=0)?"Yes":"No");
162         fStream.writeLine(currLine);
163 }
164 // -----------------------------------------
165 void    IniFile::writeLine(const char *str)
166 {
167         fStream.writeLine(str);
168 }