2001-02-09 01:45:45 +00:00
|
|
|
/*
|
2003-08-05 07:39:12 +00:00
|
|
|
******************************************************************************
|
2001-02-09 01:45:45 +00:00
|
|
|
*
|
2003-08-07 23:46:25 +00:00
|
|
|
* Copyright (C) 2001-2003, International Business Machines
|
2001-02-09 01:45:45 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
2003-08-05 07:39:12 +00:00
|
|
|
******************************************************************************
|
2001-02-09 01:45:45 +00:00
|
|
|
*
|
|
|
|
* File sprintf.c
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
2002-03-27 19:20:55 +00:00
|
|
|
* Date Name Description
|
2003-08-07 23:46:25 +00:00
|
|
|
* 02/08/2001 george Creation. Copied from uprintf.c
|
2002-03-27 19:20:55 +00:00
|
|
|
* 03/27/2002 Mark Schneckloth Many fixes regarding alignment, null termination
|
|
|
|
* (mschneckloth@atomz.com) and other various problems.
|
2003-08-07 23:46:25 +00:00
|
|
|
* 08/07/2003 george Reunify printf implementations
|
2001-02-09 01:45:45 +00:00
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
2002-10-01 01:26:49 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
2001-02-09 01:45:45 +00:00
|
|
|
#include "unicode/ustdio.h"
|
|
|
|
#include "unicode/ustring.h"
|
|
|
|
|
2004-04-28 22:19:41 +00:00
|
|
|
#include "uprintf.h"
|
2003-07-12 07:21:51 +00:00
|
|
|
#include "locbund.h"
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
#include "cmemory.h"
|
2002-02-23 00:34:39 +00:00
|
|
|
#include <ctype.h>
|
2001-02-09 01:45:45 +00:00
|
|
|
|
2004-04-28 22:19:41 +00:00
|
|
|
typedef struct u_localized_print_string {
|
|
|
|
UChar *str; /* Place to write the string */
|
|
|
|
int32_t available;/* Number of codeunits available to write to */
|
|
|
|
int32_t len; /* Maximum number of code units that can be written to output */
|
|
|
|
|
|
|
|
ULocaleBundle fBundle; /* formatters */
|
|
|
|
} u_localized_print_string;
|
|
|
|
|
2003-08-06 16:18:38 +00:00
|
|
|
/* u_minstrncpy copies the minimum number of code units of (count or output->available) */
|
2001-02-09 01:45:45 +00:00
|
|
|
static int32_t
|
2003-08-06 16:18:38 +00:00
|
|
|
u_sprintf_write(void *context,
|
|
|
|
const UChar *str,
|
|
|
|
int32_t count)
|
|
|
|
{
|
2004-04-16 01:08:35 +00:00
|
|
|
u_localized_print_string *output = (u_localized_print_string *)context;
|
2001-02-09 01:45:45 +00:00
|
|
|
int32_t size = ufmt_min(count, output->available);
|
2002-03-27 19:20:55 +00:00
|
|
|
|
2001-02-09 01:45:45 +00:00
|
|
|
u_strncpy(output->str + (output->len - output->available), str, size);
|
|
|
|
output->available -= size;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t
|
2003-08-06 16:18:38 +00:00
|
|
|
u_sprintf_pad_and_justify(void *context,
|
2003-08-05 07:39:12 +00:00
|
|
|
const u_printf_spec_info *info,
|
2002-02-23 00:34:39 +00:00
|
|
|
const UChar *result,
|
|
|
|
int32_t resultLen)
|
2001-02-09 01:45:45 +00:00
|
|
|
{
|
2004-04-16 01:08:35 +00:00
|
|
|
u_localized_print_string *output = (u_localized_print_string *)context;
|
2002-03-27 19:20:55 +00:00
|
|
|
int32_t written = 0;
|
|
|
|
|
2001-02-09 01:45:45 +00:00
|
|
|
resultLen = ufmt_min(resultLen, output->available);
|
2002-03-27 19:20:55 +00:00
|
|
|
|
2001-02-09 01:45:45 +00:00
|
|
|
/* pad and justify, if needed */
|
|
|
|
if(info->fWidth != -1 && resultLen < info->fWidth) {
|
|
|
|
int32_t paddingLeft = info->fWidth - resultLen;
|
|
|
|
int32_t outputPos = output->len - output->available;
|
2002-03-27 19:20:55 +00:00
|
|
|
|
2001-02-09 01:45:45 +00:00
|
|
|
if (paddingLeft + resultLen > output->available) {
|
|
|
|
paddingLeft = output->available - resultLen;
|
2002-03-27 19:20:55 +00:00
|
|
|
if (paddingLeft < 0) {
|
|
|
|
paddingLeft = 0;
|
|
|
|
}
|
|
|
|
/* paddingLeft = output->available - resultLen;*/
|
2001-02-09 01:45:45 +00:00
|
|
|
}
|
|
|
|
written += paddingLeft;
|
2002-03-27 19:20:55 +00:00
|
|
|
|
2001-02-09 01:45:45 +00:00
|
|
|
/* left justify */
|
|
|
|
if(info->fLeft) {
|
2003-08-06 16:18:38 +00:00
|
|
|
written += u_sprintf_write(output, result, resultLen);
|
2003-08-05 07:39:12 +00:00
|
|
|
u_memset(&output->str[outputPos + resultLen], info->fPadChar, paddingLeft);
|
2002-03-27 19:20:55 +00:00
|
|
|
output->available -= paddingLeft;
|
2001-02-09 01:45:45 +00:00
|
|
|
}
|
|
|
|
/* right justify */
|
|
|
|
else {
|
2003-08-05 07:39:12 +00:00
|
|
|
u_memset(&output->str[outputPos], info->fPadChar, paddingLeft);
|
2002-03-27 19:20:55 +00:00
|
|
|
output->available -= paddingLeft;
|
2003-08-06 16:18:38 +00:00
|
|
|
written += u_sprintf_write(output, result, resultLen);
|
2001-02-09 01:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* just write the formatted output */
|
|
|
|
else {
|
2003-08-06 16:18:38 +00:00
|
|
|
written = u_sprintf_write(output, result, resultLen);
|
2001-02-09 01:45:45 +00:00
|
|
|
}
|
2002-03-27 19:20:55 +00:00
|
|
|
|
2001-02-09 01:45:45 +00:00
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
2003-07-12 07:21:51 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_sprintf(UChar *buffer,
|
|
|
|
const char *patternSpecification,
|
|
|
|
... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int32_t written;
|
|
|
|
|
|
|
|
va_start(ap, patternSpecification);
|
2004-04-28 23:36:51 +00:00
|
|
|
written = u_vsnprintf(buffer, INT32_MAX, patternSpecification, ap);
|
2003-07-12 07:21:51 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_sprintf_u(UChar *buffer,
|
|
|
|
const UChar *patternSpecification,
|
|
|
|
... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int32_t written;
|
|
|
|
|
|
|
|
va_start(ap, patternSpecification);
|
2004-04-28 23:36:51 +00:00
|
|
|
written = u_vsnprintf_u(buffer, INT32_MAX, patternSpecification, ap);
|
2003-07-12 07:21:51 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
|
|
|
u_vsprintf(UChar *buffer,
|
|
|
|
const char *patternSpecification,
|
|
|
|
va_list ap)
|
|
|
|
{
|
2004-04-28 23:36:51 +00:00
|
|
|
return u_vsnprintf(buffer, INT32_MAX, patternSpecification, ap);
|
2003-07-12 07:21:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_snprintf(UChar *buffer,
|
|
|
|
int32_t count,
|
|
|
|
const char *patternSpecification,
|
|
|
|
... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int32_t written;
|
|
|
|
|
|
|
|
va_start(ap, patternSpecification);
|
2004-04-28 23:36:51 +00:00
|
|
|
written = u_vsnprintf(buffer, count, patternSpecification, ap);
|
2003-07-12 07:21:51 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_snprintf_u(UChar *buffer,
|
|
|
|
int32_t count,
|
|
|
|
const UChar *patternSpecification,
|
|
|
|
... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int32_t written;
|
|
|
|
|
|
|
|
va_start(ap, patternSpecification);
|
2004-04-28 23:36:51 +00:00
|
|
|
written = u_vsnprintf_u(buffer, count, patternSpecification, ap);
|
2003-07-12 07:21:51 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
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 *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;
|
|
|
|
}
|
|
|
|
u_charsToUChars(patternSpecification, pattern, size);
|
|
|
|
|
|
|
|
/* do the work */
|
2004-04-28 23:36:51 +00:00
|
|
|
written = u_vsnprintf_u(buffer, count, pattern, ap);
|
2003-07-12 07:21:51 +00:00
|
|
|
|
|
|
|
/* clean up */
|
|
|
|
if (pattern != patBuffer) {
|
|
|
|
uprv_free(pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_vsprintf_u(UChar *buffer,
|
|
|
|
const UChar *patternSpecification,
|
|
|
|
va_list ap)
|
|
|
|
{
|
2004-04-28 23:36:51 +00:00
|
|
|
return u_vsnprintf_u(buffer, INT32_MAX, patternSpecification, ap);
|
2003-07-12 07:21:51 +00:00
|
|
|
}
|
|
|
|
|
2003-08-06 16:18:38 +00:00
|
|
|
static const u_printf_stream_handler g_sprintf_stream_handler = {
|
|
|
|
u_sprintf_write,
|
|
|
|
u_sprintf_pad_and_justify
|
|
|
|
};
|
|
|
|
|
2001-11-21 01:22:16 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
2001-02-09 01:45:45 +00:00
|
|
|
u_vsnprintf_u(UChar *buffer,
|
2002-02-23 00:34:39 +00:00
|
|
|
int32_t count,
|
|
|
|
const UChar *patternSpecification,
|
|
|
|
va_list ap)
|
2001-02-09 01:45:45 +00:00
|
|
|
{
|
2003-08-05 07:39:12 +00:00
|
|
|
const UChar *alias = patternSpecification; /* alias the pattern */
|
|
|
|
const UChar *lastAlias;
|
|
|
|
int32_t patCount;
|
|
|
|
int32_t written = 0; /* haven't written anything yet */
|
2002-03-27 19:20:55 +00:00
|
|
|
|
2004-04-16 01:08:35 +00:00
|
|
|
u_localized_print_string outStr;
|
2002-03-27 19:20:55 +00:00
|
|
|
|
2002-02-23 00:34:39 +00:00
|
|
|
if (count < 0) {
|
|
|
|
count = INT32_MAX;
|
|
|
|
}
|
2002-03-27 19:20:55 +00:00
|
|
|
|
2002-02-23 00:34:39 +00:00
|
|
|
outStr.str = buffer;
|
|
|
|
outStr.len = count;
|
|
|
|
outStr.available = count;
|
2002-03-27 19:20:55 +00:00
|
|
|
|
2004-04-28 23:36:51 +00:00
|
|
|
if(u_locbund_init(&outStr.fBundle, "en_US_POSIX") == 0) {
|
2002-02-23 00:34:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-03-27 19:20:55 +00:00
|
|
|
|
2002-02-23 00:34:39 +00:00
|
|
|
/* iterate through the pattern */
|
|
|
|
while(outStr.available > 0) {
|
2002-03-27 19:20:55 +00:00
|
|
|
|
2002-02-23 00:34:39 +00:00
|
|
|
/* find the next '%' */
|
|
|
|
lastAlias = alias;
|
|
|
|
while(*alias != UP_PERCENT && *alias != 0x0000) {
|
|
|
|
alias++;
|
|
|
|
}
|
2002-03-27 19:20:55 +00:00
|
|
|
|
2002-02-23 00:34:39 +00:00
|
|
|
/* write any characters before the '%' */
|
|
|
|
if(alias > lastAlias) {
|
2003-08-06 16:18:38 +00:00
|
|
|
written += (*g_sprintf_stream_handler.write)(&outStr, lastAlias, (int32_t)(alias - lastAlias));
|
2002-02-23 00:34:39 +00:00
|
|
|
}
|
2002-03-27 19:20:55 +00:00
|
|
|
|
2002-02-23 00:34:39 +00:00
|
|
|
/* break if at end of string */
|
|
|
|
if(*alias == 0x0000) {
|
2001-02-09 01:45:45 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-03-27 19:20:55 +00:00
|
|
|
|
2003-08-07 23:46:25 +00:00
|
|
|
/* parse and print the specifier */
|
2003-11-08 01:44:43 +00:00
|
|
|
patCount = u_printf_print_spec(&g_sprintf_stream_handler, alias, &outStr, &outStr.fBundle, (int32_t)(alias - lastAlias), &written, (va_list*)&ap);
|
2002-03-27 19:20:55 +00:00
|
|
|
|
2002-02-23 00:34:39 +00:00
|
|
|
/* update the pointer in pattern and continue */
|
|
|
|
alias += patCount;
|
2001-02-09 01:45:45 +00:00
|
|
|
}
|
2002-03-27 19:20:55 +00:00
|
|
|
|
2002-03-28 21:34:57 +00:00
|
|
|
/* Terminate the buffer, if there's room. */
|
2002-03-27 19:20:55 +00:00
|
|
|
if (outStr.available > 0) {
|
|
|
|
buffer[outStr.len - outStr.available] = 0x0000;
|
|
|
|
}
|
|
|
|
|
2002-03-26 20:10:19 +00:00
|
|
|
/* Release the cloned bundle, if we cloned it. */
|
2003-08-06 16:18:38 +00:00
|
|
|
u_locbund_close(&outStr.fBundle);
|
2002-03-26 05:33:56 +00:00
|
|
|
|
2002-02-23 00:34:39 +00:00
|
|
|
/* return # of UChars written */
|
|
|
|
return written;
|
2001-02-09 01:45:45 +00:00
|
|
|
}
|
|
|
|
|
2002-10-01 01:26:49 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|
2003-08-06 16:18:38 +00:00
|
|
|
|