home *** CD-ROM | disk | FTP | other *** search
/ Troubleshooting Netware Systems / CSTRIAL0196.BIN / attach / msj / v10n08 / oleq0895.exe / S816.H < prev    next >
C/C++ Source or Header  |  1995-08-01  |  4KB  |  159 lines

  1. #ifndef _S816_H
  2. #define _S816_H
  3.  
  4. // String16 ////////////////////////////////////////////////////////
  5.  
  6. // Shim class that converts both 8-bit (foreign) and
  7. // 16-bit (native) strings to 16-bit wideness
  8.  
  9. class String16 {
  10. public:
  11. // native and foreign constructors
  12.     String16(const char *p8);
  13.     String16(const wchar_t *p16);
  14.  
  15. // non-virtual destructor (this class is concrete)
  16.   ~String16(void);
  17.  
  18. // native conversion operator
  19.   operator const wchar_t * (void) const;
  20.  
  21. private:
  22. // native wideness string
  23.     wchar_t *m_sz;
  24. // is foreign??
  25.     BOOL m_bIsForeign;
  26.  
  27. // protect against assignment!
  28.   String16(const String16&);
  29.     String16& operator=(const String16&);
  30. };
  31.  
  32. // native constructor is a pass-through
  33. inline String16::String16(const wchar_t *p16) 
  34. : m_sz((wchar_t *)p16), m_bIsForeign(FALSE) 
  35. }
  36.  
  37. // simply give out the native wideness string 
  38. inline String16::operator const wchar_t * (void) const 
  39. {
  40.   return m_sz;
  41. }
  42.  
  43. // foreign constructor requires allocation of a native
  44. // string and conversion
  45. inline String16::String16(const char *p8)
  46. : m_bIsForeign(TRUE) 
  47. {
  48. // calculate string length
  49.   size_t len = strlen(p8);
  50.  
  51. // calculate required buffer size (some characters may
  52. // already occupy 16-bits under DBCS)
  53.   size_t size = mbstowcs(0, p8, len) + 1;
  54.  
  55. // alloc native string and convert
  56.   if (m_sz = new wchar_t[size])
  57.     mbstowcs(m_sz, p8, size);
  58. }
  59.  
  60. // delete native string only if synthesized in foreign constructor
  61. inline String16::~String16(void) {
  62.   if (m_bIsForeign) 
  63.     delete[] m_sz;
  64. }
  65.  
  66.  
  67. // String8 /////////////////////////////////////////////////////////
  68.  
  69. // Shim class that converts both 8-bit (native) and
  70. // 16-bit (foreign) strings to 8-bit wideness
  71.  
  72. class String8 {
  73. public:
  74. // native and foreign constructors
  75.     String8(const char *p8);
  76.     String8(const wchar_t *p16);
  77.  
  78. // non-virtual destructor (this class is concrete)
  79.   ~String8(void);
  80.  
  81. // native conversion operator
  82.   operator const char * (void) const;
  83.  
  84. private:
  85. // native wideness string
  86.     char *m_sz;
  87. // is foreign??
  88.     BOOL m_bIsForeign;
  89.  
  90. // protect against assignment!
  91.   String8(const String8&);
  92.     String8& operator=(const String8&);
  93. };
  94.  
  95. // native constructor is a pass-through
  96. inline String8::String8(const char *p8) 
  97. : m_sz((char *)p8), // casting away constness ONLY FOR CONVENIENCE!
  98.   m_bIsForeign(FALSE) 
  99. }
  100.  
  101. // simply give out the native wideness string 
  102. inline String8::operator const char * (void) const 
  103. {
  104.   return m_sz;
  105. }
  106.  
  107. // foreign constructor requires allocation of a native
  108. // string and conversion
  109. inline String8::String8(const wchar_t *p16)
  110. : m_bIsForeign(TRUE) 
  111. {
  112. // calculate string length
  113.   size_t len = wcslen(p16);
  114.  
  115. // calculate required buffer size (some characters may
  116. // require more than one byte under DBCS)
  117.   size_t size = wcstombs(0, p16, len) + 1;
  118.  
  119. // alloc native string and convert
  120.   if (m_sz = new char[size])
  121.     wcstombs(m_sz, p16, size);
  122. }
  123.  
  124. // delete native string only if synthesized in foreign constructor
  125. inline String8::~String8(void) {
  126.   if (m_bIsForeign) 
  127.     delete[] m_sz;
  128. }
  129.  
  130. // Conditional Typedefs for Win32 and OLE Text Data Types ////////////////////
  131.  
  132. // typedef OLESTRCVAR to emulate the OLESTR 
  133. // macro (converts any string at runtime instead 
  134. // of simply changing layout of string literal at
  135. // compile-time).
  136.  
  137. #ifdef OLE2ANSI
  138. typedef String8 OLESTRCVAR;
  139. #else
  140. typedef String16 OLESTRCVAR;
  141. #endif
  142.  
  143.  
  144. // typedef __TEXTCVAR to emulate the __TEXT
  145. // macro (converts any string at runtime instead 
  146. // of simply changing layout of string literal at
  147. // compile-time).
  148.  
  149.  
  150. #ifdef UNICODE
  151. typedef String16 __TEXTCVAR;
  152. #else
  153. typedef String8 __TEXTCVAR;
  154. #endif
  155.  
  156. #endif
  157.