MLString.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #ifndef NULLOSFT_MLSTRING_HEADER
  2. #define NULLOSFT_MLSTRING_HEADER
  3. #include <windows.h>
  4. class MLString
  5. {
  6. public:
  7. MLString(void);
  8. ~MLString(void);
  9. MLString(const wchar_t* string);
  10. MLString(unsigned int cchBuffer);
  11. protected:
  12. MLString(const MLString &copy);
  13. public:
  14. HRESULT Set(const wchar_t* string, unsigned int cchLength);
  15. HRESULT Append(const wchar_t* string, unsigned int cchLength);
  16. const wchar_t* Get(void) { return (cchLen) ? buffer : NULL; }
  17. void Clear(void) { cchLen = 0; }
  18. unsigned int GetLength(void) { return cchLen; }
  19. HRESULT Format(const wchar_t *format, ...);
  20. HRESULT CopyTo(MLString *destination);
  21. HRESULT Set(const wchar_t* string) { return Set(string, lstrlenW(string)); }
  22. HRESULT Append(const wchar_t* string) { return Append(string, lstrlenW(string)); }
  23. // buffer
  24. HRESULT Allocate(unsigned int cchNewSize);
  25. void Compact(void);
  26. wchar_t* GetBuffer(void) { return buffer; }
  27. unsigned int GetBufferLength(void) { return allocated; }
  28. void UpdateBuffer(void) { cchLen = lstrlenW(buffer); }
  29. operator const wchar_t *() { return buffer; }
  30. operator wchar_t *() { return buffer; }
  31. wchar_t& operator [](unsigned int index) { return buffer[index]; }
  32. MLString& operator = (const wchar_t *source) { (source) ? Set(source, lstrlenW(source)) : Clear(); return *this;}
  33. MLString& operator + (const wchar_t *source) { if (source) Append(source, lstrlenW(source)); return *this;}
  34. MLString& operator += (const wchar_t *source) { if (source) Append(source, lstrlenW(source)); return *this;}
  35. MLString& operator = (MLString *source) { (source) ? source->CopyTo(this) : Clear(); return *this;}
  36. MLString& operator + (MLString *source) { if (source) Append(source->GetBuffer(), source->GetLength()); return *this;}
  37. MLString& operator += (MLString *source) { if (source) Append(source->GetBuffer(), source->GetLength()); return *this;}
  38. protected:
  39. wchar_t *buffer;
  40. unsigned int cchLen;
  41. unsigned int allocated;
  42. static void *heap;
  43. };
  44. #endif // NULLOSFT_STRING_HEADER