home *** CD-ROM | disk | FTP | other *** search
- /////////////////////////////////////////////////////////////////////////////
- //
- // CAString.cpp - Wide character string base class
- //
- /////////////////////////////////////////////////////////////////////////////
- //
- // (C) Copyright Black Diamond Consulting, Inc 1996. All rights reserved.
- //
- // You have a royalty-free right to use, modify, reproduce and
- // distribute the Sample Files (and/or any modified version) in
- // any way you find useful, provided that you agree that Black
- // Diamond Consulting has no warranty obligations or liability
- // for any Sample Application Files which are modified.
- //
- // Revision History:
- // 10/16/95 - Initial version - DAS
- //
- /////////////////////////////////////////////////////////////////////////////
- //
- // CAnyString is an implementation of a simple string class.
- //
- // A CAnyString object is initialized with an = assignment or in the
- // constructor, and automatically returns the UNICODE or ANSI version
- // of the string.
- //
- // Example:
- //
- // CAnyString string;
- // string = L"This is UNICODE";
- // or
- // string = "This is ANSI";
- // ...
- // ...
- // (const char*)string returns a const pointer to the ANSI representation
- // (const WCHAR*)string returns a const pointer to the UNICODE representation
- // ...
- // ...
- //
- /////////////////////////////////////////////////////////////////////////////
-
- #include "stdafx.h"
- #include "string.h"
- #include <windowsx.h>
-
- #ifndef __CASTRING_H__
- #include "CAString.h"
- #endif
-
- #ifdef _DEBUG
- #undef THIS_FILE
- static char BASED_CODE THIS_FILE[] = __FILE__;
- #endif
-
- CAnyString::CAnyString()
- {
- m_pwchar = NULL;
- m_pchar = NULL;
- }
-
- CAnyString::CAnyString( const WCHAR* pwchar )
- {
- m_pwchar = NULL;
- m_pchar = NULL;
- *this = pwchar;
- }
-
- CAnyString::CAnyString( const char* pchar )
- {
- m_pwchar = NULL;
- m_pchar = NULL;
- *this = pchar;
- }
-
- const CAnyString& CAnyString::operator=( const WCHAR* pwchar )
- {
- this->Reset();
-
- size_t length = wcslen( pwchar );
-
- m_pwchar = (WCHAR*)GlobalAllocPtr( GHND, (length+1)<<1 );
- wcscpy( m_pwchar, pwchar );
-
- m_pchar = (char*)GlobalAllocPtr( GHND, length+1 );
- wcstombs( m_pchar, (const WCHAR*)m_pwchar, length );
-
- return *this;
- }
-
- const CAnyString& CAnyString::operator=( const char* pchar )
- {
- this->Reset();
-
- size_t length = strlen( pchar );
-
- m_pchar = (char*)GlobalAllocPtr( GHND, length+1 );
- strcpy( m_pchar, pchar );
-
- m_pwchar = (WCHAR*)GlobalAllocPtr( GHND, (length+1)<<1 );
- mbstowcs( m_pwchar, (const char *)pchar, length );
-
- return *this;
- }
-
- CAnyString::~CAnyString()
- {
- this->Reset();
- }
-
- void CAnyString::Reset()
- {
- if( m_pwchar != NULL )
- {
- GlobalFreePtr( m_pwchar );
- m_pwchar = NULL;
- }
-
- if( m_pchar != NULL )
- {
- GlobalFreePtr( m_pchar );
- m_pchar = NULL;
- }
- }
-
-
-