Print this page
2964 need POSIX 2008 locale object support
   1 /*
   2  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
   3  * Copyright (c) 2000, 2001 Alexey Zelkin <phantom@FreeBSD.org>
   4  * All rights reserved.
   5  *





   6  * Redistribution and use in source and binary forms, with or without
   7  * modification, are permitted provided that the following conditions
   8  * are met:
   9  * 1. Redistributions of source code must retain the above copyright
  10  *    notice, this list of conditions and the following disclaimer.
  11  * 2. Redistributions in binary form must reproduce the above copyright
  12  *    notice, this list of conditions and the following disclaimer in the
  13  *    documentation and/or other materials provided with the distribution.
  14  *
  15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25  * SUCH DAMAGE.
  26  */
  27 
  28 #include "lint.h"
  29 #include <limits.h>
  30 #include <stddef.h>
  31 #include <stdlib.h>

  32 #include "ldpart.h"
  33 #include "lmonetary.h"
  34 
  35 extern int __mlocale_changed;
  36 extern const char *__fix_locale_grouping_str(const char *);
  37 
  38 #define LCMONETARY_SIZE_FULL (sizeof (struct lc_monetary_T) / sizeof (char *))
  39 #define LCMONETARY_SIZE_MIN \
  40         (offsetof(struct lc_monetary_T, int_p_cs_precedes) / sizeof (char *))
  41 
  42 static char     empty[] = "";
  43 static char     numempty[] = { CHAR_MAX, '\0' };
  44 
  45 static const struct lc_monetary_T _C_monetary_locale = {
  46         empty,          /* int_curr_symbol */
  47         empty,          /* currency_symbol */
  48         empty,          /* mon_decimal_point */
  49         empty,          /* mon_thousands_sep */
  50         numempty,       /* mon_grouping */
  51         empty,          /* positive_sign */


  53         numempty,       /* int_frac_digits */
  54         numempty,       /* frac_digits */
  55         numempty,       /* p_cs_precedes */
  56         numempty,       /* p_sep_by_space */
  57         numempty,       /* n_cs_precedes */
  58         numempty,       /* n_sep_by_space */
  59         numempty,       /* p_sign_posn */
  60         numempty,       /* n_sign_posn */
  61         numempty,       /* int_p_cs_precedes */
  62         numempty,       /* int_n_cs_precedes */
  63         numempty,       /* int_p_sep_by_space */
  64         numempty,       /* int_n_sep_by_space */
  65         numempty,       /* int_p_sign_posn */
  66         numempty        /* int_n_sign_posn */
  67 };
  68 
  69 static struct lc_monetary_T _monetary_locale;
  70 static int      _monetary_using_locale;
  71 static char     *_monetary_locale_buf;
  72 


  73 static char
  74 cnv(const char *str)
  75 {
  76         int i = strtol(str, NULL, 10);
  77 
  78         if (i == -1)
  79                 i = CHAR_MAX;
  80         return ((char)i);
  81 }
  82 
  83 int
  84 __monetary_load_locale(const char *name)












  85 {
  86         int ret;
  87 
  88         ret = __part_load_locale(name, &_monetary_using_locale,
  89             &_monetary_locale_buf, "LC_MONETARY",
  90             LCMONETARY_SIZE_FULL, LCMONETARY_SIZE_MIN,
  91             (const char **)&_monetary_locale);
  92         if (ret != _LDP_ERROR)
  93                 __mlocale_changed = 1;
  94         if (ret == _LDP_LOADED) {
  95                 _monetary_locale.mon_grouping =
  96                     __fix_locale_grouping_str(_monetary_locale.mon_grouping);
  97 
  98 #define M_ASSIGN_CHAR(NAME) \
  99                 (((char *)_monetary_locale.NAME)[0] = \
 100                         cnv(_monetary_locale.NAME))
 101 
 102                 M_ASSIGN_CHAR(int_frac_digits);
 103                 M_ASSIGN_CHAR(frac_digits);
 104                 M_ASSIGN_CHAR(p_cs_precedes);


 113                  * parameters default to the national parameters when
 114                  * reading FreeBSD LC_MONETARY data files.
 115                  */
 116 #define M_ASSIGN_ICHAR(NAME)                                    \
 117                 if (_monetary_locale.int_##NAME == NULL)        \
 118                         _monetary_locale.int_##NAME =           \
 119                             _monetary_locale.NAME;              \
 120                 else                                            \
 121                         M_ASSIGN_CHAR(int_##NAME);
 122 
 123                 M_ASSIGN_ICHAR(p_cs_precedes);
 124                 M_ASSIGN_ICHAR(n_cs_precedes);
 125                 M_ASSIGN_ICHAR(p_sep_by_space);
 126                 M_ASSIGN_ICHAR(n_sep_by_space);
 127                 M_ASSIGN_ICHAR(p_sign_posn);
 128                 M_ASSIGN_ICHAR(n_sign_posn);
 129         }
 130         return (ret);
 131 }
 132 

























 133 struct lc_monetary_T *
 134 __get_current_monetary_locale(void)
 135 {
 136         return (_monetary_using_locale ? &_monetary_locale :
 137             (struct lc_monetary_T *)&_C_monetary_locale);

 138 }
   1 /*
   2  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
   3  * Copyright (c) 2000, 2001 Alexey Zelkin <phantom@FreeBSD.org>
   4  * All rights reserved.
   5  *
   6  * Copyright (c) 2011 The FreeBSD Foundation
   7  * All rights reserved.
   8  * Portions of this software were developed by David Chisnall
   9  * under sponsorship from the FreeBSD Foundation.
  10  *
  11  * Redistribution and use in source and binary forms, with or without
  12  * modification, are permitted provided that the following conditions
  13  * are met:
  14  * 1. Redistributions of source code must retain the above copyright
  15  *    notice, this list of conditions and the following disclaimer.
  16  * 2. Redistributions in binary form must reproduce the above copyright
  17  *    notice, this list of conditions and the following disclaimer in the
  18  *    documentation and/or other materials provided with the distribution.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30  * SUCH DAMAGE.
  31  */
  32 
  33 #include "lint.h"
  34 #include <limits.h>
  35 #include <stddef.h>
  36 #include <stdlib.h>
  37 
  38 #include "ldpart.h"
  39 #include "lmonetary.h"
  40 
  41 extern int __mlocale_changed;
  42 extern const char *__fix_locale_grouping_str(const char *);
  43 
  44 #define LCMONETARY_SIZE_FULL (sizeof (struct lc_monetary_T) / sizeof (char *))
  45 #define LCMONETARY_SIZE_MIN \
  46         (offsetof(struct lc_monetary_T, int_p_cs_precedes) / sizeof (char *))
  47 
  48 static char     empty[] = "";
  49 static char     numempty[] = { CHAR_MAX, '\0' };
  50 
  51 static const struct lc_monetary_T _C_monetary_locale = {
  52         empty,          /* int_curr_symbol */
  53         empty,          /* currency_symbol */
  54         empty,          /* mon_decimal_point */
  55         empty,          /* mon_thousands_sep */
  56         numempty,       /* mon_grouping */
  57         empty,          /* positive_sign */


  59         numempty,       /* int_frac_digits */
  60         numempty,       /* frac_digits */
  61         numempty,       /* p_cs_precedes */
  62         numempty,       /* p_sep_by_space */
  63         numempty,       /* n_cs_precedes */
  64         numempty,       /* n_sep_by_space */
  65         numempty,       /* p_sign_posn */
  66         numempty,       /* n_sign_posn */
  67         numempty,       /* int_p_cs_precedes */
  68         numempty,       /* int_n_cs_precedes */
  69         numempty,       /* int_p_sep_by_space */
  70         numempty,       /* int_n_sep_by_space */
  71         numempty,       /* int_p_sign_posn */
  72         numempty        /* int_n_sign_posn */
  73 };
  74 
  75 static struct lc_monetary_T _monetary_locale;
  76 static int      _monetary_using_locale;
  77 static char     *_monetary_locale_buf;
  78 
  79 struct xlocale_monetary __xlocale_global_monetary;
  80 
  81 static char
  82 cnv(const char *str)
  83 {
  84         int i = strtol(str, NULL, 10);
  85 
  86         if (i == -1)
  87                 i = CHAR_MAX;
  88         return ((char)i);
  89 }
  90 
  91 static void
  92 destruct_monetary(void *v)
  93 {
  94         struct xlocale_monetary *l = v;
  95 
  96         if (l->buffer != NULL)
  97                 free(l->buffer);
  98 
  99         free(l);
 100 }
 101 
 102 static int
 103 monetary_load_locale_l(struct xlocale_monetary *loc, int *using_locale,
 104     int *changed, const char *name)
 105 {
 106         int ret;
 107 
 108         ret = __part_load_locale(name, &_monetary_using_locale,
 109             &_monetary_locale_buf, "LC_MONETARY",
 110             LCMONETARY_SIZE_FULL, LCMONETARY_SIZE_MIN,
 111             (const char **)&_monetary_locale);
 112         if (ret != _LDP_ERROR)
 113                 __mlocale_changed = 1;
 114         if (ret == _LDP_LOADED) {
 115                 _monetary_locale.mon_grouping =
 116                     __fix_locale_grouping_str(_monetary_locale.mon_grouping);
 117 
 118 #define M_ASSIGN_CHAR(NAME) \
 119                 (((char *)_monetary_locale.NAME)[0] = \
 120                         cnv(_monetary_locale.NAME))
 121 
 122                 M_ASSIGN_CHAR(int_frac_digits);
 123                 M_ASSIGN_CHAR(frac_digits);
 124                 M_ASSIGN_CHAR(p_cs_precedes);


 133                  * parameters default to the national parameters when
 134                  * reading FreeBSD LC_MONETARY data files.
 135                  */
 136 #define M_ASSIGN_ICHAR(NAME)                                    \
 137                 if (_monetary_locale.int_##NAME == NULL)        \
 138                         _monetary_locale.int_##NAME =           \
 139                             _monetary_locale.NAME;              \
 140                 else                                            \
 141                         M_ASSIGN_CHAR(int_##NAME);
 142 
 143                 M_ASSIGN_ICHAR(p_cs_precedes);
 144                 M_ASSIGN_ICHAR(n_cs_precedes);
 145                 M_ASSIGN_ICHAR(p_sep_by_space);
 146                 M_ASSIGN_ICHAR(n_sep_by_space);
 147                 M_ASSIGN_ICHAR(p_sign_posn);
 148                 M_ASSIGN_ICHAR(n_sign_posn);
 149         }
 150         return (ret);
 151 }
 152 
 153 int
 154 __monetary_load_locale(const char *name)
 155 {
 156         return (monetary_load_locale_l(&__xlocale_global_monetary,
 157             &__xlocale_global_locale.using_monetary_locale,
 158             &__xlocale_global_locale.monetary_locale_changed, name));
 159 }
 160 
 161 void *
 162 __monetary_load(const char *name, locale_t loc)
 163 {
 164         struct xlocale_monetary *new;
 165 
 166         new = calloc(sizeof(struct xlocale_monetary), 1);
 167         /* XXX */
 168         new->header.header.destructor = destruct_monetary;
 169         if (monetary_load_locale_l(new, &loc->using_monetary_locale,
 170             &loc->monetary_locale_changed, name) == _LDP_ERROR) {
 171                 xlocale_release(new);
 172                 return (NULL);
 173         }
 174 
 175         return (NULL);
 176 }
 177 
 178 struct lc_monetary_T *
 179 __get_current_monetary_locale(locale_t loc)
 180 {
 181         return (loc->using_monetary_locale
 182             ? &((struct xlocale_monetary*)loc->components[XLC_MONETARY])->locale
 183             : (struct lc_monetary_T *)&_C_monetary_locale);
 184 }