Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
miscutil.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//
20// Filename: miscutil.cpp
21// Project: wwutil
22// Author: Tom Spencer-Smith
23// Date: June 1998
24// Description:
25//
26//-----------------------------------------------------------------------------
27#include "miscutil.h" // I WANNA BE FIRST!
28
29#include <time.h>
30
31#include "rawfile.h"
32#include "wwdebug.h"
33#include "win.h"
34#include "mmsys.h"
35#include "ffactory.h"
36
37//
38// cMiscUtil statics
39//
40
41//---------------------------------------------------------------------------
43{
44 //
45 // Returns a pointer to an internal statically allocated buffer...
46 // Subsequent time operations will destroy the contents of that buffer.
47 // Note: BoundsChecker reports 2 memory leaks in ctime here.
48 //
49
50 long time_now = ::time(NULL);
51 char * time_str = ::ctime(&time_now);
52 time_str[::strlen(time_str) - 1] = 0; // remove \n
53 return time_str;
54}
55
56//---------------------------------------------------------------------------
57void cMiscUtil::Seconds_To_Hms(float seconds, int & h, int & m, int & s)
58{
59 WWASSERT(seconds >= 0);
60
61 h = (int) (seconds / 3600);
62 seconds -= h * 3600;
63 m = (int) (seconds / 60);
64 seconds -= m * 60;
65 s = (int) seconds;
66
67 WWASSERT(h >= 0);
68 WWASSERT(m >= 0 && m < 60);
69 WWASSERT(s >= 0 && s < 60);
70
71 //WWASSERT(fabs((h * 3600 + m * 60 + s) / 60) - mins < WWMATH_EPSILON);
72}
73
74//-----------------------------------------------------------------------------
76{
77 WWASSERT(str1 != NULL);
78 WWASSERT(str2 != NULL);
79
80 return(::stricmp(str1, str2) == 0);
81}
82
83//-----------------------------------------------------------------------------
85{
86 WWASSERT(str1 != NULL);
87 WWASSERT(str2 != NULL);
88
89 return(::stricmp(str1, str2) != 0);
90}
91
92//-----------------------------------------------------------------------------
94{
95#if 0
96 WWASSERT(filename != NULL);
97
98 WIN32_FIND_DATA find_info;
99 HANDLE file_handle = ::FindFirstFile(filename, &find_info);
100
101 if (file_handle != INVALID_HANDLE_VALUE) {
102 ::FindClose(file_handle);
103 return true;
104 } else {
105 return false;
106 }
107#else
108 FileClass * file = _TheFileFactory->Get_File( filename );
109 if ( file && file->Is_Available() ) {
110 return true;
111 }
112 _TheFileFactory->Return_File( file );
113 return false;
114#endif
115}
116
117//-----------------------------------------------------------------------------
119{
120 WWASSERT(filename != NULL);
121
122 DWORD attributes = ::GetFileAttributes(filename);
123 return ((attributes != 0xFFFFFFFF) && (attributes & FILE_ATTRIBUTE_READONLY));
124}
125
126//-----------------------------------------------------------------------------
128{
129 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
130}
131
132//-----------------------------------------------------------------------------
134{
135 return (c >= '0' && c <= '9');
136}
137
138//-----------------------------------------------------------------------------
140{
141 return Is_Alphabetic(c) || Is_Numeric(c);
142}
143
144//-----------------------------------------------------------------------------
146{
147 return c == ' ' || c == '\t';
148}
149
150//-----------------------------------------------------------------------------
152{
153 WWASSERT(text != NULL);
154
155 int length = ::strlen(text);
156 while (length > 0 && Is_Whitespace(text[length - 1])) {
157 text[--length] = 0;
158 }
159}
160
161//-----------------------------------------------------------------------------
163{
164 WWASSERT(filename != NULL);
165
166// WWDEBUG_SAY(("cMiscUtil::Get_File_Id_String for %s\n", filename));
167
168 //
169 // Get size
170 //
171 RawFileClass file(filename);
172 int filesize = file.Size();
173 //WWASSERT(filesize > 0);
174 if (filesize <= 0)
175 {
176 WWDEBUG_SAY(("Error: cMiscUtil::Get_File_Id_String for %s: filesize = %d\n",
177 filename, filesize));
178 //W3D_DIE;
179 }
180 file.Close();
181
182 //
183 // Note... this timedatestamp is not present for all file types...
184 //
185 IMAGE_FILE_HEADER header = {0};
186 extern bool Get_Image_File_Header(LPCSTR filename, IMAGE_FILE_HEADER *file_header);
187 /*
188 bool success;
189 success = Get_Image_File_Header(filename, &header);
190 WWASSERT(success);
191 */
192 Get_Image_File_Header(filename, &header);
193 int time_date_stamp = header.TimeDateStamp;
194
195 char working_filename[500];
196 strcpy(working_filename, filename);
197 ::strupr(working_filename);
198
199 //
200 // Strip path off filename
201 //
202 char * p_start = &working_filename[strlen(working_filename)];
203 int num_chars = 1;
204 while (p_start > working_filename && *(p_start - 1) != '\\') {
205 p_start--;
206 num_chars++;
207 }
208 ::memmove(working_filename, p_start, num_chars);
209
210 //
211 // Put all this data into a string
212 //
213 str.Format("%s %d %d", working_filename, filesize, time_date_stamp);
214
215 //WWDEBUG_SAY(("File id string: %s\n", str));
216}
217
218//-----------------------------------------------------------------------------
220{
221 WWASSERT(filename != NULL);
222
223 ::DeleteFile(filename);
224}
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242/*
243#define SIZE_OF_NT_SIGNATURE sizeof(DWORD)
244#define PEFHDROFFSET(a) ((LPVOID)((BYTE *)a + \
245 ((PIMAGE_DOS_HEADER)a)->e_lfanew + SIZE_OF_NT_SIGNATURE))
246*/
247
248/*
249int cMiscUtil::Get_Exe_Key(void)
250{
251 //
252 // Get exe name
253 //
254 char filename[500];
255 int succeeded;
256 succeeded = ::GetModuleFileName(NULL, filename, sizeof(filename));
257 ::strupr(filename);
258 WWASSERT(succeeded);
259
260 //
261 // Get size
262 //
263 RawFileClass file(filename);
264 int filesize = file.Size();
265 WWASSERT(filesize > 0);
266 file.Close();
267
268 //
269 // Strip path off filename
270 //
271 char * p_start = &filename[strlen(filename)];
272 int num_chars = 1;
273 while (*(p_start - 1) != '\\') {
274 p_start--;
275 num_chars++;
276 }
277 ::memmove(filename, p_start, num_chars);
278
279 //
280 // Pull a time/date stamp out of the exe header
281 //
282 PIMAGE_FILE_HEADER p_header = (PIMAGE_FILE_HEADER) PEFHDROFFSET(ProgramInstance);
283 WWASSERT(p_header != NULL);
284 int time_date_stamp = p_header->TimeDateStamp;
285
286 //
287 // Put all this data into a string
288 //
289 char id_string[500];
290 ::sprintf(id_string, "%s %d %d", filename, filesize, time_date_stamp);
291 WWDEBUG_SAY(("File id string: %s\n", id_string));
292
293 //
294 // return the crc of that string as the key
295 //
296 return CRCEngine()(id_string, strlen(id_string));
297}
298*/
299
300//#include <stdio.h>
301//#include "verchk.h"
302
303/*
304//-----------------------------------------------------------------------------
305int cMiscUtil::Get_Exe_Key(void)
306{
307 //
308 // Get exe name
309 //
310 char filename[500];
311 int succeeded;
312 succeeded = ::GetModuleFileName(NULL, filename, sizeof(filename));
313 ::strupr(filename);
314 WWASSERT(succeeded);
315
316 StringClass string;
317 Get_File_Id_String(filename, string);
318
319 //
320 // return the crc of that string as the key
321 //
322 return CRCEngine()(string, strlen(string));
323}
324*/
325
326//#include "crc.h"
#define NULL
Definition BaseType.h:92
#define WWASSERT
const char * LPCSTR
Definition bittype.h:62
unsigned long DWORD
Definition bittype.h:57
bool Get_Image_File_Header(const char *filename, IMAGE_FILE_HEADER *file_header)
Definition verchk.cpp:142
virtual bool Is_Available(int forced=false)=0
virtual int Size(void)
Definition rawfile.cpp:842
virtual void Close(void)
Definition rawfile.cpp:561
int _cdecl Format(const TCHAR *format,...)
Definition wwstring.cpp:273
static bool Is_Numeric(char c)
Definition miscutil.cpp:133
static LPCSTR Get_Text_Time(void)
Definition miscutil.cpp:42
static bool File_Is_Read_Only(LPCSTR filename)
Definition miscutil.cpp:118
static bool Is_Whitespace(char c)
Definition miscutil.cpp:145
static void Get_File_Id_String(LPCSTR filename, StringClass &str)
Definition miscutil.cpp:162
static bool File_Exists(LPCSTR filename)
Definition miscutil.cpp:93
static void Seconds_To_Hms(float seconds, int &h, int &m, int &s)
Definition miscutil.cpp:57
static void Trim_Trailing_Whitespace(char *text)
Definition miscutil.cpp:151
static bool Is_String_Different(LPCSTR str1, LPCSTR str2)
Definition miscutil.cpp:84
static bool Is_Alphanumeric(char c)
Definition miscutil.cpp:139
static bool Is_String_Same(LPCSTR str1, LPCSTR str2)
Definition miscutil.cpp:75
static void Remove_File(LPCSTR filename)
Definition miscutil.cpp:219
static bool Is_Alphabetic(char c)
Definition miscutil.cpp:127
FileFactoryClass * _TheFileFactory
Definition ffactory.cpp:51
#define WWDEBUG_SAY(x)
Definition wwdebug.h:114