1 // Win32++ Version 7.3
\r
2 // Released: 30th November 2011
\r
6 // url: https://sourceforge.net/projects/win32-framework
\r
9 // Copyright (c) 2005-2011 David Nash
\r
11 // Permission is hereby granted, free of charge, to
\r
12 // any person obtaining a copy of this software and
\r
13 // associated documentation files (the "Software"),
\r
14 // to deal in the Software without restriction, including
\r
15 // without limitation the rights to use, copy, modify,
\r
16 // merge, publish, distribute, sublicense, and/or sell
\r
17 // copies of the Software, and to permit persons to whom
\r
18 // the Software is furnished to do so, subject to the
\r
19 // following conditions:
\r
21 // The above copyright notice and this permission notice
\r
22 // shall be included in all copies or substantial portions
\r
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
\r
26 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
\r
27 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
\r
28 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
\r
29 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
\r
30 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
\r
31 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
32 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
\r
33 // OR OTHER DEALINGS IN THE SOFTWARE.
\r
35 ////////////////////////////////////////////////////////
\r
38 // Acknowledgements:
\r
39 // Thanks to Adam Szulc for his initial CString code.
\r
41 ////////////////////////////////////////////////////////
\r
43 // Declaration of the cstring.h
\r
45 // This class is intended to provide a simple alternative to the MFC/ATL
\r
46 // CString class that ships with Microsoft compilers. The CString class
\r
47 // specified here is compatible with other compilers such as Borland 5.5
\r
50 // Differences between this class and the MFC/ATL CString class
\r
51 // ------------------------------------------------------------
\r
52 // 1) The constructors for this class accepts both ANSI and Unicode characters and
\r
53 // automatically converts these to TCHAR as required.
\r
55 // 2) This class is not reference counted, so these CStrings should be passed as
\r
56 // references or const references when used as function arguments. As a result there
\r
57 // is no need for functions like LockBuffer and UnLockBuffer.
\r
59 // 3) The Format functions only accepts POD (Plain Old Data) arguments. It does not
\r
60 // accept arguments which are class or struct objects. In particular it does not
\r
61 // accept CString objects, unless these are cast to LPCTSTR.
\r
62 // This is demonstrates valid and invalid usage:
\r
63 // CString string1(_T("Hello World"));
\r
66 // // This is invalid, and produces undefined behaviour.
\r
67 // string2.Format(_T("String1 is: %s"), string1); // No! you can't do this
\r
70 // string2.Format(_T("String1 is: %s"), (LPCTSTR)string1); // Yes, this is correct
\r
72 // Note: The MFC/ATL CString class uses a non portable hack to make its CString class
\r
73 // behave like a POD. Other compilers (such as the MinGW compiler) specifically
\r
74 // prohibit the use of non POD types for functions with variable argument lists.
\r
76 // 4) This class provides a few additional functions:
\r
77 // c_str Returns a const TCHAR string. This is an alternative for casting to LPCTSTR.
\r
78 // GetErrorString Assigns CString to the error string for the specified System Error Code
\r
79 // (from ::GetLastErrror() for example).
\r
80 // GetString Returns a reference to the underlying std::basic_string<TCHAR>. This
\r
81 // reference can be used to modify the string directly.
\r
85 #ifndef _WIN32XX_CSTRING_H_
\r
86 #define _WIN32XX_CSTRING_H_
\r
89 #include "wincore.h"
\r
97 // friend functions allow the left hand side to be something other than CString
\r
98 friend CString operator + (const CString& string1, const CString& string2);
\r
99 friend CString operator + (const CString& string, LPCTSTR pszText);
\r
100 friend CString operator + (const CString& string, TCHAR ch);
\r
101 friend CString operator + (LPCTSTR pszText, const CString& string);
\r
102 friend CString operator + (TCHAR ch, const CString& string);
\r
103 friend bool operator < (const CString& string1, const CString& string2);
\r
104 friend bool operator > (const CString& string1, const CString& string2);
\r
105 friend bool operator < (const CString& string1, LPCTSTR pszText);
\r
106 friend bool operator > (const CString& string1, LPCTSTR pszText);
\r
107 friend bool operator <= (const CString& string1, const CString& string2);
\r
108 friend bool operator >= (const CString& string1, const CString& string2);
\r
109 friend bool operator <= (const CString& string1, LPCTSTR pszText);
\r
110 friend bool operator >= (const CString& string1, LPCTSTR pszText);
\r
115 CString(const CString& str);
\r
116 CString(LPCSTR pszText);
\r
117 CString(LPCWSTR pszText);
\r
118 CString(TCHAR ch, int nLength = 1);
\r
119 CString(LPCTSTR pszText, int nLength);
\r
121 CString& operator = (const CString& str);
\r
122 CString& operator = (const TCHAR ch);
\r
123 CString& operator = (LPCSTR pszText);
\r
124 CString& operator = (LPCWSTR pszText);
\r
125 bool operator == (LPCTSTR pszText);
\r
126 bool operator != (LPCTSTR pszText);
\r
127 operator LPCTSTR() const;
\r
128 TCHAR& operator [] (int nIndex);
\r
129 CString& operator += (const CString& str);
\r
130 CString& operator += (LPCSTR szText);
\r
131 CString& operator += (LPCWSTR szText);
\r
134 LPCTSTR c_str() const { return m_str.c_str(); } // alternative for casting to LPCTSTR
\r
135 tString& GetString() { return m_str; } // returns a reference to the underlying std::basic_string<TCHAR>
\r
136 int GetLength() const { return (int)m_str.length(); } // returns the length in characters
\r
139 BSTR AllocSysString() const;
\r
140 void AppendFormat(LPCTSTR pszFormat,...);
\r
141 void AppendFormat(UINT nFormatID, ...);
\r
142 int Compare(LPCTSTR pszText) const;
\r
143 int CompareNoCase(LPCTSTR pszText) const;
\r
144 int Delete(int nIndex, int nCount = 1);
\r
145 int Find(TCHAR ch, int nIndex = 0 ) const;
\r
146 int Find(LPCTSTR pszText, int nStart = 0) const;
\r
147 int FindOneOf(LPCTSTR pszText) const;
\r
148 void Format(UINT nID, ...);
\r
149 void Format(LPCTSTR pszFormat,...);
\r
150 void FormatV(LPCTSTR pszFormat, va_list args);
\r
151 void FormatMessage(LPCTSTR pszFormat,...);
\r
152 void FormatMessageV(LPCTSTR pszFormat, va_list args);
\r
153 TCHAR GetAt(int nIndex) const;
\r
154 LPTSTR GetBuffer(int nMinBufLength);
\r
155 void GetErrorString(DWORD dwError);
\r
157 int Insert(int nIndex, TCHAR ch);
\r
158 int Insert(int nIndex, const CString& str);
\r
159 bool IsEmpty() const;
\r
160 CString Left(int nCount) const;
\r
161 bool LoadString(UINT nID);
\r
163 void MakeReverse();
\r
165 CString Mid(int nFirst) const;
\r
166 CString Mid(int nFirst, int nCount) const;
\r
167 void ReleaseBuffer( int nNewLength = -1 );
\r
168 int Remove(LPCTSTR pszText);
\r
169 int Replace(TCHAR chOld, TCHAR chNew);
\r
170 int Replace(const LPCTSTR pszOld, LPCTSTR pszNew);
\r
171 int ReverseFind(LPCTSTR pszText, int nStart = -1) const;
\r
172 CString Right(int nCount) const;
\r
173 void SetAt(int nIndex, TCHAR ch);
\r
174 BSTR SetSysString(BSTR* pBstr) const;
\r
175 CString SpanExcluding(LPCTSTR pszText) const;
\r
176 CString SpanIncluding(LPCTSTR pszText) const;
\r
177 CString Tokenize(LPCTSTR pszTokens, int& iStart) const;
\r
180 void TrimLeft(TCHAR chTarget);
\r
181 void TrimLeft(LPCTSTR pszTargets);
\r
183 void TrimRight(TCHAR chTarget);
\r
184 void TrimRight(LPCTSTR pszTargets);
\r
185 void Truncate(int nNewLength);
\r
188 int Collate(LPCTSTR pszText) const;
\r
189 int CollateNoCase(LPCTSTR pszText) const;
\r
190 bool GetEnvironmentVariable(LPCTSTR pszVar);
\r
195 std::vector<TCHAR> m_buf;
\r
198 inline CString::CString()
\r
202 inline CString::~CString()
\r
206 inline CString::CString(const CString& str)
\r
211 inline CString::CString(LPCSTR pszText)
\r
213 m_str.assign(A2T(pszText));
\r
216 inline CString::CString(LPCWSTR pszText)
\r
218 m_str.assign(W2T(pszText));
\r
221 inline CString::CString(TCHAR ch, int nLength)
\r
223 m_str.assign(nLength, ch);
\r
226 inline CString::CString(LPCTSTR pszText, int nLength)
\r
228 m_str.assign(pszText, nLength);
\r
231 inline CString& CString::operator = (const CString& str)
\r
237 inline CString& CString::operator = (const TCHAR ch)
\r
239 m_str.assign(1, ch);
\r
243 inline CString& CString::operator = (LPCSTR pszText)
\r
245 m_str.assign(A2T(pszText));
\r
249 inline CString& CString::operator = (LPCWSTR pszText)
\r
251 m_str.assign(W2T(pszText));
\r
255 inline bool CString::operator == (LPCTSTR pszText)
\r
256 // Returns TRUE if the strings have the same content
\r
259 return (0 == Compare(pszText));
\r
262 inline bool CString::operator != (LPCTSTR pszText)
\r
263 // Returns TRUE if the strings have a different content
\r
266 return Compare(pszText) != 0;
\r
269 inline CString::operator LPCTSTR() const
\r
271 return m_str.c_str();
\r
274 inline TCHAR& CString::operator [] (int nIndex)
\r
276 assert(nIndex >= 0);
\r
277 assert(nIndex < GetLength());
\r
278 return m_str[nIndex];
\r
281 inline CString& CString::operator += (const CString& str)
\r
287 inline CString& CString::operator += (LPCSTR szText)
\r
289 m_str.append(A2T(szText));
\r
293 inline CString& CString::operator += (LPCWSTR szText)
\r
295 m_str.append(W2T(szText));
\r
299 inline BSTR CString::AllocSysString() const
\r
300 // Allocates a BSTR from the CString content.
\r
302 return ::SysAllocStringLen(T2W(m_str.c_str()), (UINT)m_str.size());
\r
305 inline void CString::AppendFormat(LPCTSTR pszFormat,...)
\r
306 // Appends formatted data to an the CString content.
\r
311 va_start(args, pszFormat);
\r
312 str.FormatV(pszFormat, args);
\r
318 inline void CString::AppendFormat(UINT nFormatID, ...)
\r
319 // Appends formatted data to an the CString content.
\r
324 if (str1.LoadString(nFormatID))
\r
327 va_start(args, nFormatID);
\r
328 str2.FormatV(str1.c_str(), args);
\r
331 m_str.append(str2);
\r
336 inline int CString::Collate(LPCTSTR pszText) const
\r
337 // Performs a case sensitive comparison of the two strings using locale-specific information.
\r
340 return _tcscoll(m_str.c_str(), pszText);
\r
343 inline int CString::CollateNoCase(LPCTSTR pszText) const
\r
344 // Performs a case insensitive comparison of the two strings using locale-specific information.
\r
347 return _tcsicoll(m_str.c_str(), pszText);
\r
349 #endif // _WIN32_WCE
\r
351 inline int CString::Compare(LPCTSTR pszText) const
\r
352 // Performs a case sensitive comparison of the two strings.
\r
355 return _tcscmp(m_str.data(), pszText);
\r
358 inline int CString::CompareNoCase(LPCTSTR pszText) const
\r
359 // Performs a case insensitive comparison of the two strings.
\r
362 return _tcsicmp(m_str.data(), pszText);
\r
365 inline int CString::Delete(int nIndex, int nCount /* = 1 */)
\r
366 // Deletes a character or characters from the string.
\r
368 assert(nIndex >= 0);
\r
369 assert(nCount >= 0);
\r
371 m_str.erase(nIndex, nCount);
\r
372 return (int)m_str.size();
\r
375 inline void CString::Empty()
\r
376 // Erases the contents of the string.
\r
381 inline int CString::Find(TCHAR ch, int nIndex /* = 0 */) const
\r
382 // Finds a character in the string.
\r
384 assert(nIndex >= 0);
\r
385 return (int)m_str.find(ch, nIndex);
\r
388 inline int CString::Find(LPCTSTR pszText, int nIndex /* = 0 */) const
\r
389 // Finds a substring within the string.
\r
392 assert(nIndex >= 0);
\r
393 return (int)m_str.find(pszText, nIndex);
\r
396 inline int CString::FindOneOf(LPCTSTR pszText) const
\r
397 // Finds the first matching character from a set.
\r
400 return (int)m_str.find_first_of(pszText);
\r
403 inline void CString::Format(LPCTSTR pszFormat,...)
\r
404 // Formats the string as sprintf does.
\r
407 va_start(args, pszFormat);
\r
408 FormatV(pszFormat, args);
\r
412 inline void CString::Format(UINT nID, ...)
\r
413 // Formats the string as sprintf does.
\r
416 if (str.LoadString(nID))
\r
419 va_start(args, nID);
\r
420 FormatV(str.c_str(), args);
\r
425 inline void CString::FormatV(LPCTSTR pszFormat, va_list args)
\r
426 // Formats the string using a variable list of arguments.
\r
430 int nResult = -1, nLength = 256;
\r
432 // A vector is used to store the TCHAR array
\r
433 std::vector<TCHAR> vBuffer;( nLength+1, _T('\0') );
\r
435 while (-1 == nResult)
\r
437 vBuffer.assign( nLength+1, _T('\0') );
\r
438 nResult = _vsntprintf(&vBuffer[0], nLength, pszFormat, args);
\r
441 m_str.assign(&vBuffer[0]);
\r
445 inline void CString::FormatMessage(LPCTSTR pszFormat,...)
\r
446 // Formats a message string.
\r
449 va_start(args, pszFormat);
\r
450 FormatMessageV(pszFormat, args);
\r
454 inline void CString::FormatMessageV(LPCTSTR pszFormat, va_list args)
\r
455 // Formats a message string using a variable argument list.
\r
457 LPTSTR pszTemp = 0;
\r
460 DWORD dwResult = ::FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER, pszFormat, 0, 0, pszTemp, 0, &args);
\r
462 if (0 == dwResult || 0 == pszTemp )
\r
463 throw std::bad_alloc();
\r
466 LocalFree(pszTemp);
\r
470 inline TCHAR CString::GetAt(int nIndex) const
\r
471 // Returns the character at the specified location within the string.
\r
473 assert(nIndex >= 0);
\r
474 assert(nIndex < GetLength());
\r
475 return m_str[nIndex];
\r
478 inline LPTSTR CString::GetBuffer(int nMinBufLength)
\r
479 // Creates a buffer of nMinBufLength charaters (+1 extra for NULL termination) and returns
\r
480 // a pointer to this buffer. This buffer can be used by any function which accepts a LPTSTR.
\r
481 // Care must be taken not to exceed the length of the buffer. Use ReleaseBuffer to safely
\r
482 // copy this buffer back to the CString object.
\r
484 // Note: The buffer uses a vector. Vectors are required to be contiguous in memory under
\r
485 // the current standard, whereas std::strings do not have this requirement.
\r
487 assert (nMinBufLength >= 0);
\r
489 m_buf.assign(nMinBufLength + 1, _T('\0'));
\r
490 tString::iterator it_end;
\r
492 if (m_str.length() >= (size_t)nMinBufLength)
\r
494 it_end = m_str.begin();
\r
495 std::advance(it_end, nMinBufLength);
\r
498 it_end = m_str.end();
\r
500 std::copy(m_str.begin(), it_end, m_buf.begin());
\r
506 inline bool CString::GetEnvironmentVariable(LPCTSTR pszVar)
\r
507 // Sets the string to the value of the specified environment variable.
\r
512 int nLength = ::GetEnvironmentVariable(pszVar, NULL, 0);
\r
515 std::vector<TCHAR> vBuffer( nLength+1, _T('\0') );
\r
516 ::GetEnvironmentVariable(pszVar, &vBuffer[0], nLength);
\r
517 m_str = &vBuffer[0];
\r
520 return (nLength != 0);
\r
522 #endif // _WIN32_WCE
\r
524 inline void CString::GetErrorString(DWORD dwError)
\r
525 // Returns the error string for the specified System Error Code (e.g from GetLastErrror).
\r
532 DWORD dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
\r
533 ::FormatMessage(dwFlags, NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&pTemp, 1, NULL);
\r
534 m_str.assign(pTemp);
\r
535 ::LocalFree(pTemp);
\r
539 inline int CString::Insert(int nIndex, TCHAR ch)
\r
540 // Inserts a single character or a substring at the given index within the string.
\r
542 assert(nIndex >= 0);
\r
545 m_str.insert(nIndex, &ch, 1);
\r
546 return (int)m_str.size();
\r
549 inline int CString::Insert(int nIndex, const CString& str)
\r
550 // Inserts a single character or a substring at the given index within the string.
\r
552 assert(nIndex >= 0);
\r
554 m_str.insert(nIndex, str);
\r
555 return (int)m_str.size();
\r
558 inline bool CString::IsEmpty() const
\r
559 // Returns TRUE if the string is empty
\r
561 return m_str.empty();
\r
564 inline CString CString::Left(int nCount) const
\r
565 // Extracts the left part of a string.
\r
567 assert(nCount >= 0);
\r
570 str.m_str.assign(c_str(), 0, nCount);
\r
574 inline bool CString::LoadString(UINT nID)
\r
575 // Loads the string from a Windows resource.
\r
580 TCHAR* pTCharArray = 0;
\r
581 std::vector<TCHAR> vString;
\r
582 int nTChars = nSize;
\r
586 // Increase the size of our array in a loop until we load the entire string
\r
587 // The ANSI and _UNICODE versions of LoadString behave differently. This technique works for both.
\r
588 while ( nSize-1 <= nTChars )
\r
591 vString.assign(nSize+1, _T('\0'));
\r
592 pTCharArray = &vString[0];
\r
593 nTChars = ::LoadString (GetApp()->GetResourceHandle(), nID, pTCharArray, nSize);
\r
597 m_str.assign(pTCharArray);
\r
599 return (nTChars != 0);
\r
602 inline void CString::MakeLower()
\r
603 // Converts all the characters in this string to lowercase characters.
\r
605 std::transform(m_str.begin(), m_str.end(), m_str.begin(), &::tolower);
\r
608 inline void CString::MakeReverse()
\r
609 // Reverses the string.
\r
611 // Error 2285 with Borland 5.5 occurs here unless option -tWM is used instead of -tW
\r
612 std::reverse(m_str.begin(), m_str.end());
\r
615 inline void CString::MakeUpper()
\r
616 // Converts all the characters in this string to uppercase characters.
\r
618 // Error 2285 with Borland 5.5 occurs here unless option -tWM is used instead of -tW
\r
619 std::transform(m_str.begin(), m_str.end(), m_str.begin(), &::toupper);
\r
622 inline CString CString::Mid(int nFirst) const
\r
623 // Extracts the middle part of a string.
\r
625 return Mid(nFirst, GetLength());
\r
628 inline CString CString::Mid(int nFirst, int nCount) const
\r
629 // Extracts the middle part of a string.
\r
631 assert(nFirst >= 0);
\r
632 assert(nCount >= 0);
\r
635 str.m_str.assign(c_str(), nFirst, nFirst + nCount);
\r
639 inline int CString::ReverseFind(LPCTSTR pszText, int nIndex /* = -1 */) const
\r
640 // Search for a substring within the string, starting from the end.
\r
643 return (int)m_str.rfind(pszText, nIndex);
\r
646 inline void CString::SetAt(int nIndex, TCHAR ch)
\r
647 // Sets the character at the specificed position to the specified value.
\r
649 assert(nIndex >= 0);
\r
650 assert(nIndex < GetLength());
\r
651 m_str[nIndex] = ch;
\r
654 inline void CString::ReleaseBuffer( int nNewLength /*= -1*/ )
\r
655 // This copies the contents of the buffer (acquired by GetBuffer) to this CString,
\r
656 // and releases the contents of the buffer. The default length of -1 copies from the
\r
657 // buffer until a null terminator is reached. If the buffer doesn't contain a null
\r
658 // terminator, you must specify the buffer's length.
\r
660 assert (nNewLength > 0 || -1 == nNewLength);
\r
661 assert (nNewLength < (int)m_buf.size());
\r
663 if (-1 == nNewLength)
\r
664 nNewLength = lstrlen(&m_buf[0]);
\r
665 m_str.assign(nNewLength+1, _T('\0'));
\r
667 std::vector<TCHAR>::iterator it_end = m_buf.begin();
\r
668 std::advance(it_end, nNewLength);
\r
670 std::copy(m_buf.begin(), it_end, m_str.begin());
\r
674 inline int CString::Remove(LPCTSTR pszText)
\r
675 // Removes each occurrence of the specified substring from the string.
\r
681 while ((pos = m_str.find(pszText, pos)) != std::string::npos)
\r
683 m_str.erase(pos, lstrlen(pszText));
\r
689 inline int CString::Replace(TCHAR chOld, TCHAR chNew)
\r
690 // Replaces each occurance of the old character with the new character.
\r
693 tString::iterator it = m_str.begin();
\r
694 while (it != m_str.end())
\r
706 inline int CString::Replace(LPCTSTR pszOld, LPCTSTR pszNew)
\r
707 // Replaces each occurance of the old substring with the new substring.
\r
714 while ((pos = m_str.find(pszOld, pos)) != std::string::npos)
\r
716 m_str.replace(pos, lstrlen(pszOld), pszNew);
\r
717 pos += lstrlen(pszNew);
\r
723 inline CString CString::Right(int nCount) const
\r
724 // Extracts the right part of a string.
\r
726 assert(nCount >= 0);
\r
729 str.m_str.assign(c_str(), m_str.size() - nCount, nCount);
\r
733 inline BSTR CString::SetSysString(BSTR* pBstr) const
\r
734 // Sets an existing BSTR object to the string.
\r
738 if ( !::SysReAllocStringLen(pBstr, T2W(m_str.c_str()), (UINT)m_str.length()) )
\r
739 throw std::bad_alloc();
\r
744 inline CString CString::SpanExcluding(LPCTSTR pszText) const
\r
745 // Extracts characters from the string, starting with the first character,
\r
746 // that are not in the set of characters identified by pszCharSet.
\r
753 while ((pos = m_str.find_first_not_of(pszText, pos)) != std::string::npos)
\r
755 str.m_str.append(1, m_str[pos++]);
\r
761 inline CString CString::SpanIncluding(LPCTSTR pszText) const
\r
762 // Extracts a substring that contains only the characters in a set.
\r
769 while ((pos = m_str.find_first_of(pszText, pos)) != std::string::npos)
\r
771 str.m_str.append(1, m_str[pos++]);
\r
777 inline CString CString::Tokenize(LPCTSTR pszTokens, int& iStart) const
\r
778 // Extracts specified tokens in a target string.
\r
781 assert(iStart >= 0);
\r
784 size_t pos1 = m_str.find_first_not_of(pszTokens, iStart);
\r
785 size_t pos2 = m_str.find_first_of(pszTokens, pos1);
\r
787 iStart = (int)pos2 + 1;
\r
788 if (pos2 == m_str.npos)
\r
791 if (pos1 != m_str.npos)
\r
792 str.m_str = m_str.substr(pos1, pos2-pos1);
\r
797 inline void CString::Trim()
\r
798 // Trims all leading and trailing whitespace characters from the string.
\r
804 inline void CString::TrimLeft()
\r
805 // Trims leading whitespace characters from the string.
\r
807 // This method is supported by the Borland 5.5 compiler
\r
808 tString::iterator iter;
\r
809 for (iter = m_str.begin(); iter < m_str.end(); ++iter)
\r
811 if (!isspace(*iter))
\r
815 m_str.erase(m_str.begin(), iter);
\r
818 inline void CString::TrimLeft(TCHAR chTarget)
\r
819 // Trims the specified character from the beginning of the string.
\r
821 m_str.erase(0, m_str.find_first_not_of(chTarget));
\r
824 inline void CString::TrimLeft(LPCTSTR pszTargets)
\r
825 // Trims the specified set of characters from the beginning of the string.
\r
827 assert(pszTargets);
\r
828 m_str.erase(0, m_str.find_first_not_of(pszTargets));
\r
831 inline void CString::TrimRight()
\r
832 // Trims trailing whitespace characters from the string.
\r
834 // This method is supported by the Borland 5.5 compiler
\r
835 tString::reverse_iterator riter;
\r
836 for (riter = m_str.rbegin(); riter < m_str.rend(); ++riter)
\r
838 if (!isspace(*riter))
\r
842 m_str.erase(riter.base(), m_str.end());
\r
845 inline void CString::TrimRight(TCHAR chTarget)
\r
846 // Trims the specified character from the end of the string.
\r
848 size_t pos = m_str.find_last_not_of(chTarget);
\r
849 if (pos != std::string::npos)
\r
850 m_str.erase(++pos);
\r
853 inline void CString::TrimRight(LPCTSTR pszTargets)
\r
854 // Trims the specified set of characters from the end of the string.
\r
856 assert(pszTargets);
\r
858 size_t pos = m_str.find_last_not_of(pszTargets);
\r
859 if (pos != std::string::npos)
\r
860 m_str.erase(++pos);
\r
863 inline void CString::Truncate(int nNewLength)
\r
864 // Reduces the length of the string to the specified amount.
\r
866 if (nNewLength < GetLength())
\r
868 assert(nNewLength >= 0);
\r
869 m_str.erase(nNewLength);
\r
874 ///////////////////////////////////
\r
875 // Global Functions
\r
878 // friend functions of CString
\r
879 inline CString operator + (const CString& string1, const CString& string2)
\r
881 CString str(string1);
\r
882 str.m_str.append(string2.m_str);
\r
886 inline CString operator + (const CString& string, LPCTSTR pszText)
\r
888 CString str(string);
\r
889 str.m_str.append(pszText);
\r
893 inline CString operator + (const CString& string, TCHAR ch)
\r
895 CString str(string);
\r
896 str.m_str.append(1, ch);
\r
900 inline CString operator + (LPCTSTR pszText, const CString& string)
\r
902 CString str(pszText);
\r
903 str.m_str.append(string);
\r
907 inline CString operator + (TCHAR ch, const CString& string)
\r
910 str.m_str.append(string);
\r
914 inline bool operator < (const CString& string1, const CString& string2)
\r
916 return string1.Compare(string2) < 0;
\r
919 inline bool operator > (const CString& string1, const CString& string2)
\r
921 return string1.Compare(string2) > 0;
\r
924 inline bool operator <= (const CString& string1, const CString& string2)
\r
926 return string1.Compare(string2) <= 0;
\r
929 inline bool operator >= (const CString& string1, const CString& string2)
\r
931 return string1.Compare(string2) >= 0;
\r
934 inline bool operator < (const CString& string1, LPCTSTR pszText)
\r
936 return string1.Compare(pszText) < 0;
\r
939 inline bool operator > (const CString& string1, LPCTSTR pszText)
\r
941 return string1.Compare(pszText) > 0;
\r
944 inline bool operator <= (const CString& string1, LPCTSTR pszText)
\r
946 return string1.Compare(pszText) <= 0;
\r
949 inline bool operator >= (const CString& string1, LPCTSTR pszText)
\r
951 return string1.Compare(pszText) >= 0;
\r
954 // Global LoadString
\r
955 inline CString LoadString(UINT nID)
\r
958 str.LoadString(nID);
\r
963 } // namespace Win32xx
\r
965 #endif//_WIN32XX_CSTRING_H_
\r