moo/t/t-003.c

80 lines
1.7 KiB
C
Raw Normal View History

#include <moo-fmt.h>
2019-05-27 10:00:27 +00:00
#include <moo-utl.h>
#include <string.h>
#include <stdio.h>
2019-05-27 17:50:24 +00:00
#include <wchar.h>
#include <locale.h>
#include <stdarg.h>
#include "t.h"
2019-05-27 10:00:27 +00:00
static int put_bcs (moo_fmtout_t* fmtout, const moo_bch_t* c, moo_oow_t len)
{
while (len > 0)
{
putchar (*c);
c++;
len--;
}
return 1;
}
static int put_ucs (moo_fmtout_t* fmtout, const moo_uch_t* c, moo_oow_t len)
{
2019-05-27 17:50:24 +00:00
moo_cmgr_t* cmgr = moo_get_utf8_cmgr();
moo_bch_t bcs[MOO_BCSIZE_MAX];
moo_oow_t bcslen, i;
2019-05-27 10:00:27 +00:00
while (len > 0)
{
2019-05-27 17:50:24 +00:00
bcslen = cmgr->uctobc(*c, bcs, MOO_COUNTOF(bcs));
/*putwchar (*c);*/
for (i = 0; i < bcslen; i++) putchar(bcs[i]);
2019-05-27 10:00:27 +00:00
c++;
len--;
}
return 1;
}
2019-05-27 17:50:24 +00:00
static moo_ooi_t bfmt_out (const moo_bch_t* fmt, ...)
2019-05-27 10:00:27 +00:00
{
moo_fmtout_t fmtout;
va_list ap;
2019-05-27 17:50:24 +00:00
int n;
2019-05-27 10:00:27 +00:00
memset (&fmtout, 0, MOO_SIZEOF(fmtout));
fmtout.putbchars = put_bcs;
fmtout.putuchars = put_ucs;
2019-05-27 10:00:27 +00:00
va_start (ap, fmt);
2019-05-27 17:50:24 +00:00
n = moo_bfmt_outv (&fmtout, fmt, ap);
2019-05-27 10:00:27 +00:00
va_end (ap);
2019-05-27 17:50:24 +00:00
return (n <= -1)? -1: fmtout.count;
2019-05-27 10:00:27 +00:00
}
2019-05-27 17:50:24 +00:00
int main ()
2019-05-27 10:00:27 +00:00
{
2019-05-27 17:50:24 +00:00
moo_uch_t x[] = { 'A', L'\uBB33', L'\uBB34', L'\uBB35', 'Q', '\0' };
moo_ooi_t cnt;
2019-05-27 10:00:27 +00:00
2019-05-27 17:50:24 +00:00
setlocale (LC_ALL, NULL);
cnt = bfmt_out ("[%s %d %020X %ls %s %w %.*lk]\n", "test", 10, 0x1232, x, "code", x, MOO_SIZEOF_UCH_T * 3, x);
2019-05-27 10:00:27 +00:00
2019-05-27 17:50:24 +00:00
/* this unit is the number of characters written. but some are written as bch and some other as uch.
* if uch and bch data are mixed, the count returned doesn't really tell how many bytes or characters written */
bfmt_out ("wrote [%ld] units\n", cnt);
#if (MOO_SIZEOF_UCH_T == 2)
# define EXPECTED_LEN 98
#elif (MOO_SIZEOF_UCH_T == 4)
# define EXPECTED_LEN 122
#else
# error UNSUPPORTED UCH SIZE
#endif
T_ASSERT1 (cnt == EXPECTED_LEN, "bfmt_out test #1");
2019-05-27 10:00:27 +00:00
return 0;
oops:
return -1;
2019-05-27 10:00:27 +00:00
}