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.

TCharArray.h 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright (C) 2002-2004 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. #define WIN32_LEAN_AND_MEAN
  39. #include <tchar.h>
  40. #include <rfb/util.h>
  41. namespace rfb {
  42. // -=- String duplication and cleanup functions.
  43. // These routines also handle conversion between WCHAR* and char*
  44. char* strDup(const WCHAR* s);
  45. WCHAR* wstrDup(const WCHAR* s);
  46. WCHAR* wstrDup(const char* s);
  47. void wstrFree(WCHAR* s);
  48. bool wstrSplit(const WCHAR* src, const WCHAR limiter, WCHAR** out1, WCHAR** out2, bool fromEnd=false);
  49. bool wstrContains(const WCHAR* src, WCHAR c);
  50. // -=- Temporary format conversion classes
  51. // CStr accepts WCHAR* or char* and behaves like a char*
  52. // WStr accepts WCHAR* or char* and behaves like a WCHAR*
  53. struct WStr {
  54. WStr(const char* s) : buf(wstrDup(s)), free_(true) {}
  55. WStr(const WCHAR* s) : buf(s), free_(false) {}
  56. ~WStr() {if (free_) wstrFree((WCHAR*)buf);}
  57. operator const WCHAR*() {return buf;}
  58. const WCHAR* buf;
  59. bool free_;
  60. };
  61. struct CStr {
  62. CStr(const char* s) : buf(s), free_(false) {}
  63. CStr(const WCHAR* s) : buf(strDup(s)), free_(true) {}
  64. ~CStr() {if (free_) strFree((char*)buf);}
  65. operator const char*() {return buf;}
  66. const char* buf;
  67. bool free_;
  68. };
  69. // -=- Class to handle cleanup of arrays of native Win32 characters
  70. class WCharArray {
  71. public:
  72. WCharArray() : buf(0) {}
  73. WCharArray(char* str) : buf(wstrDup(str)) {strFree(str);} // note: assumes ownership
  74. WCharArray(WCHAR* str) : buf(str) {} // note: assumes ownership
  75. WCharArray(int len) {
  76. buf = new WCHAR[len];
  77. }
  78. ~WCharArray() {
  79. delete [] buf;
  80. }
  81. // Get the buffer pointer & clear it (i.e. caller takes ownership)
  82. WCHAR* takeBuf() {WCHAR* tmp = buf; buf = 0; return tmp;}
  83. void replaceBuf(WCHAR* str) {delete [] buf; buf = str;}
  84. WCHAR* buf;
  85. };
  86. #ifdef _UNICODE
  87. #define tstrDup wstrDup
  88. #define tstrFree wstrFree
  89. #define tstrSplit wstrSplit
  90. #define tstrContains wstrContains
  91. typedef WCharArray TCharArray;
  92. typedef WStr TStr;
  93. #else
  94. #define tstrDup strDup
  95. #define tstrFree strFree
  96. #define tstrSplit strSplit
  97. #define tstrContains strContains
  98. typedef CharArray TCharArray;
  99. typedef CStr TStr;
  100. #endif
  101. };
  102. #endif