Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. * The following applies to stcasecmp and strncasecmp implementations:
  20. *
  21. * Copyright (c) 1987 Regents of the University of California.
  22. * All rights reserved.
  23. *
  24. * Redistribution and use in source and binary forms are permitted
  25. * provided that this notice is preserved and that due credit is given
  26. * to the University of California at Berkeley. The name of the University
  27. * may not be used to endorse or promote products derived from this
  28. * software without specific written prior permission. This software
  29. * is provided ``as is'' without express or implied warranty.
  30. */
  31. #ifdef HAVE_COMMON_CONFIG_H
  32. #include <common-config.h>
  33. #endif
  34. #include <rfb/util.h>
  35. // Provide strcasecmp() and/or strncasecmp() if absent on this system.
  36. #ifndef WIN32
  37. #if !defined(HAVE_STRCASECMP) || !defined(HAVE_STRNCASECMP)
  38. extern "C" {
  39. /*
  40. * This array is designed for mapping upper and lower case letter
  41. * together for a case independent comparison. The mappings are
  42. * based upon ascii character sequences.
  43. */
  44. static unsigned char s_charmap[] = {
  45. '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
  46. '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
  47. '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
  48. '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
  49. '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
  50. '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
  51. '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
  52. '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
  53. '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
  54. '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
  55. '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
  56. '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
  57. '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
  58. '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
  59. '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
  60. '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
  61. '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
  62. '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
  63. '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
  64. '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
  65. '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
  66. '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
  67. '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
  68. '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
  69. '\300', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
  70. '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
  71. '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
  72. '\370', '\371', '\372', '\333', '\334', '\335', '\336', '\337',
  73. '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
  74. '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
  75. '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
  76. '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
  77. };
  78. #ifndef HAVE_STRCASECMP
  79. int
  80. strcasecmp(const char *s1, const char *s2)
  81. {
  82. unsigned char u1, u2;
  83. for (;;) {
  84. u1 = (unsigned char) *s1++;
  85. u2 = (unsigned char) *s2++;
  86. if (s_charmap[u1] != s_charmap[u2]) {
  87. return s_charmap[u1] - s_charmap[u2];
  88. }
  89. if (u1 == '\0') {
  90. return 0;
  91. }
  92. }
  93. }
  94. #endif // !defined(HAVE_STRCASECMP)
  95. #ifndef HAVE_STRNCASECMP
  96. int
  97. strncasecmp(const char *s1, const char *s2, size_t n)
  98. {
  99. unsigned char u1, u2;
  100. for (; n != 0; --n) {
  101. u1 = (unsigned char) *s1++;
  102. u2 = (unsigned char) *s2++;
  103. if (s_charmap[u1] != s_charmap[u2]) {
  104. return s_charmap[u1] - s_charmap[u2];
  105. }
  106. if (u1 == '\0') {
  107. return 0;
  108. }
  109. }
  110. return 0;
  111. }
  112. #endif // !defined(HAVE_STRNCASECMP)
  113. } // extern "C"
  114. #endif // !defined(HAVE_STRCASECMP) || !defined(HAVE_STRNCASECMP)
  115. #endif // defined(WIN32)
  116. namespace rfb {
  117. char* strDup(const char* s) {
  118. if (!s) return 0;
  119. int l = strlen(s);
  120. char* r = new char[l+1];
  121. memcpy(r, s, l+1);
  122. return r;
  123. };
  124. void strFree(char* s) {
  125. delete [] s;
  126. }
  127. bool strSplit(const char* src, const char limiter, char** out1, char** out2, bool fromEnd) {
  128. CharArray out1old, out2old;
  129. if (out1) out1old.buf = *out1;
  130. if (out2) out2old.buf = *out2;
  131. int len = strlen(src);
  132. int i=0, increment=1, limit=len;
  133. if (fromEnd) {
  134. i=len-1; increment = -1; limit = -1;
  135. }
  136. while (i!=limit) {
  137. if (src[i] == limiter) {
  138. if (out1) {
  139. *out1 = new char[i+1];
  140. if (i) memcpy(*out1, src, i);
  141. (*out1)[i] = 0;
  142. }
  143. if (out2) {
  144. *out2 = new char[len-i];
  145. if (len-i-1) memcpy(*out2, &src[i+1], len-i-1);
  146. (*out2)[len-i-1] = 0;
  147. }
  148. return true;
  149. }
  150. i+=increment;
  151. }
  152. if (out1) *out1 = strDup(src);
  153. if (out2) *out2 = 0;
  154. return false;
  155. }
  156. bool strContains(const char* src, char c) {
  157. int l=strlen(src);
  158. for (int i=0; i<l; i++)
  159. if (src[i] == c) return true;
  160. return false;
  161. }
  162. void strCopy(char* dest, const char* src, int destlen) {
  163. if (src)
  164. strncpy(dest, src, destlen-1);
  165. dest[src ? destlen-1 : 0] = 0;
  166. }
  167. };