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.

266 lines
12 KiB

  1. /***
  2. *inittime.c - contains __init_time
  3. *
  4. * Copyright (c) 1991-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Contains the locale-category initialization function: __init_time().
  8. *
  9. * Each initialization function sets up locale-specific information
  10. * for their category, for use by functions which are affected by
  11. * their locale category.
  12. *
  13. * *** For internal use by setlocale() only ***
  14. *
  15. *Revision History:
  16. * 12-08-91 ETC Created.
  17. * 12-20-91 ETC Updated to use new NLSAPI GetLocaleInfo.
  18. * 12-18-92 CFW Ported to Cuda tree, changed _CALLTYPE4 to _CRTAPI3.
  19. * 12-29-92 CFW Updated to use new _getlocaleinfo wrapper function.
  20. * 01-25-93 KRS Adapted to use ctry or lang dependent data, as approp.
  21. * 02-08-93 CFW Casts to remove warnings.
  22. * 02-16-93 CFW Added support for date and time strings.
  23. * 03-09-93 CFW Use char* time_sep in storeTimeFmt.
  24. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  25. * 05-20-93 GJF Include windows.h, not individual win*.h files
  26. * 05-24-93 CFW Clean up file (brief is evil).
  27. * 06-11-93 CFW Now inithelp takes void *.
  28. * 09-15-93 CFW Use ANSI conformant "__" names.
  29. * 09-22-93 GJF Merged NT SDK and Cuda versions.
  30. * 04-11-94 GJF Made declaration of __lc_time_curr, and definition of
  31. * __lc_time_intl conditional on ndef DLL_FOR_WIN32S.
  32. * Also, made storeTimeFmt() into a static function.
  33. * 09-06-94 CFW Remove _INTL switch.
  34. * 01-10-95 CFW Debug CRT allocs.
  35. * 08-20-97 GJF Get time format string from Win32 rather than making
  36. * up our own.
  37. * 06-26-98 GJF Changed to support multithread scheme - an old
  38. * __lc_time_data struct must be kept around until all
  39. * affected threads have updated or terminated.
  40. * 03-25-01 PML Add ww_caltype & ww_lcid to __lc_time_data (vs7#196892)
  41. *
  42. *******************************************************************************/
  43. #include <stdlib.h>
  44. #include <windows.h>
  45. #include <locale.h>
  46. #include <setlocal.h>
  47. #include <malloc.h>
  48. #include <dbgint.h>
  49. static int __cdecl _get_lc_time(struct __lc_time_data *lc_time);
  50. void __cdecl __free_lc_time(struct __lc_time_data *lc_time);
  51. /* C locale time strings */
  52. extern struct __lc_time_data __lc_time_c;
  53. /* Pointer to current time strings */
  54. extern struct __lc_time_data *__lc_time_curr;
  55. /* Pointer to non-C locale time strings */
  56. struct __lc_time_data *__lc_time_intl = NULL;
  57. /***
  58. *int __init_time() - initialization for LC_TIME locale category.
  59. *
  60. *Purpose:
  61. * In non-C locales, read the localized time/date strings into
  62. * __lc_time_intl, and set __lc_time_curr to point to it. The old
  63. * __lc_time_intl is not freed until the new one is fully established.
  64. *
  65. * In the C locale, __lc_time_curr is made to point to __lc_time_c.
  66. * Any allocated __lc_time_intl structures are freed.
  67. *
  68. *Entry:
  69. * None.
  70. *
  71. *Exit:
  72. * 0 success
  73. * 1 fail
  74. *
  75. *Exceptions:
  76. *
  77. *******************************************************************************/
  78. int __cdecl __init_time (
  79. void
  80. )
  81. {
  82. /* Temporary date/time strings */
  83. struct __lc_time_data *lc_time;
  84. if ( __lc_handle[LC_TIME] != _CLOCALEHANDLE )
  85. {
  86. /* Allocate structure filled with NULL pointers */
  87. if ( (lc_time = (struct __lc_time_data *)
  88. _calloc_crt(1, sizeof(struct __lc_time_data))) == NULL )
  89. return 1;
  90. if (_get_lc_time (lc_time))
  91. {
  92. __free_lc_time (lc_time);
  93. _free_crt (lc_time);
  94. return 1;
  95. }
  96. __lc_time_curr = lc_time; /* point to new one */
  97. #ifndef _MT
  98. __free_lc_time (__lc_time_intl); /* free the old one */
  99. _free_crt (__lc_time_intl);
  100. #endif
  101. __lc_time_intl = lc_time;
  102. return 0;
  103. } else {
  104. __lc_time_curr = &__lc_time_c; /* point to new one */
  105. #ifndef _MT
  106. __free_lc_time (__lc_time_intl); /* free the old one */
  107. _free_crt (__lc_time_intl);
  108. #endif
  109. __lc_time_intl = NULL;
  110. return 0;
  111. }
  112. }
  113. /*
  114. * Get the localized time strings.
  115. * Of course, this can be beautified with some loops!
  116. */
  117. static int __cdecl _get_lc_time (
  118. struct __lc_time_data *lc_time
  119. )
  120. {
  121. int ret = 0;
  122. /* Some things are language-dependent and some are country-dependent.
  123. This works around an NT limitation and lets us distinguish the two. */
  124. LCID langid = MAKELCID(__lc_id[LC_TIME].wLanguage, SORT_DEFAULT);
  125. LCID ctryid = MAKELCID(__lc_id[LC_TIME].wCountry, SORT_DEFAULT);
  126. if (lc_time == NULL)
  127. return -1;
  128. /* All the text-strings are Language-dependent: */
  129. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVDAYNAME1, (void *)&lc_time->wday_abbr[1]);
  130. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVDAYNAME2, (void *)&lc_time->wday_abbr[2]);
  131. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVDAYNAME3, (void *)&lc_time->wday_abbr[3]);
  132. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVDAYNAME4, (void *)&lc_time->wday_abbr[4]);
  133. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVDAYNAME5, (void *)&lc_time->wday_abbr[5]);
  134. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVDAYNAME6, (void *)&lc_time->wday_abbr[6]);
  135. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVDAYNAME7, (void *)&lc_time->wday_abbr[0]);
  136. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SDAYNAME1, (void *)&lc_time->wday[1]);
  137. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SDAYNAME2, (void *)&lc_time->wday[2]);
  138. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SDAYNAME3, (void *)&lc_time->wday[3]);
  139. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SDAYNAME4, (void *)&lc_time->wday[4]);
  140. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SDAYNAME5, (void *)&lc_time->wday[5]);
  141. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SDAYNAME6, (void *)&lc_time->wday[6]);
  142. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SDAYNAME7, (void *)&lc_time->wday[0]);
  143. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME1, (void *)&lc_time->month_abbr[0]);
  144. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME2, (void *)&lc_time->month_abbr[1]);
  145. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME3, (void *)&lc_time->month_abbr[2]);
  146. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME4, (void *)&lc_time->month_abbr[3]);
  147. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME5, (void *)&lc_time->month_abbr[4]);
  148. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME6, (void *)&lc_time->month_abbr[5]);
  149. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME7, (void *)&lc_time->month_abbr[6]);
  150. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME8, (void *)&lc_time->month_abbr[7]);
  151. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME9, (void *)&lc_time->month_abbr[8]);
  152. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME10, (void *)&lc_time->month_abbr[9]);
  153. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME11, (void *)&lc_time->month_abbr[10]);
  154. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME12, (void *)&lc_time->month_abbr[11]);
  155. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME1, (void *)&lc_time->month[0]);
  156. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME2, (void *)&lc_time->month[1]);
  157. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME3, (void *)&lc_time->month[2]);
  158. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME4, (void *)&lc_time->month[3]);
  159. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME5, (void *)&lc_time->month[4]);
  160. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME6, (void *)&lc_time->month[5]);
  161. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME7, (void *)&lc_time->month[6]);
  162. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME8, (void *)&lc_time->month[7]);
  163. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME9, (void *)&lc_time->month[8]);
  164. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME10, (void *)&lc_time->month[9]);
  165. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME11, (void *)&lc_time->month[10]);
  166. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME12, (void *)&lc_time->month[11]);
  167. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_S1159, (void *)&lc_time->ampm[0]);
  168. ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_S2359, (void *)&lc_time->ampm[1]);
  169. /* The following relate to time format and are Country-dependent: */
  170. ret |= __getlocaleinfo(LC_STR_TYPE, ctryid, LOCALE_SSHORTDATE, (void *)&lc_time->ww_sdatefmt);
  171. ret |= __getlocaleinfo(LC_STR_TYPE, ctryid, LOCALE_SLONGDATE, (void *)&lc_time->ww_ldatefmt);
  172. ret |= __getlocaleinfo(LC_STR_TYPE, ctryid, LOCALE_STIMEFORMAT, (void *)&lc_time->ww_timefmt);
  173. ret |= __getlocaleinfo(LC_INT_TYPE, ctryid, LOCALE_ICALENDARTYPE, (void *)&lc_time->ww_caltype);
  174. lc_time->ww_lcid = ctryid;
  175. return ret;
  176. }
  177. /*
  178. * Free the localized time strings.
  179. * Of course, this can be beautified with some loops!
  180. */
  181. void __cdecl __free_lc_time (
  182. struct __lc_time_data *lc_time
  183. )
  184. {
  185. if (lc_time == NULL)
  186. return;
  187. _free_crt (lc_time->wday_abbr[1]);
  188. _free_crt (lc_time->wday_abbr[2]);
  189. _free_crt (lc_time->wday_abbr[3]);
  190. _free_crt (lc_time->wday_abbr[4]);
  191. _free_crt (lc_time->wday_abbr[5]);
  192. _free_crt (lc_time->wday_abbr[6]);
  193. _free_crt (lc_time->wday_abbr[0]);
  194. _free_crt (lc_time->wday[1]);
  195. _free_crt (lc_time->wday[2]);
  196. _free_crt (lc_time->wday[3]);
  197. _free_crt (lc_time->wday[4]);
  198. _free_crt (lc_time->wday[5]);
  199. _free_crt (lc_time->wday[6]);
  200. _free_crt (lc_time->wday[0]);
  201. _free_crt (lc_time->month_abbr[0]);
  202. _free_crt (lc_time->month_abbr[1]);
  203. _free_crt (lc_time->month_abbr[2]);
  204. _free_crt (lc_time->month_abbr[3]);
  205. _free_crt (lc_time->month_abbr[4]);
  206. _free_crt (lc_time->month_abbr[5]);
  207. _free_crt (lc_time->month_abbr[6]);
  208. _free_crt (lc_time->month_abbr[7]);
  209. _free_crt (lc_time->month_abbr[8]);
  210. _free_crt (lc_time->month_abbr[9]);
  211. _free_crt (lc_time->month_abbr[10]);
  212. _free_crt (lc_time->month_abbr[11]);
  213. _free_crt (lc_time->month[0]);
  214. _free_crt (lc_time->month[1]);
  215. _free_crt (lc_time->month[2]);
  216. _free_crt (lc_time->month[3]);
  217. _free_crt (lc_time->month[4]);
  218. _free_crt (lc_time->month[5]);
  219. _free_crt (lc_time->month[6]);
  220. _free_crt (lc_time->month[7]);
  221. _free_crt (lc_time->month[8]);
  222. _free_crt (lc_time->month[9]);
  223. _free_crt (lc_time->month[10]);
  224. _free_crt (lc_time->month[11]);
  225. _free_crt (lc_time->ampm[0]);
  226. _free_crt (lc_time->ampm[1]);
  227. _free_crt (lc_time->ww_sdatefmt);
  228. _free_crt (lc_time->ww_ldatefmt);
  229. _free_crt (lc_time->ww_timefmt);
  230. /* Don't need to make these pointers NULL */
  231. }