Source code of Windows XP (NT5)
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.

375 lines
8.1 KiB

  1. /*******************************************************************************
  2. Copyright (c) 1995-96 Microsoft Corporation
  3. Abstract:
  4. {Insert General Comment Here}
  5. *******************************************************************************/
  6. #define WIN32_LEAN_AND_MEAN
  7. #include <windows.h>
  8. #include <malloc.h>
  9. #include <tchar.h>
  10. #include <ctype.h>
  11. #include "crt.h"
  12. //#pragma function( strcpy, strlen, strcat )
  13. // *** Flag these as don't use ***//
  14. int vsprintf( char *buffer, const char *format, va_list argptr )
  15. {
  16. #ifdef _DEBUG
  17. OutputDebugString(_T("Don't use vsprintf(), use wsprintf if possible\n"));
  18. #endif
  19. return strlen(buffer);
  20. }
  21. // *** Memory functions *** //
  22. extern "C" void * __cdecl malloc( size_t size )
  23. {
  24. return GlobalAlloc(GMEM_FIXED,size);
  25. }
  26. extern "C" void __cdecl free( void * p )
  27. {
  28. GlobalFree(GlobalHandle(p));
  29. }
  30. extern "C" void * __cdecl realloc( void * pv, size_t newsize )
  31. {
  32. return GlobalLock(GlobalReAlloc(GlobalHandle(pv),newsize, NULL));
  33. }
  34. void __cdecl operator delete( void * p )
  35. {
  36. if (p) free(p);
  37. }
  38. void * __cdecl operator new( unsigned int cb )
  39. {
  40. return malloc(cb);
  41. }
  42. // *** String functions *** //
  43. extern "C" size_t _cdecl wcslen( const wchar_t *string )
  44. {
  45. const wchar_t *s1 = string;
  46. while( *string )
  47. {
  48. string++;
  49. }
  50. return string - s1;
  51. }
  52. extern "C" wchar_t * _cdecl wcscpy( wchar_t *strDestination, const wchar_t *strSource )
  53. {
  54. wchar_t *pRet = strDestination;
  55. while( *strDestination++ = *strSource++ )
  56. ;
  57. return pRet;
  58. }
  59. extern "C" wchar_t * _cdecl wcsncpy( wchar_t *strDest, const wchar_t *strSource, size_t count )
  60. {
  61. if( count < 0 ) return NULL;
  62. wchar_t *pRet = strDest;
  63. DWORD i = wcslen(strSource);
  64. if( i <= count )
  65. {
  66. while( *strDest++ = *strSource++ )
  67. ;
  68. while( i++ < count )
  69. *strDest++ = L'\0';
  70. }
  71. else
  72. {
  73. while( count-- )
  74. *strDest++ = *strSource++;
  75. }
  76. return pRet;
  77. }
  78. extern "C" int __cdecl wcsncmp ( const wchar_t * first, const wchar_t * last, size_t count )
  79. {
  80. if (!count)
  81. return(0);
  82. while (--count && *first && *first == *last)
  83. {
  84. first++;
  85. last++;
  86. }
  87. return((int)(*first - *last));
  88. }
  89. extern "C" int __cdecl wcscmp ( const wchar_t * first, const wchar_t * last )
  90. {
  91. while (*first && *first == *last)
  92. {
  93. first++;
  94. last++;
  95. }
  96. return((int)(*first - *last));
  97. }
  98. extern "C" wchar_t * __cdecl wcsrchr ( const wchar_t * string, wchar_t ch )
  99. {
  100. const wchar_t * p = NULL;
  101. while (TRUE)
  102. {
  103. if (*string == ch)
  104. {
  105. p = string;
  106. }
  107. if (*string == NULL)
  108. {
  109. return (wchar_t *)p;
  110. }
  111. string++;
  112. }
  113. }
  114. extern "C" wchar_t * __cdecl wcschr ( const wchar_t * string, wchar_t ch )
  115. {
  116. while (*string && *string != (wchar_t)ch)
  117. string++;
  118. if (*string == (wchar_t)ch)
  119. return((wchar_t *)string);
  120. return(NULL);
  121. }
  122. extern "C" wchar_t * __cdecl wcsstr ( const wchar_t * wcs1, const wchar_t * wcs2 )
  123. {
  124. wchar_t *cp = (wchar_t *) wcs1;
  125. wchar_t *s1, *s2;
  126. while (*cp)
  127. {
  128. s1 = cp;
  129. s2 = (wchar_t *) wcs2;
  130. while ( *s1 && *s2 && !(*s1-*s2) )
  131. s1++, s2++;
  132. if (!*s2)
  133. return(cp);
  134. cp++;
  135. }
  136. return(NULL);
  137. }
  138. extern "C" wchar_t * __cdecl wcscat ( wchar_t * dst, const wchar_t * src )
  139. {
  140. wchar_t * cp = dst;
  141. while( *cp )
  142. cp++; /* find end of dst */
  143. while( *cp++ = *src++ ) ; /* Copy src to end of dst */
  144. return( dst ); /* return dst */
  145. }
  146. // miscellaneous
  147. extern "C" int __cdecl _purecall()
  148. {
  149. DebugBreak();
  150. return 0;
  151. }
  152. extern "C" char * __cdecl _ultoa ( unsigned long val, char *buf, int radix )
  153. {
  154. xtoa(val, buf, radix, 0);
  155. return buf;
  156. }
  157. extern "C" wchar_t * __cdecl _ultow ( unsigned long val, wchar_t *buf, int radix )
  158. {
  159. char astring[40];
  160. _ultoa (val, astring, radix);
  161. MultiByteToWideChar (CP_ACP, MB_PRECOMPOSED, astring, -1, buf, 40);
  162. return (buf);
  163. }
  164. extern "C" static void __cdecl xtoa ( unsigned long val, char *buf, unsigned radix, int is_neg )
  165. {
  166. char *p; /* pointer to traverse string */
  167. char *firstdig; /* pointer to first digit */
  168. char temp; /* temp char */
  169. unsigned digval; /* value of digit */
  170. p = buf;
  171. if (is_neg) {
  172. /* negative, so output '-' and negate */
  173. *p++ = '-';
  174. val = (unsigned long)(-(long)val);
  175. }
  176. firstdig = p; /* save pointer to first digit */
  177. do {
  178. digval = (unsigned) (val % radix);
  179. val /= radix; /* get next digit */
  180. /* convert to ascii and store */
  181. if (digval > 9)
  182. *p++ = (char) (digval - 10 + 'a'); /* a letter */
  183. else
  184. *p++ = (char) (digval + '0'); /* a digit */
  185. } while (val > 0);
  186. /* We now have the digit of the number in the buffer, but in reverse
  187. order. Thus we reverse them now. */
  188. *p-- = '\0'; /* terminate string; p points to last digit */
  189. do {
  190. temp = *p;
  191. *p = *firstdig;
  192. *firstdig = temp; /* swap *p and *firstdig */
  193. --p;
  194. ++firstdig; /* advance to next two digits */
  195. } while (firstdig < p); /* repeat until halfway */
  196. }
  197. extern "C" int __cdecl isspace(int c)
  198. {
  199. if( c == ' ' || (c >= 0x09 && c <= 0x0d) )
  200. return 1;
  201. return 0;
  202. }
  203. extern "C" int __cdecl isdigit(int c)
  204. {
  205. if( c >= '0' && c <= '9' ) return 1;
  206. return 0;
  207. }
  208. extern "C" long __cdecl atol( const char *nptr )
  209. {
  210. int c; /* current char */
  211. long total; /* current total */
  212. int sign; /* if '-', then negative, otherwise positive */
  213. /* skip whitespace */
  214. while ( isspace((int)(unsigned char)*nptr) )
  215. ++nptr;
  216. c = (int)(unsigned char)*nptr++;
  217. sign = c; /* save sign indication */
  218. if (c == '-' || c == '+')
  219. c = (int)(unsigned char)*nptr++; /* skip sign */
  220. total = 0;
  221. while (isdigit(c)) {
  222. total = 10 * total + (c - '0'); /* accumulate digit */
  223. c = (int)(unsigned char)*nptr++; /* get next char */
  224. }
  225. if (sign == '-')
  226. return -total;
  227. else
  228. return total; /* return result, negated if necessary */
  229. }
  230. extern "C" long __cdecl _wtol( const wchar_t *nptr )
  231. {
  232. char astring[20];
  233. WideCharToMultiByte (CP_ACP, 0, nptr, -1, astring, 20, NULL, NULL);
  234. return (atol(astring));
  235. }
  236. extern "C" int __cdecl wprintf( const wchar_t *format, ... )
  237. {
  238. return 0; // no characters printed
  239. }
  240. extern "C" void * __cdecl calloc( size_t num, size_t size )
  241. {
  242. long nBytes = num*size;
  243. void *pRet = malloc( nBytes );
  244. if( pRet )
  245. {
  246. ZeroMemory( pRet, nBytes );
  247. }
  248. return pRet;
  249. }
  250. extern "C" wchar_t * __cdecl _wcsdup( const wchar_t *strSource )
  251. {
  252. wchar_t *pRet = NULL;
  253. if( strSource )
  254. {
  255. int nLen = wcslen( strSource );
  256. pRet = (wchar_t*)malloc( (nLen+1) * sizeof(WCHAR) );
  257. if( pRet )
  258. {
  259. wcscpy( pRet, strSource );
  260. }
  261. }
  262. return pRet;
  263. }
  264. extern "C" char * __cdecl strncpy ( char * dest, const char * source, size_t count )
  265. {
  266. char *start = dest;
  267. while (count && (*dest++ = *source++)) /* copy string */
  268. count--;
  269. if (count) /* pad out with zeroes */
  270. while (--count)
  271. *dest++ = '\0';
  272. return(start);
  273. }
  274. extern "C" char * __cdecl strcpy(char * dst, const char * src)
  275. {
  276. char * cp = dst;
  277. while( *cp++ = *src++ )
  278. ; /* Copy src over dst */
  279. return( dst );
  280. }
  281. extern "C" char * __cdecl strcat ( char * dst, const char * src )
  282. {
  283. char * cp = dst;
  284. while( *cp )
  285. cp++; /* find end of dst */
  286. while( *cp++ = *src++ ) ; /* Copy src to end of dst */
  287. return( dst ); /* return dst */
  288. }
  289. extern "C" size_t __cdecl strlen ( const char * str )
  290. {
  291. const char *eos = str;
  292. while( *eos++ ) ;
  293. return( (int)(eos - str - 1) );
  294. }