Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.4 KiB

  1. #ifndef __VSIMPLESTRING_HPP
  2. #define __VSIMPLESTRING_HPP
  3. #include "vStandard.h"
  4. // <VDOC<CLASS=VSimpleString><DESC=A very lightweight class that supports dynamically allocated C style strings. This class can also be used any time a fixed amount of dynamic memory is needed.><FAMILY=String Processing,General Utility><AUTHOR=Todd Osborne ([email protected])>VDOC>
  5. class VSimpleString
  6. {
  7. public:
  8. VSimpleString(LPCTSTR lpszText = NULL, UINT nExtraBytes = 0)
  9. { m_lpszString = NULL; m_nBufferSize = 0; String(lpszText, nExtraBytes); }
  10. virtual ~VSimpleString()
  11. { String(NULL); }
  12. operator LPTSTR()
  13. { return m_lpszString; }
  14. operator LPCTSTR()
  15. { return m_lpszString; }
  16. // Get the length of the string, or if bBuffer is TRUE, the amount of allocated memory
  17. UINT GetLength(BOOL bBuffer = FALSE)
  18. { return (bBuffer) ? m_nBufferSize : (m_lpszString) ? lstrlen(m_lpszString) : 0; }
  19. // Set a pointer to globally allocate memory into object. This can be done to replace a string pointer
  20. // will a previously "stolen" pointer from the StealBuffer() function
  21. LPTSTR ReplaceBuffer(LPTSTR lpszString, UINT nBufferSize = 0)
  22. { String(NULL, 0), m_lpszString = lpszString, m_nBufferSize = (nBufferSize) ? nBufferSize : (m_lpszString) ? lstrlen(m_lpszString) : 0; return m_lpszString; }
  23. // Steal the buffer. Calling code takes ownership of string pointer and must
  24. // free it when done using GlobalFreePtr()
  25. LPTSTR StealBuffer()
  26. { LPTSTR lpszBuffer = m_lpszString; m_lpszString = NULL; m_nBufferSize = 0; return lpszBuffer; }
  27. // Get the text pointer
  28. LPTSTR String()
  29. { return m_lpszString; }
  30. // Save lpszString in class and returns pointer to buffer if a string is held.
  31. // If nExtraBytes is set, that much more memory will be allocated in addition
  32. // to the length of lpszString. lpszString can be NULL and still have memory
  33. // allocated if nExtraBytes is not 0
  34. LPTSTR String(LPCTSTR lpszString, UINT nExtraBytes = 0)
  35. {
  36. if ( m_lpszString )
  37. GlobalFreePtr(m_lpszString);
  38. m_nBufferSize = nExtraBytes;
  39. if ( lpszString )
  40. m_nBufferSize += (lstrlen(lpszString) + 1);
  41. m_lpszString = (m_nBufferSize) ? (LPTSTR)GlobalAllocPtr(GPTR, m_nBufferSize) : NULL;
  42. if ( m_lpszString )
  43. {
  44. if ( lpszString )
  45. lstrcpy(m_lpszString, lpszString);
  46. }
  47. else
  48. m_nBufferSize = 0;
  49. return m_lpszString;
  50. }
  51. private:
  52. // Embedded Member(s)
  53. LPTSTR m_lpszString;
  54. UINT m_nBufferSize;
  55. };
  56. #endif // __VSIMPLESTRING_HPP