From: Abhinav Gupta Date: Fri, 16 Sep 2011 17:44:06 +0000 (+0000) Subject: ICU-8579 added u_printf X-Git-Tag: milestone-59-0-1~4509 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b199eb07ee0ed6aef43f19b632517bb13fa00d1d;p=icu ICU-8579 added u_printf X-SVN-Rev: 30677 --- diff --git a/icu4c/source/io/ucln_io.h b/icu4c/source/io/ucln_io.h index e26c1b68833..105eca8b136 100644 --- a/icu4c/source/io/ucln_io.h +++ b/icu4c/source/io/ucln_io.h @@ -1,7 +1,7 @@ /* ****************************************************************************** * * -* Copyright (C) 2001-2006, International Business Machines * +* Copyright (C) 2001-2011, International Business Machines * * Corporation and others. All Rights Reserved. * * * ****************************************************************************** @@ -26,6 +26,7 @@ as the functions are suppose to be called. */ typedef enum ECleanupIOType { UCLN_IO_START = -1, UCLN_IO_LOCBUND, + UCLN_IO_PRINTF, UCLN_IO_COUNT /* This must be last */ } ECleanupIOType; diff --git a/icu4c/source/io/unicode/ustdio.h b/icu4c/source/io/unicode/ustdio.h index 9bcc797b292..cae7d9baf8a 100644 --- a/icu4c/source/io/unicode/ustdio.h +++ b/icu4c/source/io/unicode/ustdio.h @@ -1,7 +1,7 @@ /* ****************************************************************************** * -* Copyright (C) 1998-2010, International Business Machines +* Copyright (C) 1998-2011, International Business Machines * Corporation and others. All Rights Reserved. * ****************************************************************************** @@ -441,6 +441,17 @@ U_STABLE UConverter* U_EXPORT2 u_fgetConverter(UFILE *f); /* Output functions */ +/** + * Write formatted data to stdout. + * @param patternSpecification A pattern specifying how u_printf will + * interpret the variable arguments received and format the data. + * @return The number of Unicode characters written to stdout + * @draft ICU 49 + */ +U_DRAFT int32_t U_EXPORT2 +u_printf(const char *patternSpecification, + ... ); + /** * Write formatted data to a UFILE. * @param f The UFILE to which to write. diff --git a/icu4c/source/io/uprintf.c b/icu4c/source/io/uprintf.c index d95c02c671c..be50f054f2f 100644 --- a/icu4c/source/io/uprintf.c +++ b/icu4c/source/io/uprintf.c @@ -1,7 +1,7 @@ /* ****************************************************************************** * -* Copyright (C) 1998-2004, International Business Machines +* Copyright (C) 1998-2011, International Business Machines * Corporation and others. All Rights Reserved. * ****************************************************************************** @@ -30,10 +30,31 @@ #include "uprintf.h" #include "ufile.h" +#include "ucln_io.h" #include "locbund.h" #include "cmemory.h" +UFILE *gStdOut = NULL; + +static UBool U_CALLCONV uprintf_cleanup() +{ + if (gStdOut != NULL) { + u_fclose(gStdOut); + gStdOut = NULL; + } +} + +static UFILE * U_EXPORT2 +u_get_stdout() +{ + if (gStdOut == NULL) { + gStdOut = u_finit(stdout, NULL, NULL); + ucln_io_registerCleanup(UCLN_IO_PRINTF, &uprintf_cleanup); + } + return gStdOut; +} + static int32_t U_EXPORT2 u_printf_write(void *context, const UChar *str, @@ -92,6 +113,18 @@ u_fprintf( UFILE *f, return count; } +U_CAPI int32_t U_EXPORT2 +u_printf(const char *patternSpecification, + ...) +{ + va_list ap; + int32_t count; + va_start(ap, patternSpecification); + count = u_vfprintf(u_get_stdout(), patternSpecification, ap); + va_end(ap); + return count; +} + U_CAPI int32_t U_EXPORT2 u_fprintf_u( UFILE *f, const UChar *patternSpecification,