home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / inittime.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  10KB  |  235 lines

  1. /***
  2. *inittime.c - contains __init_time
  3. *
  4. *       Copyright (c) 1991-1997, 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. *******************************************************************************/
  16.  
  17. #include <stdlib.h>
  18. #include <windows.h>
  19. #include <locale.h>
  20. #include <setlocal.h>
  21. #include <malloc.h>
  22. #include <dbgint.h>
  23.  
  24. static int __cdecl _get_lc_time(struct __lc_time_data *lc_time);
  25. static void __cdecl _free_lc_time(struct __lc_time_data *lc_time);
  26.  
  27. /* C locale time strings */
  28. extern struct __lc_time_data __lc_time_c;
  29.  
  30.  
  31. /* Pointer to current time strings */
  32. extern struct __lc_time_data *__lc_time_curr;
  33.  
  34. /* Pointer to non-C locale time strings */
  35. struct __lc_time_data *__lc_time_intl = NULL;
  36.  
  37.  
  38.  
  39. /***
  40. *int __init_time() - initialization for LC_TIME locale category.
  41. *
  42. *Purpose:
  43. *       In non-C locales, read the localized time/date strings into
  44. *       __lc_time_intl, and set __lc_time_curr to point to it.  The old
  45. *       __lc_time_intl is not freed until the new one is fully established.
  46. *
  47. *       In the C locale, __lc_time_curr is made to point to __lc_time_c.
  48. *       Any allocated __lc_time_intl structures are freed.
  49. *
  50. *Entry:
  51. *       None.
  52. *
  53. *Exit:
  54. *       0 success
  55. *       1 fail
  56. *
  57. *Exceptions:
  58. *
  59. *******************************************************************************/
  60.  
  61. int __cdecl __init_time (
  62.         void
  63.         )
  64. {
  65.         /* Temporary date/time strings */
  66.         struct __lc_time_data *lc_time;
  67.  
  68.         if (__lc_handle[LC_TIME] != _CLOCALEHANDLE)
  69.         {
  70.                 /* Allocate structure filled with NULL pointers */
  71.                 if ((lc_time = (struct __lc_time_data *)
  72.                 _calloc_crt (1, sizeof(struct __lc_time_data))) == NULL)
  73.                         return 1;
  74.  
  75.                 if (_get_lc_time (lc_time))
  76.                 {
  77.                         _free_lc_time (lc_time);
  78.                         _free_crt (lc_time);
  79.                         return 1;
  80.                 }
  81.  
  82.                 __lc_time_curr = lc_time;       /* point to new one */
  83.                 _free_lc_time (__lc_time_intl); /* free the old one */
  84.                 _free_crt (__lc_time_intl);
  85.                 __lc_time_intl = lc_time;
  86.                 return 0;
  87.  
  88.         } else {
  89.                 __lc_time_curr = &__lc_time_c;  /* point to new one */
  90.                 _free_lc_time (__lc_time_intl); /* free the old one */
  91.                 _free_crt (__lc_time_intl);
  92.                 __lc_time_intl = NULL;
  93.                 return 0;
  94.         }
  95. }
  96.  
  97. /*
  98.  *  Get the localized time strings.
  99.  *  Of course, this can be beautified with some loops!
  100.  */
  101. static int __cdecl _get_lc_time (
  102.         struct __lc_time_data *lc_time
  103.         )
  104. {
  105.         int ret = 0;
  106.  
  107.         /* Some things are language-dependent and some are country-dependent.
  108.         This works around an NT limitation and lets us distinguish the two. */
  109.  
  110.         LCID langid = MAKELCID(__lc_id[LC_TIME].wLanguage, SORT_DEFAULT);
  111.         LCID ctryid = MAKELCID(__lc_id[LC_TIME].wCountry, SORT_DEFAULT);
  112.  
  113.         if (lc_time == NULL)
  114.                 return -1;
  115.  
  116.         /* All the text-strings are Language-dependent: */
  117.  
  118.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVDAYNAME1, (void *)&lc_time->wday_abbr[1]);
  119.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVDAYNAME2, (void *)&lc_time->wday_abbr[2]);
  120.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVDAYNAME3, (void *)&lc_time->wday_abbr[3]);
  121.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVDAYNAME4, (void *)&lc_time->wday_abbr[4]);
  122.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVDAYNAME5, (void *)&lc_time->wday_abbr[5]);
  123.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVDAYNAME6, (void *)&lc_time->wday_abbr[6]);
  124.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVDAYNAME7, (void *)&lc_time->wday_abbr[0]);
  125.  
  126.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SDAYNAME1, (void *)&lc_time->wday[1]);
  127.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SDAYNAME2, (void *)&lc_time->wday[2]);
  128.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SDAYNAME3, (void *)&lc_time->wday[3]);
  129.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SDAYNAME4, (void *)&lc_time->wday[4]);
  130.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SDAYNAME5, (void *)&lc_time->wday[5]);
  131.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SDAYNAME6, (void *)&lc_time->wday[6]);
  132.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SDAYNAME7, (void *)&lc_time->wday[0]);
  133.  
  134.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME1, (void *)&lc_time->month_abbr[0]);
  135.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME2, (void *)&lc_time->month_abbr[1]);
  136.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME3, (void *)&lc_time->month_abbr[2]);
  137.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME4, (void *)&lc_time->month_abbr[3]);
  138.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME5, (void *)&lc_time->month_abbr[4]);
  139.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME6, (void *)&lc_time->month_abbr[5]);
  140.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME7, (void *)&lc_time->month_abbr[6]);
  141.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME8, (void *)&lc_time->month_abbr[7]);
  142.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME9, (void *)&lc_time->month_abbr[8]);
  143.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME10, (void *)&lc_time->month_abbr[9]);
  144.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME11, (void *)&lc_time->month_abbr[10]);
  145.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SABBREVMONTHNAME12, (void *)&lc_time->month_abbr[11]);
  146.  
  147.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME1, (void *)&lc_time->month[0]);
  148.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME2, (void *)&lc_time->month[1]);
  149.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME3, (void *)&lc_time->month[2]);
  150.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME4, (void *)&lc_time->month[3]);
  151.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME5, (void *)&lc_time->month[4]);
  152.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME6, (void *)&lc_time->month[5]);
  153.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME7, (void *)&lc_time->month[6]);
  154.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME8, (void *)&lc_time->month[7]);
  155.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME9, (void *)&lc_time->month[8]);
  156.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME10, (void *)&lc_time->month[9]);
  157.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME11, (void *)&lc_time->month[10]);
  158.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_SMONTHNAME12, (void *)&lc_time->month[11]);
  159.  
  160.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_S1159, (void *)&lc_time->ampm[0]);
  161.         ret |= __getlocaleinfo(LC_STR_TYPE, langid, LOCALE_S2359, (void *)&lc_time->ampm[1]);
  162.  
  163.  
  164. /* The following relate to time format and are Country-dependent: */
  165.  
  166.         ret |= __getlocaleinfo(LC_STR_TYPE, ctryid, LOCALE_SSHORTDATE, (void *)&lc_time->ww_sdatefmt);
  167.         ret |= __getlocaleinfo(LC_STR_TYPE, ctryid, LOCALE_SLONGDATE, (void *)&lc_time->ww_ldatefmt);
  168.  
  169.         ret |= __getlocaleinfo(LC_STR_TYPE, ctryid, LOCALE_STIMEFORMAT, (void *)&lc_time->ww_timefmt);
  170.  
  171.         return ret;
  172. }
  173.  
  174. /*
  175.  *  Free the localized time strings.
  176.  *  Of course, this can be beautified with some loops!
  177.  */
  178. static void __cdecl _free_lc_time (
  179.         struct __lc_time_data *lc_time
  180.         )
  181. {
  182.         if (lc_time == NULL)
  183.                 return;
  184.  
  185.         _free_crt (lc_time->wday_abbr[1]);
  186.         _free_crt (lc_time->wday_abbr[2]);
  187.         _free_crt (lc_time->wday_abbr[3]);
  188.         _free_crt (lc_time->wday_abbr[4]);
  189.         _free_crt (lc_time->wday_abbr[5]);
  190.         _free_crt (lc_time->wday_abbr[6]);
  191.         _free_crt (lc_time->wday_abbr[0]);
  192.  
  193.         _free_crt (lc_time->wday[1]);
  194.         _free_crt (lc_time->wday[2]);
  195.         _free_crt (lc_time->wday[3]);
  196.         _free_crt (lc_time->wday[4]);
  197.         _free_crt (lc_time->wday[5]);
  198.         _free_crt (lc_time->wday[6]);
  199.         _free_crt (lc_time->wday[0]);
  200.  
  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.  
  214.         _free_crt (lc_time->month[0]);
  215.         _free_crt (lc_time->month[1]);
  216.         _free_crt (lc_time->month[2]);
  217.         _free_crt (lc_time->month[3]);
  218.         _free_crt (lc_time->month[4]);
  219.         _free_crt (lc_time->month[5]);
  220.         _free_crt (lc_time->month[6]);
  221.         _free_crt (lc_time->month[7]);
  222.         _free_crt (lc_time->month[8]);
  223.         _free_crt (lc_time->month[9]);
  224.         _free_crt (lc_time->month[10]);
  225.         _free_crt (lc_time->month[11]);
  226.  
  227.         _free_crt (lc_time->ampm[0]);
  228.         _free_crt (lc_time->ampm[1]);
  229.  
  230.         _free_crt (lc_time->ww_sdatefmt);
  231.         _free_crt (lc_time->ww_ldatefmt);
  232.         _free_crt (lc_time->ww_timefmt);
  233. /* Don't need to make these pointers NULL */
  234. }
  235.