-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathINCRVER.cpp
1198 lines (946 loc) · 33.1 KB
/
INCRVER.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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////////////
// //
// ___ _ _ ____ ____ __ __ _____ ____ //
// |_ _|| \ | | / ___|| _ \\ \ / /| ____|| _ \ ___ _ __ _ __ //
// | | | \| || | | |_) |\ \ / / | _| | |_) | / __|| '_ \ | '_ \ //
// | | | |\ || |___ | _ < \ V / | |___ | _ < _| (__ | |_) || |_) | //
// |___||_| \_| \____||_| \_\ \_/ |_____||_| \_\(_)\___|| .__/ | .__/ //
// |_| |_| //
// //
//////////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2016 by S.F.T. Inc. - All rights reserved //
// Use, copying, and distribution of this software are licensed according //
// to the the 2-clause BSD license specified in LICENSE //
// //
//////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "INCRVER.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CString GetVersionFromFileName(LPCSTR szName);
CString TodaysDateStr(void);
const char szHelpMessage[]=
"COMMAND LINE:\n"
"INCRVER.EXE [-?|H|V:n.n.n.n|R:\"rcfilename\"] RC FILE NAME\n"
" where -? or -H displays this message\n"
" and -V:n.n.n.n sets the version string to 'n.n.n.n'\n"
" and -R:\"rcfilename\" extracts the version from \"rcfilename\"\n"
" and RC FILE NAME represents the RC file name\n"
" (the RC file name does not need to be quoted if it contains spaces)";
/////////////////////////////////////////////////////////////////////////////
// CINCRVERApp
BEGIN_MESSAGE_MAP(CINCRVERApp, CWinApp)
//{{AFX_MSG_MAP(CINCRVERApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CINCRVERApp construction
CINCRVERApp::CINCRVERApp()
{
m_bSetVersion = FALSE;
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CINCRVERApp object
CINCRVERApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CINCRVERApp initialization
BOOL CINCRVERApp::InitInstance()
{
int i1, iBeginEndCount;
CString csFileName = GetFileNameAndOptions();
if(!csFileName.GetLength())
{
::MessageBox(NULL, szHelpMessage, m_pszAppName,
MB_OK | MB_ICONHAND | MB_SETFOREGROUND);
return FALSE;
}
CString csNewVersion = m_csNewVersion; // TODO: fix this
CString csNewVersion0 = m_csNewVersion0; // TODO: fix this
BOOL bSetVersion = m_bSetVersion; // TODO: fix this
// open specified file name, search for 'VF_VERSIONINFO' in the
// script. When found, interpret each line and re-write to same
// file with updated version information.
CopyFile(csFileName, csFileName + ".bak", FALSE); // ignore the return value
// making a copy of the RC file in case the power goes out during the process
// TODO: verify that '.bak' file matches the original
HANDLE hFile = CreateFile(csFileName, GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
INVALID_HANDLE_VALUE);
if(hFile == INVALID_HANDLE_VALUE)
{
::MessageBox(NULL, "File open error on \"" + csFileName + "\"",
m_pszAppName, MB_OK | MB_ICONHAND | MB_SETFOREGROUND);
return FALSE;
}
DWORD cbBytes, dwLen;
BOOL bIsINF;
// differentiate between INF files and RC files
bIsINF = !(csFileName.Right(4).CompareNoCase(".INF"));
dwLen = SetFilePointer(hFile, 0, NULL, FILE_END);
SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
LPSTR lpBuf = (LPSTR)GlobalAlloc(GPTR, dwLen * 3 + 0x10000);
if(!lpBuf)
{
::MessageBox(NULL, "Not enough memory to complete operation",
m_pszAppName, MB_OK | MB_ICONHAND | MB_SETFOREGROUND);
goto error_exit;
}
if(!dwLen ||
!ReadFile(hFile, lpBuf, dwLen, &cbBytes, NULL) ||
cbBytes != dwLen)
{
::MessageBox(NULL, "Read error on input file",
m_pszAppName, MB_OK | MB_ICONHAND | MB_SETFOREGROUND);
goto error_exit;
}
LPSTR lpBuf1 = lpBuf;
LPSTR lpBuf2 = lpBuf + dwLen;
BOOL bUnicode = FALSE;
LPSTR lp1=lpBuf1, lp2=lpBuf2, lp3, lp4;
if(dwLen > 1 &&
(unsigned char)lpBuf[0] == 0xff &&
(unsigned char)lpBuf[1] == 0xfe)
{
bUnicode = TRUE;
*(lp2++) = lpBuf[0]; // I'll copy the first WCHAR that indicates it's a UNICODE file
*(lp2++) = lpBuf[1]; // and then skip it for later as 'already processed'
lp1 += sizeof(WCHAR); // skip past the first char on input
}
DWORD dwPos = 0;
BOOL bChangedFlag = FALSE, bVersionInfoFlag=FALSE;
iBeginEndCount = 0;
while((DWORD)(lp1 - lpBuf1) < (bUnicode ? dwLen - 1 : dwLen))
{
if((!bUnicode && !*lp1) ||
(bUnicode && !*((WCHAR *)lp1)))
{
break;
}
CString csTemp, csTemp2, csTemp3; // declare here (should re-init to "")
// otherwise I get this bizarre warning because of 'error_exit'
lp3 = lp1;
if(bUnicode)
{
while((DWORD)(lp3 - lpBuf1) < (dwLen - 1) &&
*((WCHAR *)lp3) &&
*((WCHAR *)lp3) != '\r' &&
*((WCHAR *)lp3) != '\n' &&
*((WCHAR *)lp3) != '\xd')
{
lp3 += sizeof(WCHAR);
}
}
else
{
while((DWORD)(lp3 - lpBuf1) < dwLen &&
*lp3 &&
*lp3 != '\r' &&
*lp3 != '\n' &&
*lp3 != '\xd')
{
lp3++;
}
}
// make a copy of the line (as is)
if(bUnicode)
{
lp4 = csTemp2.GetBufferSetLength(lp3 - lp1 + 2);
memcpy(lp4, lp1, lp3 - lp1);
lp4[lp3 - lp1] = 0;
lp4[(lp3 - lp1) + 1] = 0;
csTemp = (BSTR)lp4; // converts to ASCII
csTemp2.ReleaseBuffer(0);
}
else
{
lp4 = csTemp.GetBufferSetLength(lp3 - lp1 + 1);
memcpy(lp4, lp1, lp3 - lp1);
lp4[lp3 - lp1] = 0;
csTemp.ReleaseBuffer(-1);
}
// trim lead white space as a copy in 'csTemp2'
csTemp2 = csTemp;
csTemp2.TrimLeft();
if(bIsINF) // INF files are different - I need a date,version
{
if(!csTemp2.Left(9).CompareNoCase("DriverVer"))
{
// the 'DriverVer' string looks like this (typically)
// DriverVer = 04/04/2016,6.01.7600.16385 [white space optional]
csTemp2 = csTemp2.Mid(9); // trim off 'DriverVer' from original
csTemp2.TrimLeft();
if(csTemp2.GetLength() > 0 && csTemp2.GetAt(0) == '=')
{
csTemp2 = csTemp2.Mid(1);
csTemp2.TrimLeft();
// csTemp2 should have date,version now
// I shall re-construct this with today's date and the incremented version
if(bSetVersion)
{
for(i1=0; i1 < csNewVersion.GetLength(); i1++)
{
if(csNewVersion.GetAt(i1) == ',')
{
csTemp3 += ".";
}
else if(csNewVersion.GetAt(i1) > ' ') // not white space
{
csTemp3 += csNewVersion.GetAt(i1);
}
}
csTemp = "DriverVer = "
+ TodaysDateStr()
+ ","
+ csTemp3; // CRLF will be added later
}
else
{
i1 = csTemp2.ReverseFind('.');
csTemp3.Format("%d", atoi(csTemp2.Mid(i1 + 1)) + 1);
csTemp2 = csTemp2.Left(i1 + 1) + csTemp3; // incremented version
i1 = csTemp2.Find(',');
if(i1 >= 0)
{
csTemp2 = csTemp2.Mid(i1 + 1);
}
csTemp = "Driverver = "
+ TodaysDateStr()
+ ","
+ csTemp2; // CRLF will be added later
}
if(bUnicode)
{
BSTR bstrTemp = csTemp.AllocSysString();
if(!bstrTemp)
{
::MessageBoxA(NULL, "ERROR: unable to 'AllocSysString' (output file may be inconsistent)",
m_pszAppName, MB_OK | MB_ICONHAND | MB_SETFOREGROUND);
goto error_exit;
}
else
{
i1 = lstrlenW(bstrTemp) * sizeof(WCHAR);
memcpy(lp2, (LPCSTR)bstrTemp, i1);
lp2 += i1;
SysFreeString(bstrTemp);
bChangedFlag = TRUE;
}
}
else
{
memcpy(lp2, (LPCSTR)csTemp, csTemp.GetLength());
lp2 += csTemp.GetLength();
bChangedFlag = TRUE;
}
lp1 = lp3; // this keeps me from re-copying the same thing
}
}
}
else if(bVersionInfoFlag) // am I looking through version info?
{
// version info has nested begin/end, but when nesting completes
// the version info is complete. Look for the final 'END'
if(!iBeginEndCount &&
(!csTemp2.Left(12).CompareNoCase("FILEVERSION ") ||
!csTemp2.Left(15).CompareNoCase("PRODUCTVERSION ")) )
{
// last number in string is version to update - do it!
if(bSetVersion)
{
i1 = csTemp.ReverseFind(',');
if(i1 > 0)
{
csTemp = csTemp.Left(i1);
i1 = csTemp.ReverseFind(',');
if(i1 > 0)
{
csTemp = csTemp.Left(i1);
i1 = csTemp.ReverseFind(',');
if(i1 > 0)
{
csTemp = csTemp.Left(i1);
csTemp.TrimRight();
while(csTemp.GetLength() &&
csTemp[csTemp.GetLength() - 1] >= '0' &&
csTemp[csTemp.GetLength() - 1] <= '9')
{
csTemp = csTemp.Left(csTemp.GetLength() - 1); // trim down to white space
}
if(csTemp.GetLength())
{
csTemp = csTemp + csNewVersion;
if(bUnicode)
{
BSTR bstrTemp = csTemp.AllocSysString();
if(!bstrTemp)
{
::MessageBoxA(NULL, "ERROR: unable to 'AllocSysString' (output file may be inconsistent)",
m_pszAppName, MB_OK | MB_ICONHAND | MB_SETFOREGROUND);
goto error_exit;
}
else
{
i1 = lstrlenW(bstrTemp) * sizeof(WCHAR);
memcpy(lp2, (LPCSTR)bstrTemp, i1);
lp2 += i1;
SysFreeString(bstrTemp);
bChangedFlag = TRUE;
}
}
else
{
memcpy(lp2, (LPCSTR)csTemp, csTemp.GetLength());
lp2 += csTemp.GetLength();
bChangedFlag = TRUE;
}
lp1 = lp3; // this keeps me from re-copying the same thing
}
}
}
}
}
else
{
i1 = csTemp.ReverseFind(',');
if(i1 > 0)
{
csTemp2 = csTemp.Mid(i1 + 1); // exclude ','
csTemp = csTemp.Left(i1 + 1); // include ','
while(csTemp2.GetLength() && csTemp2.Left(1) <= ' ')
csTemp2 = csTemp2.Mid(1);
i1 = atoi(csTemp2) + 1;
csTemp2.Format("%d", i1);
csTemp += csTemp2; // re-built line, eh??
if(bUnicode)
{
BSTR bstrTemp = csTemp.AllocSysString();
if(!bstrTemp)
{
::MessageBoxA(NULL, "ERROR: unable to 'AllocSysString' (output file may be inconsistent)",
m_pszAppName, MB_OK | MB_ICONHAND | MB_SETFOREGROUND);
goto error_exit;
}
else
{
i1 = lstrlenW(bstrTemp) * sizeof(WCHAR);
memcpy(lp2, (LPCSTR)bstrTemp, i1);
lp2 += i1;
SysFreeString(bstrTemp);
bChangedFlag = TRUE;
}
}
else
{
memcpy(lp2, (LPCSTR)csTemp, csTemp.GetLength());
lp2 += csTemp.GetLength();
bChangedFlag = TRUE;
}
lp1 = lp3; // this keeps me from re-copying the same thing
}
}
}
else if(iBeginEndCount &&
!csTemp2.Left(6).CompareNoCase("VALUE "))
{
// one of the 'value' items that I need to update?
csTemp2 = csTemp2.Mid(6); // skip all of the crap
while(csTemp2.GetLength() && csTemp2.Left(1) <= ' ')
csTemp2 = csTemp2.Mid(1);
if(!csTemp2.Left(i1 = 13).CompareNoCase("\"FileVersion\"") ||
!csTemp2.Left(i1 = 16).CompareNoCase("\"ProductVersion\"") )
{
// re-build the first part of the string, then increment the
// version information and tack it onto the end, nicely formatted
csTemp2 = csTemp2.Left(i1); // trim it down
csTemp2.TrimRight(); // by convention, always do this
csTemp2.TrimLeft();
i1 = csTemp.Find(csTemp2); // find the string again
ASSERT(!strncmp(((LPCSTR)csTemp) + i1, csTemp2, csTemp2.GetLength()));
if(i1 < 0) // oops
{
::MessageBoxA(NULL, "ERROR: unable to re-find the \"" + csTemp2 + "\" string",
m_pszAppName, MB_OK | MB_ICONHAND | MB_SETFOREGROUND);
goto error_exit;
}
csTemp3 = csTemp.Left(i1 + csTemp2.GetLength());
csTemp2 = csTemp.Mid(i1 + 1 + csTemp2.GetLength()); // skip comma by adding 1
// first char in 'csTemp2' might be white space
csTemp2.TrimRight();
csTemp2.TrimLeft();
// at this point, csTemp2 will be a string formatted as follows:
// "1, 0, 2, 3\0"
// - or -
// "1.0.2.3"
// but I will *ONLY* write the 2nd version
// and 'csTemp3' contains the entire line up to and including the
// 'FileVersion' or 'ProductVersion', minus the ','
if(bSetVersion)
{
// this re-builds the line to include the 'dotted' version
// that's been saved in 'csNewVersion0'
csTemp = csTemp3 + ", \"" + csNewVersion0 + "\"";
if(bUnicode)
{
BSTR bstrTemp = csTemp.AllocSysString();
if(!bstrTemp)
{
::MessageBoxA(NULL, "ERROR: unable to 'AllocSysString' (output file may be inconsistent)",
m_pszAppName, MB_OK | MB_ICONHAND | MB_SETFOREGROUND);
goto error_exit;
}
else
{
i1 = lstrlenW(bstrTemp) * sizeof(WCHAR);
memcpy(lp2, (LPCSTR)bstrTemp, i1);
lp2 += i1;
SysFreeString(bstrTemp);
bChangedFlag = TRUE;
}
}
else
{
memcpy(lp2, (LPCSTR)csTemp, csTemp.GetLength());
lp2 += csTemp.GetLength();
bChangedFlag = TRUE;
}
lp1 = lp3; // this keeps me from re-copying the same thing
}
else
{
// older versions of this had ',', newer use a '.' method
// so it USED to be something like this: VALUE "FileVersion", "1, 1, 0, 18\0"
// but *NOW* it is like this: VALUE "FileVersion", "1.2.0.1"
csTemp = csTemp3 + ", "; // add the ',' back in (for compatibility; later re-factor this)
// now 'massage' the value to eliminate the 'crap'
if(csTemp2.GetAt(0) == '"')
{
csTemp2 = csTemp2.Mid(1);
if(csTemp2.GetLength() &&
csTemp2.GetAt(csTemp2.GetLength() - 1) == '"')
{
csTemp2 = csTemp2.Left(csTemp2.GetLength() - 1);
csTemp2.TrimRight();
// is there a '\0' at the end?
if(csTemp2.GetLength() >= 2 &&
!csTemp2.Right(2).Compare("\\0"))
{
csTemp2 = csTemp2.Left(csTemp2.GetLength() - 2);
csTemp2.TrimRight();
}
}
}
// change ',' to '.' and eliminate white space
csTemp3 = "";
for(i1=0; i1 < csTemp2.GetLength(); i1++)
{
if(csTemp2.GetAt(i1) == ',')
{
csTemp3 += '.';
}
else if(csTemp2.GetAt(i1) > ' ')
{
csTemp3 += csTemp2.GetAt(i1);
}
}
i1 = csTemp3.ReverseFind('.'); // find the last dot
if(i1 <= 0)
{
::MessageBoxA(NULL,
"ERROR: improperly formatted version string \"" + csTemp3 + "\"\n"
"'somewhat original' line: " + csTemp + "\"" + csTemp3 + "\"",
m_pszAppName, MB_OK | MB_ICONHAND | MB_SETFOREGROUND);
goto error_exit;
}
csTemp += "\"" + csTemp3.Left(i1 + 1); // include the '.'
csTemp2 = csTemp3.Mid(i1 + 1);
i1 = atoi(csTemp2) + 1;
csTemp2.Format("%d", i1);
csTemp += csTemp2 + "\""; // and that ends the line
if(bUnicode)
{
BSTR bstrTemp = csTemp.AllocSysString();
if(!bstrTemp)
{
::MessageBoxA(NULL, "ERROR: unable to 'AllocSysString' (output file may be inconsistent)",
m_pszAppName, MB_OK | MB_ICONHAND | MB_SETFOREGROUND);
goto error_exit;
}
else
{
i1 = lstrlenW(bstrTemp) * sizeof(WCHAR);
memcpy(lp2, (LPCSTR)bstrTemp, i1);
lp2 += i1;
SysFreeString(bstrTemp);
bChangedFlag = TRUE;
}
}
else
{
memcpy(lp2, (LPCSTR)csTemp, csTemp.GetLength());
lp2 += csTemp.GetLength();
bChangedFlag = TRUE;
}
lp1 = lp3; // this keeps me from re-copying the same thing
}
}
}
else if(!csTemp2.Left(5).CompareNoCase("BEGIN"))
{
iBeginEndCount++;
}
else if(!csTemp2.Left(3).CompareNoCase("END"))
{
if(iBeginEndCount)
{
iBeginEndCount--;
}
if(!iBeginEndCount)
{
bVersionInfoFlag = FALSE;
}
}
}
else if(!csTemp.Left(15).CompareNoCase("VS_VERSION_INFO"))
{
// See if this line begins with 'VS_VERSION_INFO', and if
// so, flag it so that I can do the other stuff...
bVersionInfoFlag = TRUE;
}
// now, copy the line 'as-is' to destination buffer if I haven't
// already made an 'updated' copy (in which case, lp3 == lp1)
if(lp3 > lp1) // i.e. there's something to copy
{
memcpy(lp2, lp1, lp3 - lp1);
lp2 += (DWORD)(lp3 - lp1);
lp1 = lp3;
}
if(bUnicode)
{
while((DWORD)(lp1 - lpBuf1) < (dwLen - 1) &&
(*((WCHAR *)lp1) == '\r' ||
*((WCHAR *)lp1) == '\n'))
{
lp1 += sizeof(WCHAR); // pass up additional blank lines, etc.
}
}
else
{
while((DWORD)(lp1 - lpBuf1) < dwLen &&
(*lp1 == '\r' || *lp1 == '\n'))
{
lp1++; // pass up additional blank lines, etc.
}
}
// write additional stuff (as required)
if(lp1 > lp3) // anything more to copy?
{
memcpy(lp2, lp3, lp1 - lp3);
lp2 += (DWORD)(lp1 - lp3);
}
if(bUnicode)
{
*((WCHAR *)lp2) = 0;
}
else
{
*lp2 = 0; // terminate (just because)
}
}
if(bChangedFlag)
{
// OK! 'lp2' marks the end of the file. Now, re-write
// the whole thing, but make sure I mark the end of file
// correctly when I do it.....
#ifdef _DEBUG
CString csTempOut;
TRACE0("WOULD BE WRITING OUTPUT BACK TO FILE\r\n");
memcpy(csTempOut.GetBufferSetLength(dwLen), lpBuf2, dwLen);
csTempOut.ReleaseBuffer(dwLen);
afxDump << csTempOut;
TRACE0("\r\n****************************************************\r\n\n");
#else // _DEBUG
dwLen = lp2 - lpBuf2;
SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
SetEndOfFile(hFile);
if(!WriteFile(hFile, lpBuf2, dwLen, &cbBytes, NULL) ||
cbBytes != dwLen)
{
::MessageBox(NULL, "Write error on output file (original may have been destroyed, sorry!)",
m_pszAppName, MB_OK | MB_ICONHAND | MB_SETFOREGROUND);
}
FlushFileBuffers(hFile); // just because (force write to happen NOW)
// TODO: check for error, and restore original file if needed
#endif // _DEBUG
}
error_exit:
if(lpBuf)
{
GlobalFree((HGLOBAL)lpBuf);
}
if(hFile != INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
}
return FALSE;
}
CString CINCRVERApp::GetFileNameAndOptions(void)
{
int i1;
if(m_lpCmdLine[0]) // command line contains RC file and other stuff
{
LPCSTR lpc1 = m_lpCmdLine;
while(*lpc1)
{
while(*lpc1 && *lpc1 <= ' ')
lpc1++;
if(!*lpc1)
break;
LPCSTR lpc2 = lpc1++;
LPCSTR lpcRestOfLine = lpc2;
CString csTerm;
// find "end of term"
if(*lpc2 == '"')
{
lpc2 = lpc1; // point past quote mark
lpcRestOfLine = NULL;
while(*lpc1 && *lpc1 != '"') // TODO: check for '\' escaped quotes?
lpc1++;
csTerm = ((CString)lpc2).Left(lpc1 - lpc2);
if(*lpc1) // a quote mark
lpc1++;
}
else
{
while(*lpc1 > ' ')
lpc1++;
csTerm = ((CString)lpc2).Left(lpc1 - lpc2);
}
if(csTerm.GetLength())
{
if(csTerm[0] == '-' || csTerm[0] == '/')
{
csTerm.MakeUpper();
// a switch!
if(csTerm[1] == '?' || csTerm[1] == 'H')
{
return (CString)"";
}
else if(csTerm.GetLength() > 3 &&
csTerm[1]=='V' && csTerm[2]==':')
{
m_bSetVersion = TRUE;
m_csNewVersion0 = csTerm.Mid(3);
// change the '.' into ',' and add some white space between
// 'm_csNewVersion0 will continue to have '.'s
m_csNewVersion = "";
for(i1=0; i1 < m_csNewVersion0.GetLength(); i1++)
{
if(m_csNewVersion0[i1] == '.')
{
m_csNewVersion += ", ";
}
else
{
m_csNewVersion += m_csNewVersion0[i1];
}
}
TRACE0("CHANGING VERSION TO " + m_csNewVersion + "\r\n");
}
else if(csTerm.GetLength() > 3 &&
csTerm[1]=='R' && csTerm[2]==':')
{
m_bSetVersion = TRUE;
m_csNewVersion0 = GetVersionFromFileName(csTerm.Mid(3));
if(!m_csNewVersion0.GetLength()) // an error
{
return (CString)"";
}
// change the '.' into ',' and add some white space between
// 'm_csNewVersion0 will continue to have '.'s
m_csNewVersion = "";
for(i1=0; i1 < m_csNewVersion0.GetLength(); i1++)
{
if(m_csNewVersion0[i1] == '.')
{
m_csNewVersion += ", ";
}
else
{
m_csNewVersion += m_csNewVersion0[i1];
}
}
TRACE0("CHANGING VERSION TO " + m_csNewVersion + "\r\n");
}
else
{
::MessageBox(NULL, "ERROR - Illegal switch specified - " + csTerm,
m_pszAppName, MB_OK | MB_ICONHAND | MB_SETFOREGROUND);
return (CString)"";
}
}
else
{
if(lpcRestOfLine)
return (CString)lpcRestOfLine;
else
return csTerm;
}
}
}
}
CFileDialog dlg(TRUE, "rc", NULL, OFN_HIDEREADONLY,
"Resource Script (*.rc)|*.rc|All Files (*.*)|*.*||",
NULL);
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
m_pMainWnd = NULL;
if(nResponse == IDOK)
return dlg.GetPathName();
return (CString)"";
}
// -----------------
// UTILITY FUNCTIONS
// -----------------
CString GetVersionFromFileName(LPCSTR szName)
{
int i1, iBeginEndCount;
CString csFileName;
LPSTR lpBuf = NULL;
HANDLE hFile = INVALID_HANDLE_VALUE;
if(!szName || !*szName)
{
return (CString)"";
}
csFileName = szName;
hFile = CreateFile(csFileName, GENERIC_READ, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
INVALID_HANDLE_VALUE);
if(hFile == INVALID_HANDLE_VALUE)
{
::MessageBox(NULL, "File open error on \"" + csFileName + "\"",
theApp.m_pszAppName, MB_OK | MB_ICONHAND | MB_SETFOREGROUND);
return (CString)"";
}
DWORD cbBytes, dwLen;
BOOL bIsINF;
// differentiate between INF files and RC files
bIsINF = !(csFileName.Right(4).CompareNoCase(".INF"));
dwLen = SetFilePointer(hFile, 0, NULL, FILE_END);
SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
lpBuf = (LPSTR)GlobalAlloc(GPTR, dwLen + 0x100);
if(!lpBuf)
{
::MessageBox(NULL, "Not enough memory to complete operation",
theApp.m_pszAppName, MB_OK | MB_ICONHAND | MB_SETFOREGROUND);
goto error_exit;
}
if(!dwLen ||
!ReadFile(hFile, lpBuf, dwLen, &cbBytes, NULL) ||
cbBytes != dwLen)
{
::MessageBox(NULL, "Read error on input file",
theApp.m_pszAppName, MB_OK | MB_ICONHAND | MB_SETFOREGROUND);
goto error_exit;
}
LPSTR lpBuf1 = lpBuf;
LPSTR lpBuf2 = lpBuf + dwLen;
BOOL bUnicode = FALSE;
LPSTR lp1=lpBuf1, lp2=lpBuf2, lp3, lp4;
if(dwLen > 1 &&
(unsigned char)lpBuf[0] == 0xff &&
(unsigned char)lpBuf[1] == 0xfe)
{
// detecting UNICODE - I need to support BOTH UNICODE and ASCII
// though I can convert the text for comparisons, string manipulation, etc.
// for a while, now, RC files have been unicode (but not always)
// and INF files are typical ASCII. But I must allow for EITHER ONE.
bUnicode = TRUE; // this is a UNICODE file - begins with 0xff,0xfe
lp1 += sizeof(WCHAR); // skip past the first char on input
}
DWORD dwPos = 0;
BOOL bChangedFlag = FALSE, bVersionInfoFlag=FALSE;
iBeginEndCount = 0;
while((DWORD)(lp1 - lpBuf1) < dwLen)
{
if((!bUnicode && !*lp1) || // for ASCII/UTF-8 files
(bUnicode && !*((WCHAR *)lp1))) // for WCHAR unicode files
{ // TODO: handle 24-bit and 32-bit unicode?
break;
}
CString csTemp, csTemp2;
lp3 = lp1;
if(bUnicode)
{
while((DWORD)(lp3 - lpBuf1) < (dwLen - 1) &&
*((WCHAR *)lp3) &&
*((WCHAR *)lp3) != '\r' &&
*((WCHAR *)lp3) != '\n' &&