U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ u_vsnprintf(UChar *buffer, int32_t count, const char *locale, const char *patternSpecification, va_list ap) { int32_t written; UChar *pattern; UChar patBuffer[UFMT_DEFAULT_BUFFER_SIZE]; int32_t size = (int32_t)strlen(patternSpecification) + 1; /* convert from the default codepage to Unicode */ if (size >= MAX_UCHAR_BUFFER_SIZE(patBuffer)) { pattern = (UChar *)uprv_malloc(size * sizeof(UChar)); if(pattern == 0) { return 0; } } else { pattern = patBuffer; } ufmt_defaultCPToUnicode(patternSpecification, size, pattern, size); /* do the work */ written = u_vsnprintf_u(buffer, count, locale, pattern, ap); /* clean up */ if (pattern != patBuffer) { uprv_free(pattern); } return written; }
U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ u_vfprintf( UFILE *f, const char *patternSpecification, va_list ap) { int32_t count; UChar *pattern; UChar buffer[UFMT_DEFAULT_BUFFER_SIZE]; int32_t size = (int32_t)strlen(patternSpecification) + 1; /* convert from the default codepage to Unicode */ if (size >= MAX_UCHAR_BUFFER_SIZE(buffer)) { pattern = (UChar *)uprv_malloc(size * sizeof(UChar)); if(pattern == 0) { return 0; } } else { pattern = buffer; } u_charsToUChars(patternSpecification, pattern, size); /* do the work */ count = u_vfprintf_u(f, pattern, ap); /* clean up */ if (pattern != buffer) { uprv_free(pattern); } return count; }
int32_t u_sprintf_string_handler(u_localized_string *output, const u_sprintf_spec_info *info, const ufmt_args *args) { UChar *s; UChar buffer[UFMT_DEFAULT_BUFFER_SIZE]; int32_t len, written; int32_t argSize; const char *arg = (const char*)(args[0].ptrValue); /* convert from the default codepage to Unicode */ if (arg) { argSize = (int32_t)strlen(arg) + 1; if (argSize >= MAX_UCHAR_BUFFER_SIZE(buffer)) { s = ufmt_defaultCPToUnicode(arg, argSize, (UChar *)uprv_malloc(MAX_UCHAR_BUFFER_NEEDED(argSize)), MAX_UCHAR_BUFFER_NEEDED(argSize)); if(s == NULL) { return 0; } } else { s = ufmt_defaultCPToUnicode(arg, argSize, buffer, sizeof(buffer)/sizeof(UChar)); } } else { s = (UChar *)gNullStr; } len = u_strlen(s); /* width = minimum # of characters to write */ /* precision = maximum # of characters to write */ /* precision takes precedence over width */ /* determine if the string should be truncated */ if(info->fPrecision != -1 && len > info->fPrecision) { written = u_minstrncpy(output, s, info->fPrecision); } /* determine if the string should be padded */ else { written = u_sprintf_pad_and_justify(output, info, s, len); } /* clean up */ if (gNullStr != s && buffer != s) { uprv_free(s); } return written; }
/* handle 's' */ static int32_t u_printf_string_handler(const u_printf_stream_handler *handler, void *context, ULocaleBundle *formatBundle, const u_printf_spec_info *info, const ufmt_args *args) { UChar *s; UChar buffer[UFMT_DEFAULT_BUFFER_SIZE]; int32_t len, written; int32_t argSize; const char *arg = (const char*)(args[0].ptrValue); /* convert from the default codepage to Unicode */ if (arg) { argSize = (int32_t)strlen(arg) + 1; if (argSize >= MAX_UCHAR_BUFFER_SIZE(buffer)) { s = ufmt_defaultCPToUnicode(arg, argSize, (UChar *)uprv_malloc(MAX_UCHAR_BUFFER_NEEDED(argSize)), MAX_UCHAR_BUFFER_NEEDED(argSize)); if(s == NULL) { return 0; } } else { s = ufmt_defaultCPToUnicode(arg, argSize, buffer, sizeof(buffer)/sizeof(UChar)); } } else { s = (UChar *)gNullStr; } len = u_strlen(s); /* width = minimum # of characters to write */ /* precision = maximum # of characters to write */ if (info->fPrecision != -1 && info->fPrecision < len) { len = info->fPrecision; } written = handler->pad_and_justify(context, info, s, len); /* clean up */ if (gNullStr != s && buffer != s) { uprv_free(s); } return written; }