added experimental and incomplete xprintf function.
This commit is contained in:
parent
fd4f25d330
commit
e7b4e7012c
@ -11,6 +11,7 @@ noinst_HEADERS = \
|
|||||||
cp950.h \
|
cp950.h \
|
||||||
fs.h \
|
fs.h \
|
||||||
mem.h \
|
mem.h \
|
||||||
|
printf.h \
|
||||||
str-subst.h \
|
str-subst.h \
|
||||||
syscall.h \
|
syscall.h \
|
||||||
syserr.h \
|
syserr.h \
|
||||||
@ -62,6 +63,7 @@ libqsecmn_la_SOURCES = \
|
|||||||
pma.c \
|
pma.c \
|
||||||
rbt.c \
|
rbt.c \
|
||||||
rex.c \
|
rex.c \
|
||||||
|
printf.c \
|
||||||
sio.c \
|
sio.c \
|
||||||
sll.c \
|
sll.c \
|
||||||
slmb.c \
|
slmb.c \
|
||||||
|
@ -90,15 +90,16 @@ am__libqsecmn_la_SOURCES_DIST = alg-base64.c alg-rand.c alg-search.c \
|
|||||||
fma.c fmt.c fs.c fs-err.c fs-move.c glob.c hton.c ipad.c lda.c \
|
fma.c fmt.c fs.c fs-err.c fs-move.c glob.c hton.c ipad.c lda.c \
|
||||||
main.c mb8.c mbwc.c mbwc-str.c mem.c mux.c nwad.c nwad-skad.c \
|
main.c mb8.c mbwc.c mbwc-str.c mem.c mux.c nwad.c nwad-skad.c \
|
||||||
nwif.c nwif-cfg.c nwio.c oht.c opt.c path-basename.c \
|
nwif.c nwif-cfg.c nwio.c oht.c opt.c path-basename.c \
|
||||||
path-canon.c pio.c pma.c rbt.c rex.c sio.c sll.c slmb.c \
|
path-canon.c pio.c pma.c rbt.c rex.c printf.c sio.c sll.c \
|
||||||
stdio.c str-beg.c str-cat.c str-chr.c str-cnv.c str-cmp.c \
|
slmb.c stdio.c str-beg.c str-cat.c str-chr.c str-cnv.c \
|
||||||
str-cpy.c str-del.c str-dup.c str-dynm.c str-dynw.c str-end.c \
|
str-cmp.c str-cpy.c str-del.c str-dup.c str-dynm.c str-dynw.c \
|
||||||
str-excl.c str-fcpy.c str-fnmat.c str-incl.c str-len.c \
|
str-end.c str-excl.c str-fcpy.c str-fnmat.c str-incl.c \
|
||||||
str-pac.c str-pbrk.c str-put.c str-rev.c str-rot.c str-set.c \
|
str-len.c str-pac.c str-pbrk.c str-put.c str-rev.c str-rot.c \
|
||||||
str-spl.c str-spn.c str-str.c str-subst.c str-tok.c str-trm.c \
|
str-set.c str-spl.c str-spn.c str-str.c str-subst.c str-tok.c \
|
||||||
str-word.c task.c time.c tio.c tre.c tre-ast.c tre-compile.c \
|
str-trm.c str-word.c task.c time.c tio.c tre.c tre-ast.c \
|
||||||
tre-match-backtrack.c tre-match-parallel.c tre-parse.c \
|
tre-compile.c tre-match-backtrack.c tre-match-parallel.c \
|
||||||
tre-stack.c uri.c utf8.c xma.c uni.c cp949.c cp950.c
|
tre-parse.c tre-stack.c uri.c utf8.c xma.c uni.c cp949.c \
|
||||||
|
cp950.c
|
||||||
@ENABLE_BUNDLED_UNICODE_TRUE@am__objects_1 = uni.lo
|
@ENABLE_BUNDLED_UNICODE_TRUE@am__objects_1 = uni.lo
|
||||||
@ENABLE_XCMGRS_TRUE@am__objects_2 = cp949.lo cp950.lo
|
@ENABLE_XCMGRS_TRUE@am__objects_2 = cp949.lo cp950.lo
|
||||||
am_libqsecmn_la_OBJECTS = alg-base64.lo alg-rand.lo alg-search.lo \
|
am_libqsecmn_la_OBJECTS = alg-base64.lo alg-rand.lo alg-search.lo \
|
||||||
@ -107,14 +108,14 @@ am_libqsecmn_la_OBJECTS = alg-base64.lo alg-rand.lo alg-search.lo \
|
|||||||
hton.lo ipad.lo lda.lo main.lo mb8.lo mbwc.lo mbwc-str.lo \
|
hton.lo ipad.lo lda.lo main.lo mb8.lo mbwc.lo mbwc-str.lo \
|
||||||
mem.lo mux.lo nwad.lo nwad-skad.lo nwif.lo nwif-cfg.lo nwio.lo \
|
mem.lo mux.lo nwad.lo nwad-skad.lo nwif.lo nwif-cfg.lo nwio.lo \
|
||||||
oht.lo opt.lo path-basename.lo path-canon.lo pio.lo pma.lo \
|
oht.lo opt.lo path-basename.lo path-canon.lo pio.lo pma.lo \
|
||||||
rbt.lo rex.lo sio.lo sll.lo slmb.lo stdio.lo str-beg.lo \
|
rbt.lo rex.lo printf.lo sio.lo sll.lo slmb.lo stdio.lo \
|
||||||
str-cat.lo str-chr.lo str-cnv.lo str-cmp.lo str-cpy.lo \
|
str-beg.lo str-cat.lo str-chr.lo str-cnv.lo str-cmp.lo \
|
||||||
str-del.lo str-dup.lo str-dynm.lo str-dynw.lo str-end.lo \
|
str-cpy.lo str-del.lo str-dup.lo str-dynm.lo str-dynw.lo \
|
||||||
str-excl.lo str-fcpy.lo str-fnmat.lo str-incl.lo str-len.lo \
|
str-end.lo str-excl.lo str-fcpy.lo str-fnmat.lo str-incl.lo \
|
||||||
str-pac.lo str-pbrk.lo str-put.lo str-rev.lo str-rot.lo \
|
str-len.lo str-pac.lo str-pbrk.lo str-put.lo str-rev.lo \
|
||||||
str-set.lo str-spl.lo str-spn.lo str-str.lo str-subst.lo \
|
str-rot.lo str-set.lo str-spl.lo str-spn.lo str-str.lo \
|
||||||
str-tok.lo str-trm.lo str-word.lo task.lo time.lo tio.lo \
|
str-subst.lo str-tok.lo str-trm.lo str-word.lo task.lo time.lo \
|
||||||
tre.lo tre-ast.lo tre-compile.lo tre-match-backtrack.lo \
|
tio.lo tre.lo tre-ast.lo tre-compile.lo tre-match-backtrack.lo \
|
||||||
tre-match-parallel.lo tre-parse.lo tre-stack.lo uri.lo utf8.lo \
|
tre-match-parallel.lo tre-parse.lo tre-stack.lo uri.lo utf8.lo \
|
||||||
xma.lo $(am__objects_1) $(am__objects_2)
|
xma.lo $(am__objects_1) $(am__objects_2)
|
||||||
libqsecmn_la_OBJECTS = $(am_libqsecmn_la_OBJECTS)
|
libqsecmn_la_OBJECTS = $(am_libqsecmn_la_OBJECTS)
|
||||||
@ -367,6 +368,7 @@ noinst_HEADERS = \
|
|||||||
cp950.h \
|
cp950.h \
|
||||||
fs.h \
|
fs.h \
|
||||||
mem.h \
|
mem.h \
|
||||||
|
printf.h \
|
||||||
str-subst.h \
|
str-subst.h \
|
||||||
syscall.h \
|
syscall.h \
|
||||||
syserr.h \
|
syserr.h \
|
||||||
@ -382,9 +384,9 @@ libqsecmn_la_SOURCES = alg-base64.c alg-rand.c alg-search.c alg-sort.c \
|
|||||||
fs.c fs-err.c fs-move.c glob.c hton.c ipad.c lda.c main.c \
|
fs.c fs-err.c fs-move.c glob.c hton.c ipad.c lda.c main.c \
|
||||||
mb8.c mbwc.c mbwc-str.c mem.c mux.c nwad.c nwad-skad.c nwif.c \
|
mb8.c mbwc.c mbwc-str.c mem.c mux.c nwad.c nwad-skad.c nwif.c \
|
||||||
nwif-cfg.c nwio.c oht.c opt.c path-basename.c path-canon.c \
|
nwif-cfg.c nwio.c oht.c opt.c path-basename.c path-canon.c \
|
||||||
pio.c pma.c rbt.c rex.c sio.c sll.c slmb.c stdio.c str-beg.c \
|
pio.c pma.c rbt.c rex.c printf.c sio.c sll.c slmb.c stdio.c \
|
||||||
str-cat.c str-chr.c str-cnv.c str-cmp.c str-cpy.c str-del.c \
|
str-beg.c str-cat.c str-chr.c str-cnv.c str-cmp.c str-cpy.c \
|
||||||
str-dup.c str-dynm.c str-dynw.c str-end.c str-excl.c \
|
str-del.c str-dup.c str-dynm.c str-dynw.c str-end.c str-excl.c \
|
||||||
str-fcpy.c str-fnmat.c str-incl.c str-len.c str-pac.c \
|
str-fcpy.c str-fnmat.c str-incl.c str-len.c str-pac.c \
|
||||||
str-pbrk.c str-put.c str-rev.c str-rot.c str-set.c str-spl.c \
|
str-pbrk.c str-put.c str-rev.c str-rot.c str-set.c str-spl.c \
|
||||||
str-spn.c str-str.c str-subst.c str-tok.c str-trm.c str-word.c \
|
str-spn.c str-str.c str-subst.c str-tok.c str-trm.c str-word.c \
|
||||||
@ -516,6 +518,7 @@ distclean-compile:
|
|||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/path-canon.Plo@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/path-canon.Plo@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pio.Plo@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pio.Plo@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pma.Plo@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pma.Plo@am__quote@
|
||||||
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/printf.Plo@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rbt.Plo@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rbt.Plo@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rex.Plo@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rex.Plo@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sio.Plo@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sio.Plo@am__quote@
|
||||||
|
163
qse/lib/cmn/printf.c
Normal file
163
qse/lib/cmn/printf.c
Normal file
@ -0,0 +1,163 @@
|
|||||||
|
|
||||||
|
#include <qse/cmn/chr.h>
|
||||||
|
#include <qse/cmn/str.h>
|
||||||
|
#include <qse/cmn/mbwc.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
/* 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
|
||||||
|
{
|
||||||
|
LF_C = (1 << 0),
|
||||||
|
LF_H = (1 << 1),
|
||||||
|
LF_J = (1 << 2),
|
||||||
|
LF_L = (1 << 3),
|
||||||
|
LF_Q = (1 << 4),
|
||||||
|
LF_T = (1 << 5),
|
||||||
|
LF_Z = (1 << 6)
|
||||||
|
};
|
||||||
|
|
||||||
|
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 */
|
||||||
|
{ LF_T, 0 }, /* t */
|
||||||
|
{ 0, 0 }, /* u */
|
||||||
|
{ 0, 0 }, /* v */
|
||||||
|
{ 0, 0 }, /* w */
|
||||||
|
{ 0, 0 }, /* z */
|
||||||
|
{ 0, 0 }, /* y */
|
||||||
|
{ LF_Z, 0 }, /* z */
|
||||||
|
};
|
||||||
|
|
||||||
|
#include <stdio.h> /* TODO: remove dependency on this */
|
||||||
|
|
||||||
|
static void xputwchar (qse_wchar_t c, void *arg)
|
||||||
|
{
|
||||||
|
qse_cmgr_t* cmgr;
|
||||||
|
qse_mchar_t mbsbuf[QSE_MBLEN_MAX + 1];
|
||||||
|
qse_size_t n;
|
||||||
|
|
||||||
|
cmgr = qse_getdflcmgr ();
|
||||||
|
n = cmgr->wctomb (c, mbsbuf, QSE_COUNTOF(mbsbuf));
|
||||||
|
if (n <= 0 || n > QSE_COUNTOF(mbsbuf))
|
||||||
|
{
|
||||||
|
putchar ('?');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
qse_size_t i;
|
||||||
|
for (i = 0; i < n; i++) putchar (mbsbuf[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void xputmchar (qse_mchar_t c, void *arg)
|
||||||
|
{
|
||||||
|
putchar (c);
|
||||||
|
}
|
||||||
|
|
||||||
|
#undef char_t
|
||||||
|
#undef uchar_t
|
||||||
|
#undef ochar_t
|
||||||
|
#undef T
|
||||||
|
#undef OT
|
||||||
|
#undef toupper
|
||||||
|
#undef hex2ascii
|
||||||
|
#undef sprintn
|
||||||
|
#undef xprintf
|
||||||
|
|
||||||
|
#define char_t qse_mchar_t
|
||||||
|
#define uchar_t qse_mchar_t
|
||||||
|
#define ochar_t qse_wchar_t
|
||||||
|
#define T(x) QSE_MT(x)
|
||||||
|
#define OT(x) QSE_WT(x)
|
||||||
|
#define toupper QSE_TOUPPER
|
||||||
|
#define sprintn m_sprintn
|
||||||
|
#define xprintf qse_mxprintf
|
||||||
|
|
||||||
|
static const qse_mchar_t m_hex2ascii[] = QSE_MT("0123456789abcdefghijklmnopqrstuvwxyz");
|
||||||
|
#define hex2ascii(hex) (m_hex2ascii[hex])
|
||||||
|
|
||||||
|
#include "printf.h"
|
||||||
|
|
||||||
|
int qse_mprintf (const char_t *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
int n;
|
||||||
|
va_start (ap, fmt);
|
||||||
|
n = qse_mxprintf (fmt, xputmchar, xputwchar, QSE_NULL, ap);
|
||||||
|
va_end (ap);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
int qse_mvprintf (const char_t* fmt, va_list ap)
|
||||||
|
{
|
||||||
|
return qse_mxprintf (fmt, xputmchar, xputwchar, QSE_NULL, ap);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------------ */
|
||||||
|
|
||||||
|
#undef char_t
|
||||||
|
#undef uchar_t
|
||||||
|
#undef ochar_t
|
||||||
|
#undef T
|
||||||
|
#undef OT
|
||||||
|
#undef toupper
|
||||||
|
#undef hex2ascii
|
||||||
|
#undef sprintn
|
||||||
|
#undef xprintf
|
||||||
|
|
||||||
|
#define char_t qse_wchar_t
|
||||||
|
#define uchar_t qse_wchar_t
|
||||||
|
#define ochar_t qse_mchar_t
|
||||||
|
#define T(x) QSE_WT(x)
|
||||||
|
#define OT(x) QSE_MT(x)
|
||||||
|
#define toupper QSE_TOWUPPER
|
||||||
|
#define sprintn w_sprintn
|
||||||
|
#define xprintf qse_wxprintf
|
||||||
|
|
||||||
|
static const qse_wchar_t w_hex2ascii[] = QSE_WT("0123456789abcdefghijklmnopqrstuvwxyz");
|
||||||
|
#define hex2ascii(hex) (w_hex2ascii[hex])
|
||||||
|
|
||||||
|
#include "printf.h"
|
||||||
|
|
||||||
|
int qse_wprintf (const char_t *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
int n;
|
||||||
|
va_start (ap, fmt);
|
||||||
|
n = qse_wxprintf (fmt, xputwchar, xputmchar, QSE_NULL, ap);
|
||||||
|
va_end (ap);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
int qse_wvprintf (const char_t* fmt, va_list ap)
|
||||||
|
{
|
||||||
|
return qse_wxprintf (fmt, xputwchar, xputmchar, QSE_NULL, ap);
|
||||||
|
}
|
447
qse/lib/cmn/printf.h
Normal file
447
qse/lib/cmn/printf.h
Normal file
@ -0,0 +1,447 @@
|
|||||||
|
/*-
|
||||||
|
* Copyright (c) 1986, 1988, 1991, 1993
|
||||||
|
* The Regents of the University of California. All rights reserved.
|
||||||
|
* (c) UNIX System Laboratories, Inc.
|
||||||
|
* All or some portions of this file are derived from material licensed
|
||||||
|
* to the University of California by American Telephone and Telegraph
|
||||||
|
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
|
||||||
|
* the permission of UNIX System Laboratories, Inc.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
* 4. Neither the name of the University nor the names of its contributors
|
||||||
|
* may be used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
|
||||||
|
*
|
||||||
|
* @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define NBBY 8 /* number of bits in a byte */
|
||||||
|
|
||||||
|
/* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
|
||||||
|
#define MAXNBUF (QSE_SIZEOF(qse_intmax_t) * NBBY + 1)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
|
||||||
|
* order; return an optional length and a pointer to the last character
|
||||||
|
* written in the buffer (i.e., the first character of the string).
|
||||||
|
* The buffer pointed to by `nbuf' must have length >= MAXNBUF.
|
||||||
|
*/
|
||||||
|
static char_t* sprintn (char_t* nbuf, qse_uintmax_t num, int base, int *lenp, int upper)
|
||||||
|
{
|
||||||
|
char_t *p, c;
|
||||||
|
|
||||||
|
p = nbuf;
|
||||||
|
*p = T('\0');
|
||||||
|
do
|
||||||
|
{
|
||||||
|
c = hex2ascii(num % base);
|
||||||
|
*++p = upper ? toupper(c) : c;
|
||||||
|
}
|
||||||
|
while (num /= base);
|
||||||
|
|
||||||
|
if (lenp) *lenp = p - nbuf;
|
||||||
|
return (p);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define PCHAR(c) do { \
|
||||||
|
func (c, arg); \
|
||||||
|
retval++; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define OPCHAR(c) do { \
|
||||||
|
ofunc (c, arg); \
|
||||||
|
retval++; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
int xprintf (char_t const *fmt, void (*func)(char_t, void*), void (*ofunc) (ochar_t, void*), void *arg, va_list ap)
|
||||||
|
{
|
||||||
|
char_t nbuf[MAXNBUF];
|
||||||
|
const char_t* p, * percent;
|
||||||
|
uchar_t ch;
|
||||||
|
int n;
|
||||||
|
qse_uintmax_t num;
|
||||||
|
int base, tmp, width, ladjust, sharpflag, neg, sign, dot;
|
||||||
|
int dwidth, upper;
|
||||||
|
char_t padc, * sp;
|
||||||
|
ochar_t opadc, * osp;
|
||||||
|
int stop = 0, retval = 0;
|
||||||
|
int lm_flag, lm_dflag;
|
||||||
|
|
||||||
|
num = 0;
|
||||||
|
|
||||||
|
if (fmt == QSE_NULL) fmt = T("(fmt null)\n");
|
||||||
|
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
padc = T(' ');
|
||||||
|
opadc = OT(' ');
|
||||||
|
|
||||||
|
width = 0;
|
||||||
|
while ((ch = (uchar_t)*fmt++) != T('%') || stop)
|
||||||
|
{
|
||||||
|
if (ch == T('\0')) return (retval);
|
||||||
|
PCHAR(ch);
|
||||||
|
}
|
||||||
|
percent = fmt - 1;
|
||||||
|
ladjust = 0; sharpflag = 0; neg = 0;
|
||||||
|
sign = 0; dot = 0; dwidth = 0; upper = 0;
|
||||||
|
|
||||||
|
lm_flag = 0; lm_dflag = 0;
|
||||||
|
|
||||||
|
reswitch:
|
||||||
|
switch (ch = (uchar_t)*fmt++)
|
||||||
|
{
|
||||||
|
case T('%'): /* %% */
|
||||||
|
PCHAR(ch);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case T('.'):
|
||||||
|
dot = 1;
|
||||||
|
goto reswitch;
|
||||||
|
case T('#'):
|
||||||
|
sharpflag = 1;
|
||||||
|
goto reswitch;
|
||||||
|
case T('+'):
|
||||||
|
sign = 1;
|
||||||
|
goto reswitch;
|
||||||
|
case T('-'):
|
||||||
|
ladjust = 1;
|
||||||
|
goto reswitch;
|
||||||
|
|
||||||
|
case T('*'):
|
||||||
|
if (!dot)
|
||||||
|
{
|
||||||
|
width = va_arg(ap, int);
|
||||||
|
if (width < 0)
|
||||||
|
{
|
||||||
|
ladjust = !ladjust;
|
||||||
|
width = -width;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dwidth = va_arg(ap, int);
|
||||||
|
}
|
||||||
|
goto reswitch;
|
||||||
|
|
||||||
|
case T('0'):
|
||||||
|
if (!dot)
|
||||||
|
{
|
||||||
|
padc = T('0');
|
||||||
|
opadc = OT('0');
|
||||||
|
goto reswitch;
|
||||||
|
}
|
||||||
|
case T('1'): case T('2'): case T('3'): case T('4'):
|
||||||
|
case T('5'): case T('6'): case T('7'): case T('8'): case T('9'):
|
||||||
|
for (n = 0;; ++fmt)
|
||||||
|
{
|
||||||
|
n = n * 10 + ch - T('0');
|
||||||
|
ch = *fmt;
|
||||||
|
if (ch < T('0') || ch > T('9')) break;
|
||||||
|
}
|
||||||
|
if (dot) dwidth = n;
|
||||||
|
else width = n;
|
||||||
|
goto reswitch;
|
||||||
|
|
||||||
|
case T('c'):
|
||||||
|
if (((lm_flag & LF_H) && (QSE_SIZEOF(char_t) > QSE_SIZEOF(ochar_t))) ||
|
||||||
|
((lm_flag & LF_L) && (QSE_SIZEOF(char_t) < QSE_SIZEOF(ochar_t)))) goto uppercase_c;
|
||||||
|
lowercase_c:
|
||||||
|
if (QSE_SIZEOF(char_t) < QSE_SIZEOF(int))
|
||||||
|
PCHAR(va_arg(ap, int));
|
||||||
|
else
|
||||||
|
PCHAR(va_arg(ap, char_t));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case T('C'):
|
||||||
|
if (((lm_flag & LF_H) && (QSE_SIZEOF(char_t) < QSE_SIZEOF(ochar_t))) ||
|
||||||
|
((lm_flag & LF_L) && (QSE_SIZEOF(char_t) > QSE_SIZEOF(ochar_t)))) goto lowercase_c;
|
||||||
|
uppercase_c:
|
||||||
|
if (QSE_SIZEOF(ochar_t) < QSE_SIZEOF(int))
|
||||||
|
OPCHAR(va_arg(ap, int));
|
||||||
|
else
|
||||||
|
OPCHAR(va_arg(ap, ochar_t));
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
/* length modifiers */
|
||||||
|
case T('h'): /* short int */
|
||||||
|
case T('l'): /* long int */
|
||||||
|
case T('q'): /* long long int */
|
||||||
|
case T('j'): /* uintmax_t */
|
||||||
|
case T('z'): /* size_t */
|
||||||
|
case T('t'): /* ptrdiff_t */
|
||||||
|
if (lm_dflag)
|
||||||
|
{
|
||||||
|
/* error */
|
||||||
|
PCHAR (fmt[-4]);
|
||||||
|
PCHAR (fmt[-3]);
|
||||||
|
PCHAR (fmt[-2]);
|
||||||
|
PCHAR (fmt[-1]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (lm_flag)
|
||||||
|
{
|
||||||
|
if (lm_tab[ch - T('a')].dflag && lm_flag == lm_tab[ch - T('a')].flag)
|
||||||
|
{
|
||||||
|
lm_flag &= ~lm_tab[ch - T('a')].flag;
|
||||||
|
lm_flag |= lm_tab[ch - T('a')].dflag;
|
||||||
|
lm_dflag |= lm_flag;
|
||||||
|
goto reswitch;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* error */
|
||||||
|
PCHAR (fmt[-3]);
|
||||||
|
PCHAR (fmt[-2]);
|
||||||
|
PCHAR (fmt[-1]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lm_flag |= lm_tab[ch - T('a')].flag;
|
||||||
|
goto reswitch;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
/* end of length modifiers */
|
||||||
|
|
||||||
|
case T('n'):
|
||||||
|
if (lm_flag & LF_J)
|
||||||
|
*(va_arg(ap, qse_intmax_t *)) = retval;
|
||||||
|
#if (QSE_SIZEOF_LONG_LONG > 0)
|
||||||
|
else if (lm_flag & LF_Q)
|
||||||
|
*(va_arg(ap, long long int*)) = retval;
|
||||||
|
#endif
|
||||||
|
else if (lm_flag & LF_L)
|
||||||
|
*(va_arg(ap, long int*)) = retval;
|
||||||
|
else if (lm_flag & LF_Z)
|
||||||
|
*(va_arg(ap, qse_size_t*)) = retval;
|
||||||
|
else if (lm_flag & LF_H)
|
||||||
|
*(va_arg(ap, short int*)) = retval;
|
||||||
|
else if (lm_flag & LF_C)
|
||||||
|
*(va_arg(ap, char*)) = retval;
|
||||||
|
else
|
||||||
|
*(va_arg(ap, int*)) = retval;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case T('o'):
|
||||||
|
base = 8;
|
||||||
|
goto handle_nosign;
|
||||||
|
case T('d'):
|
||||||
|
case T('i'):
|
||||||
|
base = 10;
|
||||||
|
sign = 1;
|
||||||
|
goto handle_sign;
|
||||||
|
case T('u'):
|
||||||
|
base = 10;
|
||||||
|
goto handle_nosign;
|
||||||
|
case T('X'):
|
||||||
|
upper = 1;
|
||||||
|
case T('x'):
|
||||||
|
base = 16;
|
||||||
|
goto handle_nosign;
|
||||||
|
case T('y'):
|
||||||
|
base = 16;
|
||||||
|
sign = 1;
|
||||||
|
goto handle_sign;
|
||||||
|
|
||||||
|
case T('p'):
|
||||||
|
base = 16;
|
||||||
|
sharpflag = (width == 0);
|
||||||
|
sign = 0;
|
||||||
|
num = (qse_uintptr_t)va_arg(ap, void *);
|
||||||
|
goto number;
|
||||||
|
|
||||||
|
case T('s'):
|
||||||
|
{
|
||||||
|
if (((lm_flag & LF_H) && (QSE_SIZEOF(char_t) > QSE_SIZEOF(ochar_t))) ||
|
||||||
|
((lm_flag & LF_L) && (QSE_SIZEOF(char_t) < QSE_SIZEOF(ochar_t)))) goto uppercase_s;
|
||||||
|
lowercase_s:
|
||||||
|
sp = va_arg (ap, char_t *);
|
||||||
|
if (sp == QSE_NULL) p = T("(null)");
|
||||||
|
if (!dot)
|
||||||
|
{
|
||||||
|
char_t* p = sp;
|
||||||
|
while (*p) p++;
|
||||||
|
n = p - sp;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (n = 0; n < dwidth && sp[n]; n++) continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
width -= n;
|
||||||
|
|
||||||
|
if (!ladjust && width > 0)
|
||||||
|
{
|
||||||
|
while (width--) PCHAR(padc);
|
||||||
|
}
|
||||||
|
while (n--) PCHAR(*sp++);
|
||||||
|
if (ladjust && width > 0)
|
||||||
|
{
|
||||||
|
while (width--) PCHAR(padc);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case T('S'):
|
||||||
|
{
|
||||||
|
if (((lm_flag & LF_H) && (QSE_SIZEOF(char_t) < QSE_SIZEOF(ochar_t))) ||
|
||||||
|
((lm_flag & LF_L) && (QSE_SIZEOF(char_t) > QSE_SIZEOF(ochar_t)))) goto lowercase_s;
|
||||||
|
uppercase_s:
|
||||||
|
osp = va_arg (ap, ochar_t*);
|
||||||
|
if (osp == QSE_NULL) osp = OT("(null)");
|
||||||
|
if (!dot)
|
||||||
|
{
|
||||||
|
ochar_t* p = osp;
|
||||||
|
while (*p) p++;
|
||||||
|
n = p - osp;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (n = 0; n < dwidth && osp[n]; n++) continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
width -= n;
|
||||||
|
|
||||||
|
if (!ladjust && width > 0)
|
||||||
|
{
|
||||||
|
while (width--) OPCHAR (opadc);
|
||||||
|
}
|
||||||
|
while (n--) OPCHAR(*osp++);
|
||||||
|
if (ladjust && width > 0)
|
||||||
|
{
|
||||||
|
while (width--) OPCHAR (opadc);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
handle_nosign:
|
||||||
|
sign = 0;
|
||||||
|
if (lm_flag & LF_J)
|
||||||
|
num = va_arg(ap, qse_uintmax_t);
|
||||||
|
#if (QSE_SIZEOF_LONG_LONG > 0)
|
||||||
|
else if (lm_flag & LF_Q)
|
||||||
|
num = va_arg(ap, unsigned long long int);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
else if (lm_flag & LF_T)
|
||||||
|
num = va_arg(ap, ptrdiff_t);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
else if (lm_flag & LF_L)
|
||||||
|
num = va_arg(ap, long int);
|
||||||
|
else if (lm_flag & LF_Z)
|
||||||
|
num = va_arg(ap, qse_size_t);
|
||||||
|
else if (lm_flag & LF_H)
|
||||||
|
num = (unsigned short int)va_arg(ap, int);
|
||||||
|
else if (lm_flag & LF_C)
|
||||||
|
num = (unsigned char)va_arg(ap, int);
|
||||||
|
else
|
||||||
|
num = va_arg(ap, unsigned int);
|
||||||
|
goto number;
|
||||||
|
|
||||||
|
handle_sign:
|
||||||
|
if (lm_flag & LF_J)
|
||||||
|
num = va_arg(ap, qse_intmax_t);
|
||||||
|
#if (QSE_SIZEOF_LONG_LONG > 0)
|
||||||
|
else if (lm_flag & LF_Q)
|
||||||
|
num = va_arg(ap, long long int);
|
||||||
|
#endif
|
||||||
|
#if 0
|
||||||
|
else if (lm_flag & LF_T)
|
||||||
|
num = va_arg(ap, ptrdiff_t);
|
||||||
|
#endif
|
||||||
|
else if (lm_flag & LF_L)
|
||||||
|
num = va_arg(ap, long int);
|
||||||
|
else if (lm_flag & LF_Z)
|
||||||
|
num = va_arg(ap, qse_ssize_t);
|
||||||
|
else if (lm_flag & LF_H)
|
||||||
|
num = (short int)va_arg(ap, int);
|
||||||
|
else if (lm_flag & LF_C)
|
||||||
|
num = (char)va_arg(ap, int);
|
||||||
|
else
|
||||||
|
num = va_arg(ap, int);
|
||||||
|
|
||||||
|
number:
|
||||||
|
if (sign && (qse_intmax_t)num < 0)
|
||||||
|
{
|
||||||
|
neg = 1;
|
||||||
|
num = -(qse_intmax_t)num;
|
||||||
|
}
|
||||||
|
p = sprintn (nbuf, num, base, &tmp, upper);
|
||||||
|
if (sharpflag && num != 0)
|
||||||
|
{
|
||||||
|
if (base == 8) tmp++;
|
||||||
|
else if (base == 16) tmp += 2;
|
||||||
|
}
|
||||||
|
if (neg) tmp++;
|
||||||
|
|
||||||
|
if (!ladjust && padc != T('0') && width && (width -= tmp) > 0)
|
||||||
|
{
|
||||||
|
while (width--) PCHAR(padc);
|
||||||
|
}
|
||||||
|
if (neg) PCHAR(T('-'));
|
||||||
|
|
||||||
|
if (sharpflag && num != 0)
|
||||||
|
{
|
||||||
|
if (base == 8) {
|
||||||
|
PCHAR(T('0'));
|
||||||
|
}
|
||||||
|
else if (base == 16)
|
||||||
|
{
|
||||||
|
PCHAR(T('0'));
|
||||||
|
PCHAR(T('x'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!ladjust && width && (width -= tmp) > 0)
|
||||||
|
{
|
||||||
|
while (width--) PCHAR(padc);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (*p) PCHAR(*p--);
|
||||||
|
|
||||||
|
if (ladjust && width && (width -= tmp) > 0)
|
||||||
|
{
|
||||||
|
while (width--) PCHAR(padc);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
while (percent < fmt) PCHAR(*percent++);
|
||||||
|
/*
|
||||||
|
* Since we ignore an formatting argument it is no
|
||||||
|
* longer safe to obey the remaining formatting
|
||||||
|
* arguments as the arguments will no longer match
|
||||||
|
* the format specs.
|
||||||
|
*/
|
||||||
|
stop = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#undef PCHAR
|
||||||
|
#undef OPCHAR
|
||||||
|
|
@ -57,7 +57,7 @@ int qse_vfprintf (QSE_FILE *stream, const qse_char_t* fmt, va_list ap)
|
|||||||
FILE* fp;
|
FILE* fp;
|
||||||
|
|
||||||
nf = __adjust_format (fmt);
|
nf = __adjust_format (fmt);
|
||||||
if (nf == NULL) return -1;
|
if (nf == QSE_NULL) return -1;
|
||||||
|
|
||||||
fp = STREAM_TO_FILE (stream);
|
fp = STREAM_TO_FILE (stream);
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ int qse_vprintf (const qse_char_t* fmt, va_list ap)
|
|||||||
qse_char_t* nf;
|
qse_char_t* nf;
|
||||||
|
|
||||||
nf = __adjust_format (fmt);
|
nf = __adjust_format (fmt);
|
||||||
if (nf == NULL) return -1;
|
if (nf == QSE_NULL) return -1;
|
||||||
|
|
||||||
#if defined(QSE_CHAR_IS_MCHAR)
|
#if defined(QSE_CHAR_IS_MCHAR)
|
||||||
n = vfprintf (stdout, nf, ap);
|
n = vfprintf (stdout, nf, ap);
|
||||||
@ -96,7 +96,7 @@ int qse_fprintf (QSE_FILE* stream, const qse_char_t* fmt, ...)
|
|||||||
FILE* fp;
|
FILE* fp;
|
||||||
|
|
||||||
nf = __adjust_format (fmt);
|
nf = __adjust_format (fmt);
|
||||||
if (nf == NULL) return -1;
|
if (nf == QSE_NULL) return -1;
|
||||||
|
|
||||||
fp = STREAM_TO_FILE (stream);
|
fp = STREAM_TO_FILE (stream);
|
||||||
|
|
||||||
@ -119,7 +119,7 @@ int qse_printf (const qse_char_t* fmt, ...)
|
|||||||
qse_char_t* nf;
|
qse_char_t* nf;
|
||||||
|
|
||||||
nf = __adjust_format (fmt);
|
nf = __adjust_format (fmt);
|
||||||
if (nf == NULL) return -1;
|
if (nf == QSE_NULL) return -1;
|
||||||
|
|
||||||
va_start (ap, fmt);
|
va_start (ap, fmt);
|
||||||
#if defined(QSE_CHAR_IS_MCHAR)
|
#if defined(QSE_CHAR_IS_MCHAR)
|
||||||
@ -140,7 +140,7 @@ int qse_dprintf (const qse_char_t* fmt, ...)
|
|||||||
qse_char_t* nf;
|
qse_char_t* nf;
|
||||||
|
|
||||||
nf = __adjust_format (fmt);
|
nf = __adjust_format (fmt);
|
||||||
if (nf == NULL) return -1;
|
if (nf == QSE_NULL) return -1;
|
||||||
|
|
||||||
va_start (ap, fmt);
|
va_start (ap, fmt);
|
||||||
#if defined(QSE_CHAR_IS_MCHAR)
|
#if defined(QSE_CHAR_IS_MCHAR)
|
||||||
@ -158,7 +158,7 @@ int qse_vsprintf (qse_char_t* buf, qse_size_t size, const qse_char_t* fmt, va_li
|
|||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
qse_char_t* nf = __adjust_format (fmt);
|
qse_char_t* nf = __adjust_format (fmt);
|
||||||
if (nf == NULL) return -1;
|
if (nf == QSE_NULL) return -1;
|
||||||
|
|
||||||
#if defined(QSE_CHAR_IS_MCHAR)
|
#if defined(QSE_CHAR_IS_MCHAR)
|
||||||
#if defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && (__WATCOMC__ < 1200)) || defined(HAVE__VSNPRINTF)
|
#if defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && (__WATCOMC__ < 1200)) || defined(HAVE__VSNPRINTF)
|
||||||
@ -207,10 +207,10 @@ int qse_sprintf (qse_char_t* buf, qse_size_t size, const qse_char_t* fmt, ...)
|
|||||||
tmp = (qse_char_t*) QSE_MMGR_REALLOC ( \
|
tmp = (qse_char_t*) QSE_MMGR_REALLOC ( \
|
||||||
QSE_MMGR_GETDFL(), buf.ptr, \
|
QSE_MMGR_GETDFL(), buf.ptr, \
|
||||||
QSE_SIZEOF(qse_char_t) * (buf.cap+256+1)); \
|
QSE_SIZEOF(qse_char_t) * (buf.cap+256+1)); \
|
||||||
if (tmp == NULL) \
|
if (tmp == QSE_NULL) \
|
||||||
{ \
|
{ \
|
||||||
QSE_MMGR_FREE (QSE_MMGR_GETDFL(), buf.ptr); \
|
QSE_MMGR_FREE (QSE_MMGR_GETDFL(), buf.ptr); \
|
||||||
return NULL; \
|
return QSE_NULL; \
|
||||||
} \
|
} \
|
||||||
buf.ptr = tmp; \
|
buf.ptr = tmp; \
|
||||||
buf.cap = buf.cap + 256; \
|
buf.cap = buf.cap + 256; \
|
||||||
@ -236,7 +236,7 @@ static qse_char_t* __adjust_format (const qse_char_t* format)
|
|||||||
|
|
||||||
buf.ptr = (qse_char_t*) QSE_MMGR_ALLOC (
|
buf.ptr = (qse_char_t*) QSE_MMGR_ALLOC (
|
||||||
QSE_MMGR_GETDFL(), QSE_SIZEOF(qse_char_t) * (buf.cap+1));
|
QSE_MMGR_GETDFL(), QSE_SIZEOF(qse_char_t) * (buf.cap+1));
|
||||||
if (buf.ptr == NULL) return NULL;
|
if (buf.ptr == QSE_NULL) return QSE_NULL;
|
||||||
|
|
||||||
while (*fp != QSE_T('\0'))
|
while (*fp != QSE_T('\0'))
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user