widestring.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. ** Command & Conquer Generals(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:: 8/28/01 10:26a $*
  29. * *
  30. * $Revision:: 5 $*
  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. CriticalSectionClass 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. CriticalSectionClass::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. // if ( new_len <= 1 ) {
  144. // Set_Buffer_And_Allocated_Length (m_EmptyString, 0);
  145. // } else {
  146. int allocated_len = Get_Allocated_Length ();
  147. if (new_len > allocated_len) {
  148. //
  149. // Switch to a newly allocated buffer
  150. //
  151. WCHAR *new_buffer = Allocate_Buffer (new_len);
  152. Set_Buffer_And_Allocated_Length (new_buffer, new_len);
  153. }
  154. // }
  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. CriticalSectionClass::LockClass lock(m_TempMutex);
  177. //
  178. // Release our hold on this temporary buffer
  179. //
  180. m_FreeTempPtr[index] = m_Buffer;
  181. m_ResTempPtr[index] = 0;
  182. m_UsedTempStringCount --;
  183. found = true;
  184. break;
  185. }
  186. }
  187. //
  188. // String wasn't temporary, so free the memory
  189. //
  190. if (found == false) {
  191. char *buffer = ((char *)m_Buffer) - sizeof (WideStringClass::_HEADER);
  192. delete [] buffer;
  193. }
  194. //
  195. // Reset the buffer
  196. //
  197. m_Buffer = m_EmptyString;
  198. }
  199. return ;
  200. }
  201. ///////////////////////////////////////////////////////////////////
  202. //
  203. // Format
  204. //
  205. ///////////////////////////////////////////////////////////////////
  206. int _cdecl
  207. WideStringClass::Format_Args (const WCHAR *format, const va_list & arg_list )
  208. {
  209. //
  210. // Make a guess at the maximum length of the resulting string
  211. //
  212. WCHAR temp_buffer[512] = { 0 };
  213. //
  214. // Format the string
  215. //
  216. int retval = _vsnwprintf (temp_buffer, 512, format, arg_list);
  217. //
  218. // Copy the string into our buffer
  219. //
  220. (*this) = temp_buffer;
  221. return retval;
  222. }
  223. ///////////////////////////////////////////////////////////////////
  224. //
  225. // Format
  226. //
  227. ///////////////////////////////////////////////////////////////////
  228. int _cdecl
  229. WideStringClass::Format (const WCHAR *format, ...)
  230. {
  231. va_list arg_list;
  232. va_start (arg_list, format);
  233. //
  234. // Make a guess at the maximum length of the resulting string
  235. //
  236. WCHAR temp_buffer[512] = { 0 };
  237. //
  238. // Format the string
  239. //
  240. int retval = _vsnwprintf (temp_buffer, 512, format, arg_list);
  241. //
  242. // Copy the string into our buffer
  243. //
  244. (*this) = temp_buffer;
  245. va_end (arg_list);
  246. return retval;
  247. }
  248. ///////////////////////////////////////////////////////////////////
  249. //
  250. // Release_Resources
  251. //
  252. ///////////////////////////////////////////////////////////////////
  253. void
  254. WideStringClass::Release_Resources (void)
  255. {
  256. return ;
  257. }