[c03716]: ini.cpp Maximize Restore History

Download this file

ini.cpp    177 lines (154 with data), 3.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// ini.cpp: implementation of the Cini class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "chitem.h"
#include "ini.h"
#include "ChitemDlg.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern int read_string(FILE *fpoi, char *pattern, char *tmpbuff=NULL, int length=0);
extern void SetupReader(int max);
Cini::Cini()
{
}
void Cini::RemoveAll()
{
CString key;
CStringMapString *value = NULL;
POSITION pos;
pos=GetStartPosition();
while(pos)
{
GetNextAssoc(pos, key, value);
if(value)
{
delete value;
value=NULL;
}
}
CTypedPtrMap<CMapStringToOb, CString, CStringMapString*>::RemoveAll();
valid.RemoveAll();
}
Cini::~Cini()
{
RemoveAll();
}
void Cini::AddValid(CString what)
{
valid.AddTail(what);
}
void Cini::AddValid(CString *what, int cnt)
{
while(cnt--)
{
valid.AddTail(what[cnt]);
}
}
bool Cini::IsValid(CString what)
{
if(valid.Find(what)) return true;
return false;
}
bool Cini::HasSection(CString what)
{
CStringMapString *sect = NULL;
return !!Lookup(what, sect);
}
CString Cini::GetValue(CString section, CString key)
{
CStringMapString *sect = NULL;
CString value;
Lookup(section, sect);
if(!sect) return value; //it is empty at this point
sect->Lookup(key, value);
return value;
}
int Cini::ReadIniFromFile(int fh, long ml)
{
FILE *fpoi;
int ret;
int res;
char line[MAXBUFFSIZE];
CString tmpstr;
CString section;
CString key;
CString value;
CStringMapString *sect = NULL;
RemoveAll();
ret=0;
fhandle=fh; //for safe string reads
if(ml==-1) maxlen=filelength(fhandle);
else maxlen=ml;
if(maxlen<1) return -1; //short file, invalid item
fpoi=fdopen(fhandle,"rb");
if(!fpoi)
{
close(fhandle);
return -1;
}
SetupReader(maxlen);
do
{
res=read_string(fpoi,"\n", line, sizeof(line));
tmpstr=line;
tmpstr.TrimLeft();
tmpstr.TrimRight();
if(tmpstr.IsEmpty()) continue;
if(tmpstr[0]==';') continue;
if(tmpstr[0]=='[' && tmpstr.GetLength()>3)
{
section=tmpstr.Mid(1,tmpstr.GetLength()-2);
if(Lookup(section, sect) )
{
((CChitemDlg *) AfxGetMainWnd())->log("duplicate section entry: %s", section);
}
else
{
section.MakeLower();
SetAt(section, new CStringMapString);
}
continue;
}
if (section.GetLength())
{
int separator = tmpstr.Find("=");
if (separator>1)
{
if (!Lookup(section, sect) || !sect)
{
fclose(fpoi);
return -4; //internal error
}
key=tmpstr.Left(separator);
value=tmpstr.Mid(separator+1);
key.TrimRight();
value.TrimLeft();
if(key.GetLength())
{
key.MakeLower();
value.MakeLower();
sect->SetAt(key, value);
}
}
else
{
res=-3; //invalid entry
}
}
else
{
res=-2; //invalid spawn ini file starts without section
}
}
while(res==1);
if (res==2) res=0;
fclose(fpoi);
return res;
}