-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIniFile.h
134 lines (116 loc) · 4.02 KB
/
IniFile.h
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
#pragma once
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <functional>
using namespace std;
// A function to trim whitespace from both sides of a given tstring
inline void Trim(tstring& str, const tstring & ChrsToTrim = _T(" \t\n\r"), int TrimDir = 0)
{
size_t startIndex = str.find_first_not_of(ChrsToTrim);
if (startIndex == tstring::npos){str.erase(); return;}
if (TrimDir < 2) str = str.substr(startIndex, str.size()-startIndex);
if (TrimDir!=1) str = str.substr(0, str.find_last_not_of(ChrsToTrim) + 1);
}
class CIniFile
{
public:
struct Record
{
tstring Comments;
TCHAR Commented;
tstring Section;
tstring Key;
tstring Value;
};
enum CommentChar
{
Pound = '#',
SemiColon = ';'
};
CIniFile(void);
virtual ~CIniFile(void);
static bool AddSection(tstring SectionName, tstring FileName);
static bool CommentRecord(CommentChar cc, tstring KeyName,tstring SectionName,tstring FileName);
static bool CommentSection(char CommentChar, tstring SectionName, tstring FileName);
static tstring Content(tstring FileName);
static bool Create(tstring FileName);
static bool DeleteRecord(tstring KeyName, tstring SectionName, tstring FileName);
static bool DeleteSection(tstring SectionName, tstring FileName);
static vector<Record> GetRecord(tstring KeyName, tstring SectionName, tstring FileName);
static vector<Record> GetSection(tstring SectionName, tstring FileName);
static vector<tstring> GetSectionNames(tstring FileName);
static tstring GetValue(tstring KeyName, tstring SectionName, tstring FileName);
static bool RecordExists(tstring KeyName, tstring SectionName, tstring FileName);
static bool RenameSection(tstring OldSectionName, tstring NewSectionName, tstring FileName);
static bool SectionExists(tstring SectionName, tstring FileName);
static bool SetRecordComments(tstring Comments, tstring KeyName, tstring SectionName, tstring FileName);
static bool SetSectionComments(tstring Comments, tstring SectionName, tstring FileName);
static bool SetValue(tstring KeyName, tstring Value, tstring SectionName, tstring FileName);
static bool Sort(tstring FileName, bool Descending);
static bool UnCommentRecord(tstring KeyName,tstring SectionName,tstring FileName);
static bool UnCommentSection(tstring SectionName, tstring FileName);
private:
static vector<Record> GetSections(tstring FileName);
static bool Load(tstring FileName, vector<Record>& content);
static bool Save(tstring FileName, vector<Record>& content);
struct RecordSectionIs : std::unary_function<Record, bool>
{
tstring section_;
RecordSectionIs(const tstring& section): section_(section){}
bool operator()( const Record& rec ) const
{
return rec.Section == section_;
}
};
struct RecordSectionKeyIs : std::unary_function<Record, bool>
{
tstring section_;
tstring key_;
RecordSectionKeyIs(const tstring& section, const tstring& key): section_(section),key_(key){}
bool operator()( const Record& rec ) const
{
tstring s(rec.Section.c_str()), k(rec.Key.c_str());
tstring s1(section_.c_str()), k1(key_.c_str());
std::transform(s.begin(), s.end(), s.begin(), tolower);
std::transform(k.begin(), k.end(), k.begin(), tolower);
std::transform(s1.begin(), s1.end(), s1.begin(), tolower);
std::transform(k1.begin(), k1.end(), k1.begin(), tolower);
Trim(s);
Trim(s1);
Trim(k);
Trim(k1);
return ((s == s1)&&(k == k1));
}
};
struct AscendingSectionSort
{
bool operator()(Record& Start, Record& End)
{
return Start.Section < End.Section;
}
};
struct DescendingSectionSort
{
bool operator()(Record& Start, Record& End)
{
return Start.Section > End.Section;
}
};
struct AscendingRecordSort
{
bool operator()(Record& Start, Record& End)
{
return Start.Key < End.Key;
}
};
struct DescendingRecordSort
{
bool operator()(Record& Start, Record& End)
{
return Start.Key > End.Key;
}
};
};