2013-10-14 16:33:21 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2019-06-06 05:28:23 +00:00
|
|
|
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
|
2013-10-14 16:33:21 +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.
|
2013-10-14 16:33:21 +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.
|
2013-10-14 16:33:21 +00:00
|
|
|
*/
|
|
|
|
|
2013-09-30 14:55:58 +00:00
|
|
|
|
|
|
|
#include <qse/cmn/chr.h>
|
|
|
|
#include <qse/cmn/str.h>
|
|
|
|
#include <qse/cmn/mbwc.h>
|
|
|
|
#include <stdarg.h>
|
2016-04-29 03:55:42 +00:00
|
|
|
#include "mem-prv.h"
|
|
|
|
#include "fmt-prv.h"
|
2013-10-24 14:55:15 +00:00
|
|
|
|
2014-11-18 16:10:12 +00:00
|
|
|
#include <stdio.h> /* for snrintf(). used for floating-point number formatting */
|
2013-10-29 14:14:43 +00:00
|
|
|
#if defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && (__WATCOMC__ < 1200))
|
|
|
|
# define snprintf _snprintf
|
2014-11-18 16:10:12 +00:00
|
|
|
# if !defined(HAVE_SNPRINTF)
|
|
|
|
# define HAVE_SNPRINTF
|
|
|
|
# endif
|
2013-10-29 14:14:43 +00:00
|
|
|
#endif
|
2013-11-03 16:01:39 +00:00
|
|
|
#if defined(HAVE_QUADMATH_H)
|
2014-11-18 16:10:12 +00:00
|
|
|
# include <quadmath.h> /* for quadmath_snprintf() */
|
2013-11-03 16:01:39 +00:00
|
|
|
#endif
|
|
|
|
/* TODO: remove stdio.h and quadmath.h once snprintf gets replaced by own
|
2013-10-24 14:55:15 +00:00
|
|
|
floting-point conversion implementation*/
|
2013-09-30 14:55:58 +00:00
|
|
|
|
|
|
|
/* number of bits in a byte */
|
|
|
|
#define NBBY 8
|
|
|
|
|
|
|
|
/* Max number conversion buffer length:
|
|
|
|
* qse_intmax_t in base 2, plus NUL byte. */
|
|
|
|
#define MAXNBUF (QSE_SIZEOF(qse_intmax_t) * NBBY + 1)
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2013-10-14 16:33:21 +00:00
|
|
|
/* integer */
|
2013-09-30 14:55:58 +00:00
|
|
|
LF_C = (1 << 0),
|
|
|
|
LF_H = (1 << 1),
|
|
|
|
LF_J = (1 << 2),
|
|
|
|
LF_L = (1 << 3),
|
|
|
|
LF_Q = (1 << 4),
|
2013-10-16 15:32:51 +00:00
|
|
|
LF_T = (1 << 5),
|
|
|
|
LF_Z = (1 << 6),
|
2013-10-14 16:33:21 +00:00
|
|
|
|
2017-09-16 03:21:30 +00:00
|
|
|
LF_I8 = (1 << 7),
|
|
|
|
LF_I16 = (1 << 8),
|
|
|
|
LF_I32 = (1 << 9),
|
|
|
|
LF_I64 = (1 << 10),
|
|
|
|
LF_I128 = (1 << 11),
|
|
|
|
|
2013-10-14 16:33:21 +00:00
|
|
|
/* long double */
|
2017-09-16 03:21:30 +00:00
|
|
|
LF_LD = (1 << 12),
|
2013-11-03 16:01:39 +00:00
|
|
|
/* __float128 */
|
2017-09-16 03:21:30 +00:00
|
|
|
LF_QD = (1 << 13)
|
2013-09-30 14:55:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct
|
|
|
|
{
|
|
|
|
qse_uint8_t flag; /* for single occurrence */
|
|
|
|
qse_uint8_t dflag; /* for double occurrence */
|
|
|
|
} lm_tab[26] =
|
|
|
|
{
|
|
|
|
{ 0, 0 }, /* a */
|
|
|
|
{ 0, 0 }, /* b */
|
|
|
|
{ 0, 0 }, /* c */
|
|
|
|
{ 0, 0 }, /* d */
|
|
|
|
{ 0, 0 }, /* e */
|
|
|
|
{ 0, 0 }, /* f */
|
|
|
|
{ 0, 0 }, /* g */
|
|
|
|
{ LF_H, LF_C }, /* h */
|
|
|
|
{ 0, 0 }, /* i */
|
|
|
|
{ LF_J, 0 }, /* j */
|
|
|
|
{ 0, 0 }, /* k */
|
|
|
|
{ LF_L, LF_Q }, /* l */
|
|
|
|
{ 0, 0 }, /* m */
|
|
|
|
{ 0, 0 }, /* n */
|
|
|
|
{ 0, 0 }, /* o */
|
|
|
|
{ 0, 0 }, /* p */
|
|
|
|
{ LF_Q, 0 }, /* q */
|
|
|
|
{ 0, 0 }, /* r */
|
|
|
|
{ 0, 0 }, /* s */
|
2013-10-16 15:32:51 +00:00
|
|
|
{ LF_T, 0 }, /* t */
|
2013-09-30 14:55:58 +00:00
|
|
|
{ 0, 0 }, /* u */
|
|
|
|
{ 0, 0 }, /* v */
|
|
|
|
{ 0, 0 }, /* w */
|
|
|
|
{ 0, 0 }, /* z */
|
|
|
|
{ 0, 0 }, /* y */
|
|
|
|
{ LF_Z, 0 }, /* z */
|
|
|
|
};
|
|
|
|
|
2013-10-12 17:04:21 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
FLAGC_DOT = (1 << 0),
|
2013-11-03 16:01:39 +00:00
|
|
|
FLAGC_SPACE = (1 << 1),
|
|
|
|
FLAGC_SHARP = (1 << 2),
|
|
|
|
FLAGC_SIGN = (1 << 3),
|
2013-10-12 17:04:21 +00:00
|
|
|
FLAGC_LEFTADJ = (1 << 4),
|
|
|
|
FLAGC_ZEROPAD = (1 << 5),
|
2013-10-14 16:33:21 +00:00
|
|
|
FLAGC_WIDTH = (1 << 6),
|
|
|
|
FLAGC_PRECISION = (1 << 7),
|
|
|
|
FLAGC_STAR1 = (1 << 8),
|
|
|
|
FLAGC_STAR2 = (1 << 9),
|
|
|
|
FLAGC_LENMOD = (1 << 10) /* length modifier */
|
2013-10-12 17:04:21 +00:00
|
|
|
};
|
|
|
|
|
2013-10-21 08:39:53 +00:00
|
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
static const qse_mchar_t* m_hex2ascii =
|
|
|
|
QSE_MT("0123456789abcdefghijklmnopqrstuvwxyz");
|
|
|
|
static const qse_wchar_t* w_hex2ascii =
|
|
|
|
QSE_WT("0123456789abcdefghijklmnopqrstuvwxyz");
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
|
2022-01-24 07:30:14 +00:00
|
|
|
/* char_t a normal character type for the format
|
|
|
|
* ochar_t is an opposite type to char_t
|
|
|
|
* uchar_t is a normal unsigned character type */
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
|
2013-09-30 14:55:58 +00:00
|
|
|
#undef char_t
|
|
|
|
#undef uchar_t
|
|
|
|
#undef ochar_t
|
|
|
|
#undef T
|
|
|
|
#undef OT
|
2013-10-28 09:40:25 +00:00
|
|
|
#undef CONV_MAX
|
2013-09-30 14:55:58 +00:00
|
|
|
#undef toupper
|
|
|
|
#undef hex2ascii
|
|
|
|
#undef sprintn
|
2013-10-28 09:40:25 +00:00
|
|
|
#undef fmtout_t
|
2013-10-24 14:55:15 +00:00
|
|
|
#undef fmtout
|
2013-09-30 14:55:58 +00:00
|
|
|
|
|
|
|
#define char_t qse_mchar_t
|
2022-01-24 07:30:14 +00:00
|
|
|
#define uchar_t qse_mchau_t
|
2013-09-30 14:55:58 +00:00
|
|
|
#define ochar_t qse_wchar_t
|
|
|
|
#define T(x) QSE_MT(x)
|
|
|
|
#define OT(x) QSE_WT(x)
|
2013-10-28 09:40:25 +00:00
|
|
|
#define CONV_MAX QSE_MBLEN_MAX
|
2022-01-24 07:30:14 +00:00
|
|
|
#define toupper QSE_TOMUPPER
|
2013-09-30 14:55:58 +00:00
|
|
|
#define sprintn m_sprintn
|
2013-10-28 09:40:25 +00:00
|
|
|
#define fmtout_t qse_mfmtout_t
|
2013-10-24 14:55:15 +00:00
|
|
|
#define fmtout qse_mfmtout
|
2013-09-30 14:55:58 +00:00
|
|
|
|
|
|
|
#define hex2ascii(hex) (m_hex2ascii[hex])
|
|
|
|
|
2013-10-24 14:55:15 +00:00
|
|
|
#include "fmt-out.h"
|
2013-09-30 14:55:58 +00:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
#undef char_t
|
|
|
|
#undef uchar_t
|
|
|
|
#undef ochar_t
|
|
|
|
#undef T
|
|
|
|
#undef OT
|
2013-10-28 09:40:25 +00:00
|
|
|
#undef CONV_MAX
|
2013-09-30 14:55:58 +00:00
|
|
|
#undef toupper
|
|
|
|
#undef hex2ascii
|
|
|
|
#undef sprintn
|
2013-10-28 09:40:25 +00:00
|
|
|
#undef fmtout_t
|
2013-10-24 14:55:15 +00:00
|
|
|
#undef fmtout
|
2013-09-30 14:55:58 +00:00
|
|
|
|
|
|
|
#define char_t qse_wchar_t
|
2022-01-24 07:30:14 +00:00
|
|
|
#define uchar_t qse_wchau_t
|
2013-09-30 14:55:58 +00:00
|
|
|
#define ochar_t qse_mchar_t
|
|
|
|
#define T(x) QSE_WT(x)
|
|
|
|
#define OT(x) QSE_MT(x)
|
2013-10-28 09:40:25 +00:00
|
|
|
#define CONV_MAX 1
|
2013-09-30 14:55:58 +00:00
|
|
|
#define toupper QSE_TOWUPPER
|
|
|
|
#define sprintn w_sprintn
|
2013-10-28 09:40:25 +00:00
|
|
|
#define fmtout_t qse_wfmtout_t
|
2013-10-24 14:55:15 +00:00
|
|
|
#define fmtout qse_wfmtout
|
2013-09-30 14:55:58 +00:00
|
|
|
|
|
|
|
#define hex2ascii(hex) (w_hex2ascii[hex])
|
|
|
|
|
2013-10-24 14:55:15 +00:00
|
|
|
#include "fmt-out.h"
|
2013-09-30 14:55:58 +00:00
|
|
|
|