-
Notifications
You must be signed in to change notification settings - Fork 0
/
INI.cpp
120 lines (104 loc) · 3.43 KB
/
INI.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
// IniFile.cpp: implementation of the CIniReader class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "INI.h"
#include <afxcoll.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
// Used to retrieve a value give the section and key
CString CIniReader::getKeyValue(CString strKey,CString strSection)
{
TCHAR ac_Result[MAX_PATH];
// Get the info from the .ini file
m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,(LPCTSTR)strKey,
L"", ac_Result, MAX_PATH, (LPCTSTR)m_strFileName);
return ac_Result;
}
int CIniReader::getKeyValueInt(CString strKey,CString strSection)
{
return _ttoi(getKeyValue(strKey, strSection));
}
float CIniReader::getKeyValueFloat(CString strKey, CString strSection)
{
return (float)_ttof(getKeyValue(strKey, strSection));
}
// Used to add or set a key value pair to a section
long CIniReader::setKey(CString strValue, CString strKey, CString strSection)
{
m_lRetValue = WritePrivateProfileString(strSection, strKey, strValue, m_strFileName);
return m_lRetValue;
}
long CIniReader::setKeyInt(int intValue, CString strKey, CString strSection)
{
CString strValue;
strValue.Format(L"%d", intValue);
return setKey(strValue, strKey, strSection);
}
long CIniReader::setKeyFloat(float floatValue, CString strKey, CString strSection)
{
CString strValue;
strValue.Format(L"%f", floatValue);
return setKey(strValue, strKey, strSection);
}
// Used to find out if a given section exists
BOOL CIniReader::sectionExists(CString strSection)
{
TCHAR ac_Result[100];
CString csAux;
// Get the info from the .ini file
m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,NULL,
L"",ac_Result, 90, (LPCTSTR)m_strFileName);
// Return if we could retrieve any info from that section
return (m_lRetValue > 0);
}
// Used to retrieve all of the section names in the ini file
CStringList* CIniReader::getSectionNames() //returns collection of section names
{
TCHAR ac_Result[2000];
m_sectionList->RemoveAll();
m_lRetValue = GetPrivateProfileSectionNames(ac_Result,2000,(LPCTSTR)m_strFileName);
CString strSectionName;
for(int i=0; i<m_lRetValue; i++)
{
if(ac_Result[i] != '\0') {
strSectionName = strSectionName + ac_Result[i];
} else {
if(strSectionName != "") {
m_sectionList->InsertAfter(m_sectionList->GetTailPosition(),strSectionName);
}
strSectionName = "";
}
}
return m_sectionList;
}
// Used to retrieve all key/value pairs of a given section.
CStringList* CIniReader::getSectionData(CString strSection)
{
TCHAR ac_Result[2000]; //change size depending on needs
m_sectionDataList->RemoveAll();
m_lRetValue = GetPrivateProfileSection((LPCTSTR)strSection, ac_Result, 2000, (LPCTSTR)m_strFileName);
CString strSectionData;
for(int i=0; i<m_lRetValue; i++)
{
if(ac_Result[i] != '\0') {
strSectionData = strSectionData + ac_Result[i];
} else {
if(strSectionData != "") {
m_sectionDataList->InsertAfter(m_sectionDataList->GetTailPosition(),strSectionData);
}
strSectionData = "";
}
}
return m_sectionDataList;
}
void CIniReader::setINIFileName(CString strINIFile)
{
m_strFileName = strINIFile;
}