Blame time/era.c

Packit 6c4009
/* Helper functions used by strftime/strptime to handle locale-specific "eras".
Packit 6c4009
   Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include "../locale/localeinfo.h"
Packit 6c4009
#include <libc-lock.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <wchar.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
Packit 6c4009
/* Some of the functions here must not be used while setlocale is called.  */
Packit 6c4009
__libc_rwlock_define (extern, __libc_setlocale_lock attribute_hidden)
Packit 6c4009
Packit 6c4009
#define CURRENT(item)		(current->values[_NL_ITEM_INDEX (item)].string)
Packit 6c4009
#define CURRENT_WORD(item)	(current->values[_NL_ITEM_INDEX (item)].word)
Packit 6c4009
Packit 6c4009
#define ERA_DATE_CMP(a, b) \
Packit 6c4009
  (a[0] < b[0] || (a[0] == b[0] && (a[1] < b[1]				      \
Packit 6c4009
				    || (a[1] == b[1] && a[2] <= b[2]))))
Packit 6c4009
Packit 6c4009
/* Look up the era information in CURRENT's locale strings and
Packit 6c4009
   cache it in CURRENT->private.  */
Packit 6c4009
static void
Packit 6c4009
_nl_init_era_entries (struct __locale_data *current)
Packit 6c4009
{
Packit 6c4009
  size_t cnt;
Packit 6c4009
  struct lc_time_data *data;
Packit 6c4009
Packit 6c4009
  /* Avoid touching CURRENT if there is no data at all, for _nl_C_LC_TIME.  */
Packit 6c4009
  if (CURRENT_WORD (_NL_TIME_ERA_NUM_ENTRIES) == 0)
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  __libc_rwlock_wrlock (__libc_setlocale_lock);
Packit 6c4009
Packit 6c4009
  if (current->private.time == NULL)
Packit 6c4009
    {
Packit 6c4009
      current->private.time = malloc (sizeof *current->private.time);
Packit 6c4009
      if (current->private.time == NULL)
Packit 6c4009
	goto out;
Packit 6c4009
      memset (current->private.time, 0, sizeof *current->private.time);
Packit 6c4009
      current->private.cleanup = &_nl_cleanup_time;
Packit 6c4009
    }
Packit 6c4009
  data = current->private.time;
Packit 6c4009
Packit 6c4009
  if (! data->era_initialized)
Packit 6c4009
    {
Packit 6c4009
      size_t new_num_eras = CURRENT_WORD (_NL_TIME_ERA_NUM_ENTRIES);
Packit 6c4009
      if (new_num_eras == 0)
Packit 6c4009
	{
Packit 6c4009
	  if (data->eras != NULL)
Packit 6c4009
	    {
Packit 6c4009
	      free (data->eras);
Packit 6c4009
	      data->eras = NULL;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  struct era_entry *new_eras = data->eras;
Packit 6c4009
Packit 6c4009
	  if (data->num_eras != new_num_eras)
Packit 6c4009
	    new_eras =
Packit 6c4009
	      (struct era_entry *) realloc (data->eras,
Packit 6c4009
					    new_num_eras
Packit 6c4009
					    * sizeof (struct era_entry));
Packit 6c4009
	  if (new_eras == NULL)
Packit 6c4009
	    {
Packit 6c4009
	      free (data->eras);
Packit 6c4009
	      data->num_eras = 0;
Packit 6c4009
	      data->eras = NULL;
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      const char *ptr = CURRENT (_NL_TIME_ERA_ENTRIES);
Packit 6c4009
	      data->num_eras = new_num_eras;
Packit 6c4009
	      data->eras = new_eras;
Packit 6c4009
Packit 6c4009
	      for (cnt = 0; cnt < new_num_eras; ++cnt)
Packit 6c4009
		{
Packit 6c4009
		  const char *base_ptr = ptr;
Packit 6c4009
		  memcpy ((void *) (new_eras + cnt), (const void *) ptr,
Packit 6c4009
			  sizeof (uint32_t) * 8);
Packit 6c4009
Packit 6c4009
		  if (ERA_DATE_CMP(new_eras[cnt].start_date,
Packit 6c4009
				   new_eras[cnt].stop_date))
Packit 6c4009
		    if (new_eras[cnt].direction == (uint32_t) '+')
Packit 6c4009
		      new_eras[cnt].absolute_direction = 1;
Packit 6c4009
		    else
Packit 6c4009
		      new_eras[cnt].absolute_direction = -1;
Packit 6c4009
		  else
Packit 6c4009
		    if (new_eras[cnt].direction == (uint32_t) '+')
Packit 6c4009
		      new_eras[cnt].absolute_direction = -1;
Packit 6c4009
		    else
Packit 6c4009
		      new_eras[cnt].absolute_direction = 1;
Packit 6c4009
Packit 6c4009
		  /* Skip numeric values.  */
Packit 6c4009
		  ptr += sizeof (uint32_t) * 8;
Packit 6c4009
Packit 6c4009
		  /* Set and skip era name.  */
Packit 6c4009
		  new_eras[cnt].era_name = ptr;
Packit 6c4009
		  ptr = strchr (ptr, '\0') + 1;
Packit 6c4009
Packit 6c4009
		  /* Set and skip era format.  */
Packit 6c4009
		  new_eras[cnt].era_format = ptr;
Packit 6c4009
		  ptr = strchr (ptr, '\0') + 1;
Packit 6c4009
Packit 6c4009
		  ptr += 3 - (((ptr - (const char *) base_ptr) + 3) & 3);
Packit 6c4009
Packit 6c4009
		  /* Set and skip wide era name.  */
Packit 6c4009
		  new_eras[cnt].era_wname = (wchar_t *) ptr;
Packit 6c4009
		  ptr = (char *) (__wcschr ((wchar_t *) ptr, L'\0') + 1);
Packit 6c4009
Packit 6c4009
		  /* Set and skip wide era format.  */
Packit 6c4009
		  new_eras[cnt].era_wformat = (wchar_t *) ptr;
Packit 6c4009
		  ptr = (char *) (__wcschr ((wchar_t *) ptr, L'\0') + 1);
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      data->era_initialized = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
 out:
Packit 6c4009
  __libc_rwlock_unlock (__libc_setlocale_lock);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
struct era_entry *
Packit 6c4009
_nl_get_era_entry (const struct tm *tp, struct __locale_data *current)
Packit 6c4009
{
Packit 6c4009
  if (current->private.time == NULL || !current->private.time->era_initialized)
Packit 6c4009
    _nl_init_era_entries (current);
Packit 6c4009
Packit 6c4009
  if (current->private.time != NULL)
Packit 6c4009
    {
Packit 6c4009
      /* Now compare date with the available eras.  */
Packit 6c4009
      const int32_t tdate[3] = { tp->tm_year, tp->tm_mon, tp->tm_mday };
Packit 6c4009
      size_t cnt;
Packit 6c4009
      for (cnt = 0; cnt < current->private.time->num_eras; ++cnt)
Packit 6c4009
	if ((ERA_DATE_CMP (current->private.time->eras[cnt].start_date, tdate)
Packit 6c4009
	     && ERA_DATE_CMP (tdate,
Packit 6c4009
			      current->private.time->eras[cnt].stop_date))
Packit 6c4009
	    || (ERA_DATE_CMP (current->private.time->eras[cnt].stop_date,
Packit 6c4009
			      tdate)
Packit 6c4009
		&& ERA_DATE_CMP (tdate,
Packit 6c4009
				 current->private.time->eras[cnt].start_date)))
Packit 6c4009
	  return &current->private.time->eras[cnt];
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
struct era_entry *
Packit 6c4009
_nl_select_era_entry (int cnt, struct __locale_data *current)
Packit 6c4009
{
Packit 6c4009
  if (current->private.time == NULL || !current->private.time->era_initialized)
Packit 6c4009
    _nl_init_era_entries (current);
Packit 6c4009
Packit 6c4009
  return (current->private.time == NULL
Packit 6c4009
	  ? NULL : &current->private.time->eras[cnt]);
Packit 6c4009
}