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; }
int32_t u_sprintf_char_handler(u_localized_string *output, const u_sprintf_spec_info *info, const ufmt_args *args) { UChar s[UTF_MAX_CHAR_LENGTH+1]; int32_t len, written; unsigned char arg = (unsigned char)(args[0].intValue); /* convert from default codepage to Unicode */ ufmt_defaultCPToUnicode((const char *)&arg, 2, s, sizeof(s)/sizeof(UChar)); /* Remember that this may be a surrogate pair */ 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); } else { /* determine if the string should be padded */ written = u_sprintf_pad_and_justify(output, info, s, len); } return written; }
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; }
static int32_t u_printf_char_handler(const u_printf_stream_handler *handler, void *context, ULocaleBundle *formatBundle, const u_printf_spec_info *info, const ufmt_args *args) { UChar s[UTF_MAX_CHAR_LENGTH+1]; int32_t len = 1, written; unsigned char arg = (unsigned char)(args[0].int64Value); /* convert from default codepage to Unicode */ ufmt_defaultCPToUnicode((const char *)&arg, 2, s, sizeof(s)/sizeof(UChar)); /* Remember that this may be an MBCS character */ if (arg != 0) { len = u_strlen(s); } /* width = minimum # of characters to write */ /* precision = maximum # of characters to write */ /* precision is ignored when handling a char */ written = handler->pad_and_justify(context, info, s, len); return written; }