Personal page of Konjengbam

“Bluetooth adopter, technology enabler and innovative thinking”

Text Box: StdString.txt

#ifndef __STD_STRING_H__

#define __STD_STRING_H__

//

/************************************************************************

  =======================================================================

  REVISION LOG ENTRY:

  =======================================================================

 

  Written By: J. Singh Konjengbam

  Revision By: J. Singh Konjengbam

  Revised on 9/5/2001 11:20:18 AM

 

  Comments: Since the interface is practically the same as MFC's CString,

                                                   the same block of code that uses this version of string is

                                                   workable under most cases when compiled or linked with MFC's

                                                   CString class also. All that requires is to switch between

                                                   the two versions by including appropriate header file.

                                                  

                                                   Note that at the moment, no exception handling is carried out

                                                   here in this class. Functions like Mid() throw CMemoryException

                                                   in the real case. Mimicking that would require similar

                                                   Exception class to be re-written or at least, some skeleton

                                                   classes and similar heirarchies prepared. This work is left

                                                   for future development.

 

                                                   The "dllexport" macro allows the class to be treated like a DLL

                                                   library and all that a client needs is to include this header

                                                   file and link with the libraries -

                                                   StringObject.lib, StringObject.dll

 

                                                   Warning: The use of operators >> and << are restricted for

                                                   stream operations only here. In MFC version, they represent

                                                   object persistency support via CArchive.

 

 ************************************************************************/

//

/************************************************************************

  ======================================================================

  DESCRIPTION:

  ======================================================================

 

  This class has been developed to replace CString in the case of

  applications that require no MFC but still prefers to use MFC's

  CString style of handling strings.

 

  No extra documentation is required so long as user follows CString

  of MFC, except at one or two cases, like - the function toString()

  has been provided to convert the object's type to ANSI version of

  string.

 

  It may be noted that the interface methods are essentially same as

  that of the MFC's standard CString class. However, not all methods

  have been duplicated here because some of MFC' CString are platform

  specific. Also, format functionality has been deprecated here as

  this do not represent the actual abstraction of the current string

  class. However, based on platform for deployment of applications,

  one could use either wsprintf(), or sprintf() depending on either

  windows or console based in the respective target client applications.

 

  In this class, which is ( I prefer to call StdString ) more of standard

  in representation, the string data type is based upon STL's string

  library. This CString class acts as a wrapper around this standard

  string and provides similar ingredients and features of MFC's CString.

 

************************************************************************/

//

// Following(s) warnings are disabled...

#pragma warning (disable:4251)

 

#define DllExport   __declspec( dllexport )

 

#include <string>

using namespace std;

 

namespace StdCString {

 

                 class DllExport CString 

                 {

                 public:

                                  // Constructor(s) and Destructor

                                  CString(); // Default constructor

                                 

                                  CString( const string& stdString ); // Constructs object from standard string

                                  CString( const char* lpszString ); // Constructs from ANSI string, const char

                                  CString( char* lpString ); // Constructs from ANSI string, char pointer

                                 

                                  CString( const CString& rString ); // Copy constructor

                                 

                                  ~CString(); // Destructor, empties string

 

                                  // Public operations

                                  //

                                  // The String as an Array

                                  int GetLength() const; // Get the length of the string

                                  void Empty(); // Forces a string to have 0 length

                                  char GetAt( int nIndex ) const; // Returns element at the given index

                                  void SetAt( int nIndex, char ch ); // Sets element at an index                   

                                  char operator []( int nIndex ) const; // Allowing array indexing

 

                                  // Conversion routines

                                  inline const char* toString() // Convert to ANSI string form

                                  {

                                                   return( m_stdString.c_str() );

                                  }

 

                                  // Other conversions

                                  void MakeUpper(); // Convert to Uppercase

                                  void MakeLower(); // Convert to Lowercase

                                  void MakeReverse(); // Reverse the current string

                                  int Resize( int nNewSize ); // Change size of string

                                  void TrimLeft(); // Trim leading whitespace, newline, tab etc.

                                  void TrimLeft( char ch ); // Remove given characters found at the beginning

                                  void TrimRight(); // Trim trailing whitespace

                                  void TrimRight( char ch ); // Remove given characters found at the end

 

 

                                  // Extraction routines

                                  CString Mid( int nFirst, int nCount ) const; // Extract middle character set bounded by nFirst and nCount

                                  CString Mid( int nFirst ) const; // Extract string subset bounded by 0 and nFirst

                                  CString Left( int nCount ) const; // Returns first nCount characters from start

                                  CString Right( int nCount ) const; // Returns first nCount characters from end

 

                                  // Searching routines

                                  int Find( const char* lpszSub, int nStart ) const; // Finds sub-string starting from nStart

                                  int Find( const char* lpszSub ) const; // Finds sub-string from beginning

                                  int Find( char ch, int nStart ) const; // Finds a character from position - start

                                  int Find( char ch ) const; // Finds a character from start

                                  int ReverseFind( char ch ) const; // Finds a character from end

                                  int FindOneOf( const char* lpszCharSet ) const; // Find any one of the given string sub-set

 

                                  // Overloaded operators

                                  // Assignment routines

                                  CString& operator= ( const CString& rValue );

                                  CString& operator= ( const char* lpszRightVal );

                                  CString& operator= ( const string& stdString );

 

 

                                  // Comparison routines

                                  int Compare( const char* lpszRightVal ) const; // Case sensitive comparison

                                  int CompareNoCase( const char* lpszRightVal ) const; // Compares string ( case insensitive )

                                  // Binary operators ( following - case sensitive )

                                  const bool operator== ( const CString& rValue );

                                  const bool operator== ( const char* lpszRightVal );

                                  const bool operator!= ( const CString& rValue );

                                  const bool operator!= ( const char* lpszRightVal );

 

 

                                  // Declare additional operators.

                                  // String concatenation

                                  friend inline CString operator+( const CString& rValue, const char* lpszRightVal )

                                  {

                                                   static CString tmpString( rValue );

                                                   tmpString.m_stdString.append( lpszRightVal);

 

                                                   return tmpString;

                                  }

 

                                  friend inline CString operator+( const char* lpszRightVal, const CString& rValue )

                                  {

                                                   static CString tmpString( lpszRightVal );

                                                   tmpString.m_stdString.append( rValue.m_stdString.c_str() );

 

                                                   return tmpString;

                                  }

 

                                  inline const CString& operator +=( const char* lpszRightVal )

                                  {

                                                   m_stdString.operator += ( lpszRightVal );

                                                   return *this;

                                  }

                                 

                                  // Stream operators

                                  // Output

                                  friend inline ostream& operator<< (ostream& os, const CString& rValue)

                                  {

                                                   os << rValue.m_stdString;                        

                                                   return os;

                                  }

                                  // Input

                                  friend inline istream& operator>> (istream& is, CString& rValue)

                                  {

                                                   is >> rValue.m_stdString;

                                                   return is;

                                  }

 

                 private:

                                  // Private member functions

                                  inline char _toUpper( char ch ) const;

                                  inline char _toLower( char ch ) const;

 

                 private:

                                  // Private data member

                                  string m_stdString;

 

                 };

 

};

 

 

#endif //__STD_STRING_H__

Hosted by www.Geocities.ws

1