]> git.ipfire.org Git - thirdparty/glibc.git/blame - time/alt_digit.c
Replace FSF snail mail address with URLs.
[thirdparty/glibc.git] / time / alt_digit.c
CommitLineData
df9f41c9 1/* Helper functions used by strftime/strptime to handle alternate digits.
f095bb72 2 Copyright (C) 1995-2002, 2008, 2010 Free Software Foundation, Inc.
df9f41c9
RM
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
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
df9f41c9
RM
18
19#include "../locale/localeinfo.h"
20#include <bits/libc-lock.h>
21#include <stdlib.h>
22#include <wchar.h>
23#include <string.h>
24
25/* Some of the functions here must not be used while setlocale is called. */
91e32540 26__libc_rwlock_define (extern, __libc_setlocale_lock attribute_hidden)
df9f41c9
RM
27
28#define CURRENT(item) (current->values[_NL_ITEM_INDEX (item)].string)
29#define CURRENT_WSTR(item) \
30 ((wchar_t *) current->values[_NL_ITEM_INDEX (item)].wstr)
31
32static void
f095bb72 33_nl_init_alt_digit (struct __locale_data *current)
df9f41c9
RM
34{
35 struct lc_time_data *data;
36
37 if (current->private.time == NULL)
38 {
39 current->private.time = malloc (sizeof *current->private.time);
40 if (current->private.time == NULL)
41 return;
42 memset (current->private.time, 0, sizeof *current->private.time);
43 current->private.cleanup = &_nl_cleanup_time;
44 }
45 data = current->private.time;
46
47 if (! data->alt_digits_initialized)
48 {
49 const char *ptr = CURRENT (ALT_DIGITS);
50 size_t cnt;
51
52 data->alt_digits_initialized = 1;
53
54 if (ptr != NULL)
55 {
56 data->alt_digits = malloc (100 * sizeof (const char *));
57 if (data->alt_digits != NULL)
58 for (cnt = 0; cnt < 100; ++cnt)
59 {
60 data->alt_digits[cnt] = ptr;
61
62 /* Skip digit format. */
63 ptr = strchr (ptr, '\0') + 1;
64 }
65 }
66 }
67
68}
69
70const char *
71internal_function
f095bb72 72_nl_get_alt_digit (unsigned int number, struct __locale_data *current)
df9f41c9
RM
73{
74 const char *result;
75
76 if (number >= 100 || CURRENT (ALT_DIGITS)[0] == '\0')
77 return NULL;
78
91e32540 79 __libc_rwlock_wrlock (__libc_setlocale_lock);
df9f41c9
RM
80
81 if (current->private.time == NULL
82 || ! current->private.time->alt_digits_initialized)
83 _nl_init_alt_digit (current);
84
85 result = ((current->private.time != NULL
86 && current->private.time->alt_digits != NULL)
87 ? current->private.time->alt_digits[number]
88 : NULL);
89
91e32540 90 __libc_rwlock_unlock (__libc_setlocale_lock);
df9f41c9
RM
91
92 return result;
93}
94
95
96const wchar_t *
97internal_function
f095bb72 98_nl_get_walt_digit (unsigned int number, struct __locale_data *current)
df9f41c9
RM
99{
100 const wchar_t *result = NULL;
101 struct lc_time_data *data;
102
103 if (number >= 100 || CURRENT_WSTR (_NL_WALT_DIGITS)[0] == L'\0')
104 return NULL;
105
91e32540 106 __libc_rwlock_wrlock (__libc_setlocale_lock);
df9f41c9
RM
107
108 if (current->private.time == NULL)
109 {
110 current->private.time = malloc (sizeof *current->private.time);
111 if (current->private.time == NULL)
112 goto out;
113 memset (current->private.time, 0, sizeof *current->private.time);
114 current->private.cleanup = &_nl_cleanup_time;
115 }
116 data = current->private.time;
117
118 if (! data->walt_digits_initialized)
119 {
120 const wchar_t *ptr = CURRENT_WSTR (_NL_WALT_DIGITS);
121 size_t cnt;
122
123 data->walt_digits_initialized = 1;
124
125 if (ptr != NULL)
126 {
127 data->walt_digits = malloc (100 * sizeof (const uint32_t *));
128 if (data->walt_digits != NULL)
129 for (cnt = 0; cnt < 100; ++cnt)
130 {
131 data->walt_digits[cnt] = ptr;
132
133 /* Skip digit format. */
134 ptr = wcschr (ptr, L'\0') + 1;
135 }
136 }
137 }
138
139 if (data->walt_digits != NULL)
140 result = data->walt_digits[number];
141
142 out:
91e32540 143 __libc_rwlock_unlock (__libc_setlocale_lock);
df9f41c9
RM
144
145 return (wchar_t *) result;
146}
147
148
149int
150internal_function
f095bb72 151_nl_parse_alt_digit (const char **strp, struct __locale_data *current)
df9f41c9
RM
152{
153 const char *str = *strp;
154 int result = -1;
155 size_t cnt;
156 size_t maxlen = 0;
157
158 if (CURRENT_WSTR (_NL_WALT_DIGITS)[0] == L'\0')
159 return result;
160
91e32540 161 __libc_rwlock_wrlock (__libc_setlocale_lock);
df9f41c9
RM
162
163 if (current->private.time == NULL
164 || ! current->private.time->alt_digits_initialized)
165 _nl_init_alt_digit (current);
166
167 if (current->private.time != NULL &&
168 current->private.time->alt_digits != NULL)
169 /* Matching is not unambiguous. The alternative digits could be like
170 I, II, III, ... and the first one is a substring of the second
171 and third. Therefore we must keep on searching until we found
172 the longest possible match. Note that this is not specified in
173 the standard. */
174 for (cnt = 0; cnt < 100; ++cnt)
175 {
176 const char *const dig = current->private.time->alt_digits[cnt];
177 size_t len = strlen (dig);
178
179 if (len > maxlen && strncmp (dig, str, len) == 0)
180 {
181 maxlen = len;
182 result = (int) cnt;
183 }
184 }
185
91e32540 186 __libc_rwlock_unlock (__libc_setlocale_lock);
df9f41c9
RM
187
188 if (result != -1)
189 *strp += maxlen;
190
191 return result;
192}