home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / prefs / nsdlg / public / cstring.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.3 KB  |  129 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef __CSTRING_H_
  20. #define __CSTRING_H_
  21.  
  22. class CString {
  23.     public:
  24.         // Constructors
  25.         CString();
  26.         CString(const CString& stringSrc);
  27.         CString(LPCSTR lpsz);
  28. #ifdef _WIN32
  29.         CString(LPOLESTR lpsz);
  30. #endif
  31.  
  32.         // Attributes & Operations
  33.         int        GetLength() const;
  34.         BOOL    IsEmpty() const;
  35.         void    Empty();                      // free up the data
  36.         
  37.         char    GetAt(int nIndex) const;      // 0 based
  38.         char    operator[](int nIndex) const; // same as GetAt
  39.         void    SetAt(int nIndex, char ch);
  40.         operator LPCSTR() const;              // as a 'C' string
  41.  
  42.         // overloaded assignment
  43.         const CString& operator=(const CString& str);
  44.         const CString& operator=(char ch);
  45.         const CString& operator=(LPCSTR lpsz);
  46. #ifdef _WIN32
  47.         const CString& operator=(LPOLESTR lpsz);
  48. #endif
  49.         
  50.         // string concatenation
  51.         const CString& operator+=(const CString& str);
  52.         const CString& operator+=(char ch);
  53.         const CString& operator+=(LPCSTR lpsz);
  54.  
  55.         friend CString operator+(const CString& str1, const CString& str2);
  56.         friend CString operator+(const CString& str, char ch);
  57.         friend CString operator+(char ch, const CString& str);
  58.         friend CString operator+(const CString& str, LPCSTR lpsz);
  59.         friend CString operator+(LPCSTR lpsz, const CString& str);
  60.  
  61.         // string comparison
  62.         int Compare(LPCSTR lpsz) const;         // straight character
  63.         int CompareNoCase(LPCSTR lpsz) const;   // ignore case
  64.  
  65.         // upper/lower conversion
  66.         void MakeUpper();
  67.         void MakeLower();
  68.  
  69.         // searching (return starting index, or -1 if not found)
  70.         // look for a single character match
  71.         int Find(char ch) const;               // like "C" strchr
  72.         int ReverseFind(char ch) const;
  73.         
  74.         // simple sub-string extraction
  75.         CString Mid(int nFirst, int nCount) const;
  76.         CString Mid(int nFirst) const;
  77.         CString Left(int nCount) const;
  78.         CString Right(int nCount) const;
  79.         
  80.         // load from string resource
  81.         BOOL     LoadString(HINSTANCE, UINT nID);   // 255 chars max
  82.  
  83.         // set string from window text
  84.         BOOL     GetWindowText(HWND hwnd);
  85.  
  86.         // sets the buffer length and data length to nNewLength, and
  87.         // returns the new address of the buffer
  88.         LPSTR    BufferSetLength(int nNewLength);
  89.  
  90.     // Implementation
  91.     public:
  92.         ~CString();
  93.  
  94.     private:
  95.         LPSTR     m_pchData;      // NULL terminated string
  96.         int        m_nDataLength;  // doesn't include NULL terminator
  97.         int        m_nAllocLength; // doesn't include NULL terminator
  98.  
  99.         // implementation helpers
  100.         CString(LPCSTR lpszSrc1, int nSrc1Len, LPCSTR lpszSrc2, int nSrc2Len);
  101.         CString(LPCSTR lpsz, int nLen);
  102.         void Init();//public to call explicitly when allocating an array of CStrings
  103.         void AllocBuffer(int nLen);
  104.         void AssignCopy(LPCSTR lpszSrc, int nSrcLen = -1);
  105.         void ConcatInPlace(LPCSTR lpszSrc, int nSrcLen);
  106. #ifdef _WIN32
  107.         void AssignCopy(LPOLESTR lpsz);
  108. #endif
  109. };
  110.  
  111. inline CString::CString() {Init();}
  112.  
  113. inline int CString::GetLength() const {return m_nDataLength;}
  114. inline BOOL CString::IsEmpty() const {return m_nDataLength == 0;}
  115.  
  116. inline char CString::GetAt(int nIndex) const {return m_pchData[nIndex];}
  117. inline char CString::operator[](int nIndex) const {return m_pchData[nIndex];}
  118. inline void CString::SetAt(int nIndex, char ch) {m_pchData[nIndex] = ch;}
  119. inline CString::operator LPCSTR () const {return (LPCSTR)m_pchData;}
  120.  
  121. inline int CString::Compare(LPCSTR lpsz) const {return lstrcmp(m_pchData, lpsz);}
  122. inline int CString::CompareNoCase(LPCSTR lpsz) const {return lstrcmpi(m_pchData, lpsz);}
  123.  
  124. inline void CString::MakeUpper() {AnsiUpper(m_pchData);}
  125. inline void CString::MakeLower() {AnsiLower(m_pchData);}
  126.  
  127. #endif /* __CSTRING_H_ */
  128.  
  129.