Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / intl / icu / source / io / uprintf.c
blob4f9ec18148d643204f73fa9643551d3c2417c72f
1 /*
2 ******************************************************************************
4 * Copyright (C) 1998-2012, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
9 * File uprintf.c
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"
31 #include "uprintf.h"
32 #include "ufile.h"
33 #include "ucln_io.h"
34 #include "locbund.h"
36 #include "cmemory.h"
38 static UFILE *gStdOut = NULL;
40 static UBool U_CALLCONV uprintf_cleanup(void)
42 if (gStdOut != NULL) {
43 u_fclose(gStdOut);
44 gStdOut = NULL;
46 return TRUE;
49 U_CAPI UFILE * U_EXPORT2
50 u_get_stdout()
52 if (gStdOut == NULL) {
53 gStdOut = u_finit(stdout, NULL, NULL);
54 ucln_io_registerCleanup(UCLN_IO_PRINTF, &uprintf_cleanup);
56 return gStdOut;
59 static int32_t U_EXPORT2
60 u_printf_write(void *context,
61 const UChar *str,
62 int32_t count)
64 return u_file_write(str, count, (UFILE *)context);
67 static int32_t
68 u_printf_pad_and_justify(void *context,
69 const u_printf_spec_info *info,
70 const UChar *result,
71 int32_t resultLen)
73 UFILE *output = (UFILE *)context;
74 int32_t written, i;
76 /* pad and justify, if needed */
77 if(info->fWidth != -1 && resultLen < info->fWidth) {
78 /* left justify */
79 if(info->fLeft) {
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);
85 /* right justify */
86 else {
87 written = 0;
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 */
95 else {
96 written = u_file_write(result, resultLen, output);
99 return written;
102 U_CAPI int32_t U_EXPORT2
103 u_fprintf( UFILE *f,
104 const char *patternSpecification,
105 ... )
107 va_list ap;
108 int32_t count;
110 va_start(ap, patternSpecification);
111 count = u_vfprintf(f, patternSpecification, ap);
112 va_end(ap);
114 return count;
117 U_CAPI int32_t U_EXPORT2
118 u_printf(const char *patternSpecification,
119 ...)
121 va_list ap;
122 int32_t count;
123 va_start(ap, patternSpecification);
124 count = u_vfprintf(u_get_stdout(), patternSpecification, ap);
125 va_end(ap);
126 return count;
129 U_CAPI int32_t U_EXPORT2
130 u_fprintf_u( UFILE *f,
131 const UChar *patternSpecification,
132 ... )
134 va_list ap;
135 int32_t count;
137 va_start(ap, patternSpecification);
138 count = u_vfprintf_u(f, patternSpecification, ap);
139 va_end(ap);
141 return count;
144 U_CAPI int32_t U_EXPORT2
145 u_printf_u(const UChar *patternSpecification,
146 ...)
148 va_list ap;
149 int32_t count;
150 va_start(ap, patternSpecification);
151 count = u_vfprintf_u(u_get_stdout(), patternSpecification, ap);
152 va_end(ap);
153 return count;
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,
159 va_list ap)
161 int32_t count;
162 UChar *pattern;
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));
169 if(pattern == 0) {
170 return 0;
173 else {
174 pattern = buffer;
176 u_charsToUChars(patternSpecification, pattern, size);
178 /* do the work */
179 count = u_vfprintf_u(f, pattern, ap);
181 /* clean up */
182 if (pattern != buffer) {
183 uprv_free(pattern);
186 return count;
189 static const u_printf_stream_handler g_stream_handler = {
190 u_printf_write,
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,
197 va_list ap)
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 */
205 return written;
208 #endif /* #if !UCONFIG_NO_FORMATTING */