PageRenderTime 218ms CodeModel.GetById 33ms RepoModel.GetById 4ms app.codeStats 0ms

/dep/ACE_wrappers/ace/OS_NS_wchar.cpp

https://bitbucket.org/KPsN/trinitycore/
C++ | 378 lines | 275 code | 58 blank | 45 comment | 110 complexity | 3bad2815b59dbf176da5af4a2166b5f1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, CC-BY-SA-3.0
  1. // $Id: OS_NS_wchar.cpp 80826 2008-03-04 14:51:23Z wotte $
  2. #include "ace/OS_NS_wchar.h"
  3. ACE_RCSID(ace, OS_NS_wchar, "$Id: OS_NS_wchar.cpp 80826 2008-03-04 14:51:23Z wotte $")
  4. #if !defined (ACE_HAS_INLINED_OSCALLS)
  5. # include "ace/OS_NS_wchar.inl"
  6. #endif /* ACE_HAS_INLINED_OSCALLS */
  7. #if defined (ACE_HAS_WCHAR)
  8. # include "ace/OS_NS_ctype.h"
  9. # include "ace/OS_NS_string.h"
  10. #endif /* ACE_HAS_WCHAR */
  11. // The following wcs*_emulation methods were created based on BSD code:
  12. /*-
  13. * Copyright (c) 1991, 1993
  14. * The Regents of the University of California. All rights reserved.
  15. *
  16. * This code is derived from software contributed to Berkeley by
  17. * James W. Williams of NASA Goddard Space Flight Center.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. * 2. Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in the
  26. * documentation and/or other materials provided with the distribution.
  27. * 3. All advertising materials mentioning features or use of this software
  28. * must display the following acknowledgement:
  29. * This product includes software developed by the University of
  30. * California, Berkeley and its contributors.
  31. * 4. Neither the name of the University nor the names of its contributors
  32. * may be used to endorse or promote products derived from this software
  33. * without specific prior written permission.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  36. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  38. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  39. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  40. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  41. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  42. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  43. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  44. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  45. * SUCH DAMAGE.
  46. */
  47. ACE_BEGIN_VERSIONED_NAMESPACE_DECL
  48. #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSCAT)
  49. wchar_t *
  50. ACE_OS::wcscat_emulation (wchar_t *destination,
  51. const wchar_t *source)
  52. {
  53. wchar_t *save = destination;
  54. for (; *destination; ++destination);
  55. while ((*destination++ = *source++));
  56. return save;
  57. }
  58. #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSCAT */
  59. #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSCHR)
  60. wchar_t *
  61. ACE_OS::wcschr_emulation (const wchar_t *string, wchar_t c)
  62. {
  63. for (; *string ; ++string)
  64. if (*string == c)
  65. return const_cast<wchar_t *> (string);
  66. return 0;
  67. }
  68. #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSCHR */
  69. #if !defined (ACE_HAS_WCHAR) || defined (ACE_LACKS_WCSCMP)
  70. int
  71. ACE_OS::wcscmp_emulation (const ACE_WCHAR_T *string1,
  72. const ACE_WCHAR_T *string2)
  73. {
  74. while (*string1 == *string2++)
  75. if (*string1++ == 0)
  76. return (0);
  77. return (*string1 - *--string2);
  78. }
  79. #endif /* !ACE_HAS_WCHAR || ACE_LACKS_WCSCMP */
  80. #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSCPY)
  81. wchar_t *
  82. ACE_OS::wcscpy_emulation (wchar_t *destination,
  83. const wchar_t *source)
  84. {
  85. wchar_t *save = destination;
  86. for (; (*destination = *source); ++source, ++destination);
  87. return save;
  88. }
  89. #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSCPY */
  90. #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSCSPN)
  91. size_t
  92. ACE_OS::wcscspn_emulation (const wchar_t *s, const wchar_t *reject)
  93. {
  94. const wchar_t *scan = 0;
  95. const wchar_t *rej_scan = 0;
  96. int count = 0;
  97. for (scan = s; *scan; scan++)
  98. {
  99. for (rej_scan = reject; *rej_scan; rej_scan++)
  100. if (*scan == *rej_scan)
  101. return count;
  102. count++;
  103. }
  104. return count;
  105. }
  106. #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSCSPN */
  107. #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSICMP)
  108. int
  109. ACE_OS::wcsicmp_emulation (const wchar_t *s, const wchar_t *t)
  110. {
  111. const wchar_t *scan1 = s;
  112. const wchar_t *scan2 = t;
  113. while (*scan1 != 0
  114. && ACE_OS::ace_towlower (*scan1)
  115. == ACE_OS::ace_towlower (*scan2))
  116. {
  117. ++scan1;
  118. ++scan2;
  119. }
  120. // The following case analysis is necessary so that characters which
  121. // look negative collate low against normal characters but high
  122. // against the end-of-string NUL.
  123. if (*scan1 == '\0' && *scan2 == '\0')
  124. return 0;
  125. else if (*scan1 == '\0')
  126. return -1;
  127. else if (*scan2 == '\0')
  128. return 1;
  129. else
  130. return ACE_OS::ace_tolower (*scan1) - ACE_OS::ace_towlower (*scan2);
  131. }
  132. #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSICMP */
  133. #if !defined (ACE_HAS_WCHAR) || defined (ACE_LACKS_WCSLEN)
  134. size_t
  135. ACE_OS::wcslen_emulation (const ACE_WCHAR_T *string)
  136. {
  137. const ACE_WCHAR_T *s;
  138. for (s = string; *s; ++s)
  139. continue;
  140. return s - string;
  141. }
  142. #endif /* !ACE_HAS_WCHAR || ACE_LACKS_WCSLEN */
  143. #if !defined (ACE_HAS_WCHAR) || defined (ACE_LACKS_WCSNCAT)
  144. ACE_WCHAR_T *
  145. ACE_OS::wcsncat_emulation (ACE_WCHAR_T *destination,
  146. const ACE_WCHAR_T *source,
  147. size_t count)
  148. {
  149. if (count != 0)
  150. {
  151. ACE_WCHAR_T *d = destination;
  152. const ACE_WCHAR_T *s = source;
  153. while (*d != 0)
  154. d++;
  155. do
  156. {
  157. if ((*d = *s++) == 0)
  158. break;
  159. d++;
  160. } while (--count != 0);
  161. *d = 0;
  162. }
  163. return destination;
  164. }
  165. #endif /* !ACE_HAS_WCHAR || ACE_LACKS_WCSCAT */
  166. #if !defined (ACE_HAS_WCHAR) || defined (ACE_LACKS_WCSNCMP)
  167. int
  168. ACE_OS::wcsncmp_emulation (const ACE_WCHAR_T *s1,
  169. const ACE_WCHAR_T *s2,
  170. size_t len)
  171. {
  172. if (len == 0)
  173. return 0;
  174. do
  175. {
  176. if (*s1 != *s2++)
  177. return (*s1 - *--s2);
  178. if (*s1++ == 0)
  179. break;
  180. } while (--len != 0);
  181. return 0;
  182. }
  183. #endif /* !ACE_HAS_WCHAR || ACE_LACKS_WCSNCMP */
  184. #if !defined (ACE_HAS_WCHAR) || defined (ACE_LACKS_WCSNCPY)
  185. ACE_WCHAR_T *
  186. ACE_OS::wcsncpy_emulation (ACE_WCHAR_T *destination,
  187. const ACE_WCHAR_T *source,
  188. size_t len)
  189. {
  190. if (len != 0)
  191. {
  192. ACE_WCHAR_T *d = destination;
  193. const ACE_WCHAR_T *s = source;
  194. do
  195. {
  196. if ((*d++ = *s++) == 0)
  197. {
  198. // NUL pad the remaining n-1 bytes
  199. while (--len != 0)
  200. *d++ = 0;
  201. break;
  202. }
  203. } while (--len != 0);
  204. }
  205. return destination;
  206. }
  207. #endif /* !ACE_HAS_WCHAR || ACE_LACKS_WCSNCPY */
  208. #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSNICMP)
  209. int
  210. ACE_OS::wcsnicmp_emulation (const wchar_t *s,
  211. const wchar_t *t,
  212. size_t len)
  213. {
  214. const wchar_t *scan1 = s;
  215. const wchar_t *scan2 = t;
  216. size_t count = 0;
  217. while (count++ < len
  218. && *scan1 != 0
  219. && ACE_OS::ace_towlower (*scan1)
  220. == ACE_OS::ace_towlower (*scan2))
  221. {
  222. ++scan1;
  223. ++scan2;
  224. }
  225. if (count > len)
  226. return 0;
  227. // The following case analysis is necessary so that characters which
  228. // look negative collate low against normal characters but high
  229. // against the end-of-string NUL.
  230. if (*scan1 == '\0' && *scan2 == '\0')
  231. return 0;
  232. else if (*scan1 == '\0')
  233. return -1;
  234. else if (*scan2 == '\0')
  235. return 1;
  236. else
  237. return ACE_OS::ace_towlower (*scan1) - ACE_OS::ace_towlower (*scan2);
  238. }
  239. #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSNICMP */
  240. #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSPBRK)
  241. wchar_t *
  242. ACE_OS::wcspbrk_emulation (const wchar_t *string,
  243. const wchar_t *charset)
  244. {
  245. const wchar_t *scanp;
  246. int c, sc;
  247. while ((c = *string++) != 0)
  248. {
  249. for (scanp = charset; (sc = *scanp++) != 0;)
  250. if (sc == c)
  251. return const_cast<wchar_t *> (string - 1);
  252. }
  253. return 0;
  254. }
  255. #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSPBRK */
  256. #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSRCHR)
  257. const wchar_t *
  258. ACE_OS::wcsrchr_emulation (const wchar_t *s, wint_t c)
  259. {
  260. const wchar_t *p = s + ACE_OS::strlen (s);
  261. while (*p != static_cast<wchar_t> (c))
  262. if (p == s)
  263. return 0;
  264. else
  265. p--;
  266. return p;
  267. }
  268. wchar_t *
  269. ACE_OS::wcsrchr_emulation (wchar_t *s, wint_t c)
  270. {
  271. wchar_t *p = s + ACE_OS::strlen (s);
  272. while (*p != static_cast<wchar_t> (c))
  273. if (p == s)
  274. return 0;
  275. else
  276. p--;
  277. return p;
  278. }
  279. #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSRCHR */
  280. #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSSPN)
  281. size_t
  282. ACE_OS::wcsspn_emulation (const wchar_t *string,
  283. const wchar_t *charset)
  284. {
  285. const wchar_t *p = string;
  286. const wchar_t *spanp;
  287. wchar_t c, sc;
  288. // Skip any characters in charset, excluding the terminating \0.
  289. cont:
  290. c = *p++;
  291. for (spanp = charset; (sc = *spanp++) != 0;)
  292. if (sc == c)
  293. goto cont;
  294. return (p - 1 - string);
  295. }
  296. #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSSPN */
  297. #if defined (ACE_HAS_WCHAR) && defined (ACE_LACKS_WCSSTR)
  298. wchar_t *
  299. ACE_OS::wcsstr_emulation (const wchar_t *string,
  300. const wchar_t *charset)
  301. {
  302. wchar_t c, sc;
  303. size_t len;
  304. if ((c = *charset++) != 0)
  305. {
  306. len = ACE_OS::strlen (charset);
  307. do
  308. {
  309. do
  310. {
  311. if ((sc = *string++) == 0)
  312. return 0;
  313. } while (sc != c);
  314. } while (ACE_OS::strncmp (string, charset, len) != 0);
  315. string--;
  316. }
  317. return const_cast<wchar_t *> (string);
  318. }
  319. #endif /* ACE_HAS_WCHAR && ACE_LACKS_WCSSTR */
  320. ACE_END_VERSIONED_NAMESPACE_DECL