2 ******************************************************************************
4 * Copyright (C) 1998-2014, 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 && !UCONFIG_NO_CONVERSION
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"
41 static UFILE
*gStdOut
= NULL
;
42 static UInitOnce gStdOutInitOnce
= U_INITONCE_INITIALIZER
;
44 static UBool U_CALLCONV
uprintf_cleanup(void)
46 if (gStdOut
!= NULL
) {
50 gStdOutInitOnce
.reset();
54 static void U_CALLCONV
u_stdout_init() {
55 U_ASSERT(gStdOut
== NULL
);
56 gStdOut
= u_finit(stdout
, NULL
, NULL
);
57 ucln_io_registerCleanup(UCLN_IO_PRINTF
, &uprintf_cleanup
);
60 U_CAPI UFILE
* U_EXPORT2
63 umtx_initOnce(gStdOutInitOnce
, &u_stdout_init
);
67 static int32_t U_EXPORT2
68 u_printf_write(void *context
,
72 return u_file_write(str
, count
, (UFILE
*)context
);
76 u_printf_pad_and_justify(void *context
,
77 const u_printf_spec_info
*info
,
81 UFILE
*output
= (UFILE
*)context
;
84 /* pad and justify, if needed */
85 if(info
->fWidth
!= -1 && resultLen
< info
->fWidth
) {
88 written
= u_file_write(result
, resultLen
, output
);
89 for(i
= 0; i
< info
->fWidth
- resultLen
; ++i
) {
90 written
+= u_file_write(&info
->fPadChar
, 1, output
);
96 for(i
= 0; i
< info
->fWidth
- resultLen
; ++i
) {
97 written
+= u_file_write(&info
->fPadChar
, 1, output
);
99 written
+= u_file_write(result
, resultLen
, output
);
102 /* just write the formatted output */
104 written
= u_file_write(result
, resultLen
, output
);
110 U_CAPI
int32_t U_EXPORT2
112 const char *patternSpecification
,
118 va_start(ap
, patternSpecification
);
119 count
= u_vfprintf(f
, patternSpecification
, ap
);
125 U_CAPI
int32_t U_EXPORT2
126 u_printf(const char *patternSpecification
,
131 va_start(ap
, patternSpecification
);
132 count
= u_vfprintf(u_get_stdout(), patternSpecification
, ap
);
137 U_CAPI
int32_t U_EXPORT2
138 u_fprintf_u( UFILE
*f
,
139 const UChar
*patternSpecification
,
145 va_start(ap
, patternSpecification
);
146 count
= u_vfprintf_u(f
, patternSpecification
, ap
);
152 U_CAPI
int32_t U_EXPORT2
153 u_printf_u(const UChar
*patternSpecification
,
158 va_start(ap
, patternSpecification
);
159 count
= u_vfprintf_u(u_get_stdout(), patternSpecification
, ap
);
164 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
165 u_vfprintf( UFILE
*f
,
166 const char *patternSpecification
,
171 UChar buffer
[UFMT_DEFAULT_BUFFER_SIZE
];
172 size_t size
= strlen(patternSpecification
) + 1;
174 /* convert from the default codepage to Unicode */
175 if (size
>= MAX_UCHAR_BUFFER_SIZE(buffer
)) {
176 pattern
= (UChar
*)uprv_malloc(size
* sizeof(UChar
));
184 u_charsToUChars(patternSpecification
, pattern
, size
);
187 count
= u_vfprintf_u(f
, pattern
, ap
);
190 if (pattern
!= buffer
) {
197 static const u_printf_stream_handler g_stream_handler
= {
199 u_printf_pad_and_justify
202 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
203 u_vfprintf_u( UFILE
*f
,
204 const UChar
*patternSpecification
,
207 int32_t written
= 0; /* haven't written anything yet */
209 /* parse and print the whole format string */
210 u_printf_parse(&g_stream_handler
, patternSpecification
, f
, NULL
, &f
->str
.fBundle
, &written
, ap
);
212 /* return # of UChars written */
216 #endif /* #if !UCONFIG_NO_FORMATTING */