widestring.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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.h $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 8/28/01 10:49a $*
  29. * *
  30. * $Revision:: 11 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef __WIDESTRING_H
  39. #define __WIDESTRING_H
  40. #include <string.h>
  41. #include <stdarg.h>
  42. #include "always.h"
  43. #include "wwdebug.h"
  44. #include "win.h"
  45. #include "wwstring.h"
  46. #include <wchar.h>
  47. #ifdef _UNIX
  48. #include "osdep.h"
  49. #endif
  50. //////////////////////////////////////////////////////////////////////
  51. //
  52. // WideStringClass
  53. //
  54. // This is a UNICODE (double-byte) version of StringClass. All
  55. // operations are performed on wide character strings.
  56. //
  57. //////////////////////////////////////////////////////////////////////
  58. class WideStringClass
  59. {
  60. public:
  61. ////////////////////////////////////////////////////////////
  62. // Public constructors/destructors
  63. ////////////////////////////////////////////////////////////
  64. WideStringClass (int initial_len = 0, bool hint_temporary = false);
  65. WideStringClass (const WideStringClass &string, bool hint_temporary = false);
  66. WideStringClass (const WCHAR *string, bool hint_temporary = false);
  67. WideStringClass (WCHAR ch, bool hint_temporary = false);
  68. WideStringClass (const char *string, bool hint_temporary = false);
  69. ~WideStringClass (void);
  70. ////////////////////////////////////////////////////////////
  71. // Public operators
  72. ////////////////////////////////////////////////////////////
  73. bool operator== (const WCHAR *rvalue) const;
  74. bool operator!= (const WCHAR *rvalue) const;
  75. inline const WideStringClass &operator= (const WideStringClass &string);
  76. inline const WideStringClass &operator= (const WCHAR *string);
  77. inline const WideStringClass &operator= (WCHAR ch);
  78. inline const WideStringClass &operator= (const char *string);
  79. const WideStringClass &operator+= (const WideStringClass &string);
  80. const WideStringClass &operator+= (const WCHAR *string);
  81. const WideStringClass &operator+= (WCHAR ch);
  82. friend WideStringClass operator+ (const WideStringClass &string1, const WideStringClass &string2);
  83. friend WideStringClass operator+ (const WCHAR *string1, const WideStringClass &string2);
  84. friend WideStringClass operator+ (const WideStringClass &string1, const WCHAR *string2);
  85. bool operator < (const WCHAR *string) const;
  86. bool operator <= (const WCHAR *string) const;
  87. bool operator > (const WCHAR *string) const;
  88. bool operator >= (const WCHAR *string) const;
  89. WCHAR operator[] (int index) const;
  90. operator const WCHAR * (void) const;
  91. ////////////////////////////////////////////////////////////
  92. // Public methods
  93. ////////////////////////////////////////////////////////////
  94. int Compare (const WCHAR *string) const;
  95. int Compare_No_Case (const WCHAR *string) const;
  96. inline int Get_Length (void) const;
  97. bool Is_Empty (void) const;
  98. void Erase (int start_index, int char_count);
  99. int _cdecl Format (const WCHAR *format, ...);
  100. int _cdecl Format_Args (const WCHAR *format, const va_list & arg_list );
  101. void Convert_From (const char *text);
  102. void Convert_To (StringClass &string);
  103. void Convert_To (StringClass &string) const;
  104. WCHAR * Get_Buffer (int new_length);
  105. WCHAR * Peek_Buffer (void);
  106. ////////////////////////////////////////////////////////////
  107. // Static methods
  108. ////////////////////////////////////////////////////////////
  109. static void Release_Resources (void);
  110. private:
  111. ////////////////////////////////////////////////////////////
  112. // Private structures
  113. ////////////////////////////////////////////////////////////
  114. typedef struct _HEADER
  115. {
  116. int allocated_length;
  117. int length;
  118. } HEADER;
  119. ////////////////////////////////////////////////////////////
  120. // Private constants
  121. ////////////////////////////////////////////////////////////
  122. enum
  123. {
  124. MAX_TEMP_STRING = 4,
  125. MAX_TEMP_LEN = 256,
  126. MAX_TEMP_BYTES = (MAX_TEMP_LEN * sizeof (WCHAR)) + sizeof (HEADER),
  127. };
  128. ////////////////////////////////////////////////////////////
  129. // Private methods
  130. ////////////////////////////////////////////////////////////
  131. void Get_String (int length, bool is_temp);
  132. WCHAR * Allocate_Buffer (int length);
  133. void Resize (int size);
  134. void Uninitialised_Grow (int length);
  135. void Free_String (void);
  136. inline void Store_Length (int length);
  137. inline void Store_Allocated_Length (int allocated_length);
  138. inline HEADER * Get_Header (void) const;
  139. int Get_Allocated_Length (void) const;
  140. void Set_Buffer_And_Allocated_Length (WCHAR *buffer, int length);
  141. ////////////////////////////////////////////////////////////
  142. // Private member data
  143. ////////////////////////////////////////////////////////////
  144. WCHAR * m_Buffer;
  145. ////////////////////////////////////////////////////////////
  146. // Static member data
  147. ////////////////////////////////////////////////////////////
  148. static char m_TempString1[MAX_TEMP_BYTES];
  149. static char m_TempString2[MAX_TEMP_BYTES];
  150. static char m_TempString3[MAX_TEMP_BYTES];
  151. static char m_TempString4[MAX_TEMP_BYTES];
  152. static WCHAR * m_FreeTempPtr[MAX_TEMP_STRING];
  153. static WCHAR * m_ResTempPtr[MAX_TEMP_STRING];
  154. static int m_UsedTempStringCount;
  155. static CriticalSectionClass m_TempMutex;
  156. static WCHAR m_NullChar;
  157. static WCHAR * m_EmptyString;
  158. };
  159. ///////////////////////////////////////////////////////////////////
  160. // WideStringClass
  161. ///////////////////////////////////////////////////////////////////
  162. inline
  163. WideStringClass::WideStringClass (int initial_len, bool hint_temporary)
  164. : m_Buffer (m_EmptyString)
  165. {
  166. Get_String (initial_len, hint_temporary);
  167. m_Buffer[0] = m_NullChar;
  168. return ;
  169. }
  170. ///////////////////////////////////////////////////////////////////
  171. // WideStringClass
  172. ///////////////////////////////////////////////////////////////////
  173. inline
  174. WideStringClass::WideStringClass (WCHAR ch, bool hint_temporary)
  175. : m_Buffer (m_EmptyString)
  176. {
  177. Get_String (2, hint_temporary);
  178. (*this) = ch;
  179. return ;
  180. }
  181. ///////////////////////////////////////////////////////////////////
  182. // WideStringClass
  183. ///////////////////////////////////////////////////////////////////
  184. inline
  185. WideStringClass::WideStringClass (const WideStringClass &string, bool hint_temporary)
  186. : m_Buffer (m_EmptyString)
  187. {
  188. if (hint_temporary || (string.Get_Length()>1)) {
  189. Get_String(string.Get_Length()+1, hint_temporary);
  190. }
  191. (*this) = string;
  192. return ;
  193. }
  194. ///////////////////////////////////////////////////////////////////
  195. // WideStringClass
  196. ///////////////////////////////////////////////////////////////////
  197. inline
  198. WideStringClass::WideStringClass (const WCHAR *string, bool hint_temporary)
  199. : m_Buffer (m_EmptyString)
  200. {
  201. int len=string ? wcslen(string) : 0;
  202. if (hint_temporary || len>0) {
  203. Get_String (len+1, hint_temporary);
  204. }
  205. (*this) = string;
  206. return ;
  207. }
  208. ///////////////////////////////////////////////////////////////////
  209. // WideStringClass
  210. ///////////////////////////////////////////////////////////////////
  211. inline
  212. WideStringClass::WideStringClass (const char *string, bool hint_temporary)
  213. : m_Buffer (m_EmptyString)
  214. {
  215. if (hint_temporary || (string && strlen(string)>0)) {
  216. Get_String (strlen(string) + 1, hint_temporary);
  217. }
  218. (*this) = string;
  219. return ;
  220. }
  221. ///////////////////////////////////////////////////////////////////
  222. // ~WideStringClass
  223. ///////////////////////////////////////////////////////////////////
  224. inline
  225. WideStringClass::~WideStringClass (void)
  226. {
  227. Free_String ();
  228. return ;
  229. }
  230. ///////////////////////////////////////////////////////////////////
  231. // Is_Empty
  232. ///////////////////////////////////////////////////////////////////
  233. inline bool
  234. WideStringClass::Is_Empty (void) const
  235. {
  236. return (m_Buffer[0] == m_NullChar);
  237. }
  238. ///////////////////////////////////////////////////////////////////
  239. // Compare
  240. ///////////////////////////////////////////////////////////////////
  241. inline int
  242. WideStringClass::Compare (const WCHAR *string) const
  243. {
  244. return wcscmp (m_Buffer, string);
  245. }
  246. ///////////////////////////////////////////////////////////////////
  247. // Compare_No_Case
  248. ///////////////////////////////////////////////////////////////////
  249. inline int
  250. WideStringClass::Compare_No_Case (const WCHAR *string) const
  251. {
  252. return _wcsicmp (m_Buffer, string);
  253. }
  254. ///////////////////////////////////////////////////////////////////
  255. // operator[]
  256. ///////////////////////////////////////////////////////////////////
  257. inline WCHAR
  258. WideStringClass::operator[] (int index) const
  259. {
  260. WWASSERT (index >= 0 && index < Get_Length ());
  261. return m_Buffer[index];
  262. }
  263. ///////////////////////////////////////////////////////////////////
  264. // operator const WCHAR *
  265. ///////////////////////////////////////////////////////////////////
  266. inline
  267. WideStringClass::operator const WCHAR * (void) const
  268. {
  269. return m_Buffer;
  270. }
  271. ///////////////////////////////////////////////////////////////////
  272. // operator==
  273. ///////////////////////////////////////////////////////////////////
  274. inline bool
  275. WideStringClass::operator== (const WCHAR *rvalue) const
  276. {
  277. return (Compare (rvalue) == 0);
  278. }
  279. ///////////////////////////////////////////////////////////////////
  280. // operator!=
  281. ///////////////////////////////////////////////////////////////////
  282. inline bool
  283. WideStringClass::operator!= (const WCHAR *rvalue) const
  284. {
  285. return (Compare (rvalue) != 0);
  286. }
  287. ///////////////////////////////////////////////////////////////////
  288. // operator=
  289. ///////////////////////////////////////////////////////////////////
  290. inline const WideStringClass &
  291. WideStringClass::operator= (const WideStringClass &string)
  292. {
  293. return operator= ((const WCHAR *)string);
  294. }
  295. ///////////////////////////////////////////////////////////////////
  296. // operator <
  297. ///////////////////////////////////////////////////////////////////
  298. inline bool
  299. WideStringClass::operator < (const WCHAR *string) const
  300. {
  301. return (wcscmp (m_Buffer, string) < 0);
  302. }
  303. ///////////////////////////////////////////////////////////////////
  304. // operator <=
  305. ///////////////////////////////////////////////////////////////////
  306. inline bool
  307. WideStringClass::operator <= (const WCHAR *string) const
  308. {
  309. return (wcscmp (m_Buffer, string) <= 0);
  310. }
  311. ///////////////////////////////////////////////////////////////////
  312. // operator >
  313. ///////////////////////////////////////////////////////////////////
  314. inline bool
  315. WideStringClass::operator > (const WCHAR *string) const
  316. {
  317. return (wcscmp (m_Buffer, string) > 0);
  318. }
  319. ///////////////////////////////////////////////////////////////////
  320. // operator >=
  321. ///////////////////////////////////////////////////////////////////
  322. inline bool
  323. WideStringClass::operator >= (const WCHAR *string) const
  324. {
  325. return (wcscmp (m_Buffer, string) >= 0);
  326. }
  327. ///////////////////////////////////////////////////////////////////
  328. // Erase
  329. ///////////////////////////////////////////////////////////////////
  330. inline void
  331. WideStringClass::Erase (int start_index, int char_count)
  332. {
  333. int len = Get_Length ();
  334. if (start_index < len) {
  335. if (start_index + char_count > len) {
  336. char_count = len - start_index;
  337. }
  338. ::memmove ( &m_Buffer[start_index],
  339. &m_Buffer[start_index + char_count],
  340. (len - (start_index + char_count) + 1) * sizeof (WCHAR));
  341. Store_Length( wcslen(m_Buffer) );
  342. }
  343. return ;
  344. }
  345. ///////////////////////////////////////////////////////////////////
  346. // operator=
  347. ///////////////////////////////////////////////////////////////////
  348. inline const WideStringClass &
  349. WideStringClass::operator= (const WCHAR *string)
  350. {
  351. if (string != 0) {
  352. int len = wcslen (string);
  353. Uninitialised_Grow (len + 1);
  354. Store_Length (len);
  355. ::memcpy (m_Buffer, string, (len + 1) * sizeof (WCHAR));
  356. }
  357. return (*this);
  358. }
  359. ///////////////////////////////////////////////////////////////////
  360. // operator=
  361. ///////////////////////////////////////////////////////////////////
  362. inline const WideStringClass &
  363. WideStringClass::operator= (const char *string)
  364. {
  365. if (string != 0) {
  366. Convert_From(string);
  367. }
  368. return (*this);
  369. }
  370. ///////////////////////////////////////////////////////////////////
  371. // operator=
  372. ///////////////////////////////////////////////////////////////////
  373. inline const WideStringClass &
  374. WideStringClass::operator= (WCHAR ch)
  375. {
  376. Uninitialised_Grow (2);
  377. m_Buffer[0] = ch;
  378. m_Buffer[1] = m_NullChar;
  379. Store_Length (1);
  380. return (*this);
  381. }
  382. ///////////////////////////////////////////////////////////////////
  383. // operator+=
  384. ///////////////////////////////////////////////////////////////////
  385. inline const WideStringClass &
  386. WideStringClass::operator+= (const WCHAR *string)
  387. {
  388. WWASSERT (string != NULL);
  389. int cur_len = Get_Length ();
  390. int src_len = wcslen (string);
  391. int new_len = cur_len + src_len;
  392. //
  393. // Make sure our buffer is large enough to hold the new string
  394. //
  395. Resize (new_len + 1);
  396. Store_Length (new_len);
  397. //
  398. // Copy the new string onto our the end of our existing buffer
  399. //
  400. ::memcpy (&m_Buffer[cur_len], string, (src_len + 1) * sizeof (WCHAR));
  401. return (*this);
  402. }
  403. ///////////////////////////////////////////////////////////////////
  404. // operator+=
  405. ///////////////////////////////////////////////////////////////////
  406. inline const WideStringClass &
  407. WideStringClass::operator+= (WCHAR ch)
  408. {
  409. int cur_len = Get_Length ();
  410. Resize (cur_len + 2);
  411. m_Buffer[cur_len] = ch;
  412. m_Buffer[cur_len + 1] = m_NullChar;
  413. if (ch != m_NullChar) {
  414. Store_Length (cur_len + 1);
  415. }
  416. return (*this);
  417. }
  418. ///////////////////////////////////////////////////////////////////
  419. // Get_Buffer
  420. ///////////////////////////////////////////////////////////////////
  421. inline WCHAR *
  422. WideStringClass::Get_Buffer (int new_length)
  423. {
  424. Uninitialised_Grow (new_length);
  425. return m_Buffer;
  426. }
  427. ///////////////////////////////////////////////////////////////////
  428. // Peek_Buffer
  429. ///////////////////////////////////////////////////////////////////
  430. inline WCHAR *
  431. WideStringClass::Peek_Buffer (void)
  432. {
  433. return m_Buffer;
  434. }
  435. ///////////////////////////////////////////////////////////////////
  436. // operator+=
  437. ///////////////////////////////////////////////////////////////////
  438. inline const WideStringClass &
  439. WideStringClass::operator+= (const WideStringClass &string)
  440. {
  441. int src_len = string.Get_Length();
  442. if (src_len > 0) {
  443. int cur_len = Get_Length ();
  444. int new_len = cur_len + src_len;
  445. //
  446. // Make sure our buffer is large enough to hold the new string
  447. //
  448. Resize (new_len + 1);
  449. Store_Length (new_len);
  450. //
  451. // Copy the new string onto our the end of our existing buffer
  452. //
  453. ::memcpy (&m_Buffer[cur_len], (const WCHAR *)string, (src_len + 1) * sizeof (WCHAR));
  454. }
  455. return (*this);
  456. }
  457. ///////////////////////////////////////////////////////////////////
  458. // operator+=
  459. ///////////////////////////////////////////////////////////////////
  460. inline WideStringClass
  461. operator+ (const WideStringClass &string1, const WideStringClass &string2)
  462. {
  463. WideStringClass new_string(string1, true);
  464. new_string += string2;
  465. return new_string;
  466. }
  467. ///////////////////////////////////////////////////////////////////
  468. // operator+=
  469. ///////////////////////////////////////////////////////////////////
  470. inline WideStringClass
  471. operator+ (const WCHAR *string1, const WideStringClass &string2)
  472. {
  473. WideStringClass new_string(string1, true);
  474. new_string += string2;
  475. return new_string;
  476. }
  477. ///////////////////////////////////////////////////////////////////
  478. // operator+=
  479. ///////////////////////////////////////////////////////////////////
  480. inline WideStringClass
  481. operator+ (const WideStringClass &string1, const WCHAR *string2)
  482. {
  483. WideStringClass new_string(string1, true);
  484. new_string += string2;
  485. return new_string;
  486. }
  487. ///////////////////////////////////////////////////////////////////
  488. // Get_Allocated_Length
  489. //
  490. // Return allocated size of the string buffer
  491. ///////////////////////////////////////////////////////////////////
  492. inline int
  493. WideStringClass::Get_Allocated_Length (void) const
  494. {
  495. int allocated_length = 0;
  496. //
  497. // Read the allocated length from the header
  498. //
  499. if (m_Buffer != m_EmptyString) {
  500. HEADER *header = Get_Header ();
  501. allocated_length = header->allocated_length;
  502. }
  503. return allocated_length;
  504. }
  505. ///////////////////////////////////////////////////////////////////
  506. // Get_Length
  507. //
  508. // Return text legth. If length is not known calculate it, otherwise
  509. // just return the previously stored value (strlen tends to take
  510. // quite a lot cpu time if a lot of string combining operations are
  511. // performed.
  512. ///////////////////////////////////////////////////////////////////
  513. inline int
  514. WideStringClass::Get_Length (void) const
  515. {
  516. int length = 0;
  517. if (m_Buffer != m_EmptyString) {
  518. //
  519. // Read the length from the header
  520. //
  521. HEADER *header = Get_Header ();
  522. length = header->length;
  523. //
  524. // Hmmm, a zero length was stored in the header,
  525. // we better manually get the string length.
  526. //
  527. if (length == 0) {
  528. length = wcslen (m_Buffer);
  529. ((WideStringClass *)this)->Store_Length (length);
  530. }
  531. }
  532. return length;
  533. }
  534. ///////////////////////////////////////////////////////////////////
  535. // Set_Buffer_And_Allocated_Length
  536. //
  537. // Set buffer pointer and init size variable. Length is set to 0
  538. // as the contents of the new buffer are not necessarily defined.
  539. ///////////////////////////////////////////////////////////////////
  540. inline void
  541. WideStringClass::Set_Buffer_And_Allocated_Length (WCHAR *buffer, int length)
  542. {
  543. Free_String ();
  544. m_Buffer = buffer;
  545. //
  546. // Update the header (if necessary)
  547. //
  548. if (m_Buffer != m_EmptyString) {
  549. Store_Allocated_Length (length);
  550. Store_Length (0);
  551. } else {
  552. WWASSERT (length == 0);
  553. }
  554. return ;
  555. }
  556. ///////////////////////////////////////////////////////////////////
  557. // Allocate_Buffer
  558. ///////////////////////////////////////////////////////////////////
  559. inline WCHAR *
  560. WideStringClass::Allocate_Buffer (int length)
  561. {
  562. //
  563. // Allocate a buffer that is 'length' characters long, plus the
  564. // bytes required to hold the header.
  565. //
  566. char *buffer = W3DNEWARRAY char[(sizeof (WCHAR) * length) + sizeof (WideStringClass::_HEADER)];
  567. //
  568. // Fill in the fields of the header
  569. //
  570. HEADER *header = reinterpret_cast<HEADER *>(buffer);
  571. header->length = 0;
  572. header->allocated_length = length;
  573. //
  574. // Return the buffer as if it was a WCHAR pointer
  575. //
  576. return reinterpret_cast<WCHAR *>(buffer + sizeof (WideStringClass::_HEADER));
  577. }
  578. ///////////////////////////////////////////////////////////////////
  579. // Get_Header
  580. ///////////////////////////////////////////////////////////////////
  581. inline WideStringClass::HEADER *
  582. WideStringClass::Get_Header (void) const
  583. {
  584. return reinterpret_cast<HEADER *>(((char *)m_Buffer) - sizeof (WideStringClass::_HEADER));
  585. }
  586. ///////////////////////////////////////////////////////////////////
  587. // Store_Allocated_Length
  588. ///////////////////////////////////////////////////////////////////
  589. inline void
  590. WideStringClass::Store_Allocated_Length (int allocated_length)
  591. {
  592. if (m_Buffer != m_EmptyString) {
  593. HEADER *header = Get_Header ();
  594. header->allocated_length = allocated_length;
  595. } else {
  596. WWASSERT (allocated_length == 0);
  597. }
  598. return ;
  599. }
  600. ///////////////////////////////////////////////////////////////////
  601. // Store_Length
  602. //
  603. // Set length... The caller of this (private) function better
  604. // be sure that the len is correct.
  605. ///////////////////////////////////////////////////////////////////
  606. inline void
  607. WideStringClass::Store_Length (int length)
  608. {
  609. if (m_Buffer != m_EmptyString) {
  610. HEADER *header = Get_Header ();
  611. header->length = length;
  612. } else {
  613. WWASSERT (length == 0);
  614. }
  615. return ;
  616. }
  617. ///////////////////////////////////////////////////////////////////
  618. // Convert_From
  619. ///////////////////////////////////////////////////////////////////
  620. inline void
  621. WideStringClass::Convert_From(const char * text)
  622. {
  623. int len = ::strlen ( text );
  624. Uninitialised_Grow ( len + 1 );
  625. if ( len != 0 ) {
  626. WCHAR * ptr = m_Buffer;
  627. while ( *text != 0 ) {
  628. *ptr++ = *text++;
  629. }
  630. *ptr++ = 0;
  631. }
  632. return ;
  633. }
  634. ///////////////////////////////////////////////////////////////////
  635. // Convert_To
  636. ///////////////////////////////////////////////////////////////////
  637. inline void
  638. WideStringClass::Convert_To (StringClass &string)
  639. {
  640. //
  641. // Resize the destination string
  642. //
  643. int len = Get_Length ();
  644. char *dest_buffer = string.Get_Buffer (len + 1);
  645. //
  646. // Copy the strings
  647. //
  648. if (len != 0) {
  649. WCHAR *ptr = m_Buffer;
  650. while (*ptr != 0) {
  651. *dest_buffer++ = *ptr++;
  652. }
  653. *dest_buffer++ = 0;
  654. }
  655. return ;
  656. }
  657. inline void
  658. WideStringClass::Convert_To (StringClass &string) const
  659. {
  660. //
  661. // Resize the destination string
  662. //
  663. int len = Get_Length ();
  664. char *dest_buffer = string.Get_Buffer (len + 1);
  665. //
  666. // Copy the strings
  667. //
  668. if (len != 0) {
  669. WCHAR *ptr = m_Buffer;
  670. while (*ptr != 0) {
  671. *dest_buffer++ = *ptr++;
  672. }
  673. *dest_buffer++ = 0;
  674. }
  675. return ;
  676. }
  677. #endif //__WIDESTRING_H