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

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