-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMIniFile.cpp
147 lines (135 loc) · 4.12 KB
/
MIniFile.cpp
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
/////////////////////////////////////////////////////////////////////////////
//MIniFile.cpp: implementation of the MIniFile class.
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MIniFile.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
MIniFile::MIniFile()
{
m_pFilePath = NULL;
}
MIniFile::~MIniFile()
{
try
{
if(m_pFilePath) { delete[] m_pFilePath; m_pFilePath = 0; }
}
catch (...) {;}
}
BOOL MIniFile::WriteKey(const char* pSection, const char* pKey, const char* pData)
{
if(!m_pFilePath)
return false;
return (BOOL)WritePrivateProfileString(pSection, pKey, pData, m_pFilePath);
}
int MIniFile::WriteKey(const char* pSection, const char* pKey, int nData)
{
if(!m_pFilePath)
return false;
char pData[12] = {NULL};
wsprintf(pData,"%i",nData);
return (BOOL)WritePrivateProfileString(pSection, pKey, pData, m_pFilePath);
}
void MIniFile::SetFile(const char* pFileName)
{
if(m_pFilePath) { delete[] m_pFilePath; m_pFilePath = 0; }
int nAllocAmount = strlen(pFileName) + 1;
m_pFilePath = new char[nAllocAmount];
strcpy(m_pFilePath, pFileName);
}
unsigned long int MIniFile::ReadKey(const char* pSection, const char* pKey, const char* pDefault,char* pOutputBuffer, unsigned long nOutputBufferLength)
{
if(!m_pFilePath) return false;
return GetPrivateProfileString(pSection, pKey, pDefault, pOutputBuffer, nOutputBufferLength, m_pFilePath);
}
int MIniFile::ReadKey(const char* pSection, const char* pKey, int nDefault)
{
if(!m_pFilePath) return false;
return (long int)GetPrivateProfileInt(pSection, pKey, nDefault, m_pFilePath);
}
const char *MIniFile::GetFilePath()
{
return m_pFilePath;
}
int MIniFile::ReadSection(const char* pSection, MListStrStr& ListStrStr, int Optimize)
{
if(!m_pFilePath) return false;
const int BufSize = 65536;
char AllKeys[BufSize] = {NULL};
int CharsRead = GetPrivateProfileSection(pSection, AllKeys, BufSize, m_pFilePath);
if (CharsRead)
{
char* ptmp = AllKeys;
char* peq_sign = 0;
while (*ptmp)
{
if (*ptmp == ';') //propuskaem kommentarii
{
ptmp += strlen(ptmp) + 1;
continue;
}
peq_sign = strstr(ptmp, "=");
if (peq_sign)
{
*(peq_sign++) = '\0';
if (Optimize != MINI_STANDART_ALL_VALUE) //MINI_OPTIMIZE_ONLY_VALUE
{ if (!ListStrStr.SearchNodeFromValue(peq_sign)) ListStrStr.AddNode(ptmp, peq_sign); }
else
ListStrStr.AddNode(ptmp, peq_sign);
ptmp = peq_sign + strlen(peq_sign) + 1;
}
else //propuskaem stroki bez znaka '='
ptmp += strlen(ptmp) + 1;
}
}
return CharsRead;
}
int MIniFile::ReadSectionSpecial(const char* pSection, MListStrStr& ListStrStr, int Optimize)
{
if(!m_pFilePath) return false;
const int BufSize = 65536;
char AllKeys[BufSize] = {NULL};
int CharsRead = GetPrivateProfileSection(pSection, AllKeys, BufSize, m_pFilePath);
if (CharsRead)
{
char* ptmp = AllKeys;
char* peq_sign1 = 0;
char* peq_sign2 = 0;
while (*ptmp)
{
if (*ptmp == ';') //propuskaem kommentarii
{
ptmp += strlen(ptmp) + 1;
continue;
}
peq_sign1 = strstr(ptmp, "=");
if (peq_sign1)
{
*(peq_sign1++) = '\0';
peq_sign2 = strstr(peq_sign1, ",");
if (peq_sign2)
{
peq_sign2++;
if (Optimize != MINI_STANDART_ALL_VALUE) //MINI_OPTIMIZE_ONLY_VALUE
{ if (!ListStrStr.SearchNodeFromValue(peq_sign2)) ListStrStr.AddNode(ptmp, peq_sign2); }
else
ListStrStr.AddNode(ptmp, peq_sign2);
ptmp = peq_sign2 + strlen(peq_sign2) + 1;
}
else
{
if (Optimize == MINI_OPTIMIZE_ONLY_VALUE)
{ if (!ListStrStr.SearchNodeFromValue(peq_sign1)) ListStrStr.AddNode(ptmp, peq_sign1); }
else
ListStrStr.AddNode(ptmp, peq_sign1);
ptmp = peq_sign1 + strlen(peq_sign1) + 1;
}
}
else //propuskaem stroki bez znaka '='
ptmp += strlen(ptmp) + 1;
}
}
return CharsRead;
}