era.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Helper functions used by strftime/strptime to handle locale-specific "eras".
  2. Copyright (C) 1995-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "../locale/localeinfo.h"
  16. #include <libc-lock.h>
  17. #include <stdlib.h>
  18. #include <wchar.h>
  19. #include <string.h>
  20. #include <stdint.h>
  21. /* Some of the functions here must not be used while setlocale is called. */
  22. __libc_rwlock_define (extern, __libc_setlocale_lock attribute_hidden)
  23. #define CURRENT(item) (current->values[_NL_ITEM_INDEX (item)].string)
  24. #define CURRENT_WORD(item) (current->values[_NL_ITEM_INDEX (item)].word)
  25. #define ERA_DATE_CMP(a, b) \
  26. (a[0] < b[0] || (a[0] == b[0] && (a[1] < b[1] \
  27. || (a[1] == b[1] && a[2] <= b[2]))))
  28. /* Look up the era information in CURRENT's locale strings and
  29. cache it in CURRENT->private. */
  30. static void
  31. _nl_init_era_entries (struct __locale_data *current)
  32. {
  33. size_t cnt;
  34. struct lc_time_data *data;
  35. /* Avoid touching CURRENT if there is no data at all, for _nl_C_LC_TIME. */
  36. if (CURRENT_WORD (_NL_TIME_ERA_NUM_ENTRIES) == 0)
  37. return;
  38. __libc_rwlock_wrlock (__libc_setlocale_lock);
  39. if (current->private.time == NULL)
  40. {
  41. current->private.time = malloc (sizeof *current->private.time);
  42. if (current->private.time == NULL)
  43. goto out;
  44. memset (current->private.time, 0, sizeof *current->private.time);
  45. current->private.cleanup = &_nl_cleanup_time;
  46. }
  47. data = current->private.time;
  48. if (! data->era_initialized)
  49. {
  50. size_t new_num_eras = CURRENT_WORD (_NL_TIME_ERA_NUM_ENTRIES);
  51. if (new_num_eras == 0)
  52. {
  53. if (data->eras != NULL)
  54. {
  55. free (data->eras);
  56. data->eras = NULL;
  57. }
  58. }
  59. else
  60. {
  61. struct era_entry *new_eras = data->eras;
  62. if (data->num_eras != new_num_eras)
  63. new_eras =
  64. (struct era_entry *) realloc (data->eras,
  65. new_num_eras
  66. * sizeof (struct era_entry));
  67. if (new_eras == NULL)
  68. {
  69. free (data->eras);
  70. data->num_eras = 0;
  71. data->eras = NULL;
  72. }
  73. else
  74. {
  75. const char *ptr = CURRENT (_NL_TIME_ERA_ENTRIES);
  76. data->num_eras = new_num_eras;
  77. data->eras = new_eras;
  78. for (cnt = 0; cnt < new_num_eras; ++cnt)
  79. {
  80. const char *base_ptr = ptr;
  81. memcpy ((void *) (new_eras + cnt), (const void *) ptr,
  82. sizeof (uint32_t) * 8);
  83. if (ERA_DATE_CMP(new_eras[cnt].start_date,
  84. new_eras[cnt].stop_date))
  85. if (new_eras[cnt].direction == (uint32_t) '+')
  86. new_eras[cnt].absolute_direction = 1;
  87. else
  88. new_eras[cnt].absolute_direction = -1;
  89. else
  90. if (new_eras[cnt].direction == (uint32_t) '+')
  91. new_eras[cnt].absolute_direction = -1;
  92. else
  93. new_eras[cnt].absolute_direction = 1;
  94. /* Skip numeric values. */
  95. ptr += sizeof (uint32_t) * 8;
  96. /* Set and skip era name. */
  97. new_eras[cnt].era_name = ptr;
  98. ptr = strchr (ptr, '\0') + 1;
  99. /* Set and skip era format. */
  100. new_eras[cnt].era_format = ptr;
  101. ptr = strchr (ptr, '\0') + 1;
  102. ptr += 3 - (((ptr - (const char *) base_ptr) + 3) & 3);
  103. /* Set and skip wide era name. */
  104. new_eras[cnt].era_wname = (wchar_t *) ptr;
  105. ptr = (char *) (__wcschr ((wchar_t *) ptr, L'\0') + 1);
  106. /* Set and skip wide era format. */
  107. new_eras[cnt].era_wformat = (wchar_t *) ptr;
  108. ptr = (char *) (__wcschr ((wchar_t *) ptr, L'\0') + 1);
  109. }
  110. }
  111. }
  112. data->era_initialized = 1;
  113. }
  114. out:
  115. __libc_rwlock_unlock (__libc_setlocale_lock);
  116. }
  117. struct era_entry *
  118. _nl_get_era_entry (const struct tm *tp, struct __locale_data *current)
  119. {
  120. if (current->private.time == NULL || !current->private.time->era_initialized)
  121. _nl_init_era_entries (current);
  122. if (current->private.time != NULL)
  123. {
  124. /* Now compare date with the available eras. */
  125. const int32_t tdate[3] = { tp->tm_year, tp->tm_mon, tp->tm_mday };
  126. size_t cnt;
  127. for (cnt = 0; cnt < current->private.time->num_eras; ++cnt)
  128. if ((ERA_DATE_CMP (current->private.time->eras[cnt].start_date, tdate)
  129. && ERA_DATE_CMP (tdate,
  130. current->private.time->eras[cnt].stop_date))
  131. || (ERA_DATE_CMP (current->private.time->eras[cnt].stop_date,
  132. tdate)
  133. && ERA_DATE_CMP (tdate,
  134. current->private.time->eras[cnt].start_date)))
  135. return &current->private.time->eras[cnt];
  136. }
  137. return NULL;
  138. }
  139. struct era_entry *
  140. _nl_select_era_entry (int cnt, struct __locale_data *current)
  141. {
  142. if (current->private.time == NULL || !current->private.time->era_initialized)
  143. _nl_init_era_entries (current);
  144. return (current->private.time == NULL
  145. ? NULL : &current->private.time->eras[cnt]);
  146. }