olestring.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. // OLEString.cpp
  20. //
  21. #include "stdAfx.h"
  22. #include <assert.h>
  23. #include "olestring.h"
  24. template void StripSpaces<OLECHAR> ( OLECHAR *string);
  25. template void StripSpaces<char> ( char *string );
  26. template void StripSpacesFromMetaString<OLECHAR> ( OLECHAR *string );
  27. template void StripSpacesFromMetaString<char> ( char *string);
  28. template void ConvertMetaChars<OLECHAR> ( OLECHAR *string);
  29. template void ConvertMetaChars<char> ( char *string);
  30. template int SameFormat<char> ( char *string1, char *string2 );
  31. template int SameFormat<OLECHAR> ( OLECHAR *string1, OLECHAR *string2 );
  32. template void EncodeFormat<char> ( char *string );
  33. template void EncodeFormat<OLECHAR> ( OLECHAR *string );
  34. template void DecodeFormat<char> ( char *string1 );
  35. template void DecodeFormat<OLECHAR> ( OLECHAR *string );
  36. template int IsFormatTypeChar<char> ( char string1 );
  37. template int IsFormatTypeChar<OLECHAR> ( OLECHAR string );
  38. static char *format_type = "cCdiouxXeEfgGnpsS"; // printf type as in %<width>.<presicion>{h|l|i64|L}<type>
  39. template <typename text>int IsFormatTypeChar ( text ch )
  40. {
  41. char *ptr = format_type;
  42. while ( *ptr )
  43. {
  44. if ( (unsigned int)*ptr++ == (unsigned int) ch )
  45. {
  46. return TRUE;
  47. }
  48. }
  49. return FALSE;
  50. }
  51. OLEString::OLEString ( void )
  52. {
  53. ole = NULL;
  54. sb = NULL;
  55. len = 0;
  56. Unlock ();
  57. Set ("");
  58. }
  59. OLEString::~OLEString ( )
  60. {
  61. delete [] ole;
  62. delete [] sb;
  63. ole = NULL;
  64. sb = NULL;
  65. len = 0;
  66. }
  67. void OLEString::Set ( OLECHAR *new_ole )
  68. {
  69. if ( !locked )
  70. {
  71. delete [] ole;
  72. delete [] sb;
  73. ole = NULL;
  74. sb = NULL;
  75. len = wcslen ( new_ole );
  76. {
  77. ole = new OLECHAR[len+1];
  78. wcscpy ( ole, new_ole );
  79. sb = new char[len+1];
  80. sprintf ( sb, "%S", ole );
  81. }
  82. }
  83. }
  84. void OLEString::Set ( char *new_sb )
  85. {
  86. if ( !locked )
  87. {
  88. delete [] ole;
  89. delete [] sb;
  90. ole = NULL;
  91. sb = NULL;
  92. len = strlen ( new_sb );
  93. {
  94. ole = new OLECHAR[len+1];
  95. swprintf ( ole, L"%S", new_sb );
  96. sb = new char[len+1];
  97. strcpy ( sb, new_sb );
  98. }
  99. }
  100. }
  101. void OLEString::StripSpaces ( void )
  102. {
  103. if ( locked )
  104. {
  105. return;
  106. }
  107. if ( ole )
  108. {
  109. ::StripSpaces ( ole );
  110. }
  111. if ( sb )
  112. {
  113. ::StripSpaces ( sb );
  114. }
  115. }
  116. void OLEString::FormatMetaString ( void )
  117. {
  118. char *str, *ptr;
  119. char ch, last = -1;
  120. int skipall = TRUE;
  121. int slash = FALSE;
  122. if ( !len || locked )
  123. {
  124. return;
  125. }
  126. char *string = new char[len*2];
  127. str = string;
  128. ptr = sb;
  129. while ( (ch = *ptr++) )
  130. {
  131. if ( ch == ' ' )
  132. {
  133. if ( last == ' ' )
  134. {
  135. continue;
  136. }
  137. }
  138. skipall= FALSE;
  139. if ( ch == '\\' )
  140. {
  141. char esc;
  142. slash = !slash;
  143. if ( slash && ((esc = *ptr) == 'n' || esc == 't') )
  144. {
  145. // remove last space
  146. if ( last != ' ' )
  147. {
  148. *str++ = ' ';
  149. }
  150. *str++ = '\\';
  151. ptr++;
  152. *str++ = esc;
  153. last = *str++ = ' ';
  154. continue;
  155. }
  156. }
  157. else
  158. {
  159. slash = FALSE;
  160. }
  161. last = *str++ = ch;
  162. }
  163. if ( last == ' ' )
  164. {
  165. str--;
  166. }
  167. *str = 0;
  168. Set ( string );
  169. delete [] string;
  170. string = NULL;
  171. }
  172. template <typename text> void StripSpaces ( text *string )
  173. {
  174. text *str, *ptr;
  175. text ch, last = -1;
  176. int skipall = TRUE;
  177. str = ptr = string;
  178. while ( (ch = *ptr++) )
  179. {
  180. if ( ch == ' ' )
  181. {
  182. if ( last == ' ' || skipall )
  183. {
  184. continue;
  185. }
  186. }
  187. if ( ch == '\n' || ch == '\t' )
  188. {
  189. // remove last space
  190. if ( last == ' ' )
  191. {
  192. str--;
  193. }
  194. skipall = TRUE; // skip all spaces
  195. last = *str++ = ch;
  196. continue;
  197. }
  198. last = *str++ = ch;
  199. skipall = FALSE;
  200. }
  201. if ( last == ' ' )
  202. {
  203. str--;
  204. }
  205. *str = 0;
  206. }
  207. template <typename text> void StripSpacesFromMetaString ( text *string )
  208. {
  209. text *str, *ptr;
  210. text ch, last = -1;
  211. int skipall = TRUE;
  212. int slash = FALSE;
  213. str = ptr = string;
  214. while ( (ch = *ptr++) )
  215. {
  216. if ( ch == ' ' )
  217. {
  218. if ( last == ' ' || skipall )
  219. {
  220. continue;
  221. }
  222. }
  223. skipall= FALSE;
  224. if ( ch == '\\' )
  225. {
  226. text esc;
  227. slash = !slash;
  228. if ( slash && (esc = *ptr) == 'n' || esc == 't' )
  229. {
  230. // remove last space
  231. if ( last == ' ' )
  232. {
  233. str--;
  234. }
  235. skipall = TRUE; // skip all spaces
  236. *str++ = '\\';
  237. ptr++;
  238. last = *str++ = esc;
  239. continue;
  240. }
  241. }
  242. else
  243. {
  244. slash = FALSE;
  245. }
  246. last = *str++ = ch;
  247. }
  248. if ( last == ' ' )
  249. {
  250. str--;
  251. }
  252. *str = 0;
  253. }
  254. template <typename text> void ConvertMetaChars ( text *string )
  255. {
  256. text *ptr;
  257. text ch;
  258. ptr = string;
  259. while ( (ch = *string++) )
  260. {
  261. if ( ch == '\\' )
  262. {
  263. if ( ch = *string )
  264. {
  265. switch ( ch )
  266. {
  267. case 'n':
  268. case 'N':
  269. ch = '\n';
  270. break;
  271. case 't':
  272. case 'T':
  273. ch = '\t';
  274. break;
  275. }
  276. string++;
  277. }
  278. }
  279. *ptr++ = ch;
  280. }
  281. *ptr = 0;
  282. }
  283. template <typename text> int SameFormat ( text *string1, text *string2 )
  284. {
  285. while ( *string1 && *string2 )
  286. {
  287. while ( *string1 )
  288. {
  289. if (*string1 == '%')
  290. {
  291. string1++;
  292. break;
  293. }
  294. if ( *string1 == '\\' )
  295. {
  296. string1++;
  297. }
  298. if ( *string1 )
  299. {
  300. string1++;
  301. }
  302. }
  303. while ( *string2 )
  304. {
  305. if (*string2 == '%')
  306. {
  307. string2++;
  308. break;
  309. }
  310. if ( *string2 == '\\' )
  311. {
  312. string2++;
  313. }
  314. if ( *string2)
  315. {
  316. string2++;
  317. }
  318. }
  319. if ( !*string1 && !*string2)
  320. {
  321. return TRUE;
  322. }
  323. int found_type = FALSE;
  324. while ( *string1 && *string2 && !found_type )
  325. {
  326. found_type = IsFormatTypeChar ( *string1 );
  327. if ( *string1 != *string2 )
  328. {
  329. return FALSE;
  330. }
  331. string1++;
  332. string2++;
  333. }
  334. }
  335. return TRUE;
  336. }
  337. template <typename text> void EncodeFormat ( text *string )
  338. {
  339. }
  340. template <typename text> void DecodeFormat ( text *string )
  341. {
  342. }