Implement GNU extension strptime_l

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
This commit is contained in:
Corinna Vinschen
2016-08-23 17:42:47 +02:00
parent e636fe3d48
commit 0ecb846d2b
4 changed files with 94 additions and 66 deletions

View File

@ -102,7 +102,7 @@ free_era_info (era_info_t *era_info)
}
static era_info_t *
get_era_info (const char *era)
get_era_info (const char *era, locale_t locale)
{
char *c;
era_info_t *ei = NULL;
@ -122,14 +122,14 @@ get_era_info (const char *era)
ei[cur].num = 1;
ei[cur].dir = (*era == '+') ? 1 : -1;
era += 2;
ei[cur].offset = strtol (era, &c, 10);
ei[cur].offset = strtol_l (era, &c, 10, locale);
era = c + 1;
ei[cur].start.tm_year = strtol (era, &c, 10);
ei[cur].start.tm_year = strtol_l (era, &c, 10, locale);
/* Adjust offset for negative gregorian dates. */
if (ei[cur].start.tm_year < 0)
++ei[cur].start.tm_year;
ei[cur].start.tm_mon = strtol (c + 1, &c, 10);
ei[cur].start.tm_mday = strtol (c + 1, &c, 10);
ei[cur].start.tm_mon = strtol_l (c + 1, &c, 10, locale);
ei[cur].start.tm_mday = strtol_l (c + 1, &c, 10, locale);
ei[cur].start.tm_hour = ei[cur].start.tm_min = ei[cur].start.tm_sec = 0;
era = c + 1;
if (era[0] == '-' && era[1] == '*')
@ -151,12 +151,12 @@ get_era_info (const char *era)
}
else
{
ei[cur].end.tm_year = strtol (era, &c, 10);
ei[cur].end.tm_year = strtol_l (era, &c, 10, locale);
/* Adjust offset for negative gregorian dates. */
if (ei[cur].end.tm_year < 0)
++ei[cur].end.tm_year;
ei[cur].end.tm_mon = strtol (c + 1, &c, 10);
ei[cur].end.tm_mday = strtol (c + 1, &c, 10);
ei[cur].end.tm_mon = strtol_l (c + 1, &c, 10, locale);
ei[cur].end.tm_mday = strtol_l (c + 1, &c, 10, locale);
ei[cur].end.tm_mday = 31;
ei[cur].end.tm_hour = 23;
ei[cur].end.tm_min = ei[cur].end.tm_sec = 59;
@ -305,11 +305,13 @@ static const unsigned char *conv_num(const unsigned char *, int *, uint, uint,
alt_digits_t *);
static const unsigned char *find_string(const unsigned char *, int *,
const char * const *,
const char * const *, int);
const char * const *, int,
locale_t);
static char *
__strptime(const char *buf, const char *fmt, struct tm *tm,
era_info_t **era_info, alt_digits_t **alt_digits)
era_info_t **era_info, alt_digits_t **alt_digits,
locale_t locale)
{
unsigned char c;
const unsigned char *bp;
@ -323,8 +325,7 @@ __strptime(const char *buf, const char *fmt, struct tm *tm,
int ymd = 0;
bp = (const unsigned char *)buf;
const struct lc_time_T *_CurrentTimeLocale =
__get_current_time_locale ();
const struct lc_time_T *_CurrentTimeLocale = __get_time_locale (locale);
while (bp != NULL && (c = *fmt++) != '\0') {
/* Clear `alternate' modifier prior to new conversion. */
@ -334,8 +335,8 @@ __strptime(const char *buf, const char *fmt, struct tm *tm,
i = 0;
/* Eat up white-space. */
if (isspace(c)) {
while (isspace(*bp))
if (isspace_l(c, locale)) {
while (isspace_l(*bp, locale))
bp++;
continue;
}
@ -360,7 +361,8 @@ literal:
LEGAL_ALT(0);
alt_format |= ALT_E;
if (!*era_info && *_CurrentTimeLocale->era)
*era_info = get_era_info (_CurrentTimeLocale->era);
*era_info = get_era_info (_CurrentTimeLocale->era,
locale);
goto again;
case 'O': /* "%O?" alternative conversion modifier. */
@ -385,7 +387,7 @@ literal:
LEGAL_ALT(0);
{
char *end;
width = strtoul (fmt - 1, &end, 10);
width = strtoul_l (fmt - 1, &end, 10, locale);
fmt = (const char *) end;
goto again;
}
@ -410,7 +412,8 @@ literal:
LEGAL_ALT(0);
ymd |= SET_YMD;
char *tmp = __strptime ((const char *) bp, "%Y-%m-%d",
tm, era_info, alt_digits);
tm, era_info, alt_digits,
locale);
if (tmp && (uint) (tmp - (char *) bp) > width)
return NULL;
bp = (const unsigned char *) tmp;
@ -446,7 +449,7 @@ literal:
recurse:
bp = (const unsigned char *)
__strptime((const char *)bp, new_fmt, tm,
era_info, alt_digits);
era_info, alt_digits, locale);
continue;
/*
@ -455,7 +458,7 @@ literal:
case 'A': /* The day of week, using the locale's form. */
case 'a':
bp = find_string(bp, &tm->tm_wday, _ctloc(weekday),
_ctloc(wday), 7);
_ctloc(wday), 7, locale);
LEGAL_ALT(0);
ymd |= SET_WDAY;
continue;
@ -464,7 +467,7 @@ literal:
case 'b':
case 'h':
bp = find_string(bp, &tm->tm_mon, _ctloc(month),
_ctloc(mon), 12);
_ctloc(mon), 12, locale);
LEGAL_ALT(0);
ymd |= SET_WDAY;
continue;
@ -554,7 +557,8 @@ literal:
continue;
case 'p': /* The locale's equivalent of AM/PM. */
bp = find_string(bp, &i, _ctloc(am_pm), NULL, 2);
bp = find_string(bp, &i, _ctloc(am_pm), NULL, 2,
locale);
if (tm->tm_hour > 11)
return NULL;
tm->tm_hour += i * 12;
@ -604,7 +608,7 @@ literal:
char *tmp = __strptime ((const char *) bp,
tmp_ei->era_Y,
tm, &tmp_ei,
alt_digits);
alt_digits, locale);
if (tmp)
{
bp = (const unsigned char *) tmp;
@ -675,7 +679,7 @@ literal:
ep = find_string(bp, &i,
(const char * const *)tzname,
NULL, 2);
NULL, 2, locale);
if (ep != NULL) {
tm->tm_isdst = i;
#ifdef TM_GMTOFF
@ -696,7 +700,7 @@ literal:
*/
case 'n': /* Any kind of white-space. */
case 't':
while (isspace(*bp))
while (isspace_l(*bp, locale))
bp++;
LEGAL_ALT(0);
continue;
@ -775,13 +779,28 @@ literal:
return (char *) bp;
}
char *
strptime_l (const char *__restrict buf, const char *__restrict fmt,
struct tm *__restrict tm, locale_t locale)
{
era_info_t *era_info = NULL;
alt_digits_t *alt_digits = NULL;
char *ret = __strptime (buf, fmt, tm, &era_info, &alt_digits, locale);
if (era_info)
free_era_info (era_info);
if (alt_digits)
free_alt_digits (alt_digits);
return ret;
}
char *
strptime (const char *__restrict buf, const char *__restrict fmt,
struct tm *__restrict tm)
{
era_info_t *era_info = NULL;
alt_digits_t *alt_digits = NULL;
char *ret = __strptime (buf, fmt, tm, &era_info, &alt_digits);
char *ret = __strptime (buf, fmt, tm, &era_info, &alt_digits,
__get_current_locale ());
if (era_info)
free_era_info (era_info);
if (alt_digits)
@ -824,7 +843,7 @@ conv_num(const unsigned char *buf, int *dest, uint llim, uint ulim,
static const unsigned char *
find_string(const unsigned char *bp, int *tgt, const char * const *n1,
const char * const *n2, int c)
const char * const *n2, int c, locale_t locale)
{
int i;
unsigned int len;
@ -833,7 +852,8 @@ find_string(const unsigned char *bp, int *tgt, const char * const *n1,
for (; n1 != NULL; n1 = n2, n2 = NULL) {
for (i = 0; i < c; i++, n1++) {
len = strlen(*n1);
if (strncasecmp(*n1, (const char *)bp, len) == 0) {
if (strncasecmp_l(*n1, (const char *)bp, len,
locale) == 0) {
*tgt = i;
return bp + len;
}