xref: /titanic_53/usr/src/cmd/localedef/numeric.c (revision 6b5e5868e7ebf1aff3a5abd7d0c4ef0e5fbf3648)
1*6b5e5868SGarrett D'Amore /*
2*6b5e5868SGarrett D'Amore  * This file and its contents are supplied under the terms of the
3*6b5e5868SGarrett D'Amore  * Common Development and Distribution License ("CDDL"), version 1.0.
4*6b5e5868SGarrett D'Amore  * You may only use this file in accordance with the terms version 1.0
5*6b5e5868SGarrett D'Amore  * of the CDDL.
6*6b5e5868SGarrett D'Amore  *
7*6b5e5868SGarrett D'Amore  * A full copy of the text of the CDDL should have accompanied this
8*6b5e5868SGarrett D'Amore  * source.  A copy of the CDDL is also available via the Internet at
9*6b5e5868SGarrett D'Amore  * http://www.illumos.org/license/CDDL.
10*6b5e5868SGarrett D'Amore  */
11*6b5e5868SGarrett D'Amore 
12*6b5e5868SGarrett D'Amore /*
13*6b5e5868SGarrett D'Amore  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
14*6b5e5868SGarrett D'Amore  */
15*6b5e5868SGarrett D'Amore 
16*6b5e5868SGarrett D'Amore /*
17*6b5e5868SGarrett D'Amore  * LC_NUMERIC database generation routines for localedef.
18*6b5e5868SGarrett D'Amore  */
19*6b5e5868SGarrett D'Amore 
20*6b5e5868SGarrett D'Amore #include <stdio.h>
21*6b5e5868SGarrett D'Amore #include <stdlib.h>
22*6b5e5868SGarrett D'Amore #include <errno.h>
23*6b5e5868SGarrett D'Amore #include <sys/types.h>
24*6b5e5868SGarrett D'Amore #include <string.h>
25*6b5e5868SGarrett D'Amore #include <unistd.h>
26*6b5e5868SGarrett D'Amore #include "localedef.h"
27*6b5e5868SGarrett D'Amore #include "parser.tab.h"
28*6b5e5868SGarrett D'Amore #include "lnumeric.h"
29*6b5e5868SGarrett D'Amore 
30*6b5e5868SGarrett D'Amore static struct lc_numeric_T numeric;
31*6b5e5868SGarrett D'Amore 
32*6b5e5868SGarrett D'Amore void
33*6b5e5868SGarrett D'Amore init_numeric(void)
34*6b5e5868SGarrett D'Amore {
35*6b5e5868SGarrett D'Amore 	(void) memset(&numeric, 0, sizeof (numeric));
36*6b5e5868SGarrett D'Amore }
37*6b5e5868SGarrett D'Amore 
38*6b5e5868SGarrett D'Amore void
39*6b5e5868SGarrett D'Amore add_numeric_str(wchar_t *wcs)
40*6b5e5868SGarrett D'Amore {
41*6b5e5868SGarrett D'Amore 	char *str;
42*6b5e5868SGarrett D'Amore 
43*6b5e5868SGarrett D'Amore 	if ((str = to_mb_string(wcs)) == NULL) {
44*6b5e5868SGarrett D'Amore 		INTERR;
45*6b5e5868SGarrett D'Amore 		return;
46*6b5e5868SGarrett D'Amore 	}
47*6b5e5868SGarrett D'Amore 	free(wcs);
48*6b5e5868SGarrett D'Amore 
49*6b5e5868SGarrett D'Amore 	switch (last_kw) {
50*6b5e5868SGarrett D'Amore 	case T_DECIMAL_POINT:
51*6b5e5868SGarrett D'Amore 		numeric.decimal_point = str;
52*6b5e5868SGarrett D'Amore 		break;
53*6b5e5868SGarrett D'Amore 	case T_THOUSANDS_SEP:
54*6b5e5868SGarrett D'Amore 		numeric.thousands_sep = str;
55*6b5e5868SGarrett D'Amore 		break;
56*6b5e5868SGarrett D'Amore 	default:
57*6b5e5868SGarrett D'Amore 		free(str);
58*6b5e5868SGarrett D'Amore 		INTERR;
59*6b5e5868SGarrett D'Amore 		break;
60*6b5e5868SGarrett D'Amore 	}
61*6b5e5868SGarrett D'Amore }
62*6b5e5868SGarrett D'Amore 
63*6b5e5868SGarrett D'Amore void
64*6b5e5868SGarrett D'Amore reset_numeric_group(void)
65*6b5e5868SGarrett D'Amore {
66*6b5e5868SGarrett D'Amore 	free((char *)numeric.grouping);
67*6b5e5868SGarrett D'Amore 	numeric.grouping = NULL;
68*6b5e5868SGarrett D'Amore }
69*6b5e5868SGarrett D'Amore 
70*6b5e5868SGarrett D'Amore void
71*6b5e5868SGarrett D'Amore add_numeric_group(int n)
72*6b5e5868SGarrett D'Amore {
73*6b5e5868SGarrett D'Amore 	char *s;
74*6b5e5868SGarrett D'Amore 
75*6b5e5868SGarrett D'Amore 	if (numeric.grouping == NULL) {
76*6b5e5868SGarrett D'Amore 		(void) asprintf(&s, "%d", n);
77*6b5e5868SGarrett D'Amore 	} else {
78*6b5e5868SGarrett D'Amore 		(void) asprintf(&s, "%s;%d", numeric.grouping, n);
79*6b5e5868SGarrett D'Amore 	}
80*6b5e5868SGarrett D'Amore 	if (s == NULL)
81*6b5e5868SGarrett D'Amore 		errf(_("out of memory"));
82*6b5e5868SGarrett D'Amore 
83*6b5e5868SGarrett D'Amore 	free((char *)numeric.grouping);
84*6b5e5868SGarrett D'Amore 	numeric.grouping = s;
85*6b5e5868SGarrett D'Amore }
86*6b5e5868SGarrett D'Amore 
87*6b5e5868SGarrett D'Amore void
88*6b5e5868SGarrett D'Amore dump_numeric(void)
89*6b5e5868SGarrett D'Amore {
90*6b5e5868SGarrett D'Amore 	FILE *f;
91*6b5e5868SGarrett D'Amore 
92*6b5e5868SGarrett D'Amore 	if ((f = open_category()) == NULL) {
93*6b5e5868SGarrett D'Amore 		return;
94*6b5e5868SGarrett D'Amore 	}
95*6b5e5868SGarrett D'Amore 
96*6b5e5868SGarrett D'Amore 	if ((putl_category(numeric.decimal_point, f) == EOF) ||
97*6b5e5868SGarrett D'Amore 	    (putl_category(numeric.thousands_sep, f) == EOF) ||
98*6b5e5868SGarrett D'Amore 	    (putl_category(numeric.grouping, f) == EOF)) {
99*6b5e5868SGarrett D'Amore 		return;
100*6b5e5868SGarrett D'Amore 	}
101*6b5e5868SGarrett D'Amore 	close_category(f);
102*6b5e5868SGarrett D'Amore }
103