-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathpe_ConfigFile.h
More file actions
144 lines (120 loc) · 2.28 KB
/
pe_ConfigFile.h
File metadata and controls
144 lines (120 loc) · 2.28 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
#pragma once
#include <vector>
#include "pe_File.h"
#define CUBECFGFILE_GUID_LEN 38
#define CUBECFGFILE_FILENAME_LEN 255
using namespace std;
template <typename ConfigStruct>
class pe_ConfigFile
{
public:
pe_ConfigFile()
{
m_FileName[0]='\0';
m_StructSize=sizeof(ConfigStruct);
memset(m_GUID,0,sizeof m_GUID);
}
void SetGUID(const char *GUID)
{
memcpy(m_GUID,GUID,CUBECFGFILE_GUID_LEN);
}
BOOL LoadCfgFile(const char *FileName)
{
ConfigStruct st;
Free();
strcpy(m_FileName,FileName);
if(!m_file.Open((char *)FileName))
{
return FALSE;
}
char GUID[CUBECFGFILE_GUID_LEN];
m_file.Read(GUID,CUBECFGFILE_GUID_LEN);
if (strcmp(GUID,m_GUID)!=0)
{
goto _ERROR;
}
unsigned int Count;
if(m_file.Read(&Count,sizeof(unsigned int))!=sizeof(unsigned int))
goto _ERROR;
for (unsigned int i=0;i<Count;i++)
{
if (m_file.Read(&st,sizeof(ConfigStruct))!=sizeof(ConfigStruct))
{
goto _ERROR;
}
m_vStruct.push_back(st);
}
m_file.Close();
m_file.Free();
return TRUE;
_ERROR:
m_vStruct.clear();
m_file.Close();
m_file.Free();
return FALSE;
}
ConfigStruct &GetConfigStruct(int Index)
{
return m_vStruct.at(Index);
}
int GetConfigStructCount()
{
return m_vStruct.size();
}
void Add(ConfigStruct T)
{
m_vStruct.push_back(T);
Update();
}
void Remove(int Index)
{
if (Index>m_vStruct.size()-1)
{
return;
}
m_vStruct.erase(m_vStruct.begin()+Index);
Update();
}
void Clear()
{
m_vStruct.clear();
}
BOOL Update()
{
if(!m_file.Open(m_FileName,"wb"))
return FALSE;
if(m_file.Write(m_GUID,CUBECFGFILE_GUID_LEN)!=CUBECFGFILE_GUID_LEN)
goto _ERROR;
int Count=m_vStruct.size();
if(m_file.Write(&Count,sizeof(unsigned int))!=sizeof(unsigned int))
goto _ERROR;
for(int i=0;i<Count;i++)
{
if (m_file.Write(&m_vStruct.at(i),sizeof(ConfigStruct))!=sizeof(ConfigStruct))
{
goto _ERROR;
}
}
m_file.Close();
m_file.Free();
return TRUE;
_ERROR:
m_file.Close();
m_file.Free();
return FALSE;
}
void Free()
{
m_vStruct.clear();
m_file.Free();
m_StructSize=0;
memset(m_GUID,0,sizeof m_GUID);
}
protected:
private:
pe_File m_file;
int m_StructSize;
vector<ConfigStruct> m_vStruct;
char m_FileName[CUBECFGFILE_FILENAME_LEN];
char m_GUID[CUBECFGFILE_GUID_LEN];
};