2011-11-03 14:56:26 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2019-06-06 05:28:23 +00:00
|
|
|
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
|
2011-11-03 14:56:26 +00:00
|
|
|
|
2014-11-19 14:42:24 +00:00
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
2011-11-03 14:56:26 +00:00
|
|
|
|
2014-11-19 14:42:24 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2011-11-03 14:56:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <qse/cmn/fmt.h>
|
2011-11-07 00:01:19 +00:00
|
|
|
#include <qse/cmn/str.h>
|
2011-11-03 14:56:26 +00:00
|
|
|
|
2013-10-15 16:35:23 +00:00
|
|
|
#undef T
|
|
|
|
#undef char_t
|
|
|
|
#undef fmt_uintmax
|
|
|
|
#undef strlen
|
|
|
|
|
|
|
|
#define T(x) QSE_MT(x)
|
|
|
|
#define char_t qse_mchar_t
|
|
|
|
#define fmt_uintmax fmt_unsigned_to_mbs
|
|
|
|
#define strlen(x) qse_mbslen(x)
|
2013-10-19 03:39:10 +00:00
|
|
|
#include "fmt-intmax.h"
|
2013-10-15 16:35:23 +00:00
|
|
|
|
|
|
|
#undef T
|
|
|
|
#undef char_t
|
|
|
|
#undef fmt_uintmax
|
|
|
|
#undef strlen
|
|
|
|
|
|
|
|
#define T(x) QSE_WT(x)
|
|
|
|
#define char_t qse_wchar_t
|
|
|
|
#define fmt_uintmax fmt_unsigned_to_wcs
|
|
|
|
#define strlen(x) qse_wcslen(x)
|
2013-10-19 03:39:10 +00:00
|
|
|
#include "fmt-intmax.h"
|
2011-11-15 15:58:32 +00:00
|
|
|
|
2013-10-15 16:35:23 +00:00
|
|
|
/* ==================== multibyte ===================================== */
|
2011-11-03 14:56:26 +00:00
|
|
|
|
2011-11-05 00:50:55 +00:00
|
|
|
int qse_fmtintmaxtombs (
|
|
|
|
qse_mchar_t* buf, int size,
|
2011-11-14 15:15:44 +00:00
|
|
|
qse_intmax_t value, int base_and_flags, int prec,
|
2011-11-07 00:01:19 +00:00
|
|
|
qse_mchar_t fillchar, const qse_mchar_t* prefix)
|
2011-11-05 00:50:55 +00:00
|
|
|
{
|
|
|
|
qse_mchar_t signchar;
|
|
|
|
qse_uintmax_t absvalue;
|
|
|
|
|
|
|
|
if (value < 0)
|
|
|
|
{
|
|
|
|
signchar = QSE_MT('-');
|
|
|
|
absvalue = -value;
|
|
|
|
}
|
|
|
|
else if (base_and_flags & QSE_FMTINTMAXTOMBS_PLUSSIGN)
|
|
|
|
{
|
|
|
|
signchar = QSE_MT('+');
|
|
|
|
absvalue = value;
|
|
|
|
}
|
2011-11-07 00:01:19 +00:00
|
|
|
else if (base_and_flags & QSE_FMTINTMAXTOMBS_EMPTYSIGN)
|
|
|
|
{
|
|
|
|
signchar = QSE_MT(' ');
|
|
|
|
absvalue = value;
|
|
|
|
}
|
2011-11-05 00:50:55 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
signchar = QSE_MT('\0');
|
|
|
|
absvalue = value;
|
|
|
|
}
|
|
|
|
|
2011-11-14 15:15:44 +00:00
|
|
|
return fmt_unsigned_to_mbs (
|
|
|
|
buf, size, absvalue, base_and_flags, prec, fillchar, signchar, prefix);
|
2011-11-05 00:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int qse_fmtuintmaxtombs (
|
|
|
|
qse_mchar_t* buf, int size,
|
2011-11-14 15:15:44 +00:00
|
|
|
qse_uintmax_t value, int base_and_flags, int prec,
|
2011-11-07 00:01:19 +00:00
|
|
|
qse_mchar_t fillchar, const qse_mchar_t* prefix)
|
2011-11-05 00:50:55 +00:00
|
|
|
{
|
|
|
|
qse_mchar_t signchar;
|
|
|
|
|
|
|
|
/* determine if a sign character is needed */
|
|
|
|
if (base_and_flags & QSE_FMTINTMAXTOMBS_PLUSSIGN)
|
|
|
|
{
|
|
|
|
signchar = QSE_MT('+');
|
|
|
|
}
|
2011-11-07 00:01:19 +00:00
|
|
|
else if (base_and_flags & QSE_FMTINTMAXTOMBS_EMPTYSIGN)
|
|
|
|
{
|
|
|
|
signchar = QSE_MT(' ');
|
|
|
|
}
|
2011-11-05 00:50:55 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
signchar = QSE_MT('\0');
|
|
|
|
}
|
|
|
|
|
2011-11-14 15:15:44 +00:00
|
|
|
return fmt_unsigned_to_mbs (
|
|
|
|
buf, size, value, base_and_flags, prec, fillchar, signchar, prefix);
|
2011-11-05 00:50:55 +00:00
|
|
|
}
|
|
|
|
|
2011-11-03 14:56:26 +00:00
|
|
|
/* ==================== wide-char ===================================== */
|
2011-11-05 00:50:55 +00:00
|
|
|
|
|
|
|
int qse_fmtintmaxtowcs (
|
|
|
|
qse_wchar_t* buf, int size,
|
2011-11-14 15:15:44 +00:00
|
|
|
qse_intmax_t value, int base_and_flags, int prec,
|
2011-11-07 00:01:19 +00:00
|
|
|
qse_wchar_t fillchar, const qse_wchar_t* prefix)
|
2011-11-05 00:50:55 +00:00
|
|
|
{
|
|
|
|
qse_wchar_t signchar;
|
|
|
|
qse_uintmax_t absvalue;
|
|
|
|
|
|
|
|
if (value < 0)
|
|
|
|
{
|
|
|
|
signchar = QSE_WT('-');
|
|
|
|
absvalue = -value;
|
|
|
|
}
|
|
|
|
else if (base_and_flags & QSE_FMTINTMAXTOWCS_PLUSSIGN)
|
|
|
|
{
|
|
|
|
signchar = QSE_WT('+');
|
|
|
|
absvalue = value;
|
|
|
|
}
|
2019-11-21 08:54:02 +00:00
|
|
|
else if (base_and_flags & QSE_FMTINTMAXTOWCS_EMPTYSIGN)
|
2011-11-07 00:01:19 +00:00
|
|
|
{
|
|
|
|
signchar = QSE_WT(' ');
|
|
|
|
absvalue = value;
|
|
|
|
}
|
2011-11-05 00:50:55 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
signchar = QSE_WT('\0');
|
|
|
|
absvalue = value;
|
|
|
|
}
|
|
|
|
|
2011-11-14 15:15:44 +00:00
|
|
|
return fmt_unsigned_to_wcs (
|
|
|
|
buf, size, absvalue, base_and_flags, prec, fillchar, signchar, prefix);
|
2011-11-05 00:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int qse_fmtuintmaxtowcs (
|
|
|
|
qse_wchar_t* buf, int size,
|
2011-11-14 15:15:44 +00:00
|
|
|
qse_uintmax_t value, int base_and_flags, int prec,
|
2011-11-07 00:01:19 +00:00
|
|
|
qse_wchar_t fillchar, const qse_wchar_t* prefix)
|
2011-11-05 00:50:55 +00:00
|
|
|
{
|
|
|
|
qse_wchar_t signchar;
|
|
|
|
|
|
|
|
/* determine if a sign character is needed */
|
|
|
|
if (base_and_flags & QSE_FMTINTMAXTOWCS_PLUSSIGN)
|
|
|
|
{
|
|
|
|
signchar = QSE_WT('+');
|
|
|
|
}
|
2019-11-21 08:54:02 +00:00
|
|
|
else if (base_and_flags & QSE_FMTINTMAXTOWCS_EMPTYSIGN)
|
2011-11-07 00:01:19 +00:00
|
|
|
{
|
|
|
|
signchar = QSE_WT(' ');
|
|
|
|
}
|
2011-11-05 00:50:55 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
signchar = QSE_WT('\0');
|
|
|
|
}
|
|
|
|
|
2011-11-14 15:15:44 +00:00
|
|
|
return fmt_unsigned_to_wcs (
|
|
|
|
buf, size, value, base_and_flags, prec, fillchar, signchar, prefix);
|
2011-11-05 00:50:55 +00:00
|
|
|
}
|
2011-11-03 14:56:26 +00:00
|
|
|
|
2013-10-15 16:35:23 +00:00
|
|
|
|
|
|
|
/* ==================== floating-point number =========================== */
|
|
|
|
|
|
|
|
/* TODO: finish this function */
|
|
|
|
int qse_fmtfltmaxtombs (qse_mchar_t* buf, qse_size_t bufsize, qse_fltmax_t f, qse_mchar_t point, int digits)
|
|
|
|
{
|
|
|
|
qse_size_t len;
|
|
|
|
qse_uintmax_t v;
|
|
|
|
|
|
|
|
/*if (bufsize <= 0) return -reqlen; TODO: */
|
|
|
|
|
|
|
|
if (f < 0)
|
|
|
|
{
|
|
|
|
f *= -1;
|
|
|
|
v = (qse_uintmax_t)f;
|
|
|
|
len = qse_fmtuintmaxtombs (buf, bufsize, v, 10, 0, QSE_MT('\0'), QSE_MT("-"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
v = (qse_uintmax_t)f;
|
|
|
|
len = qse_fmtuintmaxtombs (buf, bufsize, v, 10, 0, QSE_MT('\0'), QSE_NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len + 1 < bufsize)
|
|
|
|
{
|
2015-03-19 14:07:50 +00:00
|
|
|
buf[len++] = point; /* add decimal point to string */
|
2013-10-15 16:35:23 +00:00
|
|
|
buf[len] = QSE_MT('\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
while (len + 1 < bufsize && digits-- > 0)
|
|
|
|
{
|
|
|
|
f = (f - (qse_fltmax_t)v) * 10;
|
|
|
|
v = (qse_uintmax_t)f;
|
|
|
|
len += qse_fmtuintmaxtombs (&buf[len], bufsize - len, v, 10, 0, QSE_MT('\0'), QSE_NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int)len;
|
|
|
|
}
|
|
|
|
|