Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TCharArray.h 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. // -=- TCharArray.h
  19. // This library contains the wide-character equivalent of CharArray, named
  20. // WCharArray. In addition to providing wide-character equivalents of
  21. // the char* string manipulation functions (strDup, strFree, etc), special
  22. // versions of those functions are provided which attempt to convert from
  23. // one format to the other.
  24. // e.g. char* t = "hello world"; WCHAR* w = wstrDup(t);
  25. // Results in w containing the wide-character text "hello world".
  26. // For convenience, the WStr and CStr classes are also provided. These
  27. // accept an existing (const) WCHAR* or char* null-terminated string and
  28. // create a read-only copy of that in the desired format. The new copy
  29. // will actually be the original copy if the format has not changed, otherwise
  30. // it will be a new buffer owned by the WStr/CStr.
  31. // In addition to providing wide character functions, this header defines
  32. // TCHAR* handling classes & functions. TCHAR is defined at compile time to
  33. // either char or WCHAR. Programs can treat this as a third data type and
  34. // call TStr() whenever a TCHAR* is required but a char* or WCHAR* is supplied,
  35. // and TStr will do the right thing.
  36. #ifndef __RFB_WIN32_TCHARARRAY_H__
  37. #define __RFB_WIN32_TCHARARRAY_H__
  38. #include <windows.h>
  39. #include <tchar.h>
  40. #include <rfb/util.h>
  41. #include <rfb/Password.h>
  42. namespace rfb {
  43. // -=- String duplication and cleanup functions.
  44. // These routines also handle conversion between WCHAR* and char*
  45. char* strDup(const WCHAR* s);
  46. WCHAR* wstrDup(const WCHAR* s);
  47. WCHAR* wstrDup(const char* s);
  48. void wstrFree(WCHAR* s);
  49. bool wstrSplit(const WCHAR* src, const WCHAR limiter, WCHAR** out1, WCHAR** out2, bool fromEnd=false);
  50. bool wstrContains(const WCHAR* src, WCHAR c);
  51. // -=- Temporary format conversion classes
  52. // CStr accepts WCHAR* or char* and behaves like a char*
  53. // WStr accepts WCHAR* or char* and behaves like a WCHAR*
  54. struct WStr {
  55. WStr(const char* s) : buf(wstrDup(s)), free_(true) {}
  56. WStr(const WCHAR* s) : buf(s), free_(false) {}
  57. ~WStr() {if (free_) wstrFree((WCHAR*)buf);}
  58. operator const WCHAR*() {return buf;}
  59. const WCHAR* buf;
  60. bool free_;
  61. };
  62. struct CStr {
  63. CStr(const char* s) : buf(s), free_(false) {}
  64. CStr(const WCHAR* s) : buf(strDup(s)), free_(true) {}
  65. ~CStr() {if (free_) strFree((char*)buf);}
  66. operator const char*() {return buf;}
  67. const char* buf;
  68. bool free_;
  69. };
  70. // -=- Class to handle cleanup of arrays of native Win32 characters
  71. class WCharArray {
  72. public:
  73. WCharArray() : buf(0) {}
  74. WCharArray(char* str) : buf(wstrDup(str)) {strFree(str);} // note: assumes ownership
  75. WCharArray(WCHAR* str) : buf(str) {} // note: assumes ownership
  76. WCharArray(int len) {
  77. buf = new WCHAR[len];
  78. }
  79. ~WCharArray() {
  80. delete [] buf;
  81. }
  82. // Get the buffer pointer & clear it (i.e. caller takes ownership)
  83. WCHAR* takeBuf() {WCHAR* tmp = buf; buf = 0; return tmp;}
  84. void replaceBuf(WCHAR* str) {delete [] buf; buf = str;}
  85. WCHAR* buf;
  86. };
  87. // -=- Wide-character-based password-buffer handler. Zeroes the password
  88. // buffer when deleted or replaced.
  89. class WPlainPasswd : public WCharArray {
  90. public:
  91. WPlainPasswd() {}
  92. WPlainPasswd(WCHAR* str) : WCharArray(str) {}
  93. ~WPlainPasswd() {replaceBuf(0);}
  94. void replaceBuf(WCHAR* str) {
  95. if (buf)
  96. memset(buf, 0, sizeof(WCHAR)*wcslen(buf));
  97. WCharArray::replaceBuf(str);
  98. }
  99. };
  100. #ifdef _UNICODE
  101. #define tstrDup wstrDup
  102. #define tstrFree wstrFree
  103. #define tstrSplit wstrSplit
  104. #define tstrContains wstrContains
  105. typedef WCharArray TCharArray;
  106. typedef WStr TStr;
  107. typedef WPlainPasswd TPlainPasswd;
  108. #else
  109. #define tstrDup strDup
  110. #define tstrFree strFree
  111. #define tstrSplit strSplit
  112. #define tstrContains strContains
  113. typedef CharArray TCharArray;
  114. typedef CStr TStr;
  115. typedef PlainPasswd TPlainPasswd;
  116. #endif
  117. };
  118. #endif