michael@0: /* michael@0: ****************************************************************************** michael@0: * michael@0: * Copyright (C) 1998-2012, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ****************************************************************************** michael@0: * michael@0: * File uprintf.c michael@0: * michael@0: * Modification History: michael@0: * michael@0: * Date Name Description michael@0: * 11/19/98 stephen Creation. michael@0: * 03/12/99 stephen Modified for new C API. michael@0: * Added conversion from default codepage. michael@0: * 08/07/2003 george Reunify printf implementations michael@0: ****************************************************************************** michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/ustdio.h" michael@0: #include "unicode/ustring.h" michael@0: #include "unicode/unum.h" michael@0: #include "unicode/udat.h" michael@0: #include "unicode/putil.h" michael@0: michael@0: #include "uprintf.h" michael@0: #include "ufile.h" michael@0: #include "ucln_io.h" michael@0: #include "locbund.h" michael@0: michael@0: #include "cmemory.h" michael@0: michael@0: static UFILE *gStdOut = NULL; michael@0: michael@0: static UBool U_CALLCONV uprintf_cleanup(void) michael@0: { michael@0: if (gStdOut != NULL) { michael@0: u_fclose(gStdOut); michael@0: gStdOut = NULL; michael@0: } michael@0: return TRUE; michael@0: } michael@0: michael@0: U_CAPI UFILE * U_EXPORT2 michael@0: u_get_stdout() michael@0: { michael@0: if (gStdOut == NULL) { michael@0: gStdOut = u_finit(stdout, NULL, NULL); michael@0: ucln_io_registerCleanup(UCLN_IO_PRINTF, &uprintf_cleanup); michael@0: } michael@0: return gStdOut; michael@0: } michael@0: michael@0: static int32_t U_EXPORT2 michael@0: u_printf_write(void *context, michael@0: const UChar *str, michael@0: int32_t count) michael@0: { michael@0: return u_file_write(str, count, (UFILE *)context); michael@0: } michael@0: michael@0: static int32_t michael@0: u_printf_pad_and_justify(void *context, michael@0: const u_printf_spec_info *info, michael@0: const UChar *result, michael@0: int32_t resultLen) michael@0: { michael@0: UFILE *output = (UFILE *)context; michael@0: int32_t written, i; michael@0: michael@0: /* pad and justify, if needed */ michael@0: if(info->fWidth != -1 && resultLen < info->fWidth) { michael@0: /* left justify */ michael@0: if(info->fLeft) { michael@0: written = u_file_write(result, resultLen, output); michael@0: for(i = 0; i < info->fWidth - resultLen; ++i) { michael@0: written += u_file_write(&info->fPadChar, 1, output); michael@0: } michael@0: } michael@0: /* right justify */ michael@0: else { michael@0: written = 0; michael@0: for(i = 0; i < info->fWidth - resultLen; ++i) { michael@0: written += u_file_write(&info->fPadChar, 1, output); michael@0: } michael@0: written += u_file_write(result, resultLen, output); michael@0: } michael@0: } michael@0: /* just write the formatted output */ michael@0: else { michael@0: written = u_file_write(result, resultLen, output); michael@0: } michael@0: michael@0: return written; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: u_fprintf( UFILE *f, michael@0: const char *patternSpecification, michael@0: ... ) michael@0: { michael@0: va_list ap; michael@0: int32_t count; michael@0: michael@0: va_start(ap, patternSpecification); michael@0: count = u_vfprintf(f, patternSpecification, ap); michael@0: va_end(ap); michael@0: michael@0: return count; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: u_printf(const char *patternSpecification, michael@0: ...) michael@0: { michael@0: va_list ap; michael@0: int32_t count; michael@0: va_start(ap, patternSpecification); michael@0: count = u_vfprintf(u_get_stdout(), patternSpecification, ap); michael@0: va_end(ap); michael@0: return count; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: u_fprintf_u( UFILE *f, michael@0: const UChar *patternSpecification, michael@0: ... ) michael@0: { michael@0: va_list ap; michael@0: int32_t count; michael@0: michael@0: va_start(ap, patternSpecification); michael@0: count = u_vfprintf_u(f, patternSpecification, ap); michael@0: va_end(ap); michael@0: michael@0: return count; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: u_printf_u(const UChar *patternSpecification, michael@0: ...) michael@0: { michael@0: va_list ap; michael@0: int32_t count; michael@0: va_start(ap, patternSpecification); michael@0: count = u_vfprintf_u(u_get_stdout(), patternSpecification, ap); michael@0: va_end(ap); michael@0: return count; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ michael@0: u_vfprintf( UFILE *f, michael@0: const char *patternSpecification, michael@0: va_list ap) michael@0: { michael@0: int32_t count; michael@0: UChar *pattern; michael@0: UChar buffer[UFMT_DEFAULT_BUFFER_SIZE]; michael@0: int32_t size = (int32_t)strlen(patternSpecification) + 1; michael@0: michael@0: /* convert from the default codepage to Unicode */ michael@0: if (size >= MAX_UCHAR_BUFFER_SIZE(buffer)) { michael@0: pattern = (UChar *)uprv_malloc(size * sizeof(UChar)); michael@0: if(pattern == 0) { michael@0: return 0; michael@0: } michael@0: } michael@0: else { michael@0: pattern = buffer; michael@0: } michael@0: u_charsToUChars(patternSpecification, pattern, size); michael@0: michael@0: /* do the work */ michael@0: count = u_vfprintf_u(f, pattern, ap); michael@0: michael@0: /* clean up */ michael@0: if (pattern != buffer) { michael@0: uprv_free(pattern); michael@0: } michael@0: michael@0: return count; michael@0: } michael@0: michael@0: static const u_printf_stream_handler g_stream_handler = { michael@0: u_printf_write, michael@0: u_printf_pad_and_justify michael@0: }; michael@0: michael@0: U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ michael@0: u_vfprintf_u( UFILE *f, michael@0: const UChar *patternSpecification, michael@0: va_list ap) michael@0: { michael@0: int32_t written = 0; /* haven't written anything yet */ michael@0: michael@0: /* parse and print the whole format string */ michael@0: u_printf_parse(&g_stream_handler, patternSpecification, f, NULL, &f->str.fBundle, &written, ap); michael@0: michael@0: /* return # of UChars written */ michael@0: return written; michael@0: } michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: