widestring.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWSaveLoad *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/widestring.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 2/06/02 5:27p $*
  29. * *
  30. * $Revision:: 13 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #pragma warning(disable : 4514)
  36. #include "widestring.h"
  37. #include "win.h"
  38. #include <stdio.h>
  39. ///////////////////////////////////////////////////////////////////
  40. // Static member initialzation
  41. ///////////////////////////////////////////////////////////////////
  42. int WideStringClass::m_UsedTempStringCount = 0;
  43. FastCriticalSectionClass WideStringClass::m_TempMutex;
  44. WCHAR WideStringClass::m_NullChar = 0;
  45. WCHAR * WideStringClass::m_EmptyString = &m_NullChar;
  46. //
  47. // A trick to optimize strings that are allocated from the stack and used only temporarily
  48. //
  49. char WideStringClass::m_TempString1[WideStringClass::MAX_TEMP_BYTES];
  50. char WideStringClass::m_TempString2[WideStringClass::MAX_TEMP_BYTES];
  51. char WideStringClass::m_TempString3[WideStringClass::MAX_TEMP_BYTES];
  52. char WideStringClass::m_TempString4[WideStringClass::MAX_TEMP_BYTES];
  53. WCHAR * WideStringClass::m_FreeTempPtr[MAX_TEMP_STRING] = {
  54. reinterpret_cast<WCHAR *> (m_TempString1 + sizeof (WideStringClass::_HEADER)),
  55. reinterpret_cast<WCHAR *> (m_TempString2 + sizeof (WideStringClass::_HEADER)),
  56. reinterpret_cast<WCHAR *> (m_TempString3 + sizeof (WideStringClass::_HEADER)),
  57. reinterpret_cast<WCHAR *> (m_TempString4 + sizeof (WideStringClass::_HEADER))
  58. };
  59. WCHAR * WideStringClass::m_ResTempPtr[MAX_TEMP_STRING] = {
  60. NULL,
  61. NULL,
  62. NULL,
  63. NULL
  64. };
  65. ///////////////////////////////////////////////////////////////////
  66. //
  67. // Get_String
  68. //
  69. ///////////////////////////////////////////////////////////////////
  70. void
  71. WideStringClass::Get_String (int length, bool is_temp)
  72. {
  73. if (!is_temp && length <= 1) {
  74. m_Buffer = m_EmptyString;
  75. } else {
  76. WCHAR *string = NULL;
  77. //
  78. // Should we attempt to use a temp buffer for this string?
  79. //
  80. if (is_temp && length < MAX_TEMP_LEN && m_UsedTempStringCount < MAX_TEMP_STRING) {
  81. //
  82. // Make sure no one else is requesting a temp pointer
  83. // at the same time we are.
  84. //
  85. FastCriticalSectionClass::LockClass lock(m_TempMutex);
  86. //
  87. // Try to find an available temporary buffer
  88. //
  89. for (int index = 0; index < MAX_TEMP_STRING; index ++) {
  90. if (m_FreeTempPtr[index] != NULL) {
  91. //
  92. // Grab this unused buffer for our string
  93. //
  94. string = m_FreeTempPtr[index];
  95. m_ResTempPtr[index] = m_FreeTempPtr[index];
  96. m_FreeTempPtr[index] = NULL;
  97. Set_Buffer_And_Allocated_Length (string, MAX_TEMP_LEN);
  98. //
  99. // Increment the count of used buffers
  100. //
  101. m_UsedTempStringCount ++;
  102. break;
  103. }
  104. }
  105. }
  106. if (string == NULL) {
  107. Set_Buffer_And_Allocated_Length (Allocate_Buffer (length), length);
  108. }
  109. }
  110. return ;
  111. }
  112. ///////////////////////////////////////////////////////////////////
  113. //
  114. // Resize
  115. //
  116. ///////////////////////////////////////////////////////////////////
  117. void
  118. WideStringClass::Resize (int new_len)
  119. {
  120. int allocated_len = Get_Allocated_Length ();
  121. if (new_len > allocated_len) {
  122. //
  123. // Allocate the new buffer and copy the contents of our current
  124. // string.
  125. //
  126. WCHAR *new_buffer = Allocate_Buffer (new_len);
  127. wcscpy (new_buffer, m_Buffer);
  128. //
  129. // Switch to the new buffer
  130. //
  131. Set_Buffer_And_Allocated_Length (new_buffer, new_len);
  132. }
  133. return ;
  134. }
  135. ///////////////////////////////////////////////////////////////////
  136. //
  137. // Uninitialised_Grow
  138. //
  139. ///////////////////////////////////////////////////////////////////
  140. void
  141. WideStringClass::Uninitialised_Grow (int new_len)
  142. {
  143. int allocated_len = Get_Allocated_Length ();
  144. if (new_len > allocated_len) {
  145. //
  146. // Switch to a newly allocated buffer
  147. //
  148. WCHAR *new_buffer = Allocate_Buffer (new_len);
  149. Set_Buffer_And_Allocated_Length (new_buffer, new_len);
  150. }
  151. //
  152. // Whenever this function is called, clear the cached length
  153. //
  154. Store_Length (0);
  155. return ;
  156. }
  157. ///////////////////////////////////////////////////////////////////
  158. //
  159. // Uninitialised_Grow
  160. //
  161. ///////////////////////////////////////////////////////////////////
  162. void
  163. WideStringClass::Free_String (void)
  164. {
  165. if (m_Buffer != m_EmptyString) {
  166. //
  167. // Check to see if this string was a temporary string
  168. //
  169. bool found = false;
  170. for (int index = 0; index < MAX_TEMP_STRING; index ++) {
  171. if (m_Buffer == m_ResTempPtr[index]) {
  172. //
  173. // Make sure no one else is modifying a temp pointer
  174. // at the same time we are.
  175. //
  176. FastCriticalSectionClass::LockClass lock(m_TempMutex);
  177. //
  178. // Release our hold on this temporary buffer
  179. //
  180. m_Buffer[0] = 0;
  181. m_FreeTempPtr[index] = m_Buffer;
  182. m_ResTempPtr[index] = 0;
  183. m_UsedTempStringCount --;
  184. found = true;
  185. break;
  186. }
  187. }
  188. //
  189. // String wasn't temporary, so free the memory
  190. //
  191. if (found == false) {
  192. char *buffer = ((char *)m_Buffer) - sizeof (WideStringClass::_HEADER);
  193. delete [] buffer;
  194. }
  195. //
  196. // Reset the buffer
  197. //
  198. m_Buffer = m_EmptyString;
  199. }
  200. return ;
  201. }
  202. ///////////////////////////////////////////////////////////////////
  203. //
  204. // Format
  205. //
  206. ///////////////////////////////////////////////////////////////////
  207. int _cdecl
  208. WideStringClass::Format_Args (const WCHAR *format, const va_list & arg_list )
  209. {
  210. if (format == NULL) {
  211. return 0;
  212. }
  213. //
  214. // Make a guess at the maximum length of the resulting string
  215. //
  216. WCHAR temp_buffer[512] = { 0 };
  217. //
  218. // Format the string
  219. //
  220. int retval = _vsnwprintf (temp_buffer, 512, format, arg_list);
  221. //
  222. // Copy the string into our buffer
  223. //
  224. (*this) = temp_buffer;
  225. return retval;
  226. }
  227. ///////////////////////////////////////////////////////////////////
  228. //
  229. // Format
  230. //
  231. ///////////////////////////////////////////////////////////////////
  232. int _cdecl
  233. WideStringClass::Format (const WCHAR *format, ...)
  234. {
  235. if (format == NULL) {
  236. return 0;
  237. }
  238. va_list arg_list;
  239. va_start (arg_list, format);
  240. //
  241. // Make a guess at the maximum length of the resulting string
  242. //
  243. WCHAR temp_buffer[512] = { 0 };
  244. //
  245. // Format the string
  246. //
  247. int retval = _vsnwprintf (temp_buffer, 512, format, arg_list);
  248. //
  249. // Copy the string into our buffer
  250. //
  251. (*this) = temp_buffer;
  252. va_end (arg_list);
  253. return retval;
  254. }
  255. ///////////////////////////////////////////////////////////////////
  256. //
  257. // Release_Resources
  258. //
  259. ///////////////////////////////////////////////////////////////////
  260. void
  261. WideStringClass::Release_Resources (void)
  262. {
  263. return ;
  264. }
  265. ///////////////////////////////////////////////////////////////////
  266. // Convert_From
  267. ///////////////////////////////////////////////////////////////////
  268. bool WideStringClass::Convert_From (const char *text)
  269. {
  270. if (text != NULL) {
  271. int length;
  272. length = MultiByteToWideChar (CP_ACP, 0, text, -1, NULL, 0);
  273. if (length > 0) {
  274. Uninitialised_Grow (length);
  275. Store_Length (length - 1);
  276. // Convert.
  277. MultiByteToWideChar (CP_ACP, 0, text, -1, m_Buffer, length);
  278. // Success.
  279. return (true);
  280. }
  281. }
  282. // Failure.
  283. return (false);
  284. }
  285. ///////////////////////////////////////////////////////////////////
  286. // Test if a Unicode string is within the ANSI range. (0 - 255)
  287. ///////////////////////////////////////////////////////////////////
  288. bool WideStringClass::Is_ANSI(void)
  289. {
  290. if (m_Buffer) {
  291. for (int index = 0; m_Buffer[index] != 0; index++) {
  292. unsigned short value = m_Buffer[index];
  293. if (value > 255) {
  294. return false;
  295. }
  296. }
  297. }
  298. return true;
  299. }