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.cxx 2.4KB

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