PageRenderTime 61ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/dep/acelite/ace/OS_NS_wchar.cpp

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