summaryrefslogtreecommitdiffstats
path: root/private/crt32/misc/initnum.c
blob: dea832aad787211b6257177f68ffd36d953f91af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/***
*initnum.c - contains _init_numeric
*
*	Copyright (c) 1991-1993, Microsoft Corporation. All rights reserved.
*
*Purpose:
*	Contains the locale-category initialization function: _init_numeric().
*	
*	Each initialization function sets up locale-specific information
*	for their category, for use by functions which are affected by
*	their locale category.
*
*	*** For internal use by setlocale() only ***
*
*Revision History:
*	12-08-91  ETC	Created.
*	12-20-91  ETC	Updated to use new NLSAPI GetLocaleInfo.
*	12-18-92  CFW	Ported to Cuda tree, changed _CALLTYPE4 to _CRTAPI3.
*	12-29-92  CFW	Updated to use new _getlocaleinfo wrapper function.
*	01-25-93  KRS	Change interface to _getlocaleinfo again.
*	02-08-93  CFW	Added _lconv_static_*.
*	02-17-93  CFW	Removed debugging print statement.
*	03-17-93  CFW	C locale thousands sep is "", not ",".
*	05-20-93  GJF	Include windows.h, not individual win*.h files
*	06-11-93  CFW	Now inithelp takes void *.
*
*******************************************************************************/

#ifdef _INTL

#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <locale.h>
#include <setlocal.h>
#include <malloc.h>
#include <assert.h>
#include <nlsint.h>

/* Pointer to current lconv */
extern struct lconv *_lconv;

/***
*int _init_numeric() - initialization for LC_NUMERIC locale category.
*
*Purpose:
*
*Entry:
*	None.
*
*Exit:
*	0 success
*	1 fail
*
*Exceptions:
*
*******************************************************************************/

int _CRTAPI3 _init_numeric (
	void
	)
{
   static char *decimal_point = NULL;
   static char *thousands_sep = NULL;
   static char *grouping = NULL;
   int ret = 0;

   /* Numeric data is country--not language--dependent.  NT work-around. */
   LCID ctryid = MAKELCID(_lc_id[LC_NUMERIC].wCountry, SORT_DEFAULT);

   if (_lc_handle[LC_NUMERIC] != _CLOCALEHANDLE)
   {
	   ret |= _getlocaleinfo(LC_STR_TYPE, ctryid, LOCALE_SDECIMAL, (void *)&decimal_point);
	   ret |= _getlocaleinfo(LC_STR_TYPE, ctryid, LOCALE_STHOUSAND, (void *)&thousands_sep);
	   ret |= _getlocaleinfo(LC_STR_TYPE, ctryid, LOCALE_SGROUPING, (void *)&grouping);

	   if (ret == -1)
      {
		   free (decimal_point);
		   free (thousands_sep);
		   free (grouping);
		   decimal_point = NULL;
		   thousands_sep = NULL;
		   grouping = NULL;
		   return -1;
	   }

      if (_lconv->decimal_point != _lconv_static_decimal)
      {
   	   free(_lconv->decimal_point);
	      free(_lconv->thousands_sep);
	      free(_lconv->grouping);
      }

      _lconv->decimal_point = decimal_point;
	   _lconv->thousands_sep = thousands_sep;
	   _lconv->grouping = grouping;


#ifdef _DEBUG
   assert (strlen(_lconv->decimal_point) == 1);
#endif
	   /* set global decimal point character */
	   *_decimal_point = *_lconv->decimal_point;
	   _decimal_point_length = 1;

	   return 0;

   } else {
	   free (decimal_point);
	   free (thousands_sep);
	   free (grouping);
	   decimal_point = NULL;
	   thousands_sep = NULL;
	   grouping = NULL;

      // strdup them so we can free them
	   _lconv->decimal_point = _strdup(".");
	   _lconv->thousands_sep = _strdup("");
	   _lconv->grouping = strdup("");

	   /* set global decimal point character */
	   *_decimal_point = *_lconv->decimal_point;
	   _decimal_point_length = 1;

	   return 0;
	}
}
#endif /* _INTL */