michael@0: /* michael@0: ****************************************************************************** michael@0: * michael@0: * Copyright (C) 2001-2008, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ****************************************************************************** michael@0: * michael@0: * File sprintf.c michael@0: * michael@0: * Modification History: michael@0: * michael@0: * Date Name Description michael@0: * 02/08/2001 george Creation. Copied from uprintf.c michael@0: * 03/27/2002 Mark Schneckloth Many fixes regarding alignment, null termination michael@0: * (mschneckloth@atomz.com) and other various problems. michael@0: * 08/07/2003 george Reunify printf implementations michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/ustdio.h" michael@0: #include "unicode/ustring.h" michael@0: #include "unicode/putil.h" michael@0: michael@0: #include "uprintf.h" michael@0: #include "locbund.h" michael@0: michael@0: #include "cmemory.h" michael@0: #include michael@0: michael@0: /* u_minstrncpy copies the minimum number of code units of (count or output->available) */ michael@0: static int32_t michael@0: u_sprintf_write(void *context, michael@0: const UChar *str, michael@0: int32_t count) michael@0: { michael@0: u_localized_print_string *output = (u_localized_print_string *)context; michael@0: int32_t size = ufmt_min(count, output->available); michael@0: michael@0: u_strncpy(output->str + (output->len - output->available), str, size); michael@0: output->available -= size; michael@0: return size; michael@0: } michael@0: michael@0: static int32_t michael@0: u_sprintf_pad_and_justify(void *context, michael@0: const u_printf_spec_info *info, michael@0: const UChar *result, michael@0: int32_t resultLen) michael@0: { michael@0: u_localized_print_string *output = (u_localized_print_string *)context; michael@0: int32_t written = 0; michael@0: int32_t lengthOfResult = resultLen; michael@0: michael@0: resultLen = ufmt_min(resultLen, output->available); michael@0: michael@0: /* pad and justify, if needed */ michael@0: if(info->fWidth != -1 && resultLen < info->fWidth) { michael@0: int32_t paddingLeft = info->fWidth - resultLen; michael@0: int32_t outputPos = output->len - output->available; michael@0: michael@0: if (paddingLeft + resultLen > output->available) { michael@0: paddingLeft = output->available - resultLen; michael@0: if (paddingLeft < 0) { michael@0: paddingLeft = 0; michael@0: } michael@0: /* paddingLeft = output->available - resultLen;*/ michael@0: } michael@0: written += paddingLeft; michael@0: michael@0: /* left justify */ michael@0: if(info->fLeft) { michael@0: written += u_sprintf_write(output, result, resultLen); michael@0: u_memset(&output->str[outputPos + resultLen], info->fPadChar, paddingLeft); michael@0: output->available -= paddingLeft; michael@0: } michael@0: /* right justify */ michael@0: else { michael@0: u_memset(&output->str[outputPos], info->fPadChar, paddingLeft); michael@0: output->available -= paddingLeft; michael@0: written += u_sprintf_write(output, result, resultLen); michael@0: } michael@0: } michael@0: /* just write the formatted output */ michael@0: else { michael@0: written = u_sprintf_write(output, result, resultLen); michael@0: } michael@0: michael@0: if (written >= 0 && lengthOfResult > written) { michael@0: return lengthOfResult; michael@0: } michael@0: michael@0: return written; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: u_sprintf(UChar *buffer, michael@0: const char *patternSpecification, michael@0: ... ) michael@0: { michael@0: va_list ap; michael@0: int32_t written; michael@0: michael@0: va_start(ap, patternSpecification); michael@0: written = u_vsnprintf(buffer, INT32_MAX, patternSpecification, ap); michael@0: va_end(ap); michael@0: michael@0: return written; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: u_sprintf_u(UChar *buffer, michael@0: const UChar *patternSpecification, michael@0: ... ) michael@0: { michael@0: va_list ap; michael@0: int32_t written; michael@0: michael@0: va_start(ap, patternSpecification); michael@0: written = u_vsnprintf_u(buffer, INT32_MAX, patternSpecification, ap); michael@0: va_end(ap); michael@0: michael@0: return written; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ michael@0: u_vsprintf(UChar *buffer, michael@0: const char *patternSpecification, michael@0: va_list ap) michael@0: { michael@0: return u_vsnprintf(buffer, INT32_MAX, patternSpecification, ap); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: u_snprintf(UChar *buffer, michael@0: int32_t count, michael@0: const char *patternSpecification, michael@0: ... ) michael@0: { michael@0: va_list ap; michael@0: int32_t written; michael@0: michael@0: va_start(ap, patternSpecification); michael@0: written = u_vsnprintf(buffer, count, patternSpecification, ap); michael@0: va_end(ap); michael@0: michael@0: return written; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: u_snprintf_u(UChar *buffer, michael@0: int32_t count, michael@0: const UChar *patternSpecification, michael@0: ... ) michael@0: { michael@0: va_list ap; michael@0: int32_t written; michael@0: michael@0: va_start(ap, patternSpecification); michael@0: written = u_vsnprintf_u(buffer, count, patternSpecification, ap); michael@0: va_end(ap); michael@0: michael@0: return written; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ michael@0: u_vsnprintf(UChar *buffer, michael@0: int32_t count, michael@0: const char *patternSpecification, michael@0: va_list ap) michael@0: { michael@0: int32_t written; michael@0: UChar *pattern; michael@0: UChar patBuffer[UFMT_DEFAULT_BUFFER_SIZE]; michael@0: int32_t size = (int32_t)strlen(patternSpecification) + 1; michael@0: michael@0: /* convert from the default codepage to Unicode */ michael@0: if (size >= MAX_UCHAR_BUFFER_SIZE(patBuffer)) { michael@0: pattern = (UChar *)uprv_malloc(size * sizeof(UChar)); michael@0: if(pattern == 0) { michael@0: return 0; michael@0: } michael@0: } michael@0: else { michael@0: pattern = patBuffer; michael@0: } michael@0: u_charsToUChars(patternSpecification, pattern, size); michael@0: michael@0: /* do the work */ michael@0: written = u_vsnprintf_u(buffer, count, pattern, ap); michael@0: michael@0: /* clean up */ michael@0: if (pattern != patBuffer) { michael@0: uprv_free(pattern); michael@0: } michael@0: michael@0: return written; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: u_vsprintf_u(UChar *buffer, michael@0: const UChar *patternSpecification, michael@0: va_list ap) michael@0: { michael@0: return u_vsnprintf_u(buffer, INT32_MAX, patternSpecification, ap); michael@0: } michael@0: michael@0: static const u_printf_stream_handler g_sprintf_stream_handler = { michael@0: u_sprintf_write, michael@0: u_sprintf_pad_and_justify michael@0: }; michael@0: michael@0: U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ michael@0: u_vsnprintf_u(UChar *buffer, michael@0: int32_t count, michael@0: const UChar *patternSpecification, michael@0: va_list ap) michael@0: { michael@0: int32_t written = 0; /* haven't written anything yet */ michael@0: int32_t result = 0; /* test the return value of u_printf_parse */ michael@0: michael@0: u_localized_print_string outStr; michael@0: michael@0: if (count < 0) { michael@0: count = INT32_MAX; michael@0: } michael@0: michael@0: outStr.str = buffer; michael@0: outStr.len = count; michael@0: outStr.available = count; michael@0: michael@0: if(u_locbund_init(&outStr.fBundle, "en_US_POSIX") == 0) { michael@0: return 0; michael@0: } michael@0: michael@0: /* parse and print the whole format string */ michael@0: result = u_printf_parse(&g_sprintf_stream_handler, patternSpecification, &outStr, &outStr, &outStr.fBundle, &written, ap); michael@0: michael@0: /* Terminate the buffer, if there's room. */ michael@0: if (outStr.available > 0) { michael@0: buffer[outStr.len - outStr.available] = 0x0000; michael@0: } michael@0: michael@0: /* Release the cloned bundle, if we cloned it. */ michael@0: u_locbund_close(&outStr.fBundle); michael@0: michael@0: /* parsing error */ michael@0: if (result < 0) { michael@0: return result; michael@0: } michael@0: /* return # of UChars written */ michael@0: return written; michael@0: } michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: