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