2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
1999-08-16 21:50:52 +00:00
|
|
|
/*
|
2001-03-21 22:07:51 +00:00
|
|
|
******************************************************************************
|
2000-01-13 23:54:23 +00:00
|
|
|
*
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 1998-2014, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
2000-01-13 23:54:23 +00:00
|
|
|
*
|
2001-03-21 22:07:51 +00:00
|
|
|
******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
2014-05-06 21:52:26 +00:00
|
|
|
* File uprintf.cpp
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
2003-08-07 23:46:25 +00:00
|
|
|
* 11/19/98 stephen Creation.
|
1999-08-16 21:50:52 +00:00
|
|
|
* 03/12/99 stephen Modified for new C API.
|
|
|
|
* Added conversion from default codepage.
|
2003-08-07 23:46:25 +00:00
|
|
|
* 08/07/2003 george Reunify printf implementations
|
2001-03-21 22:07:51 +00:00
|
|
|
******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
|
|
|
|
2000-04-06 23:06:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
2002-10-01 01:26:49 +00:00
|
|
|
|
2014-09-05 21:27:11 +00:00
|
|
|
#if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION
|
2002-10-01 01:26:49 +00:00
|
|
|
|
2000-01-05 19:40:01 +00:00
|
|
|
#include "unicode/ustdio.h"
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "unicode/unum.h"
|
|
|
|
#include "unicode/udat.h"
|
2004-10-18 03:03:13 +00:00
|
|
|
#include "unicode/putil.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2014-05-06 21:52:26 +00:00
|
|
|
#include "cmemory.h"
|
|
|
|
#include "locbund.h"
|
|
|
|
#include "mutex.h"
|
|
|
|
#include "uassert.h"
|
2003-07-12 07:21:51 +00:00
|
|
|
#include "uprintf.h"
|
|
|
|
#include "ufile.h"
|
2011-09-16 17:44:06 +00:00
|
|
|
#include "ucln_io.h"
|
2001-02-06 20:39:23 +00:00
|
|
|
|
2014-05-06 21:52:26 +00:00
|
|
|
U_NAMESPACE_USE
|
2001-02-06 20:39:23 +00:00
|
|
|
|
2011-10-04 18:15:16 +00:00
|
|
|
static UFILE *gStdOut = NULL;
|
2014-05-06 21:52:26 +00:00
|
|
|
static UInitOnce gStdOutInitOnce = U_INITONCE_INITIALIZER;
|
2011-09-16 17:44:06 +00:00
|
|
|
|
2012-08-05 16:33:16 +00:00
|
|
|
static UBool U_CALLCONV uprintf_cleanup(void)
|
2011-09-16 17:44:06 +00:00
|
|
|
{
|
|
|
|
if (gStdOut != NULL) {
|
|
|
|
u_fclose(gStdOut);
|
|
|
|
gStdOut = NULL;
|
|
|
|
}
|
2014-05-06 21:52:26 +00:00
|
|
|
gStdOutInitOnce.reset();
|
2011-09-18 23:54:02 +00:00
|
|
|
return TRUE;
|
2011-09-16 17:44:06 +00:00
|
|
|
}
|
|
|
|
|
2014-05-06 21:52:26 +00:00
|
|
|
static void U_CALLCONV u_stdout_init() {
|
|
|
|
U_ASSERT(gStdOut == NULL);
|
|
|
|
gStdOut = u_finit(stdout, NULL, NULL);
|
|
|
|
ucln_io_registerCleanup(UCLN_IO_PRINTF, &uprintf_cleanup);
|
|
|
|
}
|
|
|
|
|
2011-09-19 17:58:14 +00:00
|
|
|
U_CAPI UFILE * U_EXPORT2
|
2011-09-16 17:44:06 +00:00
|
|
|
u_get_stdout()
|
|
|
|
{
|
2014-05-06 21:52:26 +00:00
|
|
|
umtx_initOnce(gStdOutInitOnce, &u_stdout_init);
|
2011-09-16 17:44:06 +00:00
|
|
|
return gStdOut;
|
|
|
|
}
|
|
|
|
|
2003-08-06 16:18:38 +00:00
|
|
|
static int32_t U_EXPORT2
|
|
|
|
u_printf_write(void *context,
|
|
|
|
const UChar *str,
|
|
|
|
int32_t count)
|
|
|
|
{
|
|
|
|
return u_file_write(str, count, (UFILE *)context);
|
|
|
|
}
|
|
|
|
|
2001-02-09 01:45:45 +00:00
|
|
|
static int32_t
|
2003-08-06 16:18:38 +00:00
|
|
|
u_printf_pad_and_justify(void *context,
|
2002-02-28 01:42:40 +00:00
|
|
|
const u_printf_spec_info *info,
|
|
|
|
const UChar *result,
|
|
|
|
int32_t resultLen)
|
2001-02-09 01:45:45 +00:00
|
|
|
{
|
2003-08-06 16:18:38 +00:00
|
|
|
UFILE *output = (UFILE *)context;
|
2002-02-28 01:42:40 +00:00
|
|
|
int32_t written, i;
|
|
|
|
|
|
|
|
/* pad and justify, if needed */
|
|
|
|
if(info->fWidth != -1 && resultLen < info->fWidth) {
|
|
|
|
/* left justify */
|
|
|
|
if(info->fLeft) {
|
2003-08-05 07:39:12 +00:00
|
|
|
written = u_file_write(result, resultLen, output);
|
2002-02-28 01:42:40 +00:00
|
|
|
for(i = 0; i < info->fWidth - resultLen; ++i) {
|
2003-08-05 07:39:12 +00:00
|
|
|
written += u_file_write(&info->fPadChar, 1, output);
|
2002-02-28 01:42:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* right justify */
|
|
|
|
else {
|
|
|
|
written = 0;
|
|
|
|
for(i = 0; i < info->fWidth - resultLen; ++i) {
|
2003-08-05 07:39:12 +00:00
|
|
|
written += u_file_write(&info->fPadChar, 1, output);
|
2002-02-28 01:42:40 +00:00
|
|
|
}
|
2003-08-05 07:39:12 +00:00
|
|
|
written += u_file_write(result, resultLen, output);
|
2002-02-28 01:42:40 +00:00
|
|
|
}
|
2001-02-09 01:45:45 +00:00
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
/* just write the formatted output */
|
2003-08-05 07:39:12 +00:00
|
|
|
else {
|
|
|
|
written = u_file_write(result, resultLen, output);
|
|
|
|
}
|
2001-02-09 01:45:45 +00:00
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
return written;
|
2001-02-09 01:45:45 +00:00
|
|
|
}
|
|
|
|
|
2003-07-12 07:21:51 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_fprintf( UFILE *f,
|
|
|
|
const char *patternSpecification,
|
|
|
|
... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int32_t count;
|
|
|
|
|
|
|
|
va_start(ap, patternSpecification);
|
|
|
|
count = u_vfprintf(f, patternSpecification, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:44:06 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_printf(const char *patternSpecification,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int32_t count;
|
|
|
|
va_start(ap, patternSpecification);
|
|
|
|
count = u_vfprintf(u_get_stdout(), patternSpecification, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2003-07-12 07:21:51 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_fprintf_u( UFILE *f,
|
|
|
|
const UChar *patternSpecification,
|
|
|
|
... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int32_t count;
|
|
|
|
|
|
|
|
va_start(ap, patternSpecification);
|
|
|
|
count = u_vfprintf_u(f, patternSpecification, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2011-09-19 17:58:14 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_printf_u(const UChar *patternSpecification,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int32_t count;
|
|
|
|
va_start(ap, patternSpecification);
|
|
|
|
count = u_vfprintf_u(u_get_stdout(), patternSpecification, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2003-07-12 07:21:51 +00:00
|
|
|
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];
|
2014-05-06 21:52:26 +00:00
|
|
|
size_t size = strlen(patternSpecification) + 1;
|
2003-07-12 07:21:51 +00:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
2018-09-19 17:05:27 +00:00
|
|
|
u_charsToUChars(patternSpecification, pattern, static_cast<int32_t>(size));
|
2003-07-12 07:21:51 +00:00
|
|
|
|
|
|
|
/* do the work */
|
|
|
|
count = u_vfprintf_u(f, pattern, ap);
|
|
|
|
|
|
|
|
/* clean up */
|
|
|
|
if (pattern != buffer) {
|
|
|
|
uprv_free(pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2003-08-06 16:18:38 +00:00
|
|
|
static const u_printf_stream_handler g_stream_handler = {
|
|
|
|
u_printf_write,
|
|
|
|
u_printf_pad_and_justify
|
|
|
|
};
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
|
|
|
|
u_vfprintf_u( UFILE *f,
|
|
|
|
const UChar *patternSpecification,
|
|
|
|
va_list ap)
|
|
|
|
{
|
2003-08-05 07:39:12 +00:00
|
|
|
int32_t written = 0; /* haven't written anything yet */
|
2001-02-06 20:39:23 +00:00
|
|
|
|
2004-05-25 16:59:00 +00:00
|
|
|
/* parse and print the whole format string */
|
|
|
|
u_printf_parse(&g_stream_handler, patternSpecification, f, NULL, &f->str.fBundle, &written, ap);
|
2002-02-23 00:34:39 +00:00
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
/* return # of UChars written */
|
|
|
|
return written;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2002-10-01 01:26:49 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|
2003-08-06 16:18:38 +00:00
|
|
|
|