PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/dep/acelite/ace/OS_NS_wchar.cpp

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