-
Notifications
You must be signed in to change notification settings - Fork 3
/
RegFileReader.cpp
181 lines (152 loc) · 3.31 KB
/
RegFileReader.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
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
177
178
179
180
181
// Copyright (c) 2005-2015 Ross Smith II. See Mit LICENSE in /LICENSE
#ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN 1
#endif
#include <assert.h>
#include <tchar.h>
#include <windows.h>
#include "RegFileReader.h"
RegFileReader::RegFileReader() {
line = const_cast<TCHAR *>(empty_string);
line_len = 0;
_isSection = false;
section = const_cast<TCHAR *>(empty_string);
section_len = 0;
equals_pos = 0;
};
RegFileReader::~RegFileReader() {
};
bool RegFileReader::areValuesEqual(RegFileReader *r2) {
if (_isSection || r2->isSection())
return true;
int len = length();
if (len != r2->length())
return false;
for (int i = equals_pos + 1; i < len; ++i) {
if (line[i] != r2->getChar(i))
return false;
}
return true;
};
INLINE TCHAR RegFileReader::getChar(int n) {
return line[n];
};
INLINE int RegFileReader::getEqualPos() {
return equals_pos;
};
INLINE TCHAR * RegFileReader::getLine() {
return _isSection ? const_cast<TCHAR *>(empty_string) : line;
};
INLINE TCHAR * RegFileReader::getSection() {
return section;
};
INLINE bool RegFileReader::isSection() {
return _isSection;
};
int RegFileReader::keyCompare(RegFileReader *r2) {
if (_isSection) {
if (r2->isSection()) {
return 0;
} else {
return -1;
}
} else {
if (r2->isSection()) {
return 1;
}
}
int len = length();
int len2 = r2->length();
for (int i = 0; i < len, i < len2; ++i) {
if (line[i] == '=') {
equals_pos = i;
if (r2->getChar(i) == '=')
return 0;
else
return -1;
}
if (line[i] == r2->getChar(i))
continue;
if (line[i] > r2->getChar(i))
return 1;
return -1;
}
return len > len2 ? 1 : (len < len2 ? -1 : 0);
};
TCHAR *RegFileReader::readNextLine() {
if (eof()) {
return const_cast<TCHAR *>(empty_string);
}
while (!eof()) {
line = readline();
line_len = length();
if (line_len > 0)
if (line[0] == '[' || line[0] == '@' || line[0] == '"')
break;
}
equals_pos = 0;
if (length() == 0) {
_isSection = false;
return const_cast<TCHAR *>(empty_string);
}
_isSection = line[0] == '[';
if (_isSection) {
section = line;
section_len = length();
}
return line;
};
int RegFileReader::sectionCompare(RegFileReader *r2) {
return _tcsicmp(section, r2->getSection());
}
bool RegFileReader::skipDelSection() {
if (!_isSection)
return false;
int left_bracket_pos = 0;
for (int i = section_len - 1; i >= 0; --i) {
if (section[i] == ']') {
left_bracket_pos = i;
break;
}
}
if (left_bracket_pos == 0)
return false;
while (!eof()) {
line = readline();
if (length() == 0)
continue;
if (line[0] == '[') {
if (_tcsncmp(line, section, left_bracket_pos))
break;
}
}
equals_pos = 0;
line_len = length();
if (line_len > 0) {
_isSection = line[0] == '[';
if (_isSection) {
section = line;
section_len = length();
}
}
return !eof();
};
bool RegFileReader::skipHeader() {
while (!eof()) {
readNextLine();
if (length() > 0)
if (line[0] == '[' || line[0] == '@' || line[0] == '"')
break;
}
return !eof();
};
bool RegFileReader::skipToNextSection() {
while (!eof()) {
readNextLine();
if (eof())
break;
if (_isSection)
break;
}
return !eof();
};