miscutil.cpp 7.7 KB

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