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