home *** CD-ROM | disk | FTP | other *** search
/ Using Visual Basic 5 (Platinum Edition) / vb5.iso / ACTIVEX / SRDVID / DATA.1 / castring.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-10  |  2.8 KB  |  125 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // CAString.cpp - Wide character string base class
  4. //
  5. /////////////////////////////////////////////////////////////////////////////
  6. //
  7. //  (C) Copyright Black Diamond Consulting, Inc 1996. All rights reserved.
  8. //
  9. //    You have a royalty-free right to use, modify, reproduce and 
  10. //    distribute the Sample Files (and/or any modified version) in 
  11. //    any way you find useful, provided that you agree that Black 
  12. //    Diamond Consulting has no warranty obligations or liability
  13. //    for any Sample Application Files which are modified. 
  14. //
  15. //    Revision History:
  16. //   10/16/95 - Initial version - DAS
  17. //
  18. /////////////////////////////////////////////////////////////////////////////
  19. //
  20. // CAnyString is an implementation of a simple string class.
  21. //
  22. // A CAnyString object is initialized with an = assignment or in the
  23. // constructor, and automatically returns the UNICODE or ANSI version
  24. // of the string.
  25. //
  26. // Example:
  27. //
  28. //    CAnyString string;
  29. //    string = L"This is UNICODE";
  30. // or
  31. //    string = "This is ANSI";
  32. //    ...
  33. //    ...
  34. //    (const char*)string returns a const pointer to the ANSI representation
  35. //    (const WCHAR*)string returns a const pointer to the UNICODE representation
  36. //    ...
  37. //    ...
  38. //
  39. /////////////////////////////////////////////////////////////////////////////
  40.  
  41. #include "stdafx.h"
  42. #include "string.h"
  43. #include <windowsx.h>
  44.  
  45. #ifndef __CASTRING_H__
  46.     #include "CAString.h"
  47. #endif
  48.  
  49. #ifdef _DEBUG
  50.     #undef THIS_FILE
  51.     static char BASED_CODE THIS_FILE[] = __FILE__;
  52. #endif
  53.  
  54. CAnyString::CAnyString()
  55. {
  56.     m_pwchar = NULL;
  57.     m_pchar = NULL;
  58. }
  59.  
  60. CAnyString::CAnyString( const WCHAR* pwchar )
  61. {
  62.     m_pwchar = NULL;
  63.     m_pchar = NULL;
  64.     *this = pwchar;
  65. }
  66.  
  67. CAnyString::CAnyString( const char* pchar )
  68. {
  69.     m_pwchar = NULL;
  70.     m_pchar = NULL;
  71.     *this = pchar;
  72. }
  73.  
  74. const CAnyString& CAnyString::operator=( const WCHAR* pwchar )
  75. {
  76.     this->Reset();
  77.  
  78.     size_t length = wcslen( pwchar );
  79.  
  80.     m_pwchar = (WCHAR*)GlobalAllocPtr( GHND, (length+1)<<1 );
  81.     wcscpy( m_pwchar, pwchar );
  82.     
  83.     m_pchar = (char*)GlobalAllocPtr( GHND, length+1 );
  84.     wcstombs( m_pchar, (const WCHAR*)m_pwchar, length );
  85.  
  86.     return *this;
  87. }
  88.  
  89. const CAnyString& CAnyString::operator=( const char* pchar )
  90. {
  91.     this->Reset();
  92.  
  93.     size_t length = strlen( pchar );
  94.  
  95.     m_pchar = (char*)GlobalAllocPtr( GHND, length+1 );
  96.     strcpy( m_pchar, pchar );
  97.  
  98.     m_pwchar = (WCHAR*)GlobalAllocPtr( GHND, (length+1)<<1 );
  99.     mbstowcs( m_pwchar, (const char *)pchar, length );
  100.  
  101.     return *this;
  102. }
  103.  
  104. CAnyString::~CAnyString()
  105. {
  106.     this->Reset();
  107. }
  108.  
  109. void CAnyString::Reset()
  110. {
  111.     if( m_pwchar != NULL )
  112.     {
  113.         GlobalFreePtr( m_pwchar );
  114.         m_pwchar = NULL;
  115.     }
  116.  
  117.     if( m_pchar != NULL )
  118.     {
  119.         GlobalFreePtr( m_pchar );
  120.         m_pchar = NULL;
  121.     }
  122. }
  123.  
  124.  
  125.