Back to index

glibc  2.9
era.c
Go to the documentation of this file.
00001 /* Helper functions used by strftime/strptime to handle locale-specific "eras".
00002    Copyright (C) 1995-2002, 2008 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include "../locale/localeinfo.h"
00021 #include <bits/libc-lock.h>
00022 #include <stdlib.h>
00023 #include <wchar.h>
00024 #include <string.h>
00025 
00026 /* Some of the functions here must not be used while setlocale is called.  */
00027 __libc_rwlock_define (extern, __libc_setlocale_lock attribute_hidden)
00028 
00029 #define CURRENT(item)              (current->values[_NL_ITEM_INDEX (item)].string)
00030 #define CURRENT_WORD(item)  (current->values[_NL_ITEM_INDEX (item)].word)
00031 
00032 #define ERA_DATE_CMP(a, b) \
00033   (a[0] < b[0] || (a[0] == b[0] && (a[1] < b[1]                             \
00034                                 || (a[1] == b[1] && a[2] <= b[2]))))
00035 
00036 /* Look up the era information in CURRENT's locale strings and
00037    cache it in CURRENT->private.  */
00038 static void internal_function
00039 _nl_init_era_entries (struct locale_data *current)
00040 {
00041   size_t cnt;
00042   struct lc_time_data *data;
00043 
00044   /* Avoid touching CURRENT if there is no data at all, for _nl_C_LC_TIME.  */
00045   if (CURRENT_WORD (_NL_TIME_ERA_NUM_ENTRIES) == 0)
00046     return;
00047 
00048   __libc_rwlock_wrlock (__libc_setlocale_lock);
00049 
00050   if (current->private.time == NULL)
00051     {
00052       current->private.time = malloc (sizeof *current->private.time);
00053       if (current->private.time == NULL)
00054        goto out;
00055       memset (current->private.time, 0, sizeof *current->private.time);
00056       current->private.cleanup = &_nl_cleanup_time;
00057     }
00058   data = current->private.time;
00059 
00060   if (! data->era_initialized)
00061     {
00062       size_t new_num_eras = CURRENT_WORD (_NL_TIME_ERA_NUM_ENTRIES);
00063       if (new_num_eras == 0)
00064        {
00065          if (data->eras != NULL)
00066            {
00067              free (data->eras);
00068              data->eras = NULL;
00069            }
00070        }
00071       else
00072        {
00073          struct era_entry *new_eras = data->eras;
00074 
00075          if (data->num_eras != new_num_eras)
00076            new_eras =
00077              (struct era_entry *) realloc (data->eras,
00078                                        new_num_eras
00079                                        * sizeof (struct era_entry));
00080          if (new_eras == NULL)
00081            {
00082              free (data->eras);
00083              data->num_eras = 0;
00084              data->eras = NULL;
00085            }
00086           else
00087            {
00088              const char *ptr = CURRENT (_NL_TIME_ERA_ENTRIES);
00089              data->num_eras = new_num_eras;
00090              data->eras = new_eras;
00091 
00092              for (cnt = 0; cnt < new_num_eras; ++cnt)
00093               {
00094                 const char *base_ptr = ptr;
00095                 memcpy ((void *) (new_eras + cnt), (const void *) ptr,
00096                        sizeof (uint32_t) * 8);
00097 
00098                 if (ERA_DATE_CMP(new_eras[cnt].start_date,
00099                                new_eras[cnt].stop_date))
00100                   if (new_eras[cnt].direction == (uint32_t) '+')
00101                     new_eras[cnt].absolute_direction = 1;
00102                   else
00103                     new_eras[cnt].absolute_direction = -1;
00104                 else
00105                   if (new_eras[cnt].direction == (uint32_t) '+')
00106                     new_eras[cnt].absolute_direction = -1;
00107                   else
00108                     new_eras[cnt].absolute_direction = 1;
00109 
00110                 /* Skip numeric values.  */
00111                 ptr += sizeof (uint32_t) * 8;
00112 
00113                 /* Set and skip era name.  */
00114                 new_eras[cnt].era_name = ptr;
00115                 ptr = strchr (ptr, '\0') + 1;
00116 
00117                 /* Set and skip era format.  */
00118                 new_eras[cnt].era_format = ptr;
00119                 ptr = strchr (ptr, '\0') + 1;
00120 
00121                 ptr += 3 - (((ptr - (const char *) base_ptr) + 3) & 3);
00122 
00123                 /* Set and skip wide era name.  */
00124                 new_eras[cnt].era_wname = (wchar_t *) ptr;
00125                 ptr = (char *) (wcschr ((wchar_t *) ptr, L'\0') + 1);
00126 
00127                 /* Set and skip wide era format.  */
00128                 new_eras[cnt].era_wformat = (wchar_t *) ptr;
00129                 ptr = (char *) (wcschr ((wchar_t *) ptr, L'\0') + 1);
00130               }
00131            }
00132        }
00133 
00134       data->era_initialized = 1;
00135     }
00136 
00137  out:
00138   __libc_rwlock_unlock (__libc_setlocale_lock);
00139 }
00140 
00141 struct era_entry *
00142 internal_function
00143 _nl_get_era_entry (const struct tm *tp, struct locale_data *current)
00144 {
00145   if (current->private.time == NULL || !current->private.time->era_initialized)
00146     _nl_init_era_entries (current);
00147 
00148   if (current->private.time != NULL)
00149     {
00150       /* Now compare date with the available eras.  */
00151       const int32_t tdate[3] = { tp->tm_year, tp->tm_mon, tp->tm_mday };
00152       size_t cnt;
00153       for (cnt = 0; cnt < current->private.time->num_eras; ++cnt)
00154        if ((ERA_DATE_CMP (current->private.time->eras[cnt].start_date, tdate)
00155             && ERA_DATE_CMP (tdate,
00156                            current->private.time->eras[cnt].stop_date))
00157            || (ERA_DATE_CMP (current->private.time->eras[cnt].stop_date,
00158                            tdate)
00159               && ERA_DATE_CMP (tdate,
00160                              current->private.time->eras[cnt].start_date)))
00161          return &current->private.time->eras[cnt];
00162     }
00163 
00164   return NULL;
00165 }
00166 
00167 
00168 struct era_entry *
00169 internal_function
00170 _nl_select_era_entry (int cnt, struct locale_data *current)
00171 {
00172   if (current->private.time == NULL || !current->private.time->era_initialized)
00173     _nl_init_era_entries (current);
00174 
00175   return (current->private.time == NULL
00176          ? NULL : &current->private.time->eras[cnt]);
00177 }