xref: /illumos-gate/usr/src/cmd/localedef/numeric.c (revision 75eba5b6d79ed4d2ce3daf7b2806306b6b69a938)
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 2010 Nexenta Systems, Inc.  All rights reserved.
14  */
15 
16 /*
17  * LC_NUMERIC database generation routines for localedef.
18  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <sys/types.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include "localedef.h"
27 #include "parser.tab.h"
28 #include "lnumeric.h"
29 
30 static struct lc_numeric_T numeric;
31 
32 void
33 init_numeric(void)
34 {
35 	(void) memset(&numeric, 0, sizeof (numeric));
36 }
37 
38 void
39 add_numeric_str(wchar_t *wcs)
40 {
41 	char *str;
42 
43 	if ((str = to_mb_string(wcs)) == NULL) {
44 		INTERR;
45 		return;
46 	}
47 	free(wcs);
48 
49 	switch (last_kw) {
50 	case T_DECIMAL_POINT:
51 		numeric.decimal_point = str;
52 		break;
53 	case T_THOUSANDS_SEP:
54 		numeric.thousands_sep = str;
55 		break;
56 	default:
57 		free(str);
58 		INTERR;
59 		break;
60 	}
61 }
62 
63 void
64 reset_numeric_group(void)
65 {
66 	free((char *)numeric.grouping);
67 	numeric.grouping = NULL;
68 }
69 
70 void
71 add_numeric_group(int n)
72 {
73 	char *s;
74 
75 	if (numeric.grouping == NULL) {
76 		(void) asprintf(&s, "%d", n);
77 	} else {
78 		(void) asprintf(&s, "%s;%d", numeric.grouping, n);
79 	}
80 	if (s == NULL)
81 		errf(_("out of memory"));
82 
83 	free((char *)numeric.grouping);
84 	numeric.grouping = s;
85 }
86 
87 void
88 dump_numeric(void)
89 {
90 	FILE *f;
91 
92 	if ((f = open_category()) == NULL) {
93 		return;
94 	}
95 
96 	if ((putl_category(numeric.decimal_point, f) == EOF) ||
97 	    (putl_category(numeric.thousands_sep, f) == EOF) ||
98 	    (putl_category(numeric.grouping, f) == EOF)) {
99 		return;
100 	}
101 	close_category(f);
102 }
103