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