2 ******************************************************************************
4 * Copyright (C) 1998-2012, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
11 * Modification History:
13 * Date Name Description
14 * 11/19/98 stephen Creation.
15 * 03/12/99 stephen Modified for new C API.
16 * Added conversion from default codepage.
17 * 08/07/2003 george Reunify printf implementations
18 ******************************************************************************
21 #include "unicode/utypes.h"
23 #if !UCONFIG_NO_FORMATTING
25 #include "unicode/ustdio.h"
26 #include "unicode/ustring.h"
27 #include "unicode/unum.h"
28 #include "unicode/udat.h"
29 #include "unicode/putil.h"
38 static UFILE
*gStdOut
= NULL
;
40 static UBool U_CALLCONV
uprintf_cleanup(void)
42 if (gStdOut
!= NULL
) {
49 U_CAPI UFILE
* U_EXPORT2
52 if (gStdOut
== NULL
) {
53 gStdOut
= u_finit(stdout
, NULL
, NULL
);
54 ucln_io_registerCleanup(UCLN_IO_PRINTF
, &uprintf_cleanup
);
59 static int32_t U_EXPORT2
60 u_printf_write(void *context
,
64 return u_file_write(str
, count
, (UFILE
*)context
);
68 u_printf_pad_and_justify(void *context
,
69 const u_printf_spec_info
*info
,
73 UFILE
*output
= (UFILE
*)context
;
76 /* pad and justify, if needed */
77 if(info
->fWidth
!= -1 && resultLen
< info
->fWidth
) {
80 written
= u_file_write(result
, resultLen
, output
);
81 for(i
= 0; i
< info
->fWidth
- resultLen
; ++i
) {
82 written
+= u_file_write(&info
->fPadChar
, 1, output
);
88 for(i
= 0; i
< info
->fWidth
- resultLen
; ++i
) {
89 written
+= u_file_write(&info
->fPadChar
, 1, output
);
91 written
+= u_file_write(result
, resultLen
, output
);
94 /* just write the formatted output */
96 written
= u_file_write(result
, resultLen
, output
);
102 U_CAPI
int32_t U_EXPORT2
104 const char *patternSpecification
,
110 va_start(ap
, patternSpecification
);
111 count
= u_vfprintf(f
, patternSpecification
, ap
);
117 U_CAPI
int32_t U_EXPORT2
118 u_printf(const char *patternSpecification
,
123 va_start(ap
, patternSpecification
);
124 count
= u_vfprintf(u_get_stdout(), patternSpecification
, ap
);
129 U_CAPI
int32_t U_EXPORT2
130 u_fprintf_u( UFILE
*f
,
131 const UChar
*patternSpecification
,
137 va_start(ap
, patternSpecification
);
138 count
= u_vfprintf_u(f
, patternSpecification
, ap
);
144 U_CAPI
int32_t U_EXPORT2
145 u_printf_u(const UChar
*patternSpecification
,
150 va_start(ap
, patternSpecification
);
151 count
= u_vfprintf_u(u_get_stdout(), patternSpecification
, ap
);
156 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
157 u_vfprintf( UFILE
*f
,
158 const char *patternSpecification
,
163 UChar buffer
[UFMT_DEFAULT_BUFFER_SIZE
];
164 int32_t size
= (int32_t)strlen(patternSpecification
) + 1;
166 /* convert from the default codepage to Unicode */
167 if (size
>= MAX_UCHAR_BUFFER_SIZE(buffer
)) {
168 pattern
= (UChar
*)uprv_malloc(size
* sizeof(UChar
));
176 u_charsToUChars(patternSpecification
, pattern
, size
);
179 count
= u_vfprintf_u(f
, pattern
, ap
);
182 if (pattern
!= buffer
) {
189 static const u_printf_stream_handler g_stream_handler
= {
191 u_printf_pad_and_justify
194 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
195 u_vfprintf_u( UFILE
*f
,
196 const UChar
*patternSpecification
,
199 int32_t written
= 0; /* haven't written anything yet */
201 /* parse and print the whole format string */
202 u_printf_parse(&g_stream_handler
, patternSpecification
, f
, NULL
, &f
->str
.fBundle
, &written
, ap
);
204 /* return # of UChars written */
208 #endif /* #if !UCONFIG_NO_FORMATTING */