| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786 |
- /*
- ** Command & Conquer Renegade(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** This program is free software: you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation, either version 3 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- /***********************************************************************************************
- *** 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 ***
- ***********************************************************************************************
- * *
- * Project Name : WWSaveLoad *
- * *
- * $Archive:: /Commando/Code/wwlib/widestring.h $*
- * *
- * Author:: Patrick Smith *
- * *
- * $Modtime:: 2/06/02 4:59p $*
- * *
- * $Revision:: 22 $*
- * *
- *---------------------------------------------------------------------------------------------*
- * Functions: *
- * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
- #if defined(_MSC_VER)
- #pragma once
- #endif
- #ifndef __WIDESTRING_H
- #define __WIDESTRING_H
- #include <string.h>
- #include <stdarg.h>
- #include "always.h"
- #include "wwdebug.h"
- #include "win.h"
- #include "wwstring.h"
- #include "trim.h"
- #include <wchar.h>
- #ifdef _UNIX
- #include "osdep.h"
- #endif
- //////////////////////////////////////////////////////////////////////
- //
- // WideStringClass
- //
- // This is a UNICODE (double-byte) version of StringClass. All
- // operations are performed on wide character strings.
- //
- //////////////////////////////////////////////////////////////////////
- class WideStringClass
- {
- public:
- ////////////////////////////////////////////////////////////
- // Public constructors/destructors
- ////////////////////////////////////////////////////////////
- WideStringClass (int initial_len = 0, bool hint_temporary = false);
- WideStringClass (const WideStringClass &string, bool hint_temporary = false);
- WideStringClass (const WCHAR *string, bool hint_temporary = false);
- WideStringClass (WCHAR ch, bool hint_temporary = false);
- WideStringClass (const char *string, bool hint_temporary = false);
- ~WideStringClass (void);
- ////////////////////////////////////////////////////////////
- // Public operators
- ////////////////////////////////////////////////////////////
- bool operator== (const WCHAR *rvalue) const;
- bool operator!= (const WCHAR *rvalue) const;
- inline const WideStringClass &operator= (const WideStringClass &string);
- inline const WideStringClass &operator= (const WCHAR *string);
- inline const WideStringClass &operator= (WCHAR ch);
- inline const WideStringClass &operator= (const char *string);
- const WideStringClass &operator+= (const WideStringClass &string);
- const WideStringClass &operator+= (const WCHAR *string);
- const WideStringClass &operator+= (WCHAR ch);
- friend WideStringClass operator+ (const WideStringClass &string1, const WideStringClass &string2);
- friend WideStringClass operator+ (const WCHAR *string1, const WideStringClass &string2);
- friend WideStringClass operator+ (const WideStringClass &string1, const WCHAR *string2);
- bool operator < (const WCHAR *string) const;
- bool operator <= (const WCHAR *string) const;
- bool operator > (const WCHAR *string) const;
- bool operator >= (const WCHAR *string) const;
- WCHAR operator[] (int index) const;
- WCHAR& operator[] (int index);
- operator const WCHAR * (void) const;
- ////////////////////////////////////////////////////////////
- // Public methods
- ////////////////////////////////////////////////////////////
- int Compare (const WCHAR *string) const;
- int Compare_No_Case (const WCHAR *string) const;
-
- inline int Get_Length (void) const;
- bool Is_Empty (void) const;
- void Erase (int start_index, int char_count);
- int _cdecl Format (const WCHAR *format, ...);
- int _cdecl Format_Args (const WCHAR *format, const va_list & arg_list );
- bool Convert_From (const char *text);
- bool Convert_To (StringClass &string);
- bool Convert_To (StringClass &string) const;
- // Trim leading and trailing whitespace (chars <= 32)
- void Trim(void);
- // Check if the string is composed of ANSI range characters. (0-255)
- bool Is_ANSI(void);
- WCHAR * Get_Buffer (int new_length);
- WCHAR * Peek_Buffer (void);
- ////////////////////////////////////////////////////////////
- // Static methods
- ////////////////////////////////////////////////////////////
- static void Release_Resources (void);
- private:
- ////////////////////////////////////////////////////////////
- // Private structures
- ////////////////////////////////////////////////////////////
- typedef struct _HEADER
- {
- int allocated_length;
- int length;
- } HEADER;
- ////////////////////////////////////////////////////////////
- // Private constants
- ////////////////////////////////////////////////////////////
- enum
- {
- MAX_TEMP_STRING = 4,
- MAX_TEMP_LEN = 256,
- MAX_TEMP_BYTES = (MAX_TEMP_LEN * sizeof (WCHAR)) + sizeof (HEADER),
- };
- ////////////////////////////////////////////////////////////
- // Private methods
- ////////////////////////////////////////////////////////////
- void Get_String (int length, bool is_temp);
- WCHAR * Allocate_Buffer (int length);
- void Resize (int size);
- void Uninitialised_Grow (int length);
- void Free_String (void);
- inline void Store_Length (int length);
- inline void Store_Allocated_Length (int allocated_length);
- inline HEADER * Get_Header (void) const;
- int Get_Allocated_Length (void) const;
- void Set_Buffer_And_Allocated_Length (WCHAR *buffer, int length);
- ////////////////////////////////////////////////////////////
- // Private member data
- ////////////////////////////////////////////////////////////
- WCHAR * m_Buffer;
- ////////////////////////////////////////////////////////////
- // Static member data
- ////////////////////////////////////////////////////////////
- static char m_TempString1[MAX_TEMP_BYTES];
- static char m_TempString2[MAX_TEMP_BYTES];
- static char m_TempString3[MAX_TEMP_BYTES];
- static char m_TempString4[MAX_TEMP_BYTES];
- static WCHAR * m_FreeTempPtr[MAX_TEMP_STRING];
- static WCHAR * m_ResTempPtr[MAX_TEMP_STRING];
- static int m_UsedTempStringCount;
- static FastCriticalSectionClass m_TempMutex;
- static WCHAR m_NullChar;
- static WCHAR * m_EmptyString;
- };
- ///////////////////////////////////////////////////////////////////
- // WideStringClass
- ///////////////////////////////////////////////////////////////////
- inline
- WideStringClass::WideStringClass (int initial_len, bool hint_temporary)
- : m_Buffer (m_EmptyString)
- {
- Get_String (initial_len, hint_temporary);
- m_Buffer[0] = m_NullChar;
- return ;
- }
- ///////////////////////////////////////////////////////////////////
- // WideStringClass
- ///////////////////////////////////////////////////////////////////
- inline
- WideStringClass::WideStringClass (WCHAR ch, bool hint_temporary)
- : m_Buffer (m_EmptyString)
- {
- Get_String (2, hint_temporary);
- (*this) = ch;
- return ;
- }
- ///////////////////////////////////////////////////////////////////
- // WideStringClass
- ///////////////////////////////////////////////////////////////////
- inline
- WideStringClass::WideStringClass (const WideStringClass &string, bool hint_temporary)
- : m_Buffer (m_EmptyString)
- {
- if (hint_temporary || (string.Get_Length()>1)) {
- Get_String(string.Get_Length()+1, hint_temporary);
- }
- (*this) = string;
- return ;
- }
- ///////////////////////////////////////////////////////////////////
- // WideStringClass
- ///////////////////////////////////////////////////////////////////
- inline
- WideStringClass::WideStringClass (const WCHAR *string, bool hint_temporary)
- : m_Buffer (m_EmptyString)
- {
- int len=string ? wcslen(string) : 0;
- if (hint_temporary || len>0) {
- Get_String (len+1, hint_temporary);
- }
- (*this) = string;
- return ;
- }
- ///////////////////////////////////////////////////////////////////
- // WideStringClass
- ///////////////////////////////////////////////////////////////////
- inline
- WideStringClass::WideStringClass (const char *string, bool hint_temporary)
- : m_Buffer (m_EmptyString)
- {
- if (hint_temporary || (string && strlen(string)>0)) {
- Get_String (strlen(string) + 1, hint_temporary);
- }
- (*this) = string;
- return ;
- }
- ///////////////////////////////////////////////////////////////////
- // ~WideStringClass
- ///////////////////////////////////////////////////////////////////
- inline
- WideStringClass::~WideStringClass (void)
- {
- Free_String ();
- return ;
- }
- ///////////////////////////////////////////////////////////////////
- // Is_Empty
- ///////////////////////////////////////////////////////////////////
- inline bool
- WideStringClass::Is_Empty (void) const
- {
- return (m_Buffer[0] == m_NullChar);
- }
- ///////////////////////////////////////////////////////////////////
- // Compare
- ///////////////////////////////////////////////////////////////////
- inline int
- WideStringClass::Compare (const WCHAR *string) const
- {
- if (string) {
- return wcscmp (m_Buffer, string);
- }
- return -1;
- }
- ///////////////////////////////////////////////////////////////////
- // Compare_No_Case
- ///////////////////////////////////////////////////////////////////
- inline int
- WideStringClass::Compare_No_Case (const WCHAR *string) const
- {
- if (string) {
- return _wcsicmp (m_Buffer, string);
- }
- return -1;
- }
- ///////////////////////////////////////////////////////////////////
- // operator[]
- ///////////////////////////////////////////////////////////////////
- inline WCHAR
- WideStringClass::operator[] (int index) const
- {
- WWASSERT (index >= 0 && index < Get_Length ());
- return m_Buffer[index];
- }
- inline WCHAR&
- WideStringClass::operator[] (int index)
- {
- WWASSERT (index >= 0 && index < Get_Length ());
- return m_Buffer[index];
- }
- ///////////////////////////////////////////////////////////////////
- // operator const WCHAR *
- ///////////////////////////////////////////////////////////////////
- inline
- WideStringClass::operator const WCHAR * (void) const
- {
- return m_Buffer;
- }
- ///////////////////////////////////////////////////////////////////
- // operator==
- ///////////////////////////////////////////////////////////////////
- inline bool
- WideStringClass::operator== (const WCHAR *rvalue) const
- {
- return (Compare (rvalue) == 0);
- }
- ///////////////////////////////////////////////////////////////////
- // operator!=
- ///////////////////////////////////////////////////////////////////
- inline bool
- WideStringClass::operator!= (const WCHAR *rvalue) const
- {
- return (Compare (rvalue) != 0);
- }
- ///////////////////////////////////////////////////////////////////
- // operator=
- ///////////////////////////////////////////////////////////////////
- inline const WideStringClass &
- WideStringClass::operator= (const WideStringClass &string)
- {
- return operator= ((const WCHAR *)string);
- }
- ///////////////////////////////////////////////////////////////////
- // operator <
- ///////////////////////////////////////////////////////////////////
- inline bool
- WideStringClass::operator < (const WCHAR *string) const
- {
- if (string) {
- return (wcscmp (m_Buffer, string) < 0);
- }
- return false;
- }
- ///////////////////////////////////////////////////////////////////
- // operator <=
- ///////////////////////////////////////////////////////////////////
- inline bool
- WideStringClass::operator <= (const WCHAR *string) const
- {
- if (string) {
- return (wcscmp (m_Buffer, string) <= 0);
- }
- return false;
- }
- ///////////////////////////////////////////////////////////////////
- // operator >
- ///////////////////////////////////////////////////////////////////
- inline bool
- WideStringClass::operator > (const WCHAR *string) const
- {
- if (string) {
- return (wcscmp (m_Buffer, string) > 0);
- }
- return true;
- }
- ///////////////////////////////////////////////////////////////////
- // operator >=
- ///////////////////////////////////////////////////////////////////
- inline bool
- WideStringClass::operator >= (const WCHAR *string) const
- {
- if (string) {
- return (wcscmp (m_Buffer, string) >= 0);
- }
- return true;
- }
- ///////////////////////////////////////////////////////////////////
- // Erase
- ///////////////////////////////////////////////////////////////////
- inline void
- WideStringClass::Erase (int start_index, int char_count)
- {
- int len = Get_Length ();
- if (start_index < len) {
-
- if (start_index + char_count > len) {
- char_count = len - start_index;
- }
- ::memmove ( &m_Buffer[start_index],
- &m_Buffer[start_index + char_count],
- (len - (start_index + char_count) + 1) * sizeof (WCHAR));
- Store_Length( wcslen(m_Buffer) );
- }
- return ;
- }
- ///////////////////////////////////////////////////////////////////
- // Trim leading and trailing whitespace (chars <= 32)
- ///////////////////////////////////////////////////////////////////
- inline void WideStringClass::Trim(void)
- {
- wcstrim(m_Buffer);
- int len = wcslen(m_Buffer);
- Store_Length(len);
- }
- ///////////////////////////////////////////////////////////////////
- // operator=
- ///////////////////////////////////////////////////////////////////
- inline const WideStringClass &
- WideStringClass::operator= (const WCHAR *string)
- {
- if (string) {
- int len = wcslen (string);
- Uninitialised_Grow (len + 1);
- Store_Length (len);
- ::memcpy (m_Buffer, string, (len + 1) * sizeof (WCHAR));
- }
- return (*this);
- }
- ///////////////////////////////////////////////////////////////////
- // operator=
- ///////////////////////////////////////////////////////////////////
- inline const WideStringClass &
- WideStringClass::operator= (const char *string)
- {
- Convert_From(string);
- return (*this);
- }
- ///////////////////////////////////////////////////////////////////
- // operator=
- ///////////////////////////////////////////////////////////////////
- inline const WideStringClass &
- WideStringClass::operator= (WCHAR ch)
- {
- Uninitialised_Grow (2);
- m_Buffer[0] = ch;
- m_Buffer[1] = m_NullChar;
- Store_Length (1);
- return (*this);
- }
- ///////////////////////////////////////////////////////////////////
- // operator+=
- ///////////////////////////////////////////////////////////////////
- inline const WideStringClass &
- WideStringClass::operator+= (const WCHAR *string)
- {
- if (string) {
- int cur_len = Get_Length ();
- int src_len = wcslen (string);
- int new_len = cur_len + src_len;
- //
- // Make sure our buffer is large enough to hold the new string
- //
- Resize (new_len + 1);
- Store_Length (new_len);
- //
- // Copy the new string onto our the end of our existing buffer
- //
- ::memcpy (&m_Buffer[cur_len], string, (src_len + 1) * sizeof (WCHAR));
- }
- return (*this);
- }
- ///////////////////////////////////////////////////////////////////
- // operator+=
- ///////////////////////////////////////////////////////////////////
- inline const WideStringClass &
- WideStringClass::operator+= (WCHAR ch)
- {
- int cur_len = Get_Length ();
- Resize (cur_len + 2);
- m_Buffer[cur_len] = ch;
- m_Buffer[cur_len + 1] = m_NullChar;
-
- if (ch != m_NullChar) {
- Store_Length (cur_len + 1);
- }
- return (*this);
- }
- ///////////////////////////////////////////////////////////////////
- // Get_Buffer
- ///////////////////////////////////////////////////////////////////
- inline WCHAR *
- WideStringClass::Get_Buffer (int new_length)
- {
- Uninitialised_Grow (new_length);
- return m_Buffer;
- }
- ///////////////////////////////////////////////////////////////////
- // Peek_Buffer
- ///////////////////////////////////////////////////////////////////
- inline WCHAR *
- WideStringClass::Peek_Buffer (void)
- {
- return m_Buffer;
- }
- ///////////////////////////////////////////////////////////////////
- // operator+=
- ///////////////////////////////////////////////////////////////////
- inline const WideStringClass &
- WideStringClass::operator+= (const WideStringClass &string)
- {
- int src_len = string.Get_Length();
- if (src_len > 0) {
- int cur_len = Get_Length ();
- int new_len = cur_len + src_len;
- //
- // Make sure our buffer is large enough to hold the new string
- //
- Resize (new_len + 1);
- Store_Length (new_len);
- //
- // Copy the new string onto our the end of our existing buffer
- //
- ::memcpy (&m_Buffer[cur_len], (const WCHAR *)string, (src_len + 1) * sizeof (WCHAR));
- }
- return (*this);
- }
- ///////////////////////////////////////////////////////////////////
- // operator+=
- ///////////////////////////////////////////////////////////////////
- inline WideStringClass
- operator+ (const WideStringClass &string1, const WideStringClass &string2)
- {
- WideStringClass new_string(string1, true);
- new_string += string2;
- return new_string;
- }
- ///////////////////////////////////////////////////////////////////
- // operator+=
- ///////////////////////////////////////////////////////////////////
- inline WideStringClass
- operator+ (const WCHAR *string1, const WideStringClass &string2)
- {
- WideStringClass new_string(string1, true);
- new_string += string2;
- return new_string;
- }
- ///////////////////////////////////////////////////////////////////
- // operator+=
- ///////////////////////////////////////////////////////////////////
- inline WideStringClass
- operator+ (const WideStringClass &string1, const WCHAR *string2)
- {
- WideStringClass new_string(string1, true);
- new_string += string2;
- return new_string;
- }
- ///////////////////////////////////////////////////////////////////
- // Get_Allocated_Length
- //
- // Return allocated size of the string buffer
- ///////////////////////////////////////////////////////////////////
- inline int
- WideStringClass::Get_Allocated_Length (void) const
- {
- int allocated_length = 0;
- //
- // Read the allocated length from the header
- //
- if (m_Buffer != m_EmptyString) {
- HEADER *header = Get_Header ();
- allocated_length = header->allocated_length;
- }
- return allocated_length;
- }
- ///////////////////////////////////////////////////////////////////
- // Get_Length
- //
- // Return text legth. If length is not known calculate it, otherwise
- // just return the previously stored value (strlen tends to take
- // quite a lot cpu time if a lot of string combining operations are
- // performed.
- ///////////////////////////////////////////////////////////////////
- inline int
- WideStringClass::Get_Length (void) const
- {
- int length = 0;
- if (m_Buffer != m_EmptyString) {
-
- //
- // Read the length from the header
- //
- HEADER *header = Get_Header ();
- length = header->length;
-
- //
- // Hmmm, a zero length was stored in the header,
- // we better manually get the string length.
- //
- if (length == 0) {
- length = wcslen (m_Buffer);
- ((WideStringClass *)this)->Store_Length (length);
- }
- }
- return length;
- }
- ///////////////////////////////////////////////////////////////////
- // Set_Buffer_And_Allocated_Length
- //
- // Set buffer pointer and init size variable. Length is set to 0
- // as the contents of the new buffer are not necessarily defined.
- ///////////////////////////////////////////////////////////////////
- inline void
- WideStringClass::Set_Buffer_And_Allocated_Length (WCHAR *buffer, int length)
- {
- Free_String ();
- m_Buffer = buffer;
- //
- // Update the header (if necessary)
- //
- if (m_Buffer != m_EmptyString) {
- Store_Allocated_Length (length);
- Store_Length (0);
- } else {
- WWASSERT (length == 0);
- }
- return ;
- }
- ///////////////////////////////////////////////////////////////////
- // Allocate_Buffer
- ///////////////////////////////////////////////////////////////////
- inline WCHAR *
- WideStringClass::Allocate_Buffer (int length)
- {
- //
- // Allocate a buffer that is 'length' characters long, plus the
- // bytes required to hold the header.
- //
- char *buffer = new char[(sizeof (WCHAR) * length) + sizeof (WideStringClass::_HEADER)];
-
- //
- // Fill in the fields of the header
- //
- HEADER *header = reinterpret_cast<HEADER *>(buffer);
- header->length = 0;
- header->allocated_length = length;
- //
- // Return the buffer as if it was a WCHAR pointer
- //
- return reinterpret_cast<WCHAR *>(buffer + sizeof (WideStringClass::_HEADER));
- }
- ///////////////////////////////////////////////////////////////////
- // Get_Header
- ///////////////////////////////////////////////////////////////////
- inline WideStringClass::HEADER *
- WideStringClass::Get_Header (void) const
- {
- return reinterpret_cast<HEADER *>(((char *)m_Buffer) - sizeof (WideStringClass::_HEADER));
- }
- ///////////////////////////////////////////////////////////////////
- // Store_Allocated_Length
- ///////////////////////////////////////////////////////////////////
- inline void
- WideStringClass::Store_Allocated_Length (int allocated_length)
- {
- if (m_Buffer != m_EmptyString) {
- HEADER *header = Get_Header ();
- header->allocated_length = allocated_length;
- } else {
- WWASSERT (allocated_length == 0);
- }
- return ;
- }
- ///////////////////////////////////////////////////////////////////
- // Store_Length
- //
- // Set length... The caller of this (private) function better
- // be sure that the len is correct.
- ///////////////////////////////////////////////////////////////////
- inline void
- WideStringClass::Store_Length (int length)
- {
- if (m_Buffer != m_EmptyString) {
- HEADER *header = Get_Header ();
- header->length = length;
- } else {
- WWASSERT (length == 0);
- }
- return ;
- }
- ///////////////////////////////////////////////////////////////////
- // Convert_To
- ///////////////////////////////////////////////////////////////////
- inline bool
- WideStringClass::Convert_To (StringClass &string)
- {
- return (string.Copy_Wide (m_Buffer));
- }
- inline bool
- WideStringClass::Convert_To (StringClass &string) const
- {
- return (string.Copy_Wide (m_Buffer));
- }
- #endif //__WIDESTRING_H
|