Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 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 | |
| 19 | #include <rfb_win32/TCharArray.h> |
| 20 | |
| 21 | namespace rfb { |
| 22 | |
| 23 | WCHAR* wstrDup(const WCHAR* s) { |
| 24 | if (!s) return 0; |
| 25 | WCHAR* t = new WCHAR[wcslen(s)+1]; |
| 26 | memcpy(t, s, sizeof(WCHAR)*(wcslen(s)+1)); |
| 27 | return t; |
| 28 | } |
| 29 | void wstrFree(WCHAR* s) {delete [] s;} |
| 30 | |
| 31 | char* strDup(const WCHAR* s) { |
| 32 | if (!s) return 0; |
| 33 | int len = wcslen(s); |
| 34 | char* t = new char[len+1]; |
| 35 | t[WideCharToMultiByte(CP_ACP, 0, s, len, t, len, 0, 0)] = 0; |
| 36 | return t; |
| 37 | } |
| 38 | |
| 39 | WCHAR* wstrDup(const char* s) { |
| 40 | if (!s) return 0; |
| 41 | int len = strlen(s); |
| 42 | WCHAR* t = new WCHAR[len+1]; |
| 43 | t[MultiByteToWideChar(CP_ACP, 0, s, len, t, len)] = 0; |
| 44 | return t; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | bool wstrSplit(const WCHAR* src, const WCHAR limiter, WCHAR** out1, WCHAR** out2, bool fromEnd) { |
| 49 | WCharArray out1old, out2old; |
| 50 | if (out1) out1old.buf = *out1; |
| 51 | if (out2) out2old.buf = *out2; |
| 52 | int len = wcslen(src); |
| 53 | int i=0, increment=1, limit=len; |
| 54 | if (fromEnd) { |
| 55 | i=len-1; increment = -1; limit = -1; |
| 56 | } |
| 57 | while (i!=limit) { |
| 58 | if (src[i] == limiter) { |
| 59 | if (out1) { |
| 60 | *out1 = new WCHAR[i+1]; |
| 61 | if (i) memcpy(*out1, src, sizeof(WCHAR)*i); |
| 62 | (*out1)[i] = 0; |
| 63 | } |
| 64 | if (out2) { |
| 65 | *out2 = new WCHAR[len-i]; |
| 66 | if (len-i-1) memcpy(*out2, &src[i+1], sizeof(WCHAR)*(len-i-1)); |
| 67 | (*out2)[len-i-1] = 0; |
| 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | i+=increment; |
| 72 | } |
| 73 | if (out1) *out1 = wstrDup(src); |
| 74 | if (out2) *out2 = 0; |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | bool wstrContains(const WCHAR* src, WCHAR c) { |
| 79 | int l=wcslen(src); |
| 80 | for (int i=0; i<l; i++) |
| 81 | if (src[i] == c) return true; |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | }; |