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.

112 lines
2.8 KiB

  1. // Copyright (C) 1993-1997 Microsoft Corporation. All rights reserved.
  2. #if _MSC_VER > 1000
  3. #pragma once
  4. #endif
  5. #ifndef __CSTR_H__
  6. #define __CSTR_H__
  7. #include "lcmem.h"
  8. class CStr
  9. {
  10. public:
  11. CStr(PCSTR pszOrg) { psz = lcStrDup(pszOrg); }
  12. CStr(LPCWSTR pszNew) {
  13. psz = NULL;
  14. *this = pszNew;
  15. }
  16. CStr(int idResource) { psz = lcStrDup(GetStringResource(idResource)); }
  17. CStr(void) { psz = NULL; }
  18. // Get a format string from a resource id and merge the string:
  19. // Equivalent to wsprintf(buffer, GetStringResource(id), pszString)
  20. CStr(int idFormatString, PCSTR pszSubString);
  21. // Get the text of a window
  22. CStr(HWND hwnd);
  23. ~CStr() {
  24. if (psz)
  25. lcFree(psz); }
  26. void FormatString(int idFormatString, PCSTR pszSubString);
  27. PSTR GetArg(PCSTR pszSrc, BOOL fCheckComma = FALSE);
  28. int GetText(HWND hwnd, int id_or_sel = -1);
  29. BOOL IsEmpty(void) { return (psz ? (BOOL) (*psz == '\0') : TRUE); }
  30. BOOL IsNonEmpty(void) const { return (psz ? (BOOL) (*psz != '\0') : FALSE); }
  31. int SizeAlloc(void) { return (psz ? lcSize(psz) : 0); }
  32. int strlen(void) { return (psz ? ::strlen(psz) : 0); }
  33. void ReSize(int cbNew) {
  34. if (!psz)
  35. psz = (PSTR) lcMalloc(cbNew);
  36. else
  37. psz = (PSTR) lcReAlloc(psz, cbNew);
  38. }
  39. void TransferPointer(PSTR* ppsz) {
  40. *ppsz = psz;
  41. psz = NULL;
  42. }
  43. void TransferPointer(PCSTR* ppsz) {
  44. *ppsz = psz;
  45. psz = NULL;
  46. }
  47. operator PCSTR() { return (PCSTR) psz; }
  48. operator PSTR() { return psz; } // as a C string
  49. void operator+=(PCSTR pszCat)
  50. {
  51. ASSERT(psz);
  52. ASSERT(pszCat);
  53. psz = (PSTR) lcReAlloc(psz, strlen() + ::strlen(pszCat) + 1);
  54. strcat(psz, pszCat);
  55. }
  56. void operator=(PCSTR pszNew)
  57. {
  58. ASSERT(pszNew);
  59. // Duplicate first in case we are assigning part of ourselves
  60. PSTR pszTmp = lcStrDup(pszNew);
  61. if (psz)
  62. lcFree(psz);
  63. psz = pszTmp;
  64. }
  65. void operator=(LPCWSTR pszNew);
  66. PSTR psz;
  67. };
  68. class CWStr
  69. {
  70. public:
  71. CWStr() : pw(NULL) {}
  72. CWStr(HWND hwnd);
  73. CWStr(PCSTR psz) {
  74. pw = NULL;
  75. *this = psz;
  76. }
  77. CWStr(int idResource) { pw = lcStrDupW(GetStringResourceW(idResource)); }
  78. ~CWStr() { if (pw) lcFree(pw); }
  79. void operator=(PCWSTR pszNew)
  80. {
  81. ASSERT(pszNew);
  82. PWSTR pszTmp = lcStrDupW(pszNew);
  83. if (pw)
  84. lcFree(pw);
  85. pw = pszTmp;
  86. }
  87. void operator=(PCSTR psz);
  88. operator LPWSTR() { return (LPWSTR) pw; };
  89. int Length() { return pw ? lstrlenW(pw) : 0; }
  90. int ByteLength() { return Length()*sizeof(WCHAR); }
  91. //private:
  92. LPWSTR pw;
  93. };
  94. #endif // __CSTR_H__