Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
versionUpdate.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: versionUpdate.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_BUILDNUM "VERSION_LOCALBUILDNUM"
33#define VERSION_STRING "LOCAL_BUILD_STRING"
34#define VERSION_BUILDUSER "VERSION_BUILDUSER"
35#define VERSION_BUILDLOC "VERSION_BUILDLOC"
36#define FORMAT "#define " VERSION_STRING " \"%d\"\n"
37#define COMMENTS "// Do not modify this file by hand. Auto-created and\n// Updated by versionUpdate.\n"
38#define NUMFMT "#define %s %d\n"
39#define STRFMT "#define %s \"%s\"\n"
40
41static void writeVersion(char *file, 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 unsigned long bufSize = UNLEN + 1;
50 char userName[UNLEN + 1];
51 if (!GetUserName(userName, &bufSize))
52 {
53 strcpy(userName, "unknown");
54 }
55
56 bufSize = MAX_COMPUTERNAME_LENGTH + 1;
57 char computerName[MAX_COMPUTERNAME_LENGTH + 1];
58 if (!GetComputerName(computerName, &bufSize))
59 {
60 strcpy(computerName, "unknown");
61 }
62
63 printf("Build is by %s at %s\n", userName, computerName);
64
65 fprintf(filePtr, COMMENTS);
66 fprintf(filePtr, FORMAT, build);
67 fprintf(filePtr, NUMFMT, VERSION_BUILDNUM, build);
68 fprintf(filePtr, STRFMT, VERSION_BUILDUSER, userName);
69 fprintf(filePtr, STRFMT, VERSION_BUILDLOC, computerName);
70 fclose(filePtr);
71 }
72 else
73 {
74 printf("Cannot write file\n");
75 }
76 }
77 else
78 {
79 printf("No file to write\n");
80 }
81}
82
83static void usage(char *progname)
84{
85 if (progname)
86 {
87 printf ("Usage: %s versionfile.h", progname);
88 }
89}
90
91
92// strtrim ====================================================================
94//=============================================================================
95static char* strtrim(char* buffer)
96{
97 if (buffer != NULL) {
98 // Strip leading white space from the string.
99 char * source = buffer;
100 while ((*source != 0) && ((unsigned char)*source <= 32))
101 {
102 source++;
103 }
104
105 if (source != buffer)
106 {
107 strcpy(buffer, source);
108 }
109
110 // Clip trailing white space from the string.
111 for (int index = strlen(buffer)-1; index >= 0; index--)
112 {
113 if ((*source != 0) && ((unsigned char)buffer[index] <= 32))
114 {
115 buffer[index] = '\0';
116 }
117 else
118 {
119 break;
120 }
121 }
122 }
123
124 return buffer;
125}
126
127int APIENTRY WinMain(HINSTANCE hInstance,
128 HINSTANCE hPrevInstance,
129 LPSTR lpCmdLine,
130 int nCmdShow)
131{
132 /*
133 ** Convert WinMain arguments to simple main argc and argv
134 */
135 int argc = 1;
136 char * argv[20];
137 argv[0] = NULL;
138
139 char * token = strtok(lpCmdLine, " ");
140 while (argc < 20 && token != NULL)
141 {
142 argv[argc++] = strtrim(token);
143 token = strtok(NULL, " ");
144 }
145
146 int build = 0;
147
148 if (argc != 2)
149 {
150 usage(argv[0]);
151 }
152 else
153 {
154 char *target = argv[argc-1];
155 FILE *filePtr;
156
157 if (target) {
158 filePtr = fopen(target, "r+");
159 if (filePtr)
160 {
161 char buffer[256];
162 char *stringPtr = NULL;
163
164 while (!feof(filePtr))
165 {
166 fread(buffer, 256, 1, filePtr);
167 if ((stringPtr = strstr(buffer, VERSION_STRING)) != NULL)
168 {
169 char *ptr;
170
171 // Looking for '#define VERSION "x.y.z"'
172 ptr = strtok(stringPtr, " "); // The VERSION
173 ptr = strtok(NULL, "\n"); // The remainder
174
175 if (*ptr == '\"')
176 {
177 ptr++; // Inc past the first "
178 build = atoi(ptr);
179 fclose(filePtr);
180
181 build++;
182
183 printf ("Local build is %d\n", build);
184 writeVersion(target, build);
185 break;
186 } else
187 {
188 printf ("Local build is 0. Oops, didn't find a string of the format: '#define VERSION \"x.y.z\"'");
189 }
190 } // End if if (strstr
191 } // End of while
192 } // End of if filePtr
193 else
194 {
195 // Didn't find the file, write a new one
196 printf ("Local build is %d\n", build);
197 writeVersion(target, build);
198 }
199 }
200 }
201
202 return 0;
203}
#define NULL
Definition BaseType.h:92
#define VERSION_BUILDNUM
#define VERSION_STRING
#define COMMENTS
#define NUMFMT
#define FORMAT
char * strtrim(char *buffer)
Definition trim.cpp:60
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
#define VERSION_BUILDLOC
#define STRFMT
#define VERSION_BUILDUSER