-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoVersion.h
332 lines (274 loc) · 9.42 KB
/
AutoVersion.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* autoversion.h
* Copyright (C) 2024 T. Radde
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _AUTOVERSION_H_
#define _AUTOVERSION_H_
// ========================================================================
// Globals
// ========================================================================
extern bool g_bVerbose; // program is verbose
// ========================================================================
// ToString
// ========================================================================
template<class T>
string ToString(const T& val)
{
std::stringstream strm;
strm << val;
return strm.str();
}
// ===============================================================================
// class CException
// ===============================================================================
class CException : public exception
{
protected:
string m_strMessage;
public:
CException(const string &message)
{
m_strMessage = message;
}
virtual ~CException() throw() {}
virtual const char* what() const throw()
{
return m_strMessage.c_str();
}
};
// ===============================================================================
// class CParseException
// ===============================================================================
class CParseException : public exception
{
protected:
string m_strMessage;
public:
CParseException(const string &message, int line_number)
{
m_strMessage = message + " at line " + ToString(line_number);
}
virtual ~CParseException() throw() {}
virtual const char* what() const throw()
{
return m_strMessage.c_str();
}
};
// ===============================================================================
// class CReplace
//
// A single replacement operation.
// ===============================================================================
enum EReplaceOp
{
enRoText, // text replacement
enRoBinary, // binary replacement
};
class CReplace
{
protected:
EReplaceOp m_enReplaceOp; // the operation, either text or binary
string m_strWhat; // what to replace
string m_strWith; // to replace with
bool m_bMustReplace; // true if "what" was found
bool m_bDidReplace; // true if replacement was done
public:
size_t m_nControlFilePos; // offset-position (in bytes) within the Control File, where the "what" string is found
// this is used to update the Control File
public:
CReplace(EReplaceOp op, const string &what, const string &with, size_t nControlFilePos)
{
m_enReplaceOp = op;
m_strWhat = what;
m_strWith = with;
m_nControlFilePos = nControlFilePos;
m_bMustReplace = false;
m_bDidReplace = false;
}
bool CheckReplace(const string &file_name, char *buf, size_t size); // checks, if a replacement will occur
char *DoReplace(char *buf, size_t &size); // performs the replacement
char *UpdateControlFile(char *buf, size_t &size, int &offset); // Alle Replacements auf das Control File anwenden
#ifdef _DEBUG
void Dump() // show parsed structures of Control File
{
string op;
if (m_enReplaceOp == enRoText)
op = "text";
else if (m_enReplaceOp == enRoBinary)
op = "binary";
else
throw CException("unknown type of m_enReplaceOp");
printf("Type: %s --- What: %s --- With: %s --- Must Replace: %s --- Did Replace: %s\n",
op.c_str(),
m_strWhat.c_str(),
m_strWith.c_str(),
m_bMustReplace ? "yes" : "no",
m_bDidReplace ? "yes" : "no");
}
#endif
};
// ===============================================================================
// class CFileNode
//
// This represents a file where replacements shall be performed.
// All replacement operations for a single file are held in a list here.
// ===============================================================================
class CFileNode
{
protected:
list<CReplace> m_listReplacements; // All replacement operations for a single file are held in a list here.
bool m_bMustReplace; // true if anything must be replaced in this file
bool m_bDidReplace; // true if replacement was done
public:
CFileNode()
{
m_bMustReplace = false;
m_bDidReplace = false;
}
list<CReplace> &GetReplacements() { return m_listReplacements; }
void Add(CReplace r) { m_listReplacements.push_back(r); }
bool CheckReplacements(const string &file_name); // checks, if any replacement for this file will occur
void DoReplacments(const string &file_name); // performs all replacements for this file
void Rollback(const string &file_name); // performs a rollback for this file
#ifdef _DEBUG
void Dump() // show parsed structures of Control File
{
printf("Must Replace %s\n", m_bMustReplace ? "yes" : "no");
printf("Did Replace %s\n", m_bDidReplace ? "yes" : "no");
for (auto it : m_listReplacements)
it.Dump();
}
#endif
};
// ===============================================================================
// class CCommand
// ===============================================================================
class CCommand
{
protected:
list<string> m_listArgs; // arguments
public:
void AddArg(const string &s)
{
m_listArgs.push_back(s);
}
virtual bool Execute() = 0;
};
// ===============================================================================
// class CCommandShell
// ===============================================================================
class CCommandShell : public CCommand
{
public:
virtual bool Execute() override
{
string cmd = m_listArgs.front();
if (g_bVerbose)
printf("shell: %s\n", cmd.c_str());
int ret = system(cmd.c_str());
return ret == 0;
}
};
// ===============================================================================
// class CAutoVersion
// ===============================================================================
class CAutoVersion
{
protected:
bool m_bInteractive; // program is interactive, if false, all questions are answered by default with yes
string m_strControlFile; // the name of the Control File
bool m_bControlFileSaved;// true, if a rollback file for the Control File was created
string m_strBasePath; // the base path, see %Basepath command in Control File
int m_nCurrentLine; // Current Line number while parsing Control File
char *m_pBuffer; // holds the Control File while parsing
unordered_set<string> m_setDefines; // defines through -d switch
unordered_map<string, string> m_mapConstantDefs; // definitions of constants in Control File
unordered_map<string, CFileNode> m_mapFiles; // the files listed in the Control File
list<string> m_listMessages; // messages in the Control File
list<CCommandShell> m_listDelayedCommands; // Commands executed after replacement has done, e.g. "copy"
void SkipWhiteSpaces(char *&p);
void SkipLine(char *&p, bool only_white_spaces = true);
void SkipComment(char *&p);
void Scan(char *&p, const char *token);
string GetIdentifier(char *&p);
string GetLiteral(char *&p, size_t *offset = NULL);
string GetLiteralOrSymbol(char *&p);
void ParseConstantDef(char *&p);
void ParseReplacement(EReplaceOp op, char *&p);
void ParseMessage(char *&p);
void ParseCommand(char *&p);
void UpdateControlFile();
public:
CAutoVersion()
{
m_bInteractive = true;
m_bControlFileSaved = false;
m_nCurrentLine = 1;
m_pBuffer = NULL;
}
bool GetInteractive() const { return m_bInteractive; }
void SetInteractive(bool val) { m_bInteractive = val; }
void AddDefine(const string &d) { m_setDefines.insert(d); }
const string &GetControlFile() const { return m_strControlFile; }
void SetControlFile(const string &val) { m_strControlFile = val; }
int GetCurrentLine() const { return m_nCurrentLine; }
void ParseControlFile();
void Replace();
void RescueRollback();
void Rollback();
void Clean();
void ShowMessages()
{
printf("\n\n");
for (auto it : m_listMessages)
printf("%s\n", it.c_str());
}
void ExecDelayedCommands()
{
if (g_bVerbose && m_listDelayedCommands.size() > 0)
printf("\nexecuting delayed commands\n");
for (auto it : m_listDelayedCommands)
{
if (!it.Execute())
throw CException("a delayed command failed");
}
}
#ifdef _DEBUG
void Dump() // show parsed structures of Control File
{
printf("Verbose %s\n", g_bVerbose ? "yes" : "no");
printf("Interactive %s\n", m_bInteractive ? "yes" : "no");
printf("Control File %s\n", m_strControlFile.c_str());
printf("Base Path %s\n", m_strBasePath.c_str());
printf("\nCommand-Line Defines:\n");
for (auto it : m_setDefines)
printf("%s\n", it.c_str());
printf("\nConstants:\n");
for (auto it : m_mapConstantDefs)
printf("%s\t\t%s\n", it.first.c_str(), it.second.c_str());
printf("\nReplacement-Definitions:\n");
for (auto it : m_mapFiles)
{
printf("\nFile: %s\n", it.first.c_str());
it.second.Dump();
}
printf("\nMessages:\n");
for (auto it : m_listMessages)
printf("%s\n", it.c_str());
}
#endif
};
#endif // _AUTOVERSION_H_