]> git.saurik.com Git - apple/icu.git/blame - icuSources/io/uprintf.cpp
ICU-551.24.tar.gz
[apple/icu.git] / icuSources / io / uprintf.cpp
CommitLineData
374ca955
A
1/*
2******************************************************************************
3*
b331163b 4* Copyright (C) 1998-2014, International Business Machines
374ca955
A
5* Corporation and others. All Rights Reserved.
6*
7******************************************************************************
8*
b331163b 9* File uprintf.cpp
374ca955
A
10*
11* Modification History:
12*
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******************************************************************************
19*/
20
21#include "unicode/utypes.h"
22
b331163b 23#if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION
374ca955
A
24
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"
30
b331163b
A
31#include "cmemory.h"
32#include "locbund.h"
33#include "mutex.h"
34#include "uassert.h"
374ca955
A
35#include "uprintf.h"
36#include "ufile.h"
4388f060 37#include "ucln_io.h"
374ca955 38
b331163b 39U_NAMESPACE_USE
374ca955 40
4388f060 41static UFILE *gStdOut = NULL;
b331163b 42static UInitOnce gStdOutInitOnce = U_INITONCE_INITIALIZER;
4388f060 43
51004dcb 44static UBool U_CALLCONV uprintf_cleanup(void)
4388f060
A
45{
46 if (gStdOut != NULL) {
47 u_fclose(gStdOut);
48 gStdOut = NULL;
49 }
b331163b 50 gStdOutInitOnce.reset();
4388f060
A
51 return TRUE;
52}
53
b331163b
A
54static 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);
58}
59
4388f060
A
60U_CAPI UFILE * U_EXPORT2
61u_get_stdout()
62{
b331163b 63 umtx_initOnce(gStdOutInitOnce, &u_stdout_init);
4388f060
A
64 return gStdOut;
65}
66
374ca955
A
67static int32_t U_EXPORT2
68u_printf_write(void *context,
69 const UChar *str,
70 int32_t count)
71{
72 return u_file_write(str, count, (UFILE *)context);
73}
74
75static int32_t
76u_printf_pad_and_justify(void *context,
77 const u_printf_spec_info *info,
78 const UChar *result,
79 int32_t resultLen)
80{
81 UFILE *output = (UFILE *)context;
82 int32_t written, i;
83
84 /* pad and justify, if needed */
85 if(info->fWidth != -1 && resultLen < info->fWidth) {
86 /* left justify */
87 if(info->fLeft) {
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);
91 }
92 }
93 /* right justify */
94 else {
95 written = 0;
96 for(i = 0; i < info->fWidth - resultLen; ++i) {
97 written += u_file_write(&info->fPadChar, 1, output);
98 }
99 written += u_file_write(result, resultLen, output);
100 }
101 }
102 /* just write the formatted output */
103 else {
104 written = u_file_write(result, resultLen, output);
105 }
106
107 return written;
108}
109
110U_CAPI int32_t U_EXPORT2
111u_fprintf( UFILE *f,
112 const char *patternSpecification,
113 ... )
114{
115 va_list ap;
116 int32_t count;
117
118 va_start(ap, patternSpecification);
119 count = u_vfprintf(f, patternSpecification, ap);
120 va_end(ap);
121
122 return count;
123}
124
4388f060
A
125U_CAPI int32_t U_EXPORT2
126u_printf(const char *patternSpecification,
127 ...)
128{
129 va_list ap;
130 int32_t count;
131 va_start(ap, patternSpecification);
132 count = u_vfprintf(u_get_stdout(), patternSpecification, ap);
133 va_end(ap);
134 return count;
135}
136
374ca955
A
137U_CAPI int32_t U_EXPORT2
138u_fprintf_u( UFILE *f,
139 const UChar *patternSpecification,
140 ... )
141{
142 va_list ap;
143 int32_t count;
144
145 va_start(ap, patternSpecification);
146 count = u_vfprintf_u(f, patternSpecification, ap);
147 va_end(ap);
148
149 return count;
150}
151
4388f060
A
152U_CAPI int32_t U_EXPORT2
153u_printf_u(const UChar *patternSpecification,
154 ...)
155{
156 va_list ap;
157 int32_t count;
158 va_start(ap, patternSpecification);
159 count = u_vfprintf_u(u_get_stdout(), patternSpecification, ap);
160 va_end(ap);
161 return count;
162}
163
374ca955
A
164U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
165u_vfprintf( UFILE *f,
166 const char *patternSpecification,
167 va_list ap)
168{
169 int32_t count;
170 UChar *pattern;
171 UChar buffer[UFMT_DEFAULT_BUFFER_SIZE];
b331163b 172 size_t size = strlen(patternSpecification) + 1;
374ca955
A
173
174 /* convert from the default codepage to Unicode */
175 if (size >= MAX_UCHAR_BUFFER_SIZE(buffer)) {
176 pattern = (UChar *)uprv_malloc(size * sizeof(UChar));
177 if(pattern == 0) {
178 return 0;
179 }
180 }
181 else {
182 pattern = buffer;
183 }
184 u_charsToUChars(patternSpecification, pattern, size);
185
186 /* do the work */
187 count = u_vfprintf_u(f, pattern, ap);
188
189 /* clean up */
190 if (pattern != buffer) {
191 uprv_free(pattern);
192 }
193
194 return count;
195}
196
197static const u_printf_stream_handler g_stream_handler = {
198 u_printf_write,
199 u_printf_pad_and_justify
200};
201
202U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
203u_vfprintf_u( UFILE *f,
204 const UChar *patternSpecification,
205 va_list ap)
206{
207 int32_t written = 0; /* haven't written anything yet */
208
209 /* parse and print the whole format string */
210 u_printf_parse(&g_stream_handler, patternSpecification, f, NULL, &f->str.fBundle, &written, ap);
211
212 /* return # of UChars written */
213 return written;
214}
215
216#endif /* #if !UCONFIG_NO_FORMATTING */
217