Leaked source code of windows server 2003
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.

243 lines
5.9 KiB

  1. /*
  2. *
  3. * Copyright (c) 1985-1996, Microsoft Corporation. All Rights Reserved.
  4. *
  5. * Character functions (to and from wide characters)
  6. i*
  7. ******************************************************************************
  8. */
  9. #include "h/wchar.h"
  10. #include <errno.h>
  11. /*
  12. ** Converts a single byte i.e. ascii string to the wide char format
  13. **
  14. ** NOTE: This function does NOT handle multibyte characters!
  15. ** It should be used only when wchar_t is not 2 bytes and
  16. ** we cannot use the standard functions
  17. **
  18. */
  19. #ifndef _MSC_VER
  20. size_t sbstowcs(WCHAR *pwcs, const char *s, size_t n )
  21. {
  22. size_t count=0;
  23. /* if destintation string exists, fill it in */
  24. if (pwcs)
  25. {
  26. while (count < n)
  27. {
  28. *pwcs = (WCHAR) ( (unsigned char)s[count]);
  29. if (!s[count])
  30. return count;
  31. count++;
  32. pwcs++;
  33. }
  34. return count;
  35. }
  36. else { /* pwcs == NULL, get size only, s must be NUL-terminated */
  37. return strlen(s);
  38. }
  39. }
  40. #endif
  41. /***
  42. *size_t wcstrsbs() - Convert wide char string to single byte char string.
  43. *
  44. *Purpose:
  45. * Convert a wide char string into the equivalent multibyte char string
  46. * [ANSI].
  47. *
  48. *Entry:
  49. * char *s = pointer to destination char string
  50. * const WCHAR *pwc = pointer to source wide character string
  51. * size_t n = maximum number of bytes to store in s
  52. *
  53. *Exit:
  54. * If s != NULL, returns (size_t)-1 (if a wchar cannot be converted)
  55. * Otherwise: Number of bytes modified (<=n), not including
  56. * the terminating NUL, if any.
  57. *
  58. *Exceptions
  59. * Returns (size_t)-1 if s is NULL or invalid mb character encountered.
  60. *
  61. *******************************************************************************/
  62. size_t __cdecl wcstosbs( char * s, const WCHAR * pwcs, size_t n)
  63. {
  64. size_t count=0;
  65. /* if destination string exists, fill it in */
  66. if (s)
  67. {
  68. while(count < n)
  69. {
  70. if (*pwcs > 255) /* validate high byte */
  71. {
  72. errno = EILSEQ;
  73. return (size_t)-1; /* error */
  74. }
  75. s[count] = (char) *pwcs;
  76. if (!(*pwcs++))
  77. return count;
  78. count++;
  79. }
  80. return count;
  81. } else { /* s == NULL, get size only, pwcs must be NUL-terminated */
  82. const WCHAR *eos = pwcs;
  83. while (*eos++);
  84. return ( (size_t) (eos - pwcs -1));
  85. }
  86. }
  87. /******
  88. * WCHAR *wcscat(dst, src) - concatenate (append) one wide character string
  89. * to another
  90. *
  91. *Purpose:
  92. * Concatenates src onto the end of dest. Assumes enough
  93. * space in dest.
  94. *
  95. *Entry:
  96. * WCHAR *dst - wide character string to which "src" is to be appended
  97. * const WCHAR *src - wide character string to append to end of "dst"
  98. *
  99. *Exit:
  100. * The address of "dst"
  101. *
  102. *Exceptions:
  103. *
  104. *******************************************************************************/
  105. WCHAR * __cdecl wcscat(WCHAR * dst, const WCHAR * src)
  106. {
  107. WCHAR * cp = dst;
  108. while( *cp )
  109. ++cp; /* Find end of dst */
  110. wcscpy(cp,src); /* Copy src to end of dst */
  111. return dst; /* return dst */
  112. }
  113. /***
  114. *WCHAR *wcscpy(dst, src) - copy one wide character string over another
  115. *
  116. *Purpose:
  117. * Copies the wide character string src into the spot specified by
  118. * dest; assumes enough room.
  119. *
  120. *Entry:
  121. * WCHAR * dst - wide character string over which "src" is to be copied
  122. * const WCHAR * src - string to be copied over "dst"
  123. *
  124. *Exit:
  125. * The address of "dst"
  126. *
  127. *Exceptions:
  128. *******************************************************************************/
  129. WCHAR * __cdecl wcscpy(WCHAR * dst, const WCHAR * src)
  130. {
  131. WCHAR * cp = dst;
  132. while( *cp++ = *src++ )
  133. ; /* Copy src over dst */
  134. return dst;
  135. }
  136. /***
  137. *wcslen - return the length of a null-terminated string
  138. *
  139. *Purpose:
  140. * Finds the number of wide characters in the given wide character
  141. * string, not including the final null character.
  142. *
  143. *Entry:
  144. * const wchat_t * str - string whose length is to be computed
  145. *
  146. *Exit:
  147. * length of the string "str", exclusive of the final null wide character
  148. *
  149. *Exceptions:
  150. *
  151. *******************************************************************************/
  152. size_t __cdecl wcslen(const WCHAR * str)
  153. {
  154. WCHAR *string = (WCHAR *) str;
  155. while( *string )
  156. string++;
  157. return string - str;
  158. }
  159. /****************************************************************************
  160. *wcsnicmp.c - compare first n characters of two wide character strings with
  161. * case insensitivity
  162. *
  163. * Copyright (c) 1985-1996, Microsoft Corporation. All rights reserved.
  164. *
  165. *Purpose:
  166. * defines wcsnicmp() - compare first n characters of two wide character
  167. * strings for lexical order with case insensitivity.
  168. *
  169. *****************************************************************************/
  170. /***
  171. *WCHAR wcUp(wc) - upper case wide character
  172. ****/
  173. static WCHAR wcUp(WCHAR wc)
  174. {
  175. if ('a' <= wc && wc <= 'z')
  176. wc += (WCHAR)('A' - 'a');
  177. return(wc);
  178. }
  179. /***
  180. *int wcsnicmp(first, last, count) - compare first count wide characters of wide
  181. * character strings with case insensitivity.
  182. *
  183. *Purpose:
  184. * Compares two wide character strings for lexical order. The comparison
  185. * stops after: (1) a difference between the strings is found, (2) the end
  186. * of the strings is reached, or (3) count characters have been
  187. * compared.
  188. *
  189. *Entry:
  190. * char *first, *last - wide character strings to compare
  191. * unsigned count - maximum number of wide characters to compare
  192. *
  193. *Exit:
  194. * returns <0 if first < last
  195. * returns 0 if first == last
  196. * returns >0 if first > last
  197. *
  198. *Exceptions:
  199. *
  200. *******************************************************************************/
  201. int __cdecl wcsnicmp(const WCHAR * first, const WCHAR * last, size_t count)
  202. {
  203. if (!count)
  204. return 0;
  205. while (--count && *first && wcUp(*first) == wcUp(*last))
  206. {
  207. first++;
  208. last++;
  209. }
  210. return wcUp(*first) - wcUp(*last);
  211. }