intl/icu/source/io/uprintf.c

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 ******************************************************************************
michael@0 3 *
michael@0 4 * Copyright (C) 1998-2012, International Business Machines
michael@0 5 * Corporation and others. All Rights Reserved.
michael@0 6 *
michael@0 7 ******************************************************************************
michael@0 8 *
michael@0 9 * File uprintf.c
michael@0 10 *
michael@0 11 * Modification History:
michael@0 12 *
michael@0 13 * Date Name Description
michael@0 14 * 11/19/98 stephen Creation.
michael@0 15 * 03/12/99 stephen Modified for new C API.
michael@0 16 * Added conversion from default codepage.
michael@0 17 * 08/07/2003 george Reunify printf implementations
michael@0 18 ******************************************************************************
michael@0 19 */
michael@0 20
michael@0 21 #include "unicode/utypes.h"
michael@0 22
michael@0 23 #if !UCONFIG_NO_FORMATTING
michael@0 24
michael@0 25 #include "unicode/ustdio.h"
michael@0 26 #include "unicode/ustring.h"
michael@0 27 #include "unicode/unum.h"
michael@0 28 #include "unicode/udat.h"
michael@0 29 #include "unicode/putil.h"
michael@0 30
michael@0 31 #include "uprintf.h"
michael@0 32 #include "ufile.h"
michael@0 33 #include "ucln_io.h"
michael@0 34 #include "locbund.h"
michael@0 35
michael@0 36 #include "cmemory.h"
michael@0 37
michael@0 38 static UFILE *gStdOut = NULL;
michael@0 39
michael@0 40 static UBool U_CALLCONV uprintf_cleanup(void)
michael@0 41 {
michael@0 42 if (gStdOut != NULL) {
michael@0 43 u_fclose(gStdOut);
michael@0 44 gStdOut = NULL;
michael@0 45 }
michael@0 46 return TRUE;
michael@0 47 }
michael@0 48
michael@0 49 U_CAPI UFILE * U_EXPORT2
michael@0 50 u_get_stdout()
michael@0 51 {
michael@0 52 if (gStdOut == NULL) {
michael@0 53 gStdOut = u_finit(stdout, NULL, NULL);
michael@0 54 ucln_io_registerCleanup(UCLN_IO_PRINTF, &uprintf_cleanup);
michael@0 55 }
michael@0 56 return gStdOut;
michael@0 57 }
michael@0 58
michael@0 59 static int32_t U_EXPORT2
michael@0 60 u_printf_write(void *context,
michael@0 61 const UChar *str,
michael@0 62 int32_t count)
michael@0 63 {
michael@0 64 return u_file_write(str, count, (UFILE *)context);
michael@0 65 }
michael@0 66
michael@0 67 static int32_t
michael@0 68 u_printf_pad_and_justify(void *context,
michael@0 69 const u_printf_spec_info *info,
michael@0 70 const UChar *result,
michael@0 71 int32_t resultLen)
michael@0 72 {
michael@0 73 UFILE *output = (UFILE *)context;
michael@0 74 int32_t written, i;
michael@0 75
michael@0 76 /* pad and justify, if needed */
michael@0 77 if(info->fWidth != -1 && resultLen < info->fWidth) {
michael@0 78 /* left justify */
michael@0 79 if(info->fLeft) {
michael@0 80 written = u_file_write(result, resultLen, output);
michael@0 81 for(i = 0; i < info->fWidth - resultLen; ++i) {
michael@0 82 written += u_file_write(&info->fPadChar, 1, output);
michael@0 83 }
michael@0 84 }
michael@0 85 /* right justify */
michael@0 86 else {
michael@0 87 written = 0;
michael@0 88 for(i = 0; i < info->fWidth - resultLen; ++i) {
michael@0 89 written += u_file_write(&info->fPadChar, 1, output);
michael@0 90 }
michael@0 91 written += u_file_write(result, resultLen, output);
michael@0 92 }
michael@0 93 }
michael@0 94 /* just write the formatted output */
michael@0 95 else {
michael@0 96 written = u_file_write(result, resultLen, output);
michael@0 97 }
michael@0 98
michael@0 99 return written;
michael@0 100 }
michael@0 101
michael@0 102 U_CAPI int32_t U_EXPORT2
michael@0 103 u_fprintf( UFILE *f,
michael@0 104 const char *patternSpecification,
michael@0 105 ... )
michael@0 106 {
michael@0 107 va_list ap;
michael@0 108 int32_t count;
michael@0 109
michael@0 110 va_start(ap, patternSpecification);
michael@0 111 count = u_vfprintf(f, patternSpecification, ap);
michael@0 112 va_end(ap);
michael@0 113
michael@0 114 return count;
michael@0 115 }
michael@0 116
michael@0 117 U_CAPI int32_t U_EXPORT2
michael@0 118 u_printf(const char *patternSpecification,
michael@0 119 ...)
michael@0 120 {
michael@0 121 va_list ap;
michael@0 122 int32_t count;
michael@0 123 va_start(ap, patternSpecification);
michael@0 124 count = u_vfprintf(u_get_stdout(), patternSpecification, ap);
michael@0 125 va_end(ap);
michael@0 126 return count;
michael@0 127 }
michael@0 128
michael@0 129 U_CAPI int32_t U_EXPORT2
michael@0 130 u_fprintf_u( UFILE *f,
michael@0 131 const UChar *patternSpecification,
michael@0 132 ... )
michael@0 133 {
michael@0 134 va_list ap;
michael@0 135 int32_t count;
michael@0 136
michael@0 137 va_start(ap, patternSpecification);
michael@0 138 count = u_vfprintf_u(f, patternSpecification, ap);
michael@0 139 va_end(ap);
michael@0 140
michael@0 141 return count;
michael@0 142 }
michael@0 143
michael@0 144 U_CAPI int32_t U_EXPORT2
michael@0 145 u_printf_u(const UChar *patternSpecification,
michael@0 146 ...)
michael@0 147 {
michael@0 148 va_list ap;
michael@0 149 int32_t count;
michael@0 150 va_start(ap, patternSpecification);
michael@0 151 count = u_vfprintf_u(u_get_stdout(), patternSpecification, ap);
michael@0 152 va_end(ap);
michael@0 153 return count;
michael@0 154 }
michael@0 155
michael@0 156 U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
michael@0 157 u_vfprintf( UFILE *f,
michael@0 158 const char *patternSpecification,
michael@0 159 va_list ap)
michael@0 160 {
michael@0 161 int32_t count;
michael@0 162 UChar *pattern;
michael@0 163 UChar buffer[UFMT_DEFAULT_BUFFER_SIZE];
michael@0 164 int32_t size = (int32_t)strlen(patternSpecification) + 1;
michael@0 165
michael@0 166 /* convert from the default codepage to Unicode */
michael@0 167 if (size >= MAX_UCHAR_BUFFER_SIZE(buffer)) {
michael@0 168 pattern = (UChar *)uprv_malloc(size * sizeof(UChar));
michael@0 169 if(pattern == 0) {
michael@0 170 return 0;
michael@0 171 }
michael@0 172 }
michael@0 173 else {
michael@0 174 pattern = buffer;
michael@0 175 }
michael@0 176 u_charsToUChars(patternSpecification, pattern, size);
michael@0 177
michael@0 178 /* do the work */
michael@0 179 count = u_vfprintf_u(f, pattern, ap);
michael@0 180
michael@0 181 /* clean up */
michael@0 182 if (pattern != buffer) {
michael@0 183 uprv_free(pattern);
michael@0 184 }
michael@0 185
michael@0 186 return count;
michael@0 187 }
michael@0 188
michael@0 189 static const u_printf_stream_handler g_stream_handler = {
michael@0 190 u_printf_write,
michael@0 191 u_printf_pad_and_justify
michael@0 192 };
michael@0 193
michael@0 194 U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
michael@0 195 u_vfprintf_u( UFILE *f,
michael@0 196 const UChar *patternSpecification,
michael@0 197 va_list ap)
michael@0 198 {
michael@0 199 int32_t written = 0; /* haven't written anything yet */
michael@0 200
michael@0 201 /* parse and print the whole format string */
michael@0 202 u_printf_parse(&g_stream_handler, patternSpecification, f, NULL, &f->str.fBundle, &written, ap);
michael@0 203
michael@0 204 /* return # of UChars written */
michael@0 205 return written;
michael@0 206 }
michael@0 207
michael@0 208 #endif /* #if !UCONFIG_NO_FORMATTING */
michael@0 209

mercurial