home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / Profiler / heapmon / ansibuf.hpp next >
Encoding:
C/C++ Source or Header  |  2000-05-04  |  1.8 KB  |  95 lines

  1. // ansibuf.hpp
  2. //
  3. // Created 02/07/98
  4. //
  5. // (C)Copyright 1998-1999 Microsoft Corporation, All rights reserved.
  6. //
  7.  
  8. #ifndef __ANSIBUF_HPP__
  9. #define __ANSIBUF_HPP__
  10.  
  11.  
  12. class ANSIStringBuffer
  13. {
  14.     PSTR m_str;
  15.     int m_len;
  16.     int m_size;
  17.  
  18.     // Ensures that 'needed' chars + '\0' can fit into buffer
  19.     BOOL EnsureSpace (int needed);
  20.  
  21. public:
  22.  
  23.     ANSIStringBuffer () 
  24.     {
  25.         m_str = NULL;
  26.         m_len = 0;
  27.         m_size = 0;
  28.     }
  29.  
  30.     ~ANSIStringBuffer () 
  31.     {
  32.         if (m_str)
  33.             delete(m_str);
  34.     }
  35.  
  36.     BOOL Append (PWSTR pwstr)
  37.     {
  38.         int len = WideCharToMultiByte(CP_ACP, 0, pwstr, -1, NULL, 0, NULL, NULL);
  39.         if (len >= 0)
  40.         {
  41.             if (EnsureSpace(len-1))
  42.             {
  43.                 if (WideCharToMultiByte(CP_ACP, 0, pwstr, -1, m_str+m_len, len, NULL, NULL) >= 0)
  44.                 {
  45.                     m_len += len-1;
  46.                     return TRUE;
  47.                 }
  48.                 else
  49.                 {
  50.                     // if WC2MB tromped on the buffer, ensure it remains as it was.
  51.                     m_str[m_len] = '\0';
  52.                 }
  53.             }
  54.         }
  55.         return FALSE;
  56.     }
  57.  
  58.     BOOL Append (PCSTR pstr)
  59.     {
  60.         int len = strlen(pstr);
  61.         if (EnsureSpace(len))
  62.         {
  63.             CopyMemory(m_str+m_len, pstr, len+1);
  64.             m_len += len;
  65.             return TRUE;
  66.         }
  67.         return FALSE;
  68.     }
  69.  
  70.     BOOL AppendUtf8 (PCSTR putf8);
  71.  
  72.     PSTR GetStr ()
  73.     {
  74.         return m_str;
  75.     }
  76.  
  77.     PSTR SnatchStr ()
  78.     {
  79.         PSTR ret = m_str;
  80.         m_str = NULL;
  81.         m_size = 0;
  82.         m_len = 0;
  83.         return ret;
  84.     }
  85.  
  86.     int GetLength ()
  87.     {
  88.         return m_len;
  89.     }
  90. };
  91.  
  92.  
  93. #endif /* __ANSIBUF_HPP__ */
  94.  
  95.