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.

82 lines
2.2 KiB

  1. /***
  2. *wtof.c - convert wchar_t string to floating point number
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Converts a wide character string into a floating point number.
  8. *
  9. *Revision History:
  10. * 05-18-00 GB written.
  11. * 08-29-00 GB Fixed buffer overrun.
  12. * 02-19-01 GB added _alloca and Check for return value of _malloc_crt
  13. *
  14. *******************************************************************************/
  15. #ifndef _POSIX_
  16. #ifndef _UNICODE
  17. #define _UNICODE
  18. #endif
  19. #include <cruntime.h>
  20. #include <internal.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. #include <dbgint.h>
  25. #include <errno.h>
  26. #include <malloc.h>
  27. /***
  28. *double wtof(ptr) - convert wide char string to floating point number
  29. *
  30. *Purpose:
  31. * atof recognizes an optional string of whitespace, then
  32. * an optional sign, then a string of digits optionally
  33. * containing a decimal point, then an optional e or E followed
  34. * by an optionally signed integer, and converts all this to
  35. * to a floating point number. The first unrecognized
  36. * character ends the string.
  37. *
  38. *Entry:
  39. * ptr - pointer to wide char string to convert
  40. *
  41. *Exit:
  42. * returns floating point value of wide character representation
  43. *
  44. *Exceptions:
  45. *
  46. *******************************************************************************/
  47. double __cdecl _wtof(
  48. const wchar_t *ptr
  49. )
  50. {
  51. char *cptr;
  52. int malloc_flag = 0;
  53. size_t len;
  54. double retval;
  55. while (iswspace(*ptr))
  56. ptr++;
  57. len = wcstombs(NULL, ptr, 0);
  58. __try{
  59. cptr = (char *)_alloca((len+1) * sizeof(wchar_t));
  60. }
  61. __except(1){ //EXCEPTION_EXECUTE_HANDLER
  62. _resetstkoflw();
  63. if ((cptr = (char *)_malloc_crt((len+1) * sizeof(wchar_t))) == NULL)
  64. {
  65. errno = ENOMEM;
  66. return 0.0;
  67. }
  68. malloc_flag = 1;
  69. }
  70. // UNDONE: check for errors
  71. // Add one to len so as to null terminate cptr.
  72. wcstombs(cptr, ptr, len+1);
  73. retval = atof(cptr);
  74. if (malloc_flag)
  75. _free_crt(cptr);
  76. return retval;
  77. }
  78. #endif