1 /* Formatting a monetary value according to the current locale.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>
5 and Jochen Hein <Jochen.Hein@informatik.TU-Clausthal.de>, 1996.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
27 # include "../libio/libioP.h"
28 # include "../libio/strfile.h"
34 #include "../locale/localeinfo.h"
37 #define out_char(Ch) \
39 if (dest >= s + maxsize - 1) \
41 __set_errno (E2BIG); \
48 #define out_string(String) \
50 const char *_s = (String); \
55 #define to_digit(Ch) ((Ch) - '0')
58 /* We use this code also for the extended locale handling where the
59 function gets as an additional argument the locale which has to be
60 used. To access the values we have to redefine the _NL_CURRENT
62 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
64 # define _NL_CURRENT(category, item) \
65 (current->values[_NL_ITEM_INDEX (item)].string)
68 extern int __printf_fp (FILE *, const struct printf_info
*,
70 /* This function determines the number of digit groups in the output.
71 The definition is in printf_fp.c. */
72 extern unsigned int __guess_grouping (unsigned int intdig_max
,
73 const char *grouping
, wchar_t sepchar
);
76 /* We have to overcome some problems with this implementation. On the
77 one hand the strfmon() function is specified in XPG4 and of course
78 it has to follow this. But on the other hand POSIX.2 specifies
79 some information in the LC_MONETARY category which should be used,
80 too. Some of the information contradicts the information which can
81 be specified in format string. */
82 #ifndef USE_IN_EXTENDED_LOCALE_MODEL
84 strfmon (char *s
, size_t maxsize
, const char *format
, ...)
87 __strfmon_l (char *s
, size_t maxsize
, __locale_t loc
, const char *format
, ...)
90 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
91 struct locale_data
*current
= loc
->__locales
[LC_MONETARY
];
98 struct printf_info info
;
99 va_list ap
; /* Scan through the varargs. */
100 char *dest
; /* Pointer so copy the output. */
101 const char *fmt
; /* Pointer that walks through format. */
103 va_start (ap
, format
);
108 /* Loop through the format-string. */
111 /* The floating-point value to output. */
115 __long_double_t ldbl
;
118 int print_curr_symbol
;
133 const char *currency_symbol
;
138 /* Process all character which do not introduce a format
146 /* "%%" means a single '%' character. */
154 /* Defaults for formatting. */
155 print_curr_symbol
= 1; /* Print the currency symbol. */
156 left_prec
= -1; /* No left precision specified. */
157 right_prec
= -1; /* No right precision specified. */
158 group
= 1; /* Print digits grouped. */
159 pad
= ' '; /* Fill character is <SP>. */
160 is_long_double
= 0; /* Double argument by default. */
161 p_sign_posn
= -1; /* This indicates whether the */
162 n_sign_posn
= -1; /* '(' flag is given. */
163 width
= -1; /* No width specified so far. */
164 left
= 0; /* Right justified by default. */
166 /* Parse group characters. */
171 case '=': /* Set fill character. */
176 __set_errno (EINVAL
);
181 case '^': /* Don't group digits. */
184 case '+': /* Use +/- for sign of number. */
185 if (n_sign_posn
!= -1)
187 __set_errno (EINVAL
);
191 if (*_NL_CURRENT (LC_MONETARY
, P_SIGN_POSN
) == '\0')
194 p_sign_posn
= *_NL_CURRENT (LC_MONETARY
, P_SIGN_POSN
);
195 if (*_NL_CURRENT (LC_MONETARY
, N_SIGN_POSN
) == '\0')
198 n_sign_posn
= *_NL_CURRENT (LC_MONETARY
, N_SIGN_POSN
);
200 case '(': /* Use ( ) for negative sign. */
201 if (n_sign_posn
!= -1)
203 __set_errno (EINVAL
);
207 n_sign_posn
= 5; /* This is a else unused value. */
209 case '!': /* Don't print the currency symbol. */
210 print_curr_symbol
= 0;
212 case '-': /* Print left justified. */
216 /* Will stop the loop. */;
223 /* Parse field width. */
224 width
= to_digit (*fmt
);
226 while (isdigit (*++fmt
))
229 width
+= to_digit (*fmt
);
232 /* If we don't have enough room for the demanded width we
233 can stop now and return an error. */
234 if (dest
+ width
>= s
+ maxsize
)
242 /* Recognize left precision. */
245 if (!isdigit (*++fmt
))
247 __set_errno (EINVAL
);
251 left_prec
= to_digit (*fmt
);
253 while (isdigit (*++fmt
))
256 left_prec
+= to_digit (*fmt
);
260 /* Recognize right precision. */
263 if (!isdigit (*++fmt
))
265 __set_errno (EINVAL
);
269 right_prec
= to_digit (*fmt
);
271 while (isdigit (*++fmt
))
274 right_prec
+= to_digit (*fmt
);
278 /* Handle modifier. This is an extension. */
285 /* Handle format specifier. */
288 case 'i': /* Use international currency symbol. */
289 currency_symbol
= _NL_CURRENT (LC_MONETARY
, INT_CURR_SYMBOL
);
290 if (right_prec
== -1)
291 if (*_NL_CURRENT (LC_MONETARY
, INT_FRAC_DIGITS
) == '\177')
294 right_prec
= *_NL_CURRENT (LC_MONETARY
, INT_FRAC_DIGITS
);
296 case 'n': /* Use national currency symbol. */
297 currency_symbol
= _NL_CURRENT (LC_MONETARY
, CURRENCY_SYMBOL
);
298 if (right_prec
== -1)
299 if (*_NL_CURRENT (LC_MONETARY
, FRAC_DIGITS
) == '\177')
302 right_prec
= *_NL_CURRENT (LC_MONETARY
, FRAC_DIGITS
);
304 default: /* Any unrecognized format is an error. */
305 __set_errno (EINVAL
);
310 /* If we have to print the digits grouped determine how many
311 extra characters this means. */
312 if (group
&& left_prec
!= -1)
313 left_prec
+= __guess_grouping (left_prec
,
314 _NL_CURRENT (LC_MONETARY
, MON_GROUPING
),
315 *_NL_CURRENT (LC_MONETARY
,
318 /* Now it's time to get the value. */
319 if (is_long_double
== 1)
321 fpnum
.ldbl
= va_arg (ap
, long double);
322 is_negative
= fpnum
.ldbl
< 0;
324 fpnum
.ldbl
= -fpnum
.ldbl
;
328 fpnum
.dbl
= va_arg (ap
, double);
329 is_negative
= fpnum
.dbl
< 0;
331 fpnum
.dbl
= -fpnum
.dbl
;
334 /* We now know the sign of the value and can determine the format. */
337 sign_char
= *_NL_CURRENT (LC_MONETARY
, NEGATIVE_SIGN
);
338 /* If the locale does not specify a character for the
339 negative sign we use a '-'. */
340 if (sign_char
== '\0')
342 cs_precedes
= *_NL_CURRENT (LC_MONETARY
, N_CS_PRECEDES
);
343 sep_by_space
= *_NL_CURRENT (LC_MONETARY
, N_SEP_BY_SPACE
);
344 /* If the '(' flag is not given use the sign position from
345 the current locale. */
346 if (n_sign_posn
== -1)
347 sign_posn
= *_NL_CURRENT (LC_MONETARY
, N_SIGN_POSN
);
349 /* This means use parentheses. */
354 sign_char
= *_NL_CURRENT (LC_MONETARY
, POSITIVE_SIGN
);
355 /* If the locale does not specify a character for the
356 positive sign we use a <SP>. */
357 if (sign_char
== '\0')
359 cs_precedes
= *_NL_CURRENT (LC_MONETARY
, P_CS_PRECEDES
);
360 sep_by_space
= *_NL_CURRENT (LC_MONETARY
, P_SEP_BY_SPACE
);
361 if (n_sign_posn
== -1)
362 sign_posn
= *_NL_CURRENT (LC_MONETARY
, P_SIGN_POSN
);
364 /* Here we don't set SIGN_POSN to 0 because we don'want to
365 print <SP> instead of the braces and this is what the
370 /* Set default values for unspecified information. */
371 if (cs_precedes
!= 0)
373 if (sep_by_space
== 127)
379 /* Perhaps we'll someday make these things configurable so
380 better start using symbolic names now. */
381 #define left_paren '('
382 #define right_paren ')'
384 startp
= dest
; /* Remember start so we can compute length. */
387 out_char (left_paren
);
388 if (sign_posn
== 5) /* This is for positive number and ( flag. */
393 if (sign_posn
!= 0 && sign_posn
!= 2 && sign_posn
!= 4
396 out_char (sign_char
);
397 if (sep_by_space
== 2)
401 if (print_curr_symbol
)
403 out_string (currency_symbol
);
407 if (sep_by_space
== 2)
409 out_char (sign_char
);
412 if (sep_by_space
== 1)
417 if (sign_posn
!= 0 && sign_posn
!= 2 && sign_posn
!= 3
418 && sign_posn
!= 4 && sign_posn
!= 5)
419 out_char (sign_char
);
421 /* Print the number. */
423 _IO_init ((_IO_FILE
*) &f
, 0);
424 _IO_JUMPS ((_IO_FILE
*) &f
) = &_IO_str_jumps
;
425 _IO_str_init_static ((_IO_FILE
*) &f
, dest
, (s
+ maxsize
) - dest
, dest
);
427 memset((void *) &f
, 0, sizeof(f
));
428 f
.__magic
= _IOMAGIC
;
429 f
.__mode
.__write
= 1;
430 /* The buffer size is one less than MAXLEN
431 so we have space for the null terminator. */
432 f
.__bufp
= f
.__buffer
= (char *) dest
;
433 f
.__bufsize
= (s
+ maxsize
) - dest
;
434 f
.__put_limit
= f
.__buffer
+ f
.__bufsize
;
435 f
.__get_limit
= f
.__buffer
;
436 /* After the buffer is full (MAXLEN characters have been written),
437 any more characters written will go to the bit bucket. */
438 f
.__room_funcs
= __default_room_functions
;
439 f
.__io_funcs
.__write
= NULL
;
442 /* We clear the last available byte so we can find out whether
443 the numeric representation is too long. */
444 s
[maxsize
- 1] = '\0';
446 info
.prec
= right_prec
;
447 info
.width
= left_prec
+ (right_prec
? (right_prec
+ 1) : 0);
449 info
.is_long_double
= is_long_double
;
458 info
.extra
= 1; /* This means use values from LC_MONETARY. */
461 done
= __printf_fp ((FILE *) &f
, &info
, &ptr
);
468 if (s
[maxsize
- 1] != '\0')
477 if (sep_by_space
== 1)
479 out_char (sign_char
);
482 if (print_curr_symbol
)
484 if (sign_posn
== 3 && sep_by_space
== 2)
486 out_string (currency_symbol
);
492 if (sep_by_space
== 2)
494 out_char (sign_char
);
498 out_char (right_paren
);
500 out_char (' '); /* This is for positive number and ( flag. */
502 /* Now test whether the output width is filled. */
503 if (dest
- startp
< width
)
505 /* We simply have to fill using spaces. */
508 while (dest
- startp
< width
);
511 int dist
= width
- (dest
- startp
);
513 for (cp
= dest
- 1; cp
>= startp
; --cp
)
519 startp
[--dist
] = ' ';
524 /* Terminate the string. */