1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
6 * Copyright (C) 1998-2014, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 ******************************************************************************
13 * Modification History:
15 * Date Name Description
16 * 11/19/98 stephen Creation.
17 * 03/12/99 stephen Modified for new C API.
18 * Added conversion from default codepage.
19 * 08/07/2003 george Reunify printf implementations
20 ******************************************************************************
23 #include "unicode/utypes.h"
25 #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION
27 #include "unicode/ustdio.h"
28 #include "unicode/ustring.h"
29 #include "unicode/unum.h"
30 #include "unicode/udat.h"
31 #include "unicode/putil.h"
43 static UFILE
*gStdOut
= NULL
;
44 static UInitOnce gStdOutInitOnce
= U_INITONCE_INITIALIZER
;
46 static UBool U_CALLCONV
uprintf_cleanup(void)
48 if (gStdOut
!= NULL
) {
52 gStdOutInitOnce
.reset();
56 static void U_CALLCONV
u_stdout_init() {
57 U_ASSERT(gStdOut
== NULL
);
58 gStdOut
= u_finit(stdout
, NULL
, NULL
);
59 ucln_io_registerCleanup(UCLN_IO_PRINTF
, &uprintf_cleanup
);
62 U_CAPI UFILE
* U_EXPORT2
65 umtx_initOnce(gStdOutInitOnce
, &u_stdout_init
);
69 static int32_t U_EXPORT2
70 u_printf_write(void *context
,
74 return u_file_write(str
, count
, (UFILE
*)context
);
78 u_printf_pad_and_justify(void *context
,
79 const u_printf_spec_info
*info
,
83 UFILE
*output
= (UFILE
*)context
;
86 /* pad and justify, if needed */
87 if(info
->fWidth
!= -1 && resultLen
< info
->fWidth
) {
90 written
= u_file_write(result
, resultLen
, output
);
91 for(i
= 0; i
< info
->fWidth
- resultLen
; ++i
) {
92 written
+= u_file_write(&info
->fPadChar
, 1, output
);
98 for(i
= 0; i
< info
->fWidth
- resultLen
; ++i
) {
99 written
+= u_file_write(&info
->fPadChar
, 1, output
);
101 written
+= u_file_write(result
, resultLen
, output
);
104 /* just write the formatted output */
106 written
= u_file_write(result
, resultLen
, output
);
112 U_CAPI
int32_t U_EXPORT2
114 const char *patternSpecification
,
120 va_start(ap
, patternSpecification
);
121 count
= u_vfprintf(f
, patternSpecification
, ap
);
127 U_CAPI
int32_t U_EXPORT2
128 u_printf(const char *patternSpecification
,
133 va_start(ap
, patternSpecification
);
134 count
= u_vfprintf(u_get_stdout(), patternSpecification
, ap
);
139 U_CAPI
int32_t U_EXPORT2
140 u_fprintf_u( UFILE
*f
,
141 const UChar
*patternSpecification
,
147 va_start(ap
, patternSpecification
);
148 count
= u_vfprintf_u(f
, patternSpecification
, ap
);
154 U_CAPI
int32_t U_EXPORT2
155 u_printf_u(const UChar
*patternSpecification
,
160 va_start(ap
, patternSpecification
);
161 count
= u_vfprintf_u(u_get_stdout(), patternSpecification
, ap
);
166 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
167 u_vfprintf( UFILE
*f
,
168 const char *patternSpecification
,
173 UChar buffer
[UFMT_DEFAULT_BUFFER_SIZE
];
174 size_t size
= strlen(patternSpecification
) + 1;
176 /* convert from the default codepage to Unicode */
177 if (size
>= MAX_UCHAR_BUFFER_SIZE(buffer
)) {
178 pattern
= (UChar
*)uprv_malloc(size
* sizeof(UChar
));
186 u_charsToUChars(patternSpecification
, pattern
, static_cast<int32_t>(size
));
189 count
= u_vfprintf_u(f
, pattern
, ap
);
192 if (pattern
!= buffer
) {
199 static const u_printf_stream_handler g_stream_handler
= {
201 u_printf_pad_and_justify
204 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
205 u_vfprintf_u( UFILE
*f
,
206 const UChar
*patternSpecification
,
209 int32_t written
= 0; /* haven't written anything yet */
211 /* parse and print the whole format string */
212 u_printf_parse(&g_stream_handler
, patternSpecification
, f
, NULL
, &f
->str
.fBundle
, &written
, ap
);
214 /* return # of UChars written */
218 #endif /* #if !UCONFIG_NO_FORMATTING */