1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/io/uprintf.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,209 @@ 1.4 +/* 1.5 +****************************************************************************** 1.6 +* 1.7 +* Copyright (C) 1998-2012, International Business Machines 1.8 +* Corporation and others. All Rights Reserved. 1.9 +* 1.10 +****************************************************************************** 1.11 +* 1.12 +* File uprintf.c 1.13 +* 1.14 +* Modification History: 1.15 +* 1.16 +* Date Name Description 1.17 +* 11/19/98 stephen Creation. 1.18 +* 03/12/99 stephen Modified for new C API. 1.19 +* Added conversion from default codepage. 1.20 +* 08/07/2003 george Reunify printf implementations 1.21 +****************************************************************************** 1.22 +*/ 1.23 + 1.24 +#include "unicode/utypes.h" 1.25 + 1.26 +#if !UCONFIG_NO_FORMATTING 1.27 + 1.28 +#include "unicode/ustdio.h" 1.29 +#include "unicode/ustring.h" 1.30 +#include "unicode/unum.h" 1.31 +#include "unicode/udat.h" 1.32 +#include "unicode/putil.h" 1.33 + 1.34 +#include "uprintf.h" 1.35 +#include "ufile.h" 1.36 +#include "ucln_io.h" 1.37 +#include "locbund.h" 1.38 + 1.39 +#include "cmemory.h" 1.40 + 1.41 +static UFILE *gStdOut = NULL; 1.42 + 1.43 +static UBool U_CALLCONV uprintf_cleanup(void) 1.44 +{ 1.45 + if (gStdOut != NULL) { 1.46 + u_fclose(gStdOut); 1.47 + gStdOut = NULL; 1.48 + } 1.49 + return TRUE; 1.50 +} 1.51 + 1.52 +U_CAPI UFILE * U_EXPORT2 1.53 +u_get_stdout() 1.54 +{ 1.55 + if (gStdOut == NULL) { 1.56 + gStdOut = u_finit(stdout, NULL, NULL); 1.57 + ucln_io_registerCleanup(UCLN_IO_PRINTF, &uprintf_cleanup); 1.58 + } 1.59 + return gStdOut; 1.60 +} 1.61 + 1.62 +static int32_t U_EXPORT2 1.63 +u_printf_write(void *context, 1.64 + const UChar *str, 1.65 + int32_t count) 1.66 +{ 1.67 + return u_file_write(str, count, (UFILE *)context); 1.68 +} 1.69 + 1.70 +static int32_t 1.71 +u_printf_pad_and_justify(void *context, 1.72 + const u_printf_spec_info *info, 1.73 + const UChar *result, 1.74 + int32_t resultLen) 1.75 +{ 1.76 + UFILE *output = (UFILE *)context; 1.77 + int32_t written, i; 1.78 + 1.79 + /* pad and justify, if needed */ 1.80 + if(info->fWidth != -1 && resultLen < info->fWidth) { 1.81 + /* left justify */ 1.82 + if(info->fLeft) { 1.83 + written = u_file_write(result, resultLen, output); 1.84 + for(i = 0; i < info->fWidth - resultLen; ++i) { 1.85 + written += u_file_write(&info->fPadChar, 1, output); 1.86 + } 1.87 + } 1.88 + /* right justify */ 1.89 + else { 1.90 + written = 0; 1.91 + for(i = 0; i < info->fWidth - resultLen; ++i) { 1.92 + written += u_file_write(&info->fPadChar, 1, output); 1.93 + } 1.94 + written += u_file_write(result, resultLen, output); 1.95 + } 1.96 + } 1.97 + /* just write the formatted output */ 1.98 + else { 1.99 + written = u_file_write(result, resultLen, output); 1.100 + } 1.101 + 1.102 + return written; 1.103 +} 1.104 + 1.105 +U_CAPI int32_t U_EXPORT2 1.106 +u_fprintf( UFILE *f, 1.107 + const char *patternSpecification, 1.108 + ... ) 1.109 +{ 1.110 + va_list ap; 1.111 + int32_t count; 1.112 + 1.113 + va_start(ap, patternSpecification); 1.114 + count = u_vfprintf(f, patternSpecification, ap); 1.115 + va_end(ap); 1.116 + 1.117 + return count; 1.118 +} 1.119 + 1.120 +U_CAPI int32_t U_EXPORT2 1.121 +u_printf(const char *patternSpecification, 1.122 + ...) 1.123 +{ 1.124 + va_list ap; 1.125 + int32_t count; 1.126 + va_start(ap, patternSpecification); 1.127 + count = u_vfprintf(u_get_stdout(), patternSpecification, ap); 1.128 + va_end(ap); 1.129 + return count; 1.130 +} 1.131 + 1.132 +U_CAPI int32_t U_EXPORT2 1.133 +u_fprintf_u( UFILE *f, 1.134 + const UChar *patternSpecification, 1.135 + ... ) 1.136 +{ 1.137 + va_list ap; 1.138 + int32_t count; 1.139 + 1.140 + va_start(ap, patternSpecification); 1.141 + count = u_vfprintf_u(f, patternSpecification, ap); 1.142 + va_end(ap); 1.143 + 1.144 + return count; 1.145 +} 1.146 + 1.147 +U_CAPI int32_t U_EXPORT2 1.148 +u_printf_u(const UChar *patternSpecification, 1.149 + ...) 1.150 +{ 1.151 + va_list ap; 1.152 + int32_t count; 1.153 + va_start(ap, patternSpecification); 1.154 + count = u_vfprintf_u(u_get_stdout(), patternSpecification, ap); 1.155 + va_end(ap); 1.156 + return count; 1.157 +} 1.158 + 1.159 +U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ 1.160 +u_vfprintf( UFILE *f, 1.161 + const char *patternSpecification, 1.162 + va_list ap) 1.163 +{ 1.164 + int32_t count; 1.165 + UChar *pattern; 1.166 + UChar buffer[UFMT_DEFAULT_BUFFER_SIZE]; 1.167 + int32_t size = (int32_t)strlen(patternSpecification) + 1; 1.168 + 1.169 + /* convert from the default codepage to Unicode */ 1.170 + if (size >= MAX_UCHAR_BUFFER_SIZE(buffer)) { 1.171 + pattern = (UChar *)uprv_malloc(size * sizeof(UChar)); 1.172 + if(pattern == 0) { 1.173 + return 0; 1.174 + } 1.175 + } 1.176 + else { 1.177 + pattern = buffer; 1.178 + } 1.179 + u_charsToUChars(patternSpecification, pattern, size); 1.180 + 1.181 + /* do the work */ 1.182 + count = u_vfprintf_u(f, pattern, ap); 1.183 + 1.184 + /* clean up */ 1.185 + if (pattern != buffer) { 1.186 + uprv_free(pattern); 1.187 + } 1.188 + 1.189 + return count; 1.190 +} 1.191 + 1.192 +static const u_printf_stream_handler g_stream_handler = { 1.193 + u_printf_write, 1.194 + u_printf_pad_and_justify 1.195 +}; 1.196 + 1.197 +U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ 1.198 +u_vfprintf_u( UFILE *f, 1.199 + const UChar *patternSpecification, 1.200 + va_list ap) 1.201 +{ 1.202 + int32_t written = 0; /* haven't written anything yet */ 1.203 + 1.204 + /* parse and print the whole format string */ 1.205 + u_printf_parse(&g_stream_handler, patternSpecification, f, NULL, &f->str.fBundle, &written, ap); 1.206 + 1.207 + /* return # of UChars written */ 1.208 + return written; 1.209 +} 1.210 + 1.211 +#endif /* #if !UCONFIG_NO_FORMATTING */ 1.212 +