blob: dde63b788b4b5bff2ef70101950a1cd3b0ae4ce5 [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* 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
19// -=- TCharArray.h
20
21// This library contains the wide-character equivalent of CharArray, named
22// WCharArray. In addition to providing wide-character equivalents of
23// the char* string manipulation functions (strDup, strFree, etc), special
24// versions of those functions are provided which attempt to convert from
25// one format to the other.
26// e.g. char* t = "hello world"; WCHAR* w = wstrDup(t);
27// Results in w containing the wide-character text "hello world".
28// For convenience, the WStr and CStr classes are also provided. These
29// accept an existing (const) WCHAR* or char* null-terminated string and
30// create a read-only copy of that in the desired format. The new copy
31// will actually be the original copy if the format has not changed, otherwise
32// it will be a new buffer owned by the WStr/CStr.
33
34// In addition to providing wide character functions, this header defines
35// TCHAR* handling classes & functions. TCHAR is defined at compile time to
36// either char or WCHAR. Programs can treat this as a third data type and
37// call TStr() whenever a TCHAR* is required but a char* or WCHAR* is supplied,
38// and TStr will do the right thing.
39
40#ifndef __RFB_WIN32_TCHARARRAY_H__
41#define __RFB_WIN32_TCHARARRAY_H__
42
43#include <windows.h>
44#include <tchar.h>
45#include <rfb/util.h>
46#include <rfb/Password.h>
47
48namespace rfb {
49
50 // -=- String duplication and cleanup functions.
51 // These routines also handle conversion between WCHAR* and char*
52
53 char* strDup(const WCHAR* s);
54 WCHAR* wstrDup(const WCHAR* s);
55 WCHAR* wstrDup(const char* s);
56 void wstrFree(WCHAR* s);
57
58 bool wstrSplit(const WCHAR* src, const WCHAR limiter, WCHAR** out1, WCHAR** out2, bool fromEnd=false);
59 bool wstrContains(const WCHAR* src, WCHAR c);
60
61 // -=- Temporary format conversion classes
62 // CStr accepts WCHAR* or char* and behaves like a char*
63 // WStr accepts WCHAR* or char* and behaves like a WCHAR*
64
65 struct WStr {
66 WStr(const char* s) : buf(wstrDup(s)), free_(true) {}
67 WStr(const WCHAR* s) : buf(s), free_(false) {}
68 ~WStr() {if (free_) wstrFree((WCHAR*)buf);}
69 operator const WCHAR*() {return buf;}
70 const WCHAR* buf;
71 bool free_;
72 };
73
74 struct CStr {
75 CStr(const char* s) : buf(s), free_(false) {}
76 CStr(const WCHAR* s) : buf(strDup(s)), free_(true) {}
77 ~CStr() {if (free_) strFree((char*)buf);}
78 operator const char*() {return buf;}
79 const char* buf;
80 bool free_;
81 };
82
83 // -=- Class to handle cleanup of arrays of native Win32 characters
84 class WCharArray {
85 public:
86 WCharArray() : buf(0) {}
87 WCharArray(char* str) : buf(wstrDup(str)) {strFree(str);} // note: assumes ownership
88 WCharArray(WCHAR* str) : buf(str) {} // note: assumes ownership
89 WCharArray(int len) {
90 buf = new WCHAR[len];
91 }
92 ~WCharArray() {
93 delete [] buf;
94 }
95 // Get the buffer pointer & clear it (i.e. caller takes ownership)
96 WCHAR* takeBuf() {WCHAR* tmp = buf; buf = 0; return tmp;}
97 void replaceBuf(WCHAR* str) {delete [] buf; buf = str;}
98 WCHAR* buf;
99 };
100
101 // -=- Wide-character-based password-buffer handler. Zeroes the password
102 // buffer when deleted or replaced.
103 class WPlainPasswd : public WCharArray {
104 public:
105 WPlainPasswd() {}
106 WPlainPasswd(WCHAR* str) : WCharArray(str) {}
107 ~WPlainPasswd() {replaceBuf(0);}
108 void replaceBuf(WCHAR* str) {
109 if (buf)
110 memset(buf, 0, sizeof(WCHAR)*wcslen(buf));
111 WCharArray::replaceBuf(str);
112 }
113 };
114
115#ifdef _UNICODE
116#define tstrDup wstrDup
117#define tstrFree wstrFree
118#define tstrSplit wstrSplit
119#define tstrContains wstrContains
120 typedef WCharArray TCharArray;
121 typedef WStr TStr;
122 typedef WPlainPasswd TPlainPasswd;
123#else
124#define tstrDup strDup
125#define tstrFree strFree
126#define tstrSplit strSplit
127#define tstrContains strContains
128 typedef CharArray TCharArray;
129 typedef CStr TStr;
130 typedef PlainPasswd TPlainPasswd;
131#endif
132
133};
134
135#endif