-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRCStamp.cpp
546 lines (499 loc) · 14.5 KB
/
RCStamp.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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// RCStamp.cpp : Defines the entry point for the console application.
//
/* My mods:
*
* Oct 2/12 Converted to MSVC 2010 & added code to get its
* own version in.
* Added error message if target file not found.
* Jan 12/09 made keeping file time optional with -t option
* it caused a lot of trouble since it made
* the make process skip the resource compile
* except for a complete rebuild
* Aug 4/07 added mod to save .rc file write time to avoid
* recompile due to updates to version in .rc
* when the rest of the source has not changed
* Left code in main program to avoid dealing with
* all of the possible return paths in ProcessFile()
*
* Aug 3/07 - inital version on my systems
* added mod to fix etra CR/LF at end of .rc file
*
* Note: current debug code uses the work file stamp.rc in the
* main directory for debugging purposes
*/
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <strstream>
#include <string>
#include <windows.h>
#include <stdio.h>
#include <direct.h>
//#include "version.info"
using namespace std;
char const * strUsage1 =
" RCSTAMP - ";
char const * strUsage2 =
" command line:\r\n"
" rcstamp file <g_format> [options...]\r\n"
" rcstamp @file [<g_format>] [options...]\r\n"
"\r\n"
" file : resource script to modify\r\n"
" @file : file containing a list of file(s) (see below)\r\n"
"\r\n"
" g_format: specifies version number changes, e.g. *.*.33.+\r\n"
" * : don't modify position\r\n"
" + : increment position by one\r\n"
" number : set position to this value\r\n"
" \r\n"
" options: \r\n"
" -n : don't update the FileVersion string \r\n"
" (default: set to match the FILEVERSION value)\r\n"
" -rRESNAME: update only version resource with resource id g_resName\r\n"
" (default: update all Version resources)\r\n"
" -l the specified file(s) are file list files\r\n"
" -v Verbose\r\n"
" -t do not change file time\r\n"
" file list files:\r\n"
" can specify one file on each line, e.g.\r\n"
" d:\\sources\\project1\\project1.rc\r\n"
" d:\\sources\\project2\\project2.rc=*.*.*.+\r\n"
"\r\n"
" a g_format in the list file overrides the g_format from the command line\r\n"
" using the -l option, list files themselves can be modified\r\n"
"\r\n";
// command line options:
//
// rcstamp file <g_format> [options...]
// rcstamp @file [<g_format>] [options...]
//
// file : resource script to modify
// @file : file containing a list of file (see below)
//
//
// g_format: specifies verison numbr changes, e.g. *.*.33.+
// * : don't modify
// + : increment by one
// number : set to this value
// P : take from product version
//
// options:
// -n : don't update the FileVersion string
// (by default, set to match the FILEVERSION value)
//
// -rRESNAME: replace version resource with resource id g_resName
// (default: replace
//
// -l the specified file (or files in the file list) are file list files
//
// -v Verbose
//
// file list files:
//
// can specify one file on each line, e.g.
// d:\sources\project1\project1.rc
// d:\sources\project2\project2.rc=*.*.*.+
//
// specifying a g_format overrides the g_format specified on the command line
// using the -l option, list files itself can be modified
//
//
// Command Line Information
char const * scriptFile = NULL; // single resource script (.rc), or name of list file
bool scriptFileList = false; // script file is a file list file
char const * g_format = NULL; // g_format specifier
char const * g_resName = NULL;
bool processListFile = false; // the file(s) specified are list files, not resource files
bool noReplaceString = false; // don't replace fileversion string
bool verbose = false;
bool keepFileTime = false; // keep file time unaltered
bool ParseArg(char const * arg)
{
if (*arg!='-' && *arg!='/')
{
if (scriptFile == NULL)
{
scriptFile = arg;
if (*scriptFile=='@')
{
scriptFileList = true;
++scriptFile;
}
}
else if (g_format == NULL)
{
g_format = arg;
}
else {
cerr << "Unexpected argument\"" << arg << "\"\r\n";
return false;
}
return true;
}
++arg;
char c = tolower(*arg);
if (c=='n')
noReplaceString = true;
else if (c=='r')
g_resName = arg+1;
else if (c=='l')
processListFile = true;
else if (c=='v')
verbose = true;
else if (c=='t')
keepFileTime = true;
else {
cerr << "Unknown option\"" << arg << "\"\r\n";
return false;
}
return true;
}
// ------------------------------------------------------------------
bool CalcNewVersion(char const * oldVersion, char const * fmtstr, char * version)
{
if (!fmtstr)
fmtstr = g_format;
if( strlen( oldVersion ) == 0 )
{
cerr << "Zero length old version\r\n";
return false;
}
char const * fmt[4];
char * fmtDup = strdup(fmtstr);
fmt[0] = strtok(fmtDup, " .,");
fmt[1] = strtok(NULL, " .,");
fmt[2] = strtok(NULL, " .,");
fmt[3] = strtok(NULL, " .,");
if ( (fmt[0] == 0) || (fmt[1] == 0) || (fmt[2] == 0) || (fmt[3] == 0) )
{
cerr << "Invalid format\r\n";
return false;
}
char * outHead = version;
char * verDup = strdup(oldVersion);
char * verStr = strtok(verDup, " ,");
*version = 0;
for(int i=0; i<4; ++i)
{
int oldVersion = atoi(verStr);
int newVersion = oldVersion;
char c = fmt[i][0];
if (strcmp(fmt[i], "*")==0)
newVersion = oldVersion;
else if (isdigit(c))
newVersion = atoi(fmt[i]);
else if (c=='+' || c=='-') {
if (isdigit(fmt[i][1]))
newVersion = oldVersion + atoi(fmt[i]);
else
newVersion = oldVersion + ((c=='+') ? 1 : -1);
}
itoa(newVersion, outHead, 10);
outHead += strlen(outHead);
if (i != 3)
{
strcpy(outHead, ", ");
outHead += 2;
verStr = strtok(NULL, " ,");
}
}
free(fmtDup);
free(verDup);
return true;
}
// ------------------------------------------------------------------
bool ProcessFile(char const * fileName, char const * fmt = NULL)
{
const int MAXLINELEN = 2048;
ifstream is(fileName);
if (is.fail()) {
cerr << "cannot open " << fileName << "\r\n";
return false;
}
string result;
char line[MAXLINELEN];
char version[64] = { 0 }; // "final" version string
bool inReplaceBlock = false;
while (!is.eof())
{
is.getline(line, MAXLINELEN);
if (is.bad()) {
cerr << "Error reading " << fileName << "\r\n";
return false;
}
if (processListFile)
{
char * pos = strchr(line, '=');
if (pos)
{
CalcNewVersion(pos+1, fmt, pos+1); // in-place replace
}
}
else
{
char * dupl = strdup(line);
char * word1 = strtok(dupl, " \t");
char * word2 = strtok(NULL, " \t,"); // allow comma for [VALUE "FileVersion",] entry
if (word1 && word2 && strcmpi(word2, "VERSIONINFO") == 0 && strnicmp(word1, "//",2)!=0)
{
if (g_resName==NULL || strcmpi(g_resName, word1)==0)
inReplaceBlock = true;
else
inReplaceBlock = false;
}
if (inReplaceBlock)
{
if (word1 && strcmpi(word1, "FILEVERSION") == 0)
{
int offset = word1-dupl + strlen("FILEVERSION") + 1;
CalcNewVersion(line+offset, fmt, version);
strcpy(line+offset, version);
if (verbose)
cout << line << "\r\n";
}
#if 1
if (word1 && strcmpi(word1, "PRODUCTVERSION") == 0)
{
int offset = word1-dupl + strlen("PRODUCTVERSION") + 1;
// CalcNewVersion(line+offset, fmt, version);
strcpy(line+offset, version);
if (verbose)
cout << line << "\r\n";
}
#endif
if (!noReplaceString && word1 && word2 &&
strcmpi(word2, "\"FileVersion\"")==0 && strcmpi(word1, "VALUE")==0)
{
if (!*version)
{
cerr << "Error: VALUE \"FileVersion\" encountered before FILEVERSION\r\n";
}
else
{
char * output = strchr(line, ',');
if (!output)
output = line + strlen(line);
else
++output;
sprintf(output, " \"%s\"", version);
if (verbose)
cout << line << "\r\n";
}
}
#if 1
if (!noReplaceString && word1 && word2 &&
strcmpi(word2, "\"ProductVersion\"")==0 && strcmpi(word1, "VALUE")==0)
{
if (!*version)
{
cerr << "Error: VALUE \"ProductVersion\" encountered before PRODUCTVERSION\r\n";
}
else
{
char * output = strchr(line, ',');
if (!output)
output = line + strlen(line);
else
++output;
sprintf(output, " \"%s\"", version);
if (verbose)
cout << line << "\r\n";
}
}
#endif
}
free(dupl);
}
result += line;
// fix addition of extra blank lines at end
if( ! is.eof() )
result += "\n";
}
// re-write file
is.close();
ofstream os(fileName);
if (os.fail())
{
cerr << "Cannot write " << fileName << "\r\n";
return false;
}
os.write( &result[0], result.size());
if (os.fail()) {
cerr << "Error writing " << fileName << "\r\n";
return false;
}
os.close();
return true;
}
// ------------------------------------------------------------------
void GetFileVersionOfApplication( WORD *pMajorVersion,
WORD *pMinorVersion, WORD *pBuildNumber )
{
HMODULE hModule = GetModuleHandleW(NULL);
char path[MAX_PATH];
GetModuleFileName( hModule, path, MAX_PATH );
LPTSTR lpszFilePath =path;
// from: http://vcpptips.wordpress.com/tag/verqueryvalue/
WORD RevisionNumber;
DWORD dwHandle, dwLen;
UINT BufLen;
LPTSTR lpData;
VS_FIXEDFILEINFO *pFileInfo;
dwLen = GetFileVersionInfoSize( lpszFilePath, &dwHandle );
if (!dwLen)
return;// FALSE;
lpData = (LPTSTR) malloc (dwLen);
if (!lpData)
return;// FALSE;
if( !GetFileVersionInfo( lpszFilePath, dwHandle, dwLen, lpData ) )
{
free (lpData);
return;// FALSE;
}
if( VerQueryValue( lpData, "\\", (LPVOID *) &pFileInfo, (PUINT)&BufLen ) )
{
*pMajorVersion = HIWORD(pFileInfo->dwFileVersionMS);
*pMinorVersion = LOWORD(pFileInfo->dwFileVersionMS);
*pBuildNumber = HIWORD(pFileInfo->dwFileVersionLS);
RevisionNumber = LOWORD(pFileInfo->dwFileVersionLS);
free (lpData);
return;// TRUE;
}
free (lpData);
}
// ------------------------------------------------------------------
int main(int argc, char* argv[])
{
WORD wMajorVersion;
WORD wMinorVersion;
WORD wBuildNumber;
GetFileVersionOfApplication( &wMajorVersion, &wMinorVersion, &wBuildNumber);
if (argc == 1)
{
cerr << strUsage1;
cerr << " Version: " << wMajorVersion << '.' << wMinorVersion;
cerr << " - Build: " << wBuildNumber << "\r\n";
cerr << strUsage2;
return 3;
}
for(int i=1; i<argc; ++i)
{
if (!ParseArg(argv[i]))
return 3;
}
if (!scriptFile || (!scriptFileList && !g_format))
{
cerr << "No formatting options specified\r\n";
return 3;
}
if (verbose)
{
cerr << strUsage1;
cerr << " Version: " << wMajorVersion << '.' << wMinorVersion;
cerr << " - Build: " << wBuildNumber << "\r\n";
}
bool errorOccured = false;
if (scriptFileList)
{
const int MAXLINELEN = 2048;
ifstream is(scriptFile);
char line[MAXLINELEN];
while (!is.bad() && !is.eof())
{
is.getline(line, MAXLINELEN);
if (*line==0 || *line == ';')
continue;
if (verbose)
cout << line << "\r\n";
char * eqpos = strchr(line, '=');
char const * fmt = g_format;
if (eqpos) { // '=' found
*eqpos = 0; // Null-terminate the file name
fmt = eqpos + 1;
}
HANDLE hFile = CreateFile( line,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL
);
if( hFile == INVALID_HANDLE_VALUE )
{
errorOccured = true;
continue;
}
// else proceed
FILETIME ftCreate, ftAccess, ftWrite;
if( ! GetFileTime( hFile, &ftCreate, &ftAccess,
&ftWrite ) )
{
errorOccured = true;
CloseHandle( hFile );
continue;
}
CloseHandle( hFile );
ProcessFile(line, fmt);
hFile = CreateFile( line,
GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL
);
if( hFile == INVALID_HANDLE_VALUE )
{
return FALSE;
}
// else proceed
if( ! SetFileTime( hFile, &ftCreate, &ftAccess,
&ftWrite ) )
{
CloseHandle( hFile );
return FALSE;
}
CloseHandle( hFile );
}
}
else
{
HANDLE hFile = CreateFile( scriptFile,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL
);
if( hFile == INVALID_HANDLE_VALUE )
{
cerr << "Cannot create the target file: " << scriptFile <<" \r\n";
return FALSE;
}
// else proceed
FILETIME ftCreate, ftAccess, ftWrite;
if( ! GetFileTime( hFile, &ftCreate, &ftAccess,
&ftWrite ) )
{
CloseHandle( hFile );
return FALSE;
}
CloseHandle( hFile );
errorOccured = ProcessFile(scriptFile, NULL);
hFile = CreateFile( scriptFile,
GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL
);
if( hFile == INVALID_HANDLE_VALUE )
{
return FALSE;
}
// else proceed
if( keepFileTime ) // do we want to keep the file time??
{
if( ! SetFileTime( hFile, &ftCreate, &ftAccess,
&ftWrite ) )
{
CloseHandle( hFile );
return FALSE;
}
CloseHandle( hFile );
}
}
return 0;
}
// ------------------------------- eof ------------------------------