Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
buildVersionUpdate.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19// FILE: buildVersionUpdate.cpp //////////////////////////////////////////////////////
20// Generals version number class updater
21// Author: Matthew D. Campbell, November 2001
22
23// SYSTEM INCLUDES ////////////////////////////////////////////////////////////
24#define WIN32_LEAN_AND_MEAN // only bare bones windows stuff wanted
25#include <windows.h>
26#include <lmcons.h>
27#include <stdlib.h>
28#include <stdio.h>
29#include <string.h>
30
31// Local defines
32#define VERSION_MAJOR "VERSION_MAJOR"
33#define VERSION_MINOR "VERSION_MINOR"
34#define VERSION_BUILDNUM "VERSION_BUILDNUM"
35#define VERSION_STRING "VERSION_STRING"
36#define FORMAT "#define " VERSION_STRING " \"%d.%d.%d\"\n"
37#define COMMENTS "// Do not modify this file by hand. Auto-created and\n// Updated by buildVersionUpdate.\n"
38#define NUMFMT "#define %s %d\n"
39#define NUMFMT_MINOR "#define %s %d ///< This effects the replay version number.\n"
40
41static void writeVersion(char *file, int major, int minor, int build)
42{
43 FILE *filePtr = fopen(file, "w");
44 // Clobber the file. Hey, this is a simple program.
45 if (file)
46 {
47 if (filePtr)
48 {
49 fprintf(filePtr, COMMENTS);
50 fprintf(filePtr, FORMAT, major, minor, build);
51 fprintf(filePtr, NUMFMT, VERSION_MAJOR, major);
52 fprintf(filePtr, NUMFMT_MINOR, VERSION_MINOR, minor);
53 fprintf(filePtr, NUMFMT, VERSION_BUILDNUM, build);
54 fclose(filePtr);
55 }
56 else
57 {
58 printf("Cannot write file\n");
59 }
60 }
61 else
62 {
63 printf("No file to write\n");
64 }
65}
66
67static void usage(char *progname)
68{
69 if (progname)
70 {
71 printf ("Usage: %s versionfile.h", progname);
72 }
73}
74
75
76// strtrim ====================================================================
78//=============================================================================
79static char* strtrim(char* buffer)
80{
81 if (buffer != NULL) {
82 // Strip leading white space from the string.
83 char * source = buffer;
84 while ((*source != 0) && ((unsigned char)*source <= 32))
85 {
86 source++;
87 }
88
89 if (source != buffer)
90 {
91 strcpy(buffer, source);
92 }
93
94 // Clip trailing white space from the string.
95 for (int index = strlen(buffer)-1; index >= 0; index--)
96 {
97 if ((*source != 0) && ((unsigned char)buffer[index] <= 32))
98 {
99 buffer[index] = '\0';
100 }
101 else
102 {
103 break;
104 }
105 }
106 }
107
108 return buffer;
109}
110
111int APIENTRY WinMain(HINSTANCE hInstance,
112 HINSTANCE hPrevInstance,
113 LPSTR lpCmdLine,
114 int nCmdShow)
115{
116 /*
117 ** Convert WinMain arguments to simple main argc and argv
118 */
119 int argc = 1;
120 char * argv[20];
121 argv[0] = NULL;
122
123 char * token = strtok(lpCmdLine, " ");
124 while (argc < 20 && token != NULL)
125 {
126 argv[argc++] = strtrim(token);
127 token = strtok(NULL, " ");
128 }
129
130 int major = 1;
131 int minor = 0;
132 int build = 0;
133
134 if (argc != 2)
135 {
136 usage(argv[0]);
137 }
138 else
139 {
140 char *target = argv[argc-1];
141 FILE *filePtr;
142
143 if (target) {
144 filePtr = fopen(target, "r+");
145 if (filePtr)
146 {
147 char buffer[256];
148 char *stringPtr = NULL;
149
150 while (!feof(filePtr))
151 {
152 fread(buffer, 256, 1, filePtr);
153 if ((stringPtr = strstr(buffer, VERSION_STRING)) != NULL)
154 {
155 char *ptr;
156
157 // Looking for '#define VERSION "x.y.z"'
158 ptr = strtok(stringPtr, " "); // The VERSION
159 ptr = strtok(NULL, "\n"); // The remainder
160
161 if (*ptr == '\"')
162 {
163 ptr++; // Inc past the first "
164 ptr = strtok(ptr, "."); // The first number
165 major = atoi(ptr);
166 ptr = strtok(NULL, "."); // The second number
167 minor = atoi(ptr);
168 ptr = strtok(NULL, "\""); // The final number
169 build = atoi(ptr);
170 fclose(filePtr);
171
172 writeVersion(target, major, minor, ++build);
173 printf ("Build %d Version %d.%d.%d\n", build, major, minor, build);
174 break;
175 } else
176 {
177 printf ("Build 0. Oops, didn't find a string of the format: '#define VERSION \"x.y.z\"'");
178 }
179 } // End if if (strstr
180 } // End of while
181 } // End of if filePtr
182 else
183 {
184 // Didn't find the file, write a new one
185 writeVersion(target, major, minor, build);
186 }
187 }
188 }
189
190 return 0;
191}
#define NULL
Definition BaseType.h:92
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
#define VERSION_MAJOR
#define NUMFMT_MINOR
#define VERSION_BUILDNUM
#define VERSION_STRING
#define COMMENTS
#define NUMFMT
#define FORMAT
#define VERSION_MINOR
char * strtrim(char *buffer)
Definition trim.cpp:60