| OLD | NEW |
| 1 /* | 1 /* |
| 2 ****************************************************************************** | 2 ****************************************************************************** |
| 3 * | 3 * |
| 4 * Copyright (C) 1998-2012, International Business Machines | 4 * Copyright (C) 1998-2014, International Business Machines |
| 5 * Corporation and others. All Rights Reserved. | 5 * Corporation and others. All Rights Reserved. |
| 6 * | 6 * |
| 7 ****************************************************************************** | 7 ****************************************************************************** |
| 8 * | 8 * |
| 9 * File uprintf.c | 9 * File uprintf.cpp |
| 10 * | 10 * |
| 11 * Modification History: | 11 * Modification History: |
| 12 * | 12 * |
| 13 * Date Name Description | 13 * Date Name Description |
| 14 * 11/19/98 stephen Creation. | 14 * 11/19/98 stephen Creation. |
| 15 * 03/12/99 stephen Modified for new C API. | 15 * 03/12/99 stephen Modified for new C API. |
| 16 * Added conversion from default codepage. | 16 * Added conversion from default codepage. |
| 17 * 08/07/2003 george Reunify printf implementations | 17 * 08/07/2003 george Reunify printf implementations |
| 18 ****************************************************************************** | 18 ****************************************************************************** |
| 19 */ | 19 */ |
| 20 | 20 |
| 21 #include "unicode/utypes.h" | 21 #include "unicode/utypes.h" |
| 22 | 22 |
| 23 #if !UCONFIG_NO_FORMATTING | 23 #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION |
| 24 | 24 |
| 25 #include "unicode/ustdio.h" | 25 #include "unicode/ustdio.h" |
| 26 #include "unicode/ustring.h" | 26 #include "unicode/ustring.h" |
| 27 #include "unicode/unum.h" | 27 #include "unicode/unum.h" |
| 28 #include "unicode/udat.h" | 28 #include "unicode/udat.h" |
| 29 #include "unicode/putil.h" | 29 #include "unicode/putil.h" |
| 30 | 30 |
| 31 #include "cmemory.h" |
| 32 #include "locbund.h" |
| 33 #include "mutex.h" |
| 34 #include "uassert.h" |
| 31 #include "uprintf.h" | 35 #include "uprintf.h" |
| 32 #include "ufile.h" | 36 #include "ufile.h" |
| 33 #include "ucln_io.h" | 37 #include "ucln_io.h" |
| 34 #include "locbund.h" | |
| 35 | 38 |
| 36 #include "cmemory.h" | 39 U_NAMESPACE_USE |
| 37 | 40 |
| 38 static UFILE *gStdOut = NULL; | 41 static UFILE *gStdOut = NULL; |
| 42 static UInitOnce gStdOutInitOnce = U_INITONCE_INITIALIZER; |
| 39 | 43 |
| 40 static UBool U_CALLCONV uprintf_cleanup(void) | 44 static UBool U_CALLCONV uprintf_cleanup(void) |
| 41 { | 45 { |
| 42 if (gStdOut != NULL) { | 46 if (gStdOut != NULL) { |
| 43 u_fclose(gStdOut); | 47 u_fclose(gStdOut); |
| 44 gStdOut = NULL; | 48 gStdOut = NULL; |
| 45 } | 49 } |
| 50 gStdOutInitOnce.reset(); |
| 46 return TRUE; | 51 return TRUE; |
| 47 } | 52 } |
| 48 | 53 |
| 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); |
| 58 } |
| 59 |
| 49 U_CAPI UFILE * U_EXPORT2 | 60 U_CAPI UFILE * U_EXPORT2 |
| 50 u_get_stdout() | 61 u_get_stdout() |
| 51 { | 62 { |
| 52 if (gStdOut == NULL) { | 63 umtx_initOnce(gStdOutInitOnce, &u_stdout_init); |
| 53 gStdOut = u_finit(stdout, NULL, NULL); | |
| 54 ucln_io_registerCleanup(UCLN_IO_PRINTF, &uprintf_cleanup); | |
| 55 } | |
| 56 return gStdOut; | 64 return gStdOut; |
| 57 } | 65 } |
| 58 | 66 |
| 59 static int32_t U_EXPORT2 | 67 static int32_t U_EXPORT2 |
| 60 u_printf_write(void *context, | 68 u_printf_write(void *context, |
| 61 const UChar *str, | 69 const UChar *str, |
| 62 int32_t count) | 70 int32_t count) |
| 63 { | 71 { |
| 64 return u_file_write(str, count, (UFILE *)context); | 72 return u_file_write(str, count, (UFILE *)context); |
| 65 } | 73 } |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 } | 162 } |
| 155 | 163 |
| 156 U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 200
1 */ | 164 U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 200
1 */ |
| 157 u_vfprintf( UFILE *f, | 165 u_vfprintf( UFILE *f, |
| 158 const char *patternSpecification, | 166 const char *patternSpecification, |
| 159 va_list ap) | 167 va_list ap) |
| 160 { | 168 { |
| 161 int32_t count; | 169 int32_t count; |
| 162 UChar *pattern; | 170 UChar *pattern; |
| 163 UChar buffer[UFMT_DEFAULT_BUFFER_SIZE]; | 171 UChar buffer[UFMT_DEFAULT_BUFFER_SIZE]; |
| 164 int32_t size = (int32_t)strlen(patternSpecification) + 1; | 172 size_t size = strlen(patternSpecification) + 1; |
| 165 | 173 |
| 166 /* convert from the default codepage to Unicode */ | 174 /* convert from the default codepage to Unicode */ |
| 167 if (size >= MAX_UCHAR_BUFFER_SIZE(buffer)) { | 175 if (size >= MAX_UCHAR_BUFFER_SIZE(buffer)) { |
| 168 pattern = (UChar *)uprv_malloc(size * sizeof(UChar)); | 176 pattern = (UChar *)uprv_malloc(size * sizeof(UChar)); |
| 169 if(pattern == 0) { | 177 if(pattern == 0) { |
| 170 return 0; | 178 return 0; |
| 171 } | 179 } |
| 172 } | 180 } |
| 173 else { | 181 else { |
| 174 pattern = buffer; | 182 pattern = buffer; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 200 | 208 |
| 201 /* parse and print the whole format string */ | 209 /* parse and print the whole format string */ |
| 202 u_printf_parse(&g_stream_handler, patternSpecification, f, NULL, &f->str.fBu
ndle, &written, ap); | 210 u_printf_parse(&g_stream_handler, patternSpecification, f, NULL, &f->str.fBu
ndle, &written, ap); |
| 203 | 211 |
| 204 /* return # of UChars written */ | 212 /* return # of UChars written */ |
| 205 return written; | 213 return written; |
| 206 } | 214 } |
| 207 | 215 |
| 208 #endif /* #if !UCONFIG_NO_FORMATTING */ | 216 #endif /* #if !UCONFIG_NO_FORMATTING */ |
| 209 | 217 |
| OLD | NEW |