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.

157 lines
4.4 KiB

  1. /***
  2. *wcsftime.c - String Format Time
  3. *
  4. * Copyright (c) 1993-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *
  8. *Revision History:
  9. * 03-08-93 CFW Module Created.
  10. * 03-10-93 CFW Fixed up properly.
  11. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  12. * 02-07-94 CFW POSIXify.
  13. * 12-16-94 CFW Format must be wchar_t!
  14. * 01-10-95 CFW Debug CRT allocs.
  15. * 08-27-98 GJF Revised multithread support based on threadlocinfo
  16. * struct. Also, use _alloca rather than malloc/free,
  17. * when possible.
  18. * 01-06-99 GJF Changes for 64-bit size_t.
  19. * 12-10-99 GB Added support for recovery from stack overflow around
  20. * _alloca().
  21. *
  22. *******************************************************************************/
  23. #ifndef _POSIX_
  24. #include <cruntime.h>
  25. #include <internal.h>
  26. #include <mtdll.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <wchar.h>
  30. #include <time.h>
  31. #include <dbgint.h>
  32. #include <malloc.h>
  33. #include <excpt.h>
  34. #ifdef _MT
  35. size_t __cdecl _Strftime_mt (pthreadlocinfo ptloci, char *string, size_t maxsize,
  36. const char *format, const struct tm *timeptr, void *lc_time_arg);
  37. #else
  38. size_t __cdecl _Strftime (char *string, size_t maxsize, const char *format,
  39. const struct tm *timeptr, void *lc_time_arg);
  40. #endif
  41. /***
  42. *size_t wcsftime(wstring, maxsize, format, timeptr) - Format a time string
  43. *
  44. *Purpose:
  45. * The wcsftime functions is equivalent to to the strftime function, except
  46. * that the argument 'wstring' specifies an array of a wide string into
  47. * which the generated output is to be placed. The wcsftime acts as if
  48. * strftime were called and the result string converted by mbstowcs().
  49. * [ISO]
  50. *
  51. *Entry:
  52. * wchar_t *wstring = pointer to output string
  53. * size_t maxsize = max length of string
  54. * const wchar_t *format = format control string
  55. * const struct tm *timeptr = pointer to tb data structure
  56. *
  57. *Exit:
  58. * !0 = If the total number of resulting characters including the
  59. * terminating null is not more than 'maxsize', then return the
  60. * number of wide chars placed in the 'wstring' array (not including the
  61. * null terminator).
  62. *
  63. * 0 = Otherwise, return 0 and the contents of the string are
  64. * indeterminate.
  65. *
  66. *Exceptions:
  67. *
  68. *******************************************************************************/
  69. size_t __cdecl wcsftime (
  70. wchar_t *wstring,
  71. size_t maxsize,
  72. const wchar_t *wformat,
  73. const struct tm *timeptr
  74. )
  75. {
  76. size_t retval = 0;
  77. char *format = NULL;
  78. char *string = NULL;
  79. size_t flen = wcslen(wformat) + 1;
  80. int malloc_flag1 = 0;
  81. int malloc_flag2 = 0;
  82. #ifdef _MT
  83. pthreadlocinfo ptloci = _getptd()->ptlocinfo;
  84. if ( ptloci != __ptlocinfo )
  85. ptloci = __updatetlocinfo();
  86. #endif
  87. __try {
  88. string = (char *)_alloca(sizeof(char) * maxsize * 2);
  89. }
  90. __except( EXCEPTION_EXECUTE_HANDLER ) {
  91. _resetstkoflw();
  92. string = NULL;
  93. }
  94. if ( string == NULL ) {
  95. if ((string = (char *)_malloc_crt(sizeof(char) * maxsize * 2)) == NULL)
  96. return 0;
  97. else
  98. malloc_flag1++;
  99. }
  100. __try {
  101. format = (char *)_alloca(sizeof(char) * flen * 2);
  102. }
  103. __except( EXCEPTION_EXECUTE_HANDLER ) {
  104. _resetstkoflw();
  105. format = NULL;
  106. }
  107. if ( format == NULL ) {
  108. if ((format = (char *)_malloc_crt(sizeof(char) * flen * 2)) == NULL)
  109. goto done;
  110. else
  111. malloc_flag2++;
  112. }
  113. #ifdef _MT
  114. if (__wcstombs_mt(ptloci, format, wformat, flen * 2) == -1)
  115. #else
  116. if (wcstombs(format, wformat, flen * 2) == -1)
  117. #endif
  118. goto done;
  119. #ifdef _MT
  120. if (_Strftime_mt(ptloci, string, maxsize * 2, format, timeptr, 0))
  121. #else
  122. if (_Strftime(string, maxsize * 2, format, timeptr, 0))
  123. #endif
  124. {
  125. #ifdef _MT
  126. if ((retval = __mbstowcs_mt(ptloci, wstring, string, maxsize))
  127. == -1)
  128. #else
  129. if ((retval = mbstowcs(wstring, string, maxsize)) == -1)
  130. #endif
  131. retval = 0;
  132. }
  133. done:
  134. if ( malloc_flag1 )
  135. _free_crt(string);
  136. if ( malloc_flag2 )
  137. _free_crt(format);
  138. return retval;
  139. }
  140. #endif /* _POSIX_ */