branch: master
Switch branches/tags
Nothing to show
strictsoft
file 384 lines (331 sloc) 9.772 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include <cstdio>
#include <exception>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <regex>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef vector<string> arguments;
const string version_file_name = ".versionrc";
const string strmajor = "major";
const string strminor = "minor";
const string strdebug = "debug";
const string strbuild = "build";

map<string, long> variables;

bool req_quiet = false;
bool req_init = false;

namespace calc {
long assign();
long pre();
long expression();
long term();
long factor();

string::iterator first, last;
locale l("C");

void skipws() {
while(first != last) {
if(!isspace(*first, l)) break;
first++;
}
}

long main(string src) {
first = begin(src);
last = end(src);
skipws();
return assign();
}

void nextch() {
first++;
skipws();
}

bool is_variable_name() {
return first != last && isalpha(*first, l);
}

string get_variable_name() {
string dst;
while(first != last && isalpha(*first, l)) dst.push_back(*(first++));
skipws();
return dst;
}

string peek_variable_name() {
string::iterator backtrack = first;
string dst = get_variable_name();
first = backtrack;
return dst;
}

long get_variable(string name) {
return variables[name];
}

void set_variable(string name, long value) {
variables[name] = value;
}

string peek_two_char() {
string dst;
dst.push_back(first != last ? *first : ' ');
dst.push_back(first != last && next(first, 1) != last ? *(next(first, 1)) : ' ');
return dst;
}

long number() {
if(is_variable_name()) {
string variable_name = get_variable_name();
long dst = get_variable(variable_name);
if(peek_two_char() == "++") {
first = next(first, 2);
skipws();
set_variable(variable_name, dst + 1);
} else if(peek_two_char() == "--") {
first = next(first, 2);
skipws();
set_variable(variable_name, dst - 1);
}
return dst;
} else {
char sign = first != last ? *first : '\0';
if(sign == '+' || sign == '-') return first++, sign == '-' ? -number() : number();
size_t pos;
string src(first, last);
long dst;
try {
dst = stol(src, &pos);
} catch(invalid_argument &) {
throw exception("invalid expression");
}
first = next(first, pos);
skipws();
return dst;
}
}

long assign() {
if(is_variable_name()) {
string::iterator backtrack = first;
string variable_name = get_variable_name();
if(*first == '=') {
nextch();
long dst = assign();
set_variable(variable_name, dst);
return dst;
} else {
string op = peek_two_char();
if(op == "+=") {
first = next(first, 2);
skipws();
long dst = assign();
set_variable(variable_name, get_variable(variable_name) + dst);
return dst;
} else if(op == "-=") {
first = next(first, 2);
skipws();
long dst = assign();
set_variable(variable_name, get_variable(variable_name) - dst);
return dst;
} else if(op == "*=") {
first = next(first, 2);
skipws();
long dst = assign();
set_variable(variable_name, get_variable(variable_name) * dst);
return dst;
} else if(op == "/=") {
first = next(first, 2);
skipws();
long dst = assign();
set_variable(variable_name, get_variable(variable_name) / dst);
return dst;
} else if(op == "%=") {
first = next(first, 2);
skipws();
long dst = assign();
set_variable(variable_name, get_variable(variable_name) % dst);
return dst;
} else {
first = backtrack;
return pre();
}
}
} else {
return pre();
}
}

long pre() {
if(peek_two_char() == "++") {
first = next(first, 2);
skipws();
if(!is_variable_name()) throw exception("can't pre-increment");
string variable_name = peek_variable_name();
set_variable(variable_name, get_variable(variable_name) + 1);
} else if(peek_two_char() == "--") {
first = next(first, 2);
skipws();
if(!is_variable_name()) throw exception("can't pre-decrement");
string variable_name = peek_variable_name();
set_variable(variable_name, get_variable(variable_name) - 1);
}
return expression();
}

long expression() {
long dst = term();
while(true) {
if(*first == '+') nextch(), dst += term();
else if(*first == '-') nextch(), dst -= term();
else break;
}
return dst;
}

long term() {
long dst = factor();
while(true) {
if(*first == '*') nextch(), dst *= factor();
else if(*first == '/') nextch(), dst /= factor();
else if(*first == '%') nextch(), dst %= factor();
else break;
}
return dst;
}

long factor() {
if(*first == '(') {
nextch();
long dst = assign();
if(*first != ')') throw exception("missing ')'");
nextch();
return dst;
} else {
return number();
}
}
}

namespace command {
struct entry {
string name;
arguments::const_iterator(*function)(arguments::const_iterator, arguments::const_iterator);
};

const entry * search(string name);

template<typename T> T command_internal_error(T first, T last) {
throw exception((*first + "is not a command").c_str());
}

template<typename T> T command_evaluation(T first, T last) {
calc::main(*first);
return ++first;
}

template<typename T> T command_help(T first, T last) {
cout <<
"usage: version [-h] [-q] [-i] expression\n"
"\n"
" format: major.minor.debug.build\n"
" example:\n"
" version build++\n"
" version debug=0 minor+=1\n"
" version major=major+1\n"
"\n"
" options:\n"
" -h this message\n"
" -q quiet mode\n"
" -i initialize .versionrc file\n"
" -C print C language header\n"
" -N print nmake.exe format\n"
<< ends;
req_quiet = true;
return ++first;
}

template<typename T> T command_quiet(T first, T last) {
req_quiet = true;
return ++first;
}

template<typename T> T command_init(T first, T last) {
req_init = true;
return ++first;
}

template<typename T> T command_clang(T first, T last) {
cout <<
"#pragma once\n" <<
"#define VERSION_MAJOR " << variables[strmajor] << endl <<
"#define VERSION_MINOR " << variables[strminor] << endl <<
"#define VERSION_DEBUG " << variables[strdebug] << endl <<
"#define VERSION_BUILD " << variables[strbuild] << endl <<
ends;
req_quiet = true;
return ++first;
}

template<typename T> T command_nmake(T first, T last) {
cout <<
"VERSION_MAJOR = " << variables[strmajor] << endl <<
"VERSION_MINOR = " << variables[strminor] << endl <<
"VERSION_DEBUG = " << variables[strdebug] << endl <<
"VERSION_BUILD = " << variables[strbuild] << endl <<
ends;
req_quiet = true;
return ++first;
}

const entry table[] = {
{"internal_error", command_internal_error},
{"evaluation", command_evaluation},
{"-h", command_help},
{"-q", command_quiet},
{"-i", command_init},
{"-C", command_clang},
{"-N", command_nmake},
};

const auto size = sizeof(table) / sizeof(*table);
const entry * begin = table;
const entry * end = table + size;

const entry * search(string name) {
auto I = find_if(command::begin, command::end, [name](const command::entry & I) -> bool {return I.name == name;});
return I != command::end ? I : search("internal_error");
}

const entry * search_and_eval(string name) {
auto I = find_if(command::begin, command::end, [name](const command::entry & I) -> bool {return I.name == name;});
return I != command::end ? I : search("evaluation");
}
}

tuple<long, long, long, long> read_version_txt(string filename) {
ifstream in(filename);
if(!in) throw exception("file not found");

string version;
getline(in, version);
if(!in) throw exception("file read error");

smatch matches;
regex_search(version, matches, regex("([+-]?\\d+)\\.([+-]?\\d+)\\.([+-]?\\d+)\\.([+-]?\\d+)"));
if(matches.size() != 5) throw exception((string("invalid format in ") + filename).c_str());

return make_tuple(stol(matches[1].str()), stol(matches[2].str()), stol(matches[3].str()), stol(matches[4].str()));
}

void write_version_txt(string filename, long major, long minor, long debug, long build) {
string tempname = filename + ".temp";

ofstream out(tempname);
out << major << "." << minor << "." << debug << "." << build << endl;
out.close();
if(!out) throw exception("file write error");

if(remove(filename.c_str()) != 0) throw exception((filename + " can't remove").c_str());

if(rename(tempname.c_str(), filename.c_str()) != 0) throw exception((tempname + " can't rename").c_str());
}

void print_version(long major, long minor, long debug, long build) {
cout << major << "." << minor << "." << debug << "." << build << endl;
}

int main(int argc, char * argv[]) {
try {
arguments args(&argv[1], &argv[argc]);

long major, minor, debug, build;
tie(major, minor, debug, build) = read_version_txt(version_file_name);
if(args.empty()) return print_version(major, minor, debug, build), EXIT_SUCCESS;

variables[strmajor] = major;
variables[strminor] = minor;
variables[strdebug] = debug;
variables[strbuild] = build;

arguments::const_iterator first = begin(args), last = end(args);
while(first != last) first = (*(command::search_and_eval(*first)->function))(first, last);

if(req_init) return write_version_txt(version_file_name, 0, 0, 0, 0), EXIT_SUCCESS;

major = variables[strmajor];
minor = variables[strminor];
debug = variables[strdebug];
build = variables[strbuild];
write_version_txt(version_file_name, major, minor, debug, build);

if(!req_quiet) print_version(major, minor, debug, build);

return EXIT_SUCCESS;

} catch(const exception & x) {
cout << x.what() << endl;
}
return EXIT_FAILURE;
}

// (c) 2012 Strictsoft

Markdown Cheat Sheet

Format Text

Headers

# This is an <h1> tag
## This is an <h2> tag
###### This is an <h6> tag

Text styles

*This text will be italic*
_This will also be italic_
**This text will be bold**
__This will also be bold__

*You **can** combine them*

Lists

Unordered

* Item 1
* Item 2
  * Item 2a
  * Item 2b

Ordered

1. Item 1
2. Item 2
3. Item 3
   * Item 3a
   * Item 3b

Miscellaneous

Images

![GitHub Logo](/images/logo.png)
Format: ![Alt Text](url)

Links

http://github.com - automatic!
[GitHub](http://github.com)

Blockquotes

As Kanye West said:

> We're living the future so
> the present is our past.

Code Examples in Markdown

Syntax highlighting with GFM

```javascript
function fancyAlert(arg) {
  if(arg) {
    $.facebox({div:'#foo'})
  }
}
```

Or, indent your code 4 spaces

Here is a Python code example
without syntax highlighting:

    def foo:
      if not bar:
        return true

Inline code for comments

I think you should use an
`<addr>` element here instead.
Something went wrong with that request. Please try again.

Looking for the GitHub logo?