wwstring.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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/wwstring.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 12/13/01 10:48p $*
  29. * *
  30. * $Revision:: 17 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "wwstring.h"
  36. #include "win.h"
  37. #include "wwmemlog.h"
  38. #include "mutex.h"
  39. #include <stdio.h>
  40. ///////////////////////////////////////////////////////////////////
  41. // Static member initialzation
  42. ///////////////////////////////////////////////////////////////////
  43. FastCriticalSectionClass StringClass::m_Mutex;
  44. TCHAR StringClass::m_NullChar = 0;
  45. TCHAR * StringClass::m_EmptyString = &m_NullChar;
  46. //
  47. // A trick to optimize strings that are allocated from the stack and used only temporarily
  48. //
  49. // For alignment reasons we need twice as large block...
  50. char StringClass::m_TempStrings[(StringClass::MAX_TEMP_STRING*2)*StringClass::MAX_TEMP_BYTES];
  51. unsigned StringClass::ReservedMask=0;
  52. ///////////////////////////////////////////////////////////////////
  53. //
  54. // Get_String
  55. //
  56. ///////////////////////////////////////////////////////////////////
  57. void
  58. StringClass::Get_String (int length, bool is_temp)
  59. {
  60. WWMEMLOG(MEM_STRINGS);
  61. if (!is_temp && length == 0) {
  62. m_Buffer = m_EmptyString;
  63. return;
  64. }
  65. TCHAR *string = NULL;
  66. //
  67. // Should we attempt to use a temp buffer for this string?
  68. //
  69. if (is_temp && length <= MAX_TEMP_LEN && ReservedMask!=ALL_TEMP_STRINGS_USED_MASK) {
  70. //
  71. // Make sure no one else is requesting a temp pointer
  72. // at the same time we are. There is a slight possibility that another
  73. // thread stole the last available buffer in between the if sentence and
  74. // the mutex lock, but that is a feature by design and doesn't cause
  75. // anything bad to happen.
  76. //
  77. FastCriticalSectionClass::LockClass m(m_Mutex);
  78. //
  79. // Try to find an available temporary buffer
  80. //
  81. // TODO: Don't loop, there are better ways
  82. unsigned mask=1;
  83. for (int index = 0; index < MAX_TEMP_STRING; index ++, mask<<=1) {
  84. unsigned mask=1<<index;
  85. if (!(ReservedMask&mask)) {
  86. ReservedMask|=mask;
  87. //
  88. // Grab this unused buffer for our string
  89. //
  90. unsigned temp_string=reinterpret_cast<unsigned>(m_TempStrings);
  91. temp_string+=MAX_TEMP_BYTES*MAX_TEMP_STRING;
  92. temp_string&=~(MAX_TEMP_BYTES*MAX_TEMP_STRING-1);
  93. temp_string+=index*MAX_TEMP_BYTES;
  94. temp_string+=sizeof(_HEADER); // The buffer contains header as well, and it needs to be at the start
  95. string=reinterpret_cast<char*>(temp_string);
  96. Set_Buffer_And_Allocated_Length (string, MAX_TEMP_LEN);
  97. break;
  98. }
  99. }
  100. }
  101. if (string == NULL) {
  102. //
  103. // Allocate a new string as necessary
  104. //
  105. if (length > 0) {
  106. Set_Buffer_And_Allocated_Length (Allocate_Buffer (length), length);
  107. } else {
  108. Free_String ();
  109. }
  110. }
  111. }
  112. ///////////////////////////////////////////////////////////////////
  113. //
  114. // Resize
  115. //
  116. ///////////////////////////////////////////////////////////////////
  117. void
  118. StringClass::Resize (int new_len)
  119. {
  120. WWMEMLOG(MEM_STRINGS);
  121. int allocated_len = Get_Allocated_Length ();
  122. if (new_len > allocated_len) {
  123. //
  124. // Allocate the new buffer and copy the contents of our current
  125. // string.
  126. //
  127. TCHAR *new_buffer = Allocate_Buffer (new_len);
  128. _tcscpy (new_buffer, m_Buffer);
  129. //
  130. // Switch to the new buffer
  131. //
  132. Set_Buffer_And_Allocated_Length (new_buffer, new_len);
  133. }
  134. return ;
  135. }
  136. ///////////////////////////////////////////////////////////////////
  137. //
  138. // Uninitialised_Grow
  139. //
  140. ///////////////////////////////////////////////////////////////////
  141. void
  142. StringClass::Uninitialised_Grow (int new_len)
  143. {
  144. WWMEMLOG(MEM_STRINGS);
  145. int allocated_len = Get_Allocated_Length ();
  146. if (new_len > allocated_len) {
  147. //
  148. // Switch to a newly allocated buffer
  149. //
  150. TCHAR *new_buffer = Allocate_Buffer (new_len);
  151. Set_Buffer_And_Allocated_Length (new_buffer, new_len);
  152. }
  153. //
  154. // Whenever this function is called, clear the cached length
  155. //
  156. Store_Length (0);
  157. return ;
  158. }
  159. ///////////////////////////////////////////////////////////////////
  160. //
  161. // Uninitialised_Grow
  162. //
  163. ///////////////////////////////////////////////////////////////////
  164. void
  165. StringClass::Free_String (void)
  166. {
  167. if (m_Buffer != m_EmptyString) {
  168. unsigned buffer_base=reinterpret_cast<unsigned>(m_Buffer-sizeof (StringClass::_HEADER));
  169. unsigned temp_base=reinterpret_cast<unsigned>(m_TempStrings+MAX_TEMP_BYTES*MAX_TEMP_STRING);
  170. if ((buffer_base>>11)==(temp_base>>11)) {
  171. m_Buffer[0] = 0;
  172. //
  173. // Make sure no one else is changing the reserved mask
  174. // at the same time we are.
  175. //
  176. FastCriticalSectionClass::LockClass m(m_Mutex);
  177. unsigned index=(buffer_base/MAX_TEMP_BYTES)&(MAX_TEMP_STRING-1);
  178. unsigned mask=1<<index;
  179. ReservedMask&=~mask;
  180. }
  181. else {
  182. //
  183. // String wasn't temporary, so free the memory
  184. //
  185. char *buffer = ((char *)m_Buffer) - sizeof (StringClass::_HEADER);
  186. delete [] buffer;
  187. }
  188. //
  189. // Reset the buffer
  190. //
  191. m_Buffer = m_EmptyString;
  192. }
  193. return ;
  194. }
  195. ///////////////////////////////////////////////////////////////////
  196. //
  197. // Format
  198. //
  199. ///////////////////////////////////////////////////////////////////
  200. int _cdecl
  201. StringClass::Format_Args (const TCHAR *format, const va_list & arg_list )
  202. {
  203. //
  204. // Make a guess at the maximum length of the resulting string
  205. //
  206. TCHAR temp_buffer[512] = { 0 };
  207. int retval = 0;
  208. //
  209. // Format the string
  210. //
  211. #ifdef _UNICODE
  212. retval = _vsnwprintf (temp_buffer, 512, format, arg_list);
  213. #else
  214. retval = _vsnprintf (temp_buffer, 512, format, arg_list);
  215. #endif
  216. //
  217. // Copy the string into our buffer
  218. //
  219. (*this) = temp_buffer;
  220. return retval;
  221. }
  222. ///////////////////////////////////////////////////////////////////
  223. //
  224. // Format
  225. //
  226. ///////////////////////////////////////////////////////////////////
  227. int _cdecl
  228. StringClass::Format (const TCHAR *format, ...)
  229. {
  230. va_list arg_list;
  231. va_start (arg_list, format);
  232. //
  233. // Make a guess at the maximum length of the resulting string
  234. //
  235. TCHAR temp_buffer[512] = { 0 };
  236. int retval = 0;
  237. //
  238. // Format the string
  239. //
  240. #ifdef _UNICODE
  241. retval = _vsnwprintf (temp_buffer, 512, format, arg_list);
  242. #else
  243. retval = _vsnprintf (temp_buffer, 512, format, arg_list);
  244. #endif
  245. //
  246. // Copy the string into our buffer
  247. //
  248. (*this) = temp_buffer;
  249. va_end (arg_list);
  250. return retval;
  251. }
  252. ///////////////////////////////////////////////////////////////////
  253. //
  254. // Release_Resources
  255. //
  256. ///////////////////////////////////////////////////////////////////
  257. void
  258. StringClass::Release_Resources (void)
  259. {
  260. }
  261. ///////////////////////////////////////////////////////////////////
  262. // Copy_Wide
  263. //
  264. ///////////////////////////////////////////////////////////////////
  265. bool StringClass::Copy_Wide (const WCHAR *source)
  266. {
  267. if (source != NULL) {
  268. int length;
  269. BOOL unmapped;
  270. length = WideCharToMultiByte (CP_ACP, 0 , source, -1, NULL, 0, NULL, &unmapped);
  271. if (length > 0) {
  272. // Convert.
  273. WideCharToMultiByte (CP_ACP, 0, source, -1, Get_Buffer (length), length, NULL, NULL);
  274. // Update length.
  275. Store_Length (length - 1);
  276. }
  277. // Were all characters successfully mapped?
  278. return (!unmapped);
  279. }
  280. // Failure.
  281. return (false);
  282. }