started writing qse_strfmt()/qse_strxfmt() and the famaily

This commit is contained in:
hyung-hwan 2013-10-24 14:55:15 +00:00
parent 636967779d
commit 198bd714b0
14 changed files with 875 additions and 733 deletions

View File

@ -1,7 +1,6 @@
#include <qse/http/stdhttpd.h> #include <qse/http/stdhttpd.h>
#include <qse/xli/stdxli.h> #include <qse/xli/stdxli.h>
#include <qse/cmn/stdio.h>
#include <qse/cmn/main.h> #include <qse/cmn/main.h>
#include <qse/cmn/str.h> #include <qse/cmn/str.h>
#include <qse/cmn/mem.h> #include <qse/cmn/mem.h>
@ -48,6 +47,9 @@
# include <sys/resource.h> # include <sys/resource.h>
#endif #endif
#define QSE_STDOUT qse_getstdout()
#define QSE_STDERR qse_getstderr()
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
static qse_httpd_t* g_httpd = QSE_NULL; static qse_httpd_t* g_httpd = QSE_NULL;
@ -853,8 +855,8 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
cfg->xcfg[i] = qse_httpd_strntombsdup (httpd, ((qse_xli_str_t*)pair->val)->ptr, ((qse_xli_str_t*)pair->val)->len); cfg->xcfg[i] = qse_httpd_strntombsdup (httpd, ((qse_xli_str_t*)pair->val)->ptr, ((qse_xli_str_t*)pair->val)->len);
if (cfg->xcfg[i] == QSE_NULL) if (cfg->xcfg[i] == QSE_NULL)
{ {
/*qse_printf (QSE_T("ERROR in copying - %s\n"), qse_httpd_geterrmsg (httpd));*/ /*qse_putstrf (QSE_T("ERROR in copying - %s\n"), qse_httpd_geterrmsg (httpd));*/
qse_printf (QSE_T("ERROR in copying\n")); qse_putstrf (QSE_T("ERROR in copying\n"));
return -1; return -1;
} }
} }
@ -870,7 +872,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
duptmp = qse_xli_dupflatstr (httpd_xtn->xli, (qse_xli_str_t*)pair->val, &duplen, &count); duptmp = qse_xli_dupflatstr (httpd_xtn->xli, (qse_xli_str_t*)pair->val, &duplen, &count);
if (duptmp == QSE_NULL) if (duptmp == QSE_NULL)
{ {
qse_printf (QSE_T("ERROR: memory failure in copying index\n")); qse_putstrf (QSE_T("ERROR: memory failure in copying index\n"));
return -1; return -1;
} }
@ -879,7 +881,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
if (cfg->index.files == QSE_NULL) if (cfg->index.files == QSE_NULL)
{ {
qse_printf (QSE_T("ERROR: memory failure in copying index\n")); qse_putstrf (QSE_T("ERROR: memory failure in copying index\n"));
return -1; return -1;
} }
@ -908,7 +910,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
cgi = qse_httpd_callocmem (httpd, QSE_SIZEOF(*cgi)); cgi = qse_httpd_callocmem (httpd, QSE_SIZEOF(*cgi));
if (cgi == QSE_NULL) if (cgi == QSE_NULL)
{ {
qse_printf (QSE_T("ERROR: memory failure in copying cgi\n")); qse_putstrf (QSE_T("ERROR: memory failure in copying cgi\n"));
return -1; return -1;
} }
@ -917,7 +919,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
if (!cgi->spec) if (!cgi->spec)
{ {
qse_httpd_freemem (httpd, cgi); qse_httpd_freemem (httpd, cgi);
qse_printf (QSE_T("ERROR: memory failure in copying cgi name\n")); qse_putstrf (QSE_T("ERROR: memory failure in copying cgi name\n"));
return -1; return -1;
} }
if (pair->val->type == QSE_XLI_STR) if (pair->val->type == QSE_XLI_STR)
@ -940,7 +942,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
{ {
qse_httpd_freemem (httpd, cgi->spec); qse_httpd_freemem (httpd, cgi->spec);
qse_httpd_freemem (httpd, cgi); qse_httpd_freemem (httpd, cgi);
qse_printf (QSE_T("ERROR: memory failure in copying cgi shebang\n")); qse_putstrf (QSE_T("ERROR: memory failure in copying cgi shebang\n"));
return -1; return -1;
} }
} }
@ -982,7 +984,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
auth_rule = qse_httpd_callocmem (httpd, QSE_SIZEOF(*auth_rule)); auth_rule = qse_httpd_callocmem (httpd, QSE_SIZEOF(*auth_rule));
if (auth_rule == QSE_NULL) if (auth_rule == QSE_NULL)
{ {
qse_printf (QSE_T("ERROR: memory failure in copying auth-rule\n")); qse_putstrf (QSE_T("ERROR: memory failure in copying auth-rule\n"));
return -1; return -1;
} }
@ -993,7 +995,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
if (!auth_rule->spec) if (!auth_rule->spec)
{ {
qse_httpd_freemem (httpd, auth_rule); qse_httpd_freemem (httpd, auth_rule);
qse_printf (QSE_T("ERROR: memory failure in copying auth-rule\n")); qse_putstrf (QSE_T("ERROR: memory failure in copying auth-rule\n"));
return -1; return -1;
} }
} }
@ -1032,7 +1034,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
mime = qse_httpd_callocmem (httpd, QSE_SIZEOF(*mime)); mime = qse_httpd_callocmem (httpd, QSE_SIZEOF(*mime));
if (mime == QSE_NULL) if (mime == QSE_NULL)
{ {
qse_printf (QSE_T("ERROR: memory failure in copying mime\n")); qse_putstrf (QSE_T("ERROR: memory failure in copying mime\n"));
return -1; return -1;
} }
@ -1041,7 +1043,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
if (!mime->spec) if (!mime->spec)
{ {
qse_httpd_freemem (httpd, mime); qse_httpd_freemem (httpd, mime);
qse_printf (QSE_T("ERROR: memory failure in copying mime\n")); qse_putstrf (QSE_T("ERROR: memory failure in copying mime\n"));
return -1; return -1;
} }
@ -1050,7 +1052,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
{ {
qse_httpd_freemem (httpd, mime->spec); qse_httpd_freemem (httpd, mime->spec);
qse_httpd_freemem (httpd, mime); qse_httpd_freemem (httpd, mime);
qse_printf (QSE_T("ERROR: memory failure in copying mime\n")); qse_putstrf (QSE_T("ERROR: memory failure in copying mime\n"));
return -1; return -1;
} }
@ -1097,7 +1099,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
acc = qse_httpd_callocmem (httpd, QSE_SIZEOF(*acc)); acc = qse_httpd_callocmem (httpd, QSE_SIZEOF(*acc));
if (acc == QSE_NULL) if (acc == QSE_NULL)
{ {
qse_printf (QSE_T("ERROR: memory failure in copying acc\n")); qse_putstrf (QSE_T("ERROR: memory failure in copying acc\n"));
return -1; return -1;
} }
@ -1108,7 +1110,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
if (!acc->spec) if (!acc->spec)
{ {
qse_httpd_freemem (httpd, acc); qse_httpd_freemem (httpd, acc);
qse_printf (QSE_T("ERROR: memory failure in copying access\n")); qse_putstrf (QSE_T("ERROR: memory failure in copying access\n"));
return -1; return -1;
} }
} }
@ -1129,7 +1131,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg)
/* TODO: perform more sanity check */ /* TODO: perform more sanity check */
if (qse_mbschr (cfg->xcfg[XCFG_AUTH], QSE_MT(':')) == QSE_NULL) if (qse_mbschr (cfg->xcfg[XCFG_AUTH], QSE_MT(':')) == QSE_NULL)
{ {
qse_printf (QSE_T("WARNING: no colon in the auth string - [%hs]\n"), cfg->xcfg[XCFG_AUTH]); qse_putstrf (QSE_T("WARNING: no colon in the auth string - [%hs]\n"), cfg->xcfg[XCFG_AUTH]);
} }
#endif #endif
@ -1205,8 +1207,8 @@ static int load_server_config (qse_httpd_t* httpd, qse_httpd_server_t* server, q
server_xtn->scfg[i] = qse_httpd_strntombsdup (httpd, ((qse_xli_str_t*)pair->val)->ptr, ((qse_xli_str_t*)pair->val)->len); server_xtn->scfg[i] = qse_httpd_strntombsdup (httpd, ((qse_xli_str_t*)pair->val)->ptr, ((qse_xli_str_t*)pair->val)->len);
if (server_xtn->scfg[i] == QSE_NULL) if (server_xtn->scfg[i] == QSE_NULL)
{ {
/*qse_printf (QSE_T("ERROR in copying - %s\n"), qse_httpd_geterrmsg (httpd));*/ /*qse_putstrf (QSE_T("ERROR in copying - %s\n"), qse_httpd_geterrmsg (httpd));*/
qse_printf (QSE_T("ERROR in copying\n")); qse_putstrf (QSE_T("ERROR in copying\n"));
return -1; return -1;
} }
} }
@ -1329,7 +1331,7 @@ static qse_httpd_server_t* attach_server (qse_httpd_t* httpd, int num, qse_xli_l
if (pair == QSE_NULL || pair->val->type != QSE_XLI_STR) if (pair == QSE_NULL || pair->val->type != QSE_XLI_STR)
{ {
/* TOOD: logging */ /* TOOD: logging */
qse_printf (QSE_T("WARNING: no value or invalid value specified for bind\n")); qse_putstrf (QSE_T("WARNING: no value or invalid value specified for bind\n"));
return QSE_NULL; return QSE_NULL;
} }
@ -1337,7 +1339,7 @@ static qse_httpd_server_t* attach_server (qse_httpd_t* httpd, int num, qse_xli_l
if (qse_strntonwad (((qse_xli_str_t*)pair->val)->ptr, ((qse_xli_str_t*)pair->val)->len, &dope.nwad) <= -1) if (qse_strntonwad (((qse_xli_str_t*)pair->val)->ptr, ((qse_xli_str_t*)pair->val)->len, &dope.nwad) <= -1)
{ {
/* TOOD: logging */ /* TOOD: logging */
qse_printf (QSE_T("WARNING: invalid value for bind - %s\n"), ((qse_xli_str_t*)pair->val)->ptr); qse_putstrf (QSE_T("WARNING: invalid value for bind - %s\n"), ((qse_xli_str_t*)pair->val)->ptr);
return QSE_NULL; return QSE_NULL;
} }
@ -1351,7 +1353,7 @@ static qse_httpd_server_t* attach_server (qse_httpd_t* httpd, int num, qse_xli_l
if (xserver == QSE_NULL) if (xserver == QSE_NULL)
{ {
/* TODO: logging */ /* TODO: logging */
qse_printf (QSE_T("WARNING: failed to attach server\n")); qse_putstrf (QSE_T("WARNING: failed to attach server\n"));
return QSE_NULL; return QSE_NULL;
} }
@ -1474,7 +1476,7 @@ static int open_config_file (qse_httpd_t* httpd)
httpd_xtn->xli = qse_xli_openstd (0, 0); httpd_xtn->xli = qse_xli_openstd (0, 0);
if (httpd_xtn->xli == QSE_NULL) if (httpd_xtn->xli == QSE_NULL)
{ {
qse_fprintf (QSE_STDERR, QSE_T("Cannot open xli\n")); qse_sio_putstrf (QSE_STDERR, QSE_T("Cannot open xli\n"));
return -1; return -1;
} }
@ -1486,7 +1488,7 @@ static int open_config_file (qse_httpd_t* httpd)
{ {
if (qse_xli_definepair (httpd_xtn->xli, defs[i].name, &defs[i].scm) <= -1) if (qse_xli_definepair (httpd_xtn->xli, defs[i].name, &defs[i].scm) <= -1)
{ {
qse_fprintf (QSE_STDERR, QSE_T("Cannot define %s - %s\n"), defs[i].name, qse_xli_geterrmsg(httpd_xtn->xli)); qse_sio_putstrf (QSE_STDERR, QSE_T("Cannot define %s - %s\n"), defs[i].name, qse_xli_geterrmsg(httpd_xtn->xli));
qse_xli_close (httpd_xtn->xli); qse_xli_close (httpd_xtn->xli);
httpd_xtn->xli = QSE_NULL; httpd_xtn->xli = QSE_NULL;
return -1; return -1;
@ -1505,12 +1507,12 @@ static int open_config_file (qse_httpd_t* httpd)
if (errloc->line > 0 || errloc->colm > 0) if (errloc->line > 0 || errloc->colm > 0)
{ {
qse_fprintf (QSE_STDERR, QSE_T("Cannot load %s at line %lu column %lu - %s\n"), qse_sio_putstrf (QSE_STDERR, QSE_T("Cannot load %s at line %lu column %lu - %s\n"),
xli_in.u.file.path, (unsigned long int)errloc->line, (unsigned long int)errloc->colm, qse_xli_geterrmsg(httpd_xtn->xli)); xli_in.u.file.path, (unsigned long int)errloc->line, (unsigned long int)errloc->colm, qse_xli_geterrmsg(httpd_xtn->xli));
} }
else else
{ {
qse_fprintf (QSE_STDERR, QSE_T("Cannot load %s - %s\n"), xli_in.u.file.path, qse_xli_geterrmsg(httpd_xtn->xli)); qse_sio_putstrf (QSE_STDERR, QSE_T("Cannot load %s - %s\n"), xli_in.u.file.path, qse_xli_geterrmsg(httpd_xtn->xli));
} }
qse_xli_close (httpd_xtn->xli); qse_xli_close (httpd_xtn->xli);
@ -1609,7 +1611,7 @@ static int load_config (qse_httpd_t* httpd)
if (pair->val->type != QSE_XLI_LIST) if (pair->val->type != QSE_XLI_LIST)
{ {
qse_fprintf (QSE_STDERR, QSE_T("WARNING: non-list value for server\n")); qse_sio_putstrf (QSE_STDERR, QSE_T("WARNING: non-list value for server\n"));
} }
else else
{ {
@ -1620,7 +1622,7 @@ static int load_config (qse_httpd_t* httpd)
{ {
if (load_server_config (httpd, server, (qse_xli_list_t*)pair->val) <= -1) if (load_server_config (httpd, server, (qse_xli_list_t*)pair->val) <= -1)
{ {
qse_fprintf (QSE_STDERR, QSE_T("failed to load configuration from %s\n"), httpd_xtn->cfgfile); qse_sio_putstrf (QSE_STDERR, QSE_T("failed to load configuration from %s\n"), httpd_xtn->cfgfile);
goto oops; goto oops;
} }
} }
@ -1629,7 +1631,7 @@ static int load_config (qse_httpd_t* httpd)
if (i == 0) if (i == 0)
{ {
qse_fprintf (QSE_STDERR, QSE_T("No valid server specified in %s\n"), httpd_xtn->cfgfile); qse_sio_putstrf (QSE_STDERR, QSE_T("No valid server specified in %s\n"), httpd_xtn->cfgfile);
goto oops; goto oops;
} }
@ -1639,7 +1641,7 @@ static int load_config (qse_httpd_t* httpd)
{ {
if (load_loccfg (httpd, (qse_xli_list_t*)pair->val, &httpd_xtn->dflcfg) <= -1) if (load_loccfg (httpd, (qse_xli_list_t*)pair->val, &httpd_xtn->dflcfg) <= -1)
{ {
qse_fprintf (QSE_STDERR, QSE_T("failed to load configuration from %s\n"), httpd_xtn->cfgfile); qse_sio_putstrf (QSE_STDERR, QSE_T("failed to load configuration from %s\n"), httpd_xtn->cfgfile);
goto oops; goto oops;
} }
} }
@ -1713,27 +1715,27 @@ static void logact_httpd (qse_httpd_t* httpd, const qse_httpd_act_t* act)
switch (act->code) switch (act->code)
{ {
case QSE_HTTPD_CATCH_MERRMSG: case QSE_HTTPD_CATCH_MERRMSG:
qse_printf (QSE_T("ERROR: %hs\n"), act->u.merrmsg); qse_putstrf (QSE_T("ERROR: %hs\n"), act->u.merrmsg);
break; break;
case QSE_HTTPD_CATCH_MDBGMSG: case QSE_HTTPD_CATCH_MDBGMSG:
qse_printf (QSE_T("DEBUG: %hs\n"), act->u.mdbgmsg); qse_putstrf (QSE_T("DEBUG: %hs\n"), act->u.mdbgmsg);
break; break;
case QSE_HTTPD_ACCEPT_CLIENT: case QSE_HTTPD_ACCEPT_CLIENT:
qse_nwadtostr (&act->u.client->local_addr, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL); qse_nwadtostr (&act->u.client->local_addr, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL);
qse_nwadtostr (&act->u.client->orgdst_addr, tmp2, QSE_COUNTOF(tmp2), QSE_NWADTOSTR_ALL); qse_nwadtostr (&act->u.client->orgdst_addr, tmp2, QSE_COUNTOF(tmp2), QSE_NWADTOSTR_ALL);
qse_nwadtostr (&act->u.client->remote_addr, tmp3, QSE_COUNTOF(tmp3), QSE_NWADTOSTR_ALL); qse_nwadtostr (&act->u.client->remote_addr, tmp3, QSE_COUNTOF(tmp3), QSE_NWADTOSTR_ALL);
qse_printf (QSE_T("accepted client %s(%s) from %s\n"), tmp, tmp2, tmp3); qse_putstrf (QSE_T("accepted client %s(%s) from %s\n"), tmp, tmp2, tmp3);
case QSE_HTTPD_PURGE_CLIENT: case QSE_HTTPD_PURGE_CLIENT:
qse_nwadtostr (&act->u.client->remote_addr, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL); qse_nwadtostr (&act->u.client->remote_addr, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL);
qse_printf (QSE_T("purged client - %s\n"), tmp); qse_putstrf (QSE_T("purged client - %s\n"), tmp);
break; break;
case QSE_HTTPD_READERR_CLIENT: case QSE_HTTPD_READERR_CLIENT:
qse_nwadtostr (&act->u.client->remote_addr, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL); qse_nwadtostr (&act->u.client->remote_addr, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL);
qse_printf (QSE_T("failed to read client - %s\n"), tmp); qse_putstrf (QSE_T("failed to read client - %s\n"), tmp);
break; break;
} }
} }
@ -1741,23 +1743,23 @@ static void logact_httpd (qse_httpd_t* httpd, const qse_httpd_act_t* act)
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
static void print_version (void) static void print_version (void)
{ {
qse_printf (QSE_T("QSEHTTPD version %hs\n"), QSE_PACKAGE_VERSION); qse_putstrf (QSE_T("QSEHTTPD version %hs\n"), QSE_PACKAGE_VERSION);
} }
static void print_usage (QSE_FILE* out, int argc, qse_char_t* argv[]) static void print_usage (qse_sio_t* out, int argc, qse_char_t* argv[])
{ {
const qse_char_t* b = qse_basename (argv[0]); const qse_char_t* b = qse_basename (argv[0]);
qse_fprintf (out, QSE_T("USAGE: %s [options] -c file\n"), b); qse_sio_putstrf (out, QSE_T("USAGE: %s [options] -c file\n"), b);
qse_fprintf (out, QSE_T(" %s [options] --config-file file\n"), b); qse_sio_putstrf (out, QSE_T(" %s [options] --config-file file\n"), b);
qse_fprintf (out, QSE_T("options as follows:\n")); qse_sio_putstrf (out, QSE_T("options as follows:\n"));
qse_fprintf (out, QSE_T(" -h/--help show this message\n")); qse_sio_putstrf (out, QSE_T(" -h/--help show this message\n"));
qse_fprintf (out, QSE_T(" --version show version\n")); qse_sio_putstrf (out, QSE_T(" --version show version\n"));
qse_fprintf (out, QSE_T(" -c/--config-file file specify a configuration file\n")); qse_sio_putstrf (out, QSE_T(" -c/--config-file file specify a configuration file\n"));
qse_fprintf (out, QSE_T(" -d/--daemon run in the background\n")); qse_sio_putstrf (out, QSE_T(" -d/--daemon run in the background\n"));
qse_fprintf (out, QSE_T(" -n string specify a process name if supported\n")); qse_sio_putstrf (out, QSE_T(" -n string specify a process name if supported\n"));
qse_fprintf (out, QSE_T(" -x output debugging messages\n")); qse_sio_putstrf (out, QSE_T(" -x output debugging messages\n"));
} }
static int handle_args (int argc, qse_char_t* argv[]) static int handle_args (int argc, qse_char_t* argv[])
@ -1785,14 +1787,14 @@ static int handle_args (int argc, qse_char_t* argv[])
goto wrongusage; goto wrongusage;
case QSE_T('?'): case QSE_T('?'):
qse_fprintf (QSE_STDERR, qse_sio_putstrf (QSE_STDERR,
QSE_T("ERROR: bad option - %c\n"), QSE_T("ERROR: bad option - %c\n"),
opt.opt opt.opt
); );
goto wrongusage; goto wrongusage;
case QSE_T(':'): case QSE_T(':'):
qse_fprintf (QSE_STDERR, qse_sio_putstrf (QSE_STDERR,
QSE_T("ERROR: bad parameter for %c\n"), QSE_T("ERROR: bad parameter for %c\n"),
opt.opt opt.opt
); );
@ -1880,7 +1882,7 @@ static int httpd_main (int argc, qse_char_t* argv[])
httpd = qse_httpd_openstd (QSE_SIZEOF(httpd_xtn_t)); httpd = qse_httpd_openstd (QSE_SIZEOF(httpd_xtn_t));
if (httpd == QSE_NULL) if (httpd == QSE_NULL)
{ {
qse_fprintf (QSE_STDERR, QSE_T("ERROR: Cannot open httpd\n")); qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: Cannot open httpd\n"));
goto oops; goto oops;
} }
@ -1896,7 +1898,7 @@ static int httpd_main (int argc, qse_char_t* argv[])
{ {
if (daemonize (1) <= -1) if (daemonize (1) <= -1)
{ {
qse_fprintf (QSE_STDERR, QSE_T("ERROR: Cannot daemonize\n")); qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: Cannot daemonize\n"));
goto oops; goto oops;
} }
} }
@ -1927,7 +1929,7 @@ static int httpd_main (int argc, qse_char_t* argv[])
restore_signal_handlers (); restore_signal_handlers ();
g_httpd = QSE_NULL; g_httpd = QSE_NULL;
if (ret <= -1) qse_fprintf (QSE_STDERR, QSE_T("Httpd error - %d\n"), qse_httpd_geterrnum (httpd)); if (ret <= -1) qse_sio_putstrf (QSE_STDERR, QSE_T("Httpd error - %d\n"), qse_httpd_geterrnum (httpd));
oops: oops:
if (httpd) qse_httpd_close (httpd); if (httpd) qse_httpd_close (httpd);
@ -1942,6 +1944,13 @@ int qse_main (int argc, qse_achar_t* argv[])
char locale[100]; char locale[100];
UINT codepage; UINT codepage;
WSADATA wsadata; WSADATA wsadata;
#else
/* nothing */
#endif
qse_openstdsios ();
#if defined(_WIN32)
codepage = GetConsoleOutputCP(); codepage = GetConsoleOutputCP();
if (codepage == CP_UTF8) if (codepage == CP_UTF8)
@ -1960,7 +1969,7 @@ int qse_main (int argc, qse_achar_t* argv[])
if (WSAStartup (MAKEWORD(2,0), &wsadata) != 0) if (WSAStartup (MAKEWORD(2,0), &wsadata) != 0)
{ {
qse_fprintf (QSE_STDERR, QSE_T("Failed to start up winsock\n")); qse_sio_putstrf (QSE_STDERR, QSE_T("Failed to start up winsock\n"));
return -1; return -1;
} }
@ -1989,5 +1998,6 @@ int qse_main (int argc, qse_achar_t* argv[])
WSACleanup (); WSACleanup ();
#endif #endif
qse_closestdsios ();
return ret; return ret;
} }

View File

@ -27,7 +27,6 @@
#include <qse/cmn/xma.h> #include <qse/cmn/xma.h>
#include <qse/cmn/path.h> #include <qse/cmn/path.h>
#include <qse/cmn/fs.h> #include <qse/cmn/fs.h>
#include <qse/cmn/stdio.h>
#include <qse/cmn/main.h> #include <qse/cmn/main.h>
#include <qse/cmn/mbwc.h> #include <qse/cmn/mbwc.h>
#include <qse/cmn/glob.h> #include <qse/cmn/glob.h>
@ -55,6 +54,9 @@
# include <signal.h> # include <signal.h>
#endif #endif
#define QSE_STDOUT qse_getstdout()
#define QSE_STDERR qse_getstderr()
static struct static struct
{ {
qse_sed_iostd_t* io; qse_sed_iostd_t* io;
@ -140,46 +142,46 @@ static qse_mmgr_t xma_mmgr =
static void print_version (void) static void print_version (void)
{ {
qse_printf (QSE_T("QSESED version %hs\n"), QSE_PACKAGE_VERSION); qse_putstrf (QSE_T("QSESED version %hs\n"), QSE_PACKAGE_VERSION);
} }
static void print_usage (QSE_FILE* out, int argc, qse_char_t* argv[]) static void print_usage (qse_sio_t* out, int argc, qse_char_t* argv[])
{ {
const qse_char_t* b = qse_basename (argv[0]); const qse_char_t* b = qse_basename (argv[0]);
qse_fprintf (out, QSE_T("USAGE: %s [options] script [file]\n"), b); qse_sio_putstrf (out, QSE_T("USAGE: %s [options] script [file]\n"), b);
qse_fprintf (out, QSE_T(" %s [options] -f script-file [file]\n"), b); qse_sio_putstrf (out, QSE_T(" %s [options] -f script-file [file]\n"), b);
qse_fprintf (out, QSE_T(" %s [options] -e script [file]\n"), b); qse_sio_putstrf (out, QSE_T(" %s [options] -e script [file]\n"), b);
qse_fprintf (out, QSE_T("options as follows:\n")); qse_sio_putstrf (out, QSE_T("options as follows:\n"));
qse_fprintf (out, QSE_T(" -h/--help show this message\n")); qse_sio_putstrf (out, QSE_T(" -h/--help show this message\n"));
qse_fprintf (out, QSE_T(" --version show version\n")); qse_sio_putstrf (out, QSE_T(" --version show version\n"));
qse_fprintf (out, QSE_T(" -n disable auto-print\n")); qse_sio_putstrf (out, QSE_T(" -n disable auto-print\n"));
qse_fprintf (out, QSE_T(" -e script specify a script\n")); qse_sio_putstrf (out, QSE_T(" -e script specify a script\n"));
qse_fprintf (out, QSE_T(" -f file specify a script file\n")); qse_sio_putstrf (out, QSE_T(" -f file specify a script file\n"));
qse_fprintf (out, QSE_T(" -o file specify an output file\n")); qse_sio_putstrf (out, QSE_T(" -o file specify an output file\n"));
qse_fprintf (out, QSE_T(" -r use the extended regular expression\n")); qse_sio_putstrf (out, QSE_T(" -r use the extended regular expression\n"));
qse_fprintf (out, QSE_T(" -R enable non-standard extensions to the regular\n")); qse_sio_putstrf (out, QSE_T(" -R enable non-standard extensions to the regular\n"));
qse_fprintf (out, QSE_T(" expression\n")); qse_sio_putstrf (out, QSE_T(" expression\n"));
qse_fprintf (out, QSE_T(" -i perform in-place editing. imply -s\n")); qse_sio_putstrf (out, QSE_T(" -i perform in-place editing. imply -s\n"));
qse_fprintf (out, QSE_T(" -s process input files separately\n")); qse_sio_putstrf (out, QSE_T(" -s process input files separately\n"));
qse_fprintf (out, QSE_T(" -a perform strict address and label check\n")); qse_sio_putstrf (out, QSE_T(" -a perform strict address and label check\n"));
qse_fprintf (out, QSE_T(" -b allow extended address formats\n")); qse_sio_putstrf (out, QSE_T(" -b allow extended address formats\n"));
qse_fprintf (out, QSE_T(" <start~step>,<start,+line>,<start,~line>,<0,/regex/>\n")); qse_sio_putstrf (out, QSE_T(" <start~step>,<start,+line>,<start,~line>,<0,/regex/>\n"));
qse_fprintf (out, QSE_T(" -x allow text on the same line as c, a, i\n")); qse_sio_putstrf (out, QSE_T(" -x allow text on the same line as c, a, i\n"));
qse_fprintf (out, QSE_T(" -y ensure a newline at text end\n")); qse_sio_putstrf (out, QSE_T(" -y ensure a newline at text end\n"));
qse_fprintf (out, QSE_T(" -m number specify the maximum amount of memory to use in bytes\n")); qse_sio_putstrf (out, QSE_T(" -m number specify the maximum amount of memory to use in bytes\n"));
qse_fprintf (out, QSE_T(" -w expand file wildcards\n")); qse_sio_putstrf (out, QSE_T(" -w expand file wildcards\n"));
#if defined(QSE_ENABLE_SEDTRACER) #if defined(QSE_ENABLE_SEDTRACER)
qse_fprintf (out, QSE_T(" -t print command traces\n")); qse_sio_putstrf (out, QSE_T(" -t print command traces\n"));
#endif #endif
#if defined(QSE_BUILD_DEBUG) #if defined(QSE_BUILD_DEBUG)
qse_fprintf (out, QSE_T(" -X number fail the number'th memory allocation\n")); qse_sio_putstrf (out, QSE_T(" -X number fail the number'th memory allocation\n"));
#endif #endif
#if defined(QSE_CHAR_IS_WCHAR) #if defined(QSE_CHAR_IS_WCHAR)
qse_fprintf (out, QSE_T(" --script-encoding string specify script file encoding name\n")); qse_sio_putstrf (out, QSE_T(" --script-encoding string specify script file encoding name\n"));
qse_fprintf (out, QSE_T(" --infile-encoding string specify input file encoding name\n")); qse_sio_putstrf (out, QSE_T(" --infile-encoding string specify input file encoding name\n"));
qse_fprintf (out, QSE_T(" --outfile-encoding string specify output file encoding name\n")); qse_sio_putstrf (out, QSE_T(" --outfile-encoding string specify output file encoding name\n"));
#endif #endif
} }
@ -195,7 +197,7 @@ static int add_script (const qse_char_t* str, int mem)
QSE_SIZEOF(*g_script.io) * (g_script.capa + 16 + 1)); QSE_SIZEOF(*g_script.io) * (g_script.capa + 16 + 1));
if (tmp == QSE_NULL) if (tmp == QSE_NULL)
{ {
qse_fprintf (QSE_STDERR, QSE_T("ERROR: out of memory while processing %s\n"), str); qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: out of memory while processing %s\n"), str);
return -1; return -1;
} }
@ -266,7 +268,7 @@ static int handle_args (int argc, qse_char_t* argv[])
goto oops; goto oops;
case QSE_T('?'): case QSE_T('?'):
qse_fprintf (QSE_STDERR, qse_sio_putstrf (QSE_STDERR,
QSE_T("ERROR: bad option - %c\n"), QSE_T("ERROR: bad option - %c\n"),
opt.opt opt.opt
); );
@ -274,7 +276,7 @@ static int handle_args (int argc, qse_char_t* argv[])
goto oops; goto oops;
case QSE_T(':'): case QSE_T(':'):
qse_fprintf (QSE_STDERR, qse_sio_putstrf (QSE_STDERR,
QSE_T("ERROR: bad parameter for %c\n"), QSE_T("ERROR: bad parameter for %c\n"),
opt.opt opt.opt
); );
@ -368,7 +370,7 @@ static int handle_args (int argc, qse_char_t* argv[])
g_script_cmgr = qse_findcmgr (opt.arg); g_script_cmgr = qse_findcmgr (opt.arg);
if (g_script_cmgr == QSE_NULL) if (g_script_cmgr == QSE_NULL)
{ {
qse_fprintf (QSE_STDERR, QSE_T("ERROR: unknown script encoding - %s\n"), opt.arg); qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: unknown script encoding - %s\n"), opt.arg);
goto oops; goto oops;
} }
} }
@ -377,7 +379,7 @@ static int handle_args (int argc, qse_char_t* argv[])
g_infile_cmgr = qse_findcmgr (opt.arg); g_infile_cmgr = qse_findcmgr (opt.arg);
if (g_infile_cmgr == QSE_NULL) if (g_infile_cmgr == QSE_NULL)
{ {
qse_fprintf (QSE_STDERR, QSE_T("ERROR: unknown input file encoding - %s\n"), opt.arg); qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: unknown input file encoding - %s\n"), opt.arg);
goto oops; goto oops;
} }
} }
@ -386,7 +388,7 @@ static int handle_args (int argc, qse_char_t* argv[])
g_outfile_cmgr = qse_findcmgr (opt.arg); g_outfile_cmgr = qse_findcmgr (opt.arg);
if (g_outfile_cmgr == QSE_NULL) if (g_outfile_cmgr == QSE_NULL)
{ {
qse_fprintf (QSE_STDERR, QSE_T("ERROR: unknown output file encoding - %s\n"), opt.arg); qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: unknown output file encoding - %s\n"), opt.arg);
goto oops; goto oops;
} }
} }
@ -425,7 +427,7 @@ void print_exec_error (qse_sed_t* sed)
const qse_sed_loc_t* errloc = qse_sed_geterrloc(sed); const qse_sed_loc_t* errloc = qse_sed_geterrloc(sed);
if (errloc->line > 0 || errloc->colm > 0) if (errloc->line > 0 || errloc->colm > 0)
{ {
qse_fprintf (QSE_STDERR, qse_sio_putstrf (QSE_STDERR,
QSE_T("ERROR: cannot execute - %s at line %lu column %lu\n"), QSE_T("ERROR: cannot execute - %s at line %lu column %lu\n"),
qse_sed_geterrmsg(sed), qse_sed_geterrmsg(sed),
(unsigned long)errloc->line, (unsigned long)errloc->line,
@ -434,7 +436,7 @@ void print_exec_error (qse_sed_t* sed)
} }
else else
{ {
qse_fprintf (QSE_STDERR, qse_sio_putstrf (QSE_STDERR,
QSE_T("ERROR: cannot execute - %s\n"), QSE_T("ERROR: cannot execute - %s\n"),
qse_sed_geterrmsg(sed) qse_sed_geterrmsg(sed)
); );
@ -567,16 +569,16 @@ static void trace_exec (qse_sed_t* sed, qse_sed_tracer_op_t op, const qse_sed_cm
switch (op) switch (op)
{ {
case QSE_SED_TRACER_READ: case QSE_SED_TRACER_READ:
/*qse_fprintf (QSE_STDERR, QSE_T("reading...\n"));*/ /*qse_sio_putstrf (QSE_STDERR, QSE_T("reading...\n"));*/
break; break;
case QSE_SED_TRACER_WRITE: case QSE_SED_TRACER_WRITE:
/*qse_fprintf (QSE_STDERR, QSE_T("wrting...\n"));*/ /*qse_sio_putstrf (QSE_STDERR, QSE_T("wrting...\n"));*/
break; break;
/* TODO: use function to get hold space and pattern space and print them */ /* TODO: use function to get hold space and pattern space and print them */
case QSE_SED_TRACER_MATCH: case QSE_SED_TRACER_MATCH:
qse_fprintf (QSE_STDERR, QSE_T("%s:%lu [%c] MA\n"), qse_sio_putstrf (QSE_STDERR, QSE_T("%s:%lu [%c] MA\n"),
((cmd->lid && cmd->lid[0])? cmd->lid: QSE_T("<<UNKNOWN>>")), ((cmd->lid && cmd->lid[0])? cmd->lid: QSE_T("<<UNKNOWN>>")),
(unsigned long)cmd->loc.line, (unsigned long)cmd->loc.line,
cmd->type cmd->type
@ -584,7 +586,7 @@ static void trace_exec (qse_sed_t* sed, qse_sed_tracer_op_t op, const qse_sed_cm
break; break;
case QSE_SED_TRACER_EXEC: case QSE_SED_TRACER_EXEC:
qse_fprintf (QSE_STDERR, QSE_T("%s:%lu [%c] EC\n"), qse_sio_putstrf (QSE_STDERR, QSE_T("%s:%lu [%c] EC\n"),
((cmd->lid && cmd->lid[0])? cmd->lid: QSE_T("<<UNKNOWN>>")), ((cmd->lid && cmd->lid[0])? cmd->lid: QSE_T("<<UNKNOWN>>")),
(unsigned long)cmd->loc.line, (unsigned long)cmd->loc.line,
cmd->type cmd->type
@ -709,7 +711,7 @@ static int sed_main (int argc, qse_char_t* argv[])
xma_mmgr.ctx = qse_xma_open (QSE_MMGR_GETDFL(), 0, g_memlimit); xma_mmgr.ctx = qse_xma_open (QSE_MMGR_GETDFL(), 0, g_memlimit);
if (xma_mmgr.ctx == QSE_NULL) if (xma_mmgr.ctx == QSE_NULL)
{ {
qse_printf (QSE_T("ERROR: cannot open memory heap\n")); qse_putstrf (QSE_T("ERROR: cannot open memory heap\n"));
goto oops; goto oops;
} }
mmgr = &xma_mmgr; mmgr = &xma_mmgr;
@ -720,13 +722,13 @@ static int sed_main (int argc, qse_char_t* argv[])
fs = qse_fs_open (mmgr, 0); fs = qse_fs_open (mmgr, 0);
if (fs == QSE_NULL) if (fs == QSE_NULL)
{ {
qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open file system handler\n")); qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: cannot open file system handler\n"));
goto oops; goto oops;
} }
if (qse_fs_chdir (fs, QSE_T(".")) <= -1) if (qse_fs_chdir (fs, QSE_T(".")) <= -1)
{ {
qse_fprintf (QSE_STDERR, qse_sio_putstrf (QSE_STDERR,
QSE_T("ERROR: cannot change direcotry in file system handler\n")); QSE_T("ERROR: cannot change direcotry in file system handler\n"));
goto oops; goto oops;
} }
@ -735,7 +737,7 @@ static int sed_main (int argc, qse_char_t* argv[])
sed = qse_sed_openstdwithmmgr (mmgr, 0); sed = qse_sed_openstdwithmmgr (mmgr, 0);
if (sed == QSE_NULL) if (sed == QSE_NULL)
{ {
qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open stream editor\n")); qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: cannot open stream editor\n"));
goto oops; goto oops;
} }
@ -757,14 +759,14 @@ static int sed_main (int argc, qse_char_t* argv[])
{ {
/* i dont' use QSE_SED_IOSTD_SIO for input */ /* i dont' use QSE_SED_IOSTD_SIO for input */
QSE_ASSERT (g_script.io[script_count].type == QSE_SED_IOSTD_STR); QSE_ASSERT (g_script.io[script_count].type == QSE_SED_IOSTD_STR);
qse_sprintf (exprbuf, QSE_COUNTOF(exprbuf), qse_fmtuintmax (exprbuf, QSE_COUNTOF(exprbuf),
QSE_T("expression #%lu"), (unsigned long)script_count); script_count, 10, -1, QSE_T('\0'), QSE_T("expression #"));
target = exprbuf; target = exprbuf;
} }
if (errloc->line > 0 || errloc->colm > 0) if (errloc->line > 0 || errloc->colm > 0)
{ {
qse_fprintf (QSE_STDERR, qse_sio_putstrf (QSE_STDERR,
QSE_T("ERROR: cannot compile %s - %s at line %lu column %lu\n"), QSE_T("ERROR: cannot compile %s - %s at line %lu column %lu\n"),
target, target,
qse_sed_geterrmsg(sed), qse_sed_geterrmsg(sed),
@ -774,7 +776,7 @@ static int sed_main (int argc, qse_char_t* argv[])
} }
else else
{ {
qse_fprintf (QSE_STDERR, qse_sio_putstrf (QSE_STDERR,
QSE_T("ERROR: cannot compile %s - %s\n"), QSE_T("ERROR: cannot compile %s - %s\n"),
target, target,
qse_sed_geterrmsg(sed) qse_sed_geterrmsg(sed)
@ -817,7 +819,7 @@ static int sed_main (int argc, qse_char_t* argv[])
); );
if (out_file.u.sio == QSE_NULL) if (out_file.u.sio == QSE_NULL)
{ {
qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open %s\n"), g_output_file); qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: cannot open %s\n"), g_output_file);
goto oops; goto oops;
} }
@ -828,7 +830,7 @@ static int sed_main (int argc, qse_char_t* argv[])
/* perform wild-card expansions for non-unix platforms */ /* perform wild-card expansions for non-unix platforms */
if (expand_wildcards (argc - g_infile_pos, &argv[g_infile_pos], g_wildcard, &xarg) <= -1) if (expand_wildcards (argc - g_infile_pos, &argv[g_infile_pos], g_wildcard, &xarg) <= -1)
{ {
qse_fprintf (QSE_STDERR, QSE_T("ERROR: out of memory\n")); qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: out of memory\n"));
goto oops; goto oops;
} }
@ -850,7 +852,7 @@ static int sed_main (int argc, qse_char_t* argv[])
tmpl_tmpfile = qse_strdup2 (in[0].u.file.path, QSE_T(".XXXX"), qse_sed_getmmgr(sed)); tmpl_tmpfile = qse_strdup2 (in[0].u.file.path, QSE_T(".XXXX"), qse_sed_getmmgr(sed));
if (tmpl_tmpfile == QSE_NULL) if (tmpl_tmpfile == QSE_NULL)
{ {
qse_fprintf (QSE_STDERR, QSE_T("ERROR: out of memory\n")); qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: out of memory\n"));
goto oops; goto oops;
} }
@ -870,7 +872,7 @@ static int sed_main (int argc, qse_char_t* argv[])
{ {
if (retried) if (retried)
{ {
qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open %s\n"), tmpl_tmpfile); qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: cannot open %s\n"), tmpl_tmpfile);
QSE_MMGR_FREE (qse_sed_getmmgr(sed), tmpl_tmpfile); QSE_MMGR_FREE (qse_sed_getmmgr(sed), tmpl_tmpfile);
goto oops; goto oops;
} }
@ -881,7 +883,7 @@ static int sed_main (int argc, qse_char_t* argv[])
tmpl_tmpfile = qse_strdup (QSE_T("TMP-XXXX"), qse_sed_getmmgr(sed)); tmpl_tmpfile = qse_strdup (QSE_T("TMP-XXXX"), qse_sed_getmmgr(sed));
if (tmpl_tmpfile == QSE_NULL) if (tmpl_tmpfile == QSE_NULL)
{ {
qse_fprintf (QSE_STDERR, QSE_T("ERROR: out of memory\n")); qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: out of memory\n"));
goto oops; goto oops;
} }
retried = 1; retried = 1;
@ -913,7 +915,7 @@ static int sed_main (int argc, qse_char_t* argv[])
if (qse_fs_move (fs, tmpl_tmpfile, in[0].u.file.path) <= -1) if (qse_fs_move (fs, tmpl_tmpfile, in[0].u.file.path) <= -1)
{ {
qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot rename %s to %s. not deleting %s\n"), qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: cannot rename %s to %s. not deleting %s\n"),
tmpl_tmpfile, in[0].u.file.path, tmpl_tmpfile); tmpl_tmpfile, in[0].u.file.path, tmpl_tmpfile);
QSE_MMGR_FREE (qse_sed_getmmgr(sed), tmpl_tmpfile); QSE_MMGR_FREE (qse_sed_getmmgr(sed), tmpl_tmpfile);
goto oops; goto oops;
@ -943,14 +945,14 @@ static int sed_main (int argc, qse_char_t* argv[])
/* perform wild-card expansions for non-unix platforms */ /* perform wild-card expansions for non-unix platforms */
if (expand_wildcards (argc - g_infile_pos, &argv[g_infile_pos], g_wildcard, &xarg) <= -1) if (expand_wildcards (argc - g_infile_pos, &argv[g_infile_pos], g_wildcard, &xarg) <= -1)
{ {
qse_fprintf (QSE_STDERR, QSE_T("ERROR: out of memory\n")); qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: out of memory\n"));
goto oops; goto oops;
} }
in = QSE_MMGR_ALLOC (qse_sed_getmmgr(sed), QSE_SIZEOF(*in) * (xarg.size + 1)); in = QSE_MMGR_ALLOC (qse_sed_getmmgr(sed), QSE_SIZEOF(*in) * (xarg.size + 1));
if (in == QSE_NULL) if (in == QSE_NULL)
{ {
qse_fprintf (QSE_STDERR, QSE_T("ERROR: out of memory\n")); qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: out of memory\n"));
goto oops; goto oops;
} }
@ -1008,13 +1010,13 @@ oops:
#if defined(QSE_BUILD_DEBUG) #if defined(QSE_BUILD_DEBUG)
if (g_failmalloc > 0) if (g_failmalloc > 0)
{ {
qse_fprintf (QSE_STDERR, QSE_T("\n")); qse_sio_putstrf (QSE_STDERR, QSE_T("\n"));
qse_fprintf (QSE_STDERR, QSE_T("-[MALLOC COUNTS]---------------------------------------\n")); qse_sio_putstrf (QSE_STDERR, QSE_T("-[MALLOC COUNTS]---------------------------------------\n"));
qse_fprintf (QSE_STDERR, QSE_T("ALLOC: %lu FREE: %lu: REALLOC: %lu\n"), qse_sio_putstrf (QSE_STDERR, QSE_T("ALLOC: %lu FREE: %lu: REALLOC: %lu\n"),
(unsigned long)debug_mmgr_alloc_count, (unsigned long)debug_mmgr_alloc_count,
(unsigned long)debug_mmgr_free_count, (unsigned long)debug_mmgr_free_count,
(unsigned long)debug_mmgr_realloc_count); (unsigned long)debug_mmgr_realloc_count);
qse_fprintf (QSE_STDERR, QSE_T("-------------------------------------------------------\n")); qse_sio_putstrf (QSE_STDERR, QSE_T("-------------------------------------------------------\n"));
} }
#endif #endif
return ret; return ret;
@ -1022,9 +1024,18 @@ oops:
int qse_main (int argc, qse_achar_t* argv[]) int qse_main (int argc, qse_achar_t* argv[])
{ {
int ret;
#if defined(_WIN32) #if defined(_WIN32)
char locale[100]; char locale[100];
UINT codepage = GetConsoleOutputCP(); UINT codepage;
#else
/* nothing */
#endif
qse_openstdsios ();
#if defined(_WIN32)
codepage = GetConsoleOutputCP();
if (codepage == CP_UTF8) if (codepage == CP_UTF8)
{ {
/*SetConsoleOUtputCP (CP_UTF8);*/ /*SetConsoleOUtputCP (CP_UTF8);*/
@ -1043,6 +1054,9 @@ int qse_main (int argc, qse_achar_t* argv[])
qse_setdflcmgrbyid (QSE_CMGR_SLMB); qse_setdflcmgrbyid (QSE_CMGR_SLMB);
#endif #endif
return qse_runmain (argc, argv, sed_main); ret = qse_runmain (argc, argv, sed_main);
qse_closestdsios ();
return ret;
} }

View File

@ -685,6 +685,46 @@ QSE_EXPORT qse_size_t qse_wcsxfncpy (
# define qse_strxfncpy(buf,bsz,fmt,str) qse_wcsxfncpy(buf,bsz,fmt,str) # define qse_strxfncpy(buf,bsz,fmt,str) qse_wcsxfncpy(buf,bsz,fmt,str)
#endif #endif
QSE_EXPORT qse_size_t qse_mbsfmt (
qse_mchar_t* buf,
const qse_mchar_t* fmt,
...
);
QSE_EXPORT qse_size_t qse_mbsxfmt (
qse_mchar_t* buf,
qse_size_t bsz,
const qse_mchar_t* fmt,
...
);
QSE_EXPORT qse_size_t qse_wcsfmt (
qse_wchar_t* buf,
const qse_wchar_t* fmt,
...
);
QSE_EXPORT qse_size_t qse_wcsxfmt (
qse_wchar_t* buf,
qse_size_t bsz,
const qse_wchar_t* fmt,
...
);
QSE_EXPORT qse_size_t qse_strfmt (
qse_char_t* buf,
const qse_char_t* fmt,
...
);
QSE_EXPORT qse_size_t qse_strxfmt (
qse_char_t* buf,
qse_size_t bsz,
const qse_char_t* fmt,
...
);
/** /**
* The qse_mbsxsubst() function expands \a fmt into a buffer \a buf of the size * The qse_mbsxsubst() function expands \a fmt into a buffer \a buf of the size
* \a bsz by substituting new values for ${} segments within it. The actual * \a bsz by substituting new values for ${} segments within it. The actual

View File

@ -11,9 +11,11 @@ noinst_HEADERS = \
cp950.h \ cp950.h \
fmt.h \ fmt.h \
fmt-intmax.h \ fmt-intmax.h \
fmt-print.h \ fmt-out.h \
fs.h \ fs.h \
mem.h \ mem.h \
str-fcpy.h \
str-fmt.h \
str-join.h \ str-join.h \
str-subst.h \ str-subst.h \
syscall.h \ syscall.h \
@ -40,7 +42,7 @@ libqsecmn_la_SOURCES = \
fio.c \ fio.c \
fma.c \ fma.c \
fmt-intmax.c \ fmt-intmax.c \
fmt-print.c \ fmt-out.c \
fs.c \ fs.c \
fs-err.c \ fs-err.c \
fs-move.c \ fs-move.c \
@ -84,6 +86,7 @@ libqsecmn_la_SOURCES = \
str-end.c \ str-end.c \
str-excl.c \ str-excl.c \
str-fcpy.c \ str-fcpy.c \
str-fmt.c \
str-fnmat.c \ str-fnmat.c \
str-incl.c \ str-incl.c \
str-join.c \ str-join.c \

View File

@ -87,38 +87,38 @@ am__DEPENDENCIES_1 =
libqsecmn_la_DEPENDENCIES = $(am__DEPENDENCIES_1) libqsecmn_la_DEPENDENCIES = $(am__DEPENDENCIES_1)
am__libqsecmn_la_SOURCES_DIST = alg-base64.c alg-rand.c alg-search.c \ am__libqsecmn_la_SOURCES_DIST = alg-base64.c alg-rand.c alg-search.c \
alg-sort.c assert.c chr.c dir.c dll.c env.c gdl.c htb.c fio.c \ alg-sort.c assert.c chr.c dir.c dll.c env.c gdl.c htb.c fio.c \
fma.c fmt-intmax.c fmt-print.c fs.c fs-err.c fs-move.c glob.c \ fma.c fmt-intmax.c fmt-out.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 \ 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 nwif-cfg.c nwio.c oht.c opt.c \ nwad.c nwad-skad.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 \ 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 str-cat.c str-chr.c str-cnv.c \ sll.c slmb.c stdio.c str-beg.c str-cat.c str-chr.c str-cnv.c \
str-cmp.c str-cpy.c str-del.c str-dup.c str-dynm.c str-dynw.c \ str-cmp.c str-cpy.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-end.c str-excl.c str-fcpy.c str-fmt.c str-fnmat.c \
str-join.c str-len.c str-pac.c str-pbrk.c str-put.c str-rev.c \ str-incl.c str-join.c str-len.c str-pac.c str-pbrk.c str-put.c \
str-rot.c str-set.c str-spl.c str-spn.c str-str.c str-subst.c \ str-rev.c str-rot.c str-set.c str-spl.c str-spn.c str-str.c \
str-tok.c str-trm.c str-word.c task.c time.c tio.c tre.c \ str-subst.c str-tok.c str-trm.c str-word.c task.c time.c tio.c \
tre-ast.c tre-compile.c tre-match-backtrack.c \ tre.c tre-ast.c tre-compile.c tre-match-backtrack.c \
tre-match-parallel.c tre-parse.c tre-stack.c uri.c utf8.c \ tre-match-parallel.c tre-parse.c tre-stack.c uri.c utf8.c \
xma.c uni.c cp949.c cp950.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 \
alg-sort.lo assert.lo chr.lo dir.lo dll.lo env.lo gdl.lo \ alg-sort.lo assert.lo chr.lo dir.lo dll.lo env.lo gdl.lo \
htb.lo fio.lo fma.lo fmt-intmax.lo fmt-print.lo fs.lo \ htb.lo fio.lo fma.lo fmt-intmax.lo fmt-out.lo fs.lo fs-err.lo \
fs-err.lo fs-move.lo glob.lo hton.lo ipad.lo lda.lo main.lo \ fs-move.lo glob.lo hton.lo ipad.lo lda.lo main.lo mb8.lo \
mb8.lo mbwc.lo mbwc-str.lo mem.lo mux.lo nwad.lo nwad-skad.lo \ mbwc.lo mbwc-str.lo mem.lo mux.lo nwad.lo nwad-skad.lo nwif.lo \
nwif.lo nwif-cfg.lo nwio.lo oht.lo opt.lo path-basename.lo \ nwif-cfg.lo nwio.lo oht.lo opt.lo path-basename.lo \
path-canon.lo pio.lo pma.lo rbt.lo rex.lo sio.lo sll.lo \ path-canon.lo pio.lo pma.lo rbt.lo rex.lo sio.lo sll.lo \
slmb.lo stdio.lo str-beg.lo str-cat.lo str-chr.lo str-cnv.lo \ slmb.lo stdio.lo str-beg.lo str-cat.lo str-chr.lo str-cnv.lo \
str-cmp.lo str-cpy.lo str-del.lo str-dup.lo str-dynm.lo \ str-cmp.lo str-cpy.lo str-del.lo str-dup.lo str-dynm.lo \
str-dynw.lo str-end.lo str-excl.lo str-fcpy.lo str-fnmat.lo \ str-dynw.lo str-end.lo str-excl.lo str-fcpy.lo str-fmt.lo \
str-incl.lo str-join.lo str-len.lo str-pac.lo str-pbrk.lo \ str-fnmat.lo str-incl.lo str-join.lo str-len.lo str-pac.lo \
str-put.lo str-rev.lo str-rot.lo str-set.lo str-spl.lo \ str-pbrk.lo str-put.lo str-rev.lo str-rot.lo str-set.lo \
str-spn.lo str-str.lo str-subst.lo str-tok.lo str-trm.lo \ str-spl.lo str-spn.lo str-str.lo str-subst.lo str-tok.lo \
str-word.lo task.lo time.lo tio.lo tre.lo tre-ast.lo \ str-trm.lo str-word.lo task.lo time.lo tio.lo tre.lo \
tre-compile.lo tre-match-backtrack.lo tre-match-parallel.lo \ tre-ast.lo tre-compile.lo tre-match-backtrack.lo \
tre-parse.lo tre-stack.lo uri.lo utf8.lo xma.lo \ tre-match-parallel.lo tre-parse.lo tre-stack.lo uri.lo utf8.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)
AM_V_lt = $(am__v_lt_@AM_V@) AM_V_lt = $(am__v_lt_@AM_V@)
am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
@ -369,9 +369,11 @@ noinst_HEADERS = \
cp950.h \ cp950.h \
fmt.h \ fmt.h \
fmt-intmax.h \ fmt-intmax.h \
fmt-print.h \ fmt-out.h \
fs.h \ fs.h \
mem.h \ mem.h \
str-fcpy.h \
str-fmt.h \
str-join.h \ str-join.h \
str-subst.h \ str-subst.h \
syscall.h \ syscall.h \
@ -385,17 +387,17 @@ noinst_HEADERS = \
libqsecmn_la_SOURCES = alg-base64.c alg-rand.c alg-search.c alg-sort.c \ libqsecmn_la_SOURCES = alg-base64.c alg-rand.c alg-search.c alg-sort.c \
assert.c chr.c dir.c dll.c env.c gdl.c htb.c fio.c fma.c \ assert.c chr.c dir.c dll.c env.c gdl.c htb.c fio.c fma.c \
fmt-intmax.c fmt-print.c fs.c fs-err.c fs-move.c glob.c hton.c \ fmt-intmax.c fmt-out.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 \ ipad.c lda.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 \ nwad-skad.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 \ 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 str-cat.c str-chr.c str-cnv.c \ sll.c slmb.c stdio.c str-beg.c str-cat.c str-chr.c str-cnv.c \
str-cmp.c str-cpy.c str-del.c str-dup.c str-dynm.c str-dynw.c \ str-cmp.c str-cpy.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-end.c str-excl.c str-fcpy.c str-fmt.c str-fnmat.c \
str-join.c str-len.c str-pac.c str-pbrk.c str-put.c str-rev.c \ str-incl.c str-join.c str-len.c str-pac.c str-pbrk.c str-put.c \
str-rot.c str-set.c str-spl.c str-spn.c str-str.c str-subst.c \ str-rev.c str-rot.c str-set.c str-spl.c str-spn.c str-str.c \
str-tok.c str-trm.c str-word.c task.c time.c tio.c tre.c \ str-subst.c str-tok.c str-trm.c str-word.c task.c time.c tio.c \
tre-ast.c tre-compile.c tre-match-backtrack.c \ tre.c tre-ast.c tre-compile.c tre-match-backtrack.c \
tre-match-parallel.c tre-parse.c tre-stack.c uri.c utf8.c \ tre-match-parallel.c tre-parse.c tre-stack.c uri.c utf8.c \
xma.c $(am__append_1) $(am__append_2) xma.c $(am__append_1) $(am__append_2)
libqsecmn_la_LDFLAGS = -version-info 1:0:0 -no-undefined libqsecmn_la_LDFLAGS = -version-info 1:0:0 -no-undefined
@ -497,7 +499,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fio.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fio.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fma.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fma.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fmt-intmax.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fmt-intmax.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fmt-print.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fmt-out.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fs-err.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fs-err.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fs-move.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fs-move.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fs.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fs.Plo@am__quote@
@ -543,6 +545,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str-end.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str-end.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str-excl.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str-excl.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str-fcpy.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str-fcpy.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str-fmt.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str-fnmat.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str-fnmat.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str-incl.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str-incl.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str-join.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str-join.Plo@am__quote@

View File

@ -24,6 +24,11 @@
#include <qse/cmn/mbwc.h> #include <qse/cmn/mbwc.h>
#include <stdarg.h> #include <stdarg.h>
#include "mem.h" #include "mem.h"
#include "fmt.h"
#include <stdio.h> /* for snrintf() */
/* TODO: remove stdio.h once snprintf gets replaced by own
floting-point conversion implementation*/
/* number of bits in a byte */ /* number of bits in a byte */
#define NBBY 8 #define NBBY 8
@ -106,44 +111,6 @@ static const qse_wchar_t* w_hex2ascii =
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
#include <stdio.h> /* TODO: remove dependency on this */
#if defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && (__WATCOMC__ < 1200))
# define snprintf _snprintf
# define vsnprintf _vsnprintf
#endif
/* ------------------------------------------------------------------ */
static int put_wchar (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))
{
return (putchar ('?') == EOF)? -1: 0;
}
else
{
qse_size_t i;
for (i = 0; i < n; i++)
{
if (putchar (mbsbuf[i]) == EOF) return -1;
}
return 0;
}
}
static int put_mchar (qse_mchar_t c, void *arg)
{
return (putchar (c) == EOF)? -1: 0;
}
/* ------------------------------------------------------------------ */
#undef char_t #undef char_t
#undef uchar_t #undef uchar_t
#undef ochar_t #undef ochar_t
@ -152,7 +119,9 @@ static int put_mchar (qse_mchar_t c, void *arg)
#undef toupper #undef toupper
#undef hex2ascii #undef hex2ascii
#undef sprintn #undef sprintn
#undef xprintf #undef put_char
#undef put_ochar
#undef fmtout
#define char_t qse_mchar_t #define char_t qse_mchar_t
#define uchar_t qse_mchar_t #define uchar_t qse_mchar_t
@ -161,26 +130,13 @@ static int put_mchar (qse_mchar_t c, void *arg)
#define OT(x) QSE_WT(x) #define OT(x) QSE_WT(x)
#define toupper QSE_TOUPPER #define toupper QSE_TOUPPER
#define sprintn m_sprintn #define sprintn m_sprintn
#define xprintf qse_mxprintf #define put_char put_mchar
#define put_ochar put_wchar
#define fmtout qse_mfmtout
#define hex2ascii(hex) (m_hex2ascii[hex]) #define hex2ascii(hex) (m_hex2ascii[hex])
#include "fmt-print.h" #include "fmt-out.h"
qse_ssize_t qse_mprintf (const char_t *fmt, ...)
{
va_list ap;
qse_ssize_t n;
va_start (ap, fmt);
n = qse_mxprintf (fmt, put_mchar, put_wchar, QSE_NULL, ap);
va_end (ap);
return n;
}
qse_ssize_t qse_mvprintf (const char_t* fmt, va_list ap)
{
return qse_mxprintf (fmt, put_mchar, put_wchar, QSE_NULL, ap);
}
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
@ -192,7 +148,9 @@ qse_ssize_t qse_mvprintf (const char_t* fmt, va_list ap)
#undef toupper #undef toupper
#undef hex2ascii #undef hex2ascii
#undef sprintn #undef sprintn
#undef xprintf #undef put_char
#undef put_ochar
#undef fmtout
#define char_t qse_wchar_t #define char_t qse_wchar_t
#define uchar_t qse_wchar_t #define uchar_t qse_wchar_t
@ -201,23 +159,11 @@ qse_ssize_t qse_mvprintf (const char_t* fmt, va_list ap)
#define OT(x) QSE_MT(x) #define OT(x) QSE_MT(x)
#define toupper QSE_TOWUPPER #define toupper QSE_TOWUPPER
#define sprintn w_sprintn #define sprintn w_sprintn
#define xprintf qse_wxprintf #define put_char put_wchar
#define put_ochar put_mchar
#define fmtout qse_wfmtout
#define hex2ascii(hex) (w_hex2ascii[hex]) #define hex2ascii(hex) (w_hex2ascii[hex])
#include "fmt-print.h" #include "fmt-out.h"
qse_ssize_t qse_wprintf (const char_t *fmt, ...)
{
va_list ap;
qse_ssize_t n;
va_start (ap, fmt);
n = qse_wxprintf (fmt, put_wchar, put_mchar, QSE_NULL, ap);
va_end (ap);
return n;
}
qse_ssize_t qse_wvprintf (const char_t* fmt, va_list ap)
{
return qse_wxprintf (fmt, put_wchar, put_mchar, QSE_NULL, ap);
}

View File

@ -84,18 +84,17 @@ static char_t* sprintn (char_t* nbuf, qse_uintmax_t num, int base, int *lenp, in
#undef PUT_CHAR #undef PUT_CHAR
#undef PUT_OCHAR #undef PUT_OCHAR
/* TODO: error check */
#define PUT_CHAR(c) do { \ #define PUT_CHAR(c) do { \
if (put_char (c, arg) <= -1) goto oops; \ if (data->put_char (c, data->ctx) <= -1) goto oops; \
outcnt++; \ data->count++; \
} while (0) } while (0)
#define PUT_OCHAR(c) do { \ #define PUT_OCHAR(c) do { \
if (put_ochar (c, arg) <= -1) goto oops; \ if (data->put_ochar (c, data->ctx) <= -1) goto oops; \
outcnt++; \ data->count++; \
} while (0) } while (0)
qse_ssize_t xprintf (const char_t* fmt, int (*put_char)(char_t, void*), int (*put_ochar) (ochar_t, void*), void *arg, va_list ap) int fmtout (const char_t* fmt, qse_fmtout_t* data, va_list ap)
{ {
char_t nbuf[MAXNBUF]; char_t nbuf[MAXNBUF];
const char_t* p, * percent; const char_t* p, * percent;
@ -106,7 +105,8 @@ qse_ssize_t xprintf (const char_t* fmt, int (*put_char)(char_t, void*), int (*pu
int lm_flag, lm_dflag, flagc, numlen; int lm_flag, lm_dflag, flagc, numlen;
qse_uintmax_t num = 0; qse_uintmax_t num = 0;
int stop = 0; int stop = 0;
qse_ssize_t outcnt = 0;
data->count = 0;
struct struct
{ {
@ -133,7 +133,7 @@ qse_ssize_t xprintf (const char_t* fmt, int (*put_char)(char_t, void*), int (*pu
while ((ch = (uchar_t)*fmt++) != T('%') || stop) while ((ch = (uchar_t)*fmt++) != T('%') || stop)
{ {
if (ch == T('\0')) goto done; if (ch == T('\0')) goto done;
PUT_CHAR(ch); PUT_CHAR (ch);
} }
percent = fmt - 1; percent = fmt - 1;
@ -308,21 +308,21 @@ reswitch:
case T('n'): case T('n'):
if (lm_flag & LF_J) /* j */ if (lm_flag & LF_J) /* j */
*(va_arg(ap, qse_intmax_t*)) = outcnt; *(va_arg(ap, qse_intmax_t*)) = data->count;
else if (lm_flag & LF_Z) /* z */ else if (lm_flag & LF_Z) /* z */
*(va_arg(ap, qse_size_t*)) = outcnt; *(va_arg(ap, qse_size_t*)) = data->count;
#if (QSE_SIZEOF_LONG_LONG > 0) #if (QSE_SIZEOF_LONG_LONG > 0)
else if (lm_flag & LF_Q) /* ll */ else if (lm_flag & LF_Q) /* ll */
*(va_arg(ap, long long int*)) = outcnt; *(va_arg(ap, long long int*)) = data->count;
#endif #endif
else if (lm_flag & LF_L) /* l */ else if (lm_flag & LF_L) /* l */
*(va_arg(ap, long int*)) = outcnt; *(va_arg(ap, long int*)) = data->count;
else if (lm_flag & LF_H) /* h */ else if (lm_flag & LF_H) /* h */
*(va_arg(ap, short int*)) = outcnt; *(va_arg(ap, short int*)) = data->count;
else if (lm_flag & LF_C) /* hh */ else if (lm_flag & LF_C) /* hh */
*(va_arg(ap, char*)) = outcnt; *(va_arg(ap, char*)) = data->count;
else else
*(va_arg(ap, int*)) = outcnt; *(va_arg(ap, int*)) = data->count;
break; break;
@ -733,7 +733,7 @@ done:
QSE_MMGR_FREE (QSE_MMGR_GETDFL(), fltfmt.ptr); QSE_MMGR_FREE (QSE_MMGR_GETDFL(), fltfmt.ptr);
if (fltout.ptr != fltout.sbuf) if (fltout.ptr != fltout.sbuf)
QSE_MMGR_FREE (QSE_MMGR_GETDFL(), fltout.ptr); QSE_MMGR_FREE (QSE_MMGR_GETDFL(), fltout.ptr);
return outcnt; return 0;
oops: oops:
if (fltfmt.ptr != fltfmt.sbuf) if (fltfmt.ptr != fltfmt.sbuf)

View File

@ -24,36 +24,49 @@
#include <qse/cmn/fmt.h> #include <qse/cmn/fmt.h>
#include <stdarg.h> #include <stdarg.h>
typedef int (*qse_printf_mchar_t) ( typedef int (*qse_fmtout_mchar_t) (
qse_mchar_t c, qse_mchar_t c,
void* ctx void* ctx
); );
typedef int (*qse_printf_wchar_t) ( typedef int (*qse_fmtout_wchar_t) (
qse_mchar_t c, qse_wchar_t c,
void* ctx void* ctx
); );
struct qse_fmtout_t
{
qse_size_t count; /* out */
qse_size_t limit; /* in */
void* ctx; /* in */
qse_fmtout_mchar_t put_mchar; /* in */
qse_fmtout_wchar_t put_wchar; /* in */
};
typedef struct qse_fmtout_t qse_fmtout_t;
#ifdef __cplusplus #ifdef __cplusplus
extern { extern "C" {
#endif #endif
qse_ssize_t qse_mxprintf ( int qse_mfmtout (
const qse_mchar_t* fmt, const qse_mchar_t* fmt,
qse_printf_mchar_t put_mchar, qse_fmtout_t* data,
qse_printf_wchar_t put_wchar,
void* arg,
va_list ap va_list ap
); );
qse_ssize_t qse_wxprintf ( int qse_wfmtout (
const qse_wchar_t* fmt, const qse_wchar_t* fmt,
qse_printf_wchar_t put_wchar, qse_fmtout_t* data,
qse_printf_mchar_t put_mchar,
void* arg,
va_list ap va_list ap
); );
#if defined(QSE_CHAR_IS_MCHAR)
# define qse_fmtout(fmt,fo,ap) qse_mfmtout(fmt,fo,ap)
#else
# define qse_fmtout(fmt,fo,ap) qse_wfmtout(fmt,fo,ap)
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -636,12 +636,12 @@ qse_ssize_t qse_sio_putwcsn (
return n; return n;
} }
static int put_wchar (qse_wchar_t c, void *arg) static int put_wchar (qse_wchar_t c, void* arg)
{ {
return qse_sio_putwc ((qse_sio_t*)arg, c); return qse_sio_putwc ((qse_sio_t*)arg, c);
} }
static int put_mchar (qse_mchar_t c, void *arg) static int put_mchar (qse_mchar_t c, void* arg)
{ {
return qse_sio_putmb ((qse_sio_t*)arg, c); return qse_sio_putmb ((qse_sio_t*)arg, c);
} }
@ -650,59 +650,90 @@ qse_ssize_t qse_sio_putmbsf (qse_sio_t* sio, const qse_mchar_t* fmt, ...)
{ {
va_list ap; va_list ap;
qse_ssize_t x; qse_ssize_t x;
qse_fmtout_t fo;
fo.limit = QSE_TYPE_MAX(qse_ssize_t);
fo.ctx = sio;
fo.put_mchar = put_mchar;
fo.put_wchar = put_wchar;
va_start (ap, fmt); va_start (ap, fmt);
x = qse_mxprintf (fmt, put_mchar, put_wchar, sio, ap); x = qse_mfmtout (fmt, &fo, ap);
va_end (ap); va_end (ap);
return x; return (x <= -1)? -1: fo.count;
} }
qse_ssize_t qse_sio_putwcsf (qse_sio_t* sio, const qse_wchar_t* fmt, ...) qse_ssize_t qse_sio_putwcsf (qse_sio_t* sio, const qse_wchar_t* fmt, ...)
{ {
va_list ap; va_list ap;
qse_ssize_t x; int x;
qse_fmtout_t fo;
fo.limit = QSE_TYPE_MAX(qse_ssize_t);
fo.ctx = sio;
fo.put_mchar = put_mchar;
fo.put_wchar = put_wchar;
va_start (ap, fmt); va_start (ap, fmt);
x = qse_wxprintf (fmt, put_wchar, put_mchar, sio, ap); x = qse_wfmtout (fmt, &fo, ap);
va_end (ap); va_end (ap);
return x; return (x <= -1)? -1: fo.count;
} }
qse_ssize_t qse_sio_putstrf (qse_sio_t* sio, const qse_char_t* fmt, ...) qse_ssize_t qse_sio_putstrf (qse_sio_t* sio, const qse_char_t* fmt, ...)
{ {
va_list ap; va_list ap;
qse_ssize_t x; int x;
qse_fmtout_t fo;
fo.limit = QSE_TYPE_MAX(qse_ssize_t);
fo.ctx = sio;
fo.put_mchar = put_mchar;
fo.put_wchar = put_wchar;
va_start (ap, fmt); va_start (ap, fmt);
#if defined(QSE_CHAR_IS_MCHAR) x = qse_fmtout (fmt, &fo, ap);
x = qse_mxprintf (fmt, put_mchar, put_wchar, sio, ap);
#else
x = qse_wxprintf (fmt, put_wchar, put_mchar, sio, ap);
#endif
va_end (ap); va_end (ap);
return x; return (x <= -1)? -1: fo.count;
} }
qse_ssize_t qse_sio_putmbsvf (qse_sio_t* sio, const qse_mchar_t* fmt, va_list ap) qse_ssize_t qse_sio_putmbsvf (qse_sio_t* sio, const qse_mchar_t* fmt, va_list ap)
{ {
return qse_mxprintf (fmt, put_mchar, put_wchar, sio, ap); qse_fmtout_t fo;
fo.limit = QSE_TYPE_MAX(qse_ssize_t);
fo.ctx = sio;
fo.put_mchar = put_mchar;
fo.put_wchar = put_wchar;
return (qse_mfmtout (fmt, &fo, ap) <= -1)? -1: fo.count;
} }
qse_ssize_t qse_sio_putwcsvf (qse_sio_t* sio, const qse_wchar_t* fmt, va_list ap) qse_ssize_t qse_sio_putwcsvf (qse_sio_t* sio, const qse_wchar_t* fmt, va_list ap)
{ {
return qse_wxprintf (fmt, put_wchar, put_mchar, sio, ap); qse_fmtout_t fo;
fo.limit = QSE_TYPE_MAX(qse_ssize_t);
fo.ctx = sio;
fo.put_mchar = put_mchar;
fo.put_wchar = put_wchar;
return (qse_wfmtout (fmt, &fo, ap) <= -1)? -1: fo.count;
} }
qse_ssize_t qse_sio_putstrvf (qse_sio_t* sio, const qse_char_t* fmt, va_list ap) qse_ssize_t qse_sio_putstrvf (qse_sio_t* sio, const qse_char_t* fmt, va_list ap)
{ {
#if defined(QSE_CHAR_IS_MCHAR) qse_fmtout_t fo;
return qse_mxprintf (fmt, put_mchar, put_wchar, sio, ap);
#else fo.limit = QSE_TYPE_MAX(qse_ssize_t);
return qse_wxprintf (fmt, put_wchar, put_mchar, sio, ap); fo.ctx = sio;
#endif fo.put_mchar = put_mchar;
fo.put_wchar = put_wchar;
return (qse_fmtout (fmt, &fo, ap) <= -1)? -1: fo.count;
} }
int qse_sio_getpos (qse_sio_t* sio, qse_sio_pos_t* pos) int qse_sio_getpos (qse_sio_t* sio, qse_sio_pos_t* pos)
@ -843,58 +874,89 @@ qse_sio_t* qse_getstderr (void)
qse_ssize_t qse_putmbsf (const qse_mchar_t* fmt, ...) qse_ssize_t qse_putmbsf (const qse_mchar_t* fmt, ...)
{ {
va_list ap; va_list ap;
qse_ssize_t x; int x;
qse_fmtout_t fo;
fo.limit = QSE_TYPE_MAX(qse_ssize_t);
fo.ctx = sio_stdout;
fo.put_mchar = put_mchar;
fo.put_wchar = put_wchar;
va_start (ap, fmt); va_start (ap, fmt);
x = qse_mxprintf (fmt, put_mchar, put_wchar, sio_stdout, ap); x = qse_mfmtout (fmt, &fo, ap);
va_end (ap); va_end (ap);
return x; return (x <= -1)? -1: fo.count;
} }
qse_ssize_t qse_putwcsf (const qse_wchar_t* fmt, ...) qse_ssize_t qse_putwcsf (const qse_wchar_t* fmt, ...)
{ {
va_list ap; va_list ap;
qse_ssize_t x; int x;
qse_fmtout_t fo;
fo.limit = QSE_TYPE_MAX(qse_ssize_t);
fo.ctx = sio_stdout;
fo.put_mchar = put_mchar;
fo.put_wchar = put_wchar;
va_start (ap, fmt); va_start (ap, fmt);
x = qse_wxprintf (fmt, put_wchar, put_mchar, sio_stdout, ap); x = qse_wfmtout (fmt, &fo, ap);
va_end (ap); va_end (ap);
return x; return (x <= -1)? -1: fo.count;
} }
qse_ssize_t qse_putstrf (const qse_char_t* fmt, ...) qse_ssize_t qse_putstrf (const qse_char_t* fmt, ...)
{ {
va_list ap; va_list ap;
qse_ssize_t x; int x;
qse_fmtout_t fo;
fo.limit = QSE_TYPE_MAX(qse_ssize_t);
fo.ctx = sio_stdout;
fo.put_mchar = put_mchar;
fo.put_wchar = put_wchar;
va_start (ap, fmt); va_start (ap, fmt);
#if defined(QSE_CHAR_IS_MCHAR) x = qse_fmtout (fmt, &fo, ap);
x = qse_mxprintf (fmt, put_mchar, put_wchar, sio_stdout, ap);
#else
x = qse_wxprintf (fmt, put_wchar, put_mchar, sio_stdout, ap);
#endif
va_end (ap); va_end (ap);
return x; return (x <= -1)? -1: fo.count;
} }
qse_ssize_t qse_putmbsvf (const qse_mchar_t* fmt, va_list ap) qse_ssize_t qse_putmbsvf (const qse_mchar_t* fmt, va_list ap)
{ {
return qse_mxprintf (fmt, put_mchar, put_wchar, sio_stdout, ap); qse_fmtout_t fo;
fo.limit = QSE_TYPE_MAX(qse_ssize_t);
fo.ctx = sio_stdout;
fo.put_mchar = put_mchar;
fo.put_wchar = put_wchar;
return (qse_mfmtout (fmt, &fo, ap) <= -1)? -1: fo.count;
} }
qse_ssize_t qse_putwcsvf (const qse_wchar_t* fmt, va_list ap) qse_ssize_t qse_putwcsvf (const qse_wchar_t* fmt, va_list ap)
{ {
return qse_wxprintf (fmt, put_wchar, put_mchar, sio_stdout, ap); qse_fmtout_t fo;
fo.limit = QSE_TYPE_MAX(qse_ssize_t);
fo.ctx = sio_stdout;
fo.put_mchar = put_mchar;
fo.put_wchar = put_wchar;
return (qse_wfmtout (fmt, &fo, ap) <= -1)? -1: fo.count;
} }
qse_ssize_t qse_putstrvf (const qse_char_t* fmt, va_list ap) qse_ssize_t qse_putstrvf (const qse_char_t* fmt, va_list ap)
{ {
#if defined(QSE_CHAR_IS_MCHAR) qse_fmtout_t fo;
return qse_mxprintf (fmt, put_mchar, put_wchar, sio_stdout, ap);
#else fo.limit = QSE_TYPE_MAX(qse_ssize_t);
return qse_wxprintf (fmt, put_wchar, put_mchar, sio_stdout, ap); fo.ctx = sio_stdout;
#endif fo.put_mchar = put_mchar;
fo.put_wchar = put_wchar;
return (qse_fmtout (fmt, &fo, ap) <= -1)? -1: fo.count;
} }

View File

@ -20,452 +20,39 @@
#include <qse/cmn/str.h> #include <qse/cmn/str.h>
qse_size_t qse_mbsfcpy ( #undef T
qse_mchar_t* buf, const qse_mchar_t* fmt, const qse_mchar_t* str[]) #undef char_t
{ #undef cstr_t
qse_mchar_t* b = buf; #undef strfcpy
const qse_mchar_t* f = fmt; #undef strfncpy
#undef strxfcpy
#undef strxfncpy
#define T(x) QSE_MT(x)
#define char_t qse_mchar_t
#define cstr_t qse_mcstr_t
#define strfcpy qse_mbsfcpy
#define strfncpy qse_mbsfncpy
#define strxfcpy qse_mbsxfcpy
#define strxfncpy qse_mbsxfncpy
#include "str-fcpy.h"
/* ----------------------------------- */
#undef T
#undef char_t
#undef cstr_t
#undef strfcpy
#undef strfncpy
#undef strxfcpy
#undef strxfncpy
#define T(x) QSE_WT(x)
#define char_t qse_wchar_t
#define cstr_t qse_wcstr_t
#define strfcpy qse_wcsfcpy
#define strfncpy qse_wcsfncpy
#define strxfcpy qse_wcsxfcpy
#define strxfncpy qse_wcsxfncpy
#include "str-fcpy.h"
while (*f != QSE_MT('\0'))
{
if (*f == QSE_MT('$'))
{
if (f[1] == QSE_MT('{') &&
(f[2] >= QSE_MT('0') && f[2] <= QSE_MT('9')))
{
const qse_mchar_t* tmp;
qse_size_t idx = 0;
tmp = f;
f += 2;
do idx = idx * 10 + (*f++ - QSE_MT('0'));
while (*f >= QSE_MT('0') && *f <= QSE_MT('9'));
if (*f != QSE_MT('}'))
{
f = tmp;
goto normal;
}
f++;
tmp = str[idx];
while (*tmp != QSE_MT('\0')) *b++ = *tmp++;
continue;
}
else if (f[1] == QSE_MT('$')) f++;
}
normal:
*b++ = *f++;
}
*b = QSE_MT('\0');
return b - buf;
}
qse_size_t qse_mbsfncpy (
qse_mchar_t* buf, const qse_mchar_t* fmt, const qse_mcstr_t str[])
{
qse_mchar_t* b = buf;
const qse_mchar_t* f = fmt;
while (*f != QSE_MT('\0'))
{
if (*f == QSE_MT('\\'))
{
/* get the escaped character and treat it normally.
* if the escaper is the last character, treat it
* normally also. */
if (f[1] != QSE_MT('\0')) f++;
}
else if (*f == QSE_MT('$'))
{
if (f[1] == QSE_MT('{') &&
(f[2] >= QSE_MT('0') && f[2] <= QSE_MT('9')))
{
const qse_mchar_t* tmp, * tmpend;
qse_size_t idx = 0;
tmp = f;
f += 2;
do idx = idx * 10 + (*f++ - QSE_MT('0'));
while (*f >= QSE_MT('0') && *f <= QSE_MT('9'));
if (*f != QSE_MT('}'))
{
f = tmp;
goto normal;
}
f++;
tmp = str[idx].ptr;
tmpend = tmp + str[idx].len;
while (tmp < tmpend) *b++ = *tmp++;
continue;
}
else if (f[1] == QSE_MT('$')) f++;
}
normal:
*b++ = *f++;
}
*b = QSE_MT('\0');
return b - buf;
}
qse_size_t qse_mbsxfcpy (
qse_mchar_t* buf, qse_size_t bsz,
const qse_mchar_t* fmt, const qse_mchar_t* str[])
{
qse_mchar_t* b = buf;
qse_mchar_t* end = buf + bsz - 1;
const qse_mchar_t* f = fmt;
if (bsz <= 0) return 0;
while (*f != QSE_MT('\0'))
{
if (*f == QSE_MT('\\'))
{
/* get the escaped character and treat it normally.
* if the escaper is the last character, treat it
* normally also. */
if (f[1] != QSE_MT('\0')) f++;
}
else if (*f == QSE_MT('$'))
{
if (f[1] == QSE_MT('{') &&
(f[2] >= QSE_MT('0') && f[2] <= QSE_MT('9')))
{
const qse_mchar_t* tmp;
qse_size_t idx = 0;
tmp = f;
f += 2;
do idx = idx * 10 + (*f++ - QSE_MT('0'));
while (*f >= QSE_MT('0') && *f <= QSE_MT('9'));
if (*f != QSE_MT('}'))
{
f = tmp;
goto normal;
}
f++;
tmp = str[idx];
while (*tmp != QSE_MT('\0'))
{
if (b >= end) goto fini;
*b++ = *tmp++;
}
continue;
}
else if (f[1] == QSE_MT('$')) f++;
}
normal:
if (b >= end) break;
*b++ = *f++;
}
fini:
*b = QSE_MT('\0');
return b - buf;
}
qse_size_t qse_mbsxfncpy (
qse_mchar_t* buf, qse_size_t bsz,
const qse_mchar_t* fmt, const qse_mcstr_t str[])
{
qse_mchar_t* b = buf;
qse_mchar_t* end = buf + bsz - 1;
const qse_mchar_t* f = fmt;
if (bsz <= 0) return 0;
while (*f != QSE_MT('\0'))
{
if (*f == QSE_MT('\\'))
{
/* get the escaped character and treat it normally.
* if the escaper is the last character, treat it
* normally also. */
if (f[1] != QSE_MT('\0')) f++;
}
else if (*f == QSE_MT('$'))
{
if (f[1] == QSE_MT('{') &&
(f[2] >= QSE_MT('0') && f[2] <= QSE_MT('9')))
{
const qse_mchar_t* tmp, * tmpend;
qse_size_t idx = 0;
tmp = f;
f += 2;
do idx = idx * 10 + (*f++ - QSE_MT('0'));
while (*f >= QSE_MT('0') && *f <= QSE_MT('9'));
if (*f != QSE_MT('}'))
{
f = tmp;
goto normal;
}
f++;
tmp = str[idx].ptr;
tmpend = tmp + str[idx].len;
while (tmp < tmpend)
{
if (b >= end) goto fini;
*b++ = *tmp++;
}
continue;
}
else if (f[1] == QSE_MT('$')) f++;
}
normal:
if (b >= end) break;
*b++ = *f++;
}
fini:
*b = QSE_MT('\0');
return b - buf;
}
qse_size_t qse_wcsfcpy (
qse_wchar_t* buf, const qse_wchar_t* fmt, const qse_wchar_t* str[])
{
qse_wchar_t* b = buf;
const qse_wchar_t* f = fmt;
while (*f != QSE_WT('\0'))
{
if (*f == QSE_WT('$'))
{
if (f[1] == QSE_WT('{') &&
(f[2] >= QSE_WT('0') && f[2] <= QSE_WT('9')))
{
const qse_wchar_t* tmp;
qse_size_t idx = 0;
tmp = f;
f += 2;
do idx = idx * 10 + (*f++ - QSE_WT('0'));
while (*f >= QSE_WT('0') && *f <= QSE_WT('9'));
if (*f != QSE_WT('}'))
{
f = tmp;
goto normal;
}
f++;
tmp = str[idx];
while (*tmp != QSE_WT('\0')) *b++ = *tmp++;
continue;
}
else if (f[1] == QSE_WT('$')) f++;
}
normal:
*b++ = *f++;
}
*b = QSE_WT('\0');
return b - buf;
}
qse_size_t qse_wcsfncpy (
qse_wchar_t* buf, const qse_wchar_t* fmt, const qse_wcstr_t str[])
{
qse_wchar_t* b = buf;
const qse_wchar_t* f = fmt;
while (*f != QSE_WT('\0'))
{
if (*f == QSE_WT('\\'))
{
/* get the escaped character and treat it normally.
* if the escaper is the last character, treat it
* normally also. */
if (f[1] != QSE_WT('\0')) f++;
}
else if (*f == QSE_WT('$'))
{
if (f[1] == QSE_WT('{') &&
(f[2] >= QSE_WT('0') && f[2] <= QSE_WT('9')))
{
const qse_wchar_t* tmp, * tmpend;
qse_size_t idx = 0;
tmp = f;
f += 2;
do idx = idx * 10 + (*f++ - QSE_WT('0'));
while (*f >= QSE_WT('0') && *f <= QSE_WT('9'));
if (*f != QSE_WT('}'))
{
f = tmp;
goto normal;
}
f++;
tmp = str[idx].ptr;
tmpend = tmp + str[idx].len;
while (tmp < tmpend) *b++ = *tmp++;
continue;
}
else if (f[1] == QSE_WT('$')) f++;
}
normal:
*b++ = *f++;
}
*b = QSE_WT('\0');
return b - buf;
}
qse_size_t qse_wcsxfcpy (
qse_wchar_t* buf, qse_size_t bsz,
const qse_wchar_t* fmt, const qse_wchar_t* str[])
{
qse_wchar_t* b = buf;
qse_wchar_t* end = buf + bsz - 1;
const qse_wchar_t* f = fmt;
if (bsz <= 0) return 0;
while (*f != QSE_WT('\0'))
{
if (*f == QSE_WT('\\'))
{
/* get the escaped character and treat it normally.
* if the escaper is the last character, treat it
* normally also. */
if (f[1] != QSE_WT('\0')) f++;
}
else if (*f == QSE_WT('$'))
{
if (f[1] == QSE_WT('{') &&
(f[2] >= QSE_WT('0') && f[2] <= QSE_WT('9')))
{
const qse_wchar_t* tmp;
qse_size_t idx = 0;
tmp = f;
f += 2;
do idx = idx * 10 + (*f++ - QSE_WT('0'));
while (*f >= QSE_WT('0') && *f <= QSE_WT('9'));
if (*f != QSE_WT('}'))
{
f = tmp;
goto normal;
}
f++;
tmp = str[idx];
while (*tmp != QSE_WT('\0'))
{
if (b >= end) goto fini;
*b++ = *tmp++;
}
continue;
}
else if (f[1] == QSE_WT('$')) f++;
}
normal:
if (b >= end) break;
*b++ = *f++;
}
fini:
*b = QSE_WT('\0');
return b - buf;
}
qse_size_t qse_wcsxfncpy (
qse_wchar_t* buf, qse_size_t bsz,
const qse_wchar_t* fmt, const qse_wcstr_t str[])
{
qse_wchar_t* b = buf;
qse_wchar_t* end = buf + bsz - 1;
const qse_wchar_t* f = fmt;
if (bsz <= 0) return 0;
while (*f != QSE_WT('\0'))
{
if (*f == QSE_WT('\\'))
{
/* get the escaped character and treat it normally.
* if the escaper is the last character, treat it
* normally also. */
if (f[1] != QSE_WT('\0')) f++;
}
else if (*f == QSE_WT('$'))
{
if (f[1] == QSE_WT('{') &&
(f[2] >= QSE_WT('0') && f[2] <= QSE_WT('9')))
{
const qse_wchar_t* tmp, * tmpend;
qse_size_t idx = 0;
tmp = f;
f += 2;
do idx = idx * 10 + (*f++ - QSE_WT('0'));
while (*f >= QSE_WT('0') && *f <= QSE_WT('9'));
if (*f != QSE_WT('}'))
{
f = tmp;
goto normal;
}
f++;
tmp = str[idx].ptr;
tmpend = tmp + str[idx].len;
while (tmp < tmpend)
{
if (b >= end) goto fini;
*b++ = *tmp++;
}
continue;
}
else if (f[1] == QSE_WT('$')) f++;
}
normal:
if (b >= end) break;
*b++ = *f++;
}
fini:
*b = QSE_WT('\0');
return b - buf;
}

244
qse/lib/cmn/str-fcpy.h Normal file
View File

@ -0,0 +1,244 @@
/*
* $Id$
*
Copyright 2006-2012 Chung, Hyung-Hwan.
This file is part of QSE.
QSE is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
QSE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with QSE. If not, see <http://www.gnu.org/licenses/>.
*/
qse_size_t strfcpy (
char_t* buf, const char_t* fmt, const char_t* str[])
{
char_t* b = buf;
const char_t* f = fmt;
while (*f != T('\0'))
{
if (*f == T('$'))
{
if (f[1] == T('{') &&
(f[2] >= T('0') && f[2] <= T('9')))
{
const char_t* tmp;
qse_size_t idx = 0;
tmp = f;
f += 2;
do idx = idx * 10 + (*f++ - T('0'));
while (*f >= T('0') && *f <= T('9'));
if (*f != T('}'))
{
f = tmp;
goto normal;
}
f++;
tmp = str[idx];
while (*tmp != T('\0')) *b++ = *tmp++;
continue;
}
else if (f[1] == T('$')) f++;
}
normal:
*b++ = *f++;
}
*b = T('\0');
return b - buf;
}
qse_size_t strfncpy (
char_t* buf, const char_t* fmt, const cstr_t str[])
{
char_t* b = buf;
const char_t* f = fmt;
while (*f != T('\0'))
{
if (*f == T('\\'))
{
/* get the escaped character and treat it normally.
* if the escaper is the last character, treat it
* normally also. */
if (f[1] != T('\0')) f++;
}
else if (*f == T('$'))
{
if (f[1] == T('{') &&
(f[2] >= T('0') && f[2] <= T('9')))
{
const char_t* tmp, * tmpend;
qse_size_t idx = 0;
tmp = f;
f += 2;
do idx = idx * 10 + (*f++ - T('0'));
while (*f >= T('0') && *f <= T('9'));
if (*f != T('}'))
{
f = tmp;
goto normal;
}
f++;
tmp = str[idx].ptr;
tmpend = tmp + str[idx].len;
while (tmp < tmpend) *b++ = *tmp++;
continue;
}
else if (f[1] == T('$')) f++;
}
normal:
*b++ = *f++;
}
*b = T('\0');
return b - buf;
}
qse_size_t strxfcpy (
char_t* buf, qse_size_t bsz,
const char_t* fmt, const char_t* str[])
{
char_t* b = buf;
char_t* end = buf + bsz - 1;
const char_t* f = fmt;
if (bsz <= 0) return 0;
while (*f != T('\0'))
{
if (*f == T('\\'))
{
/* get the escaped character and treat it normally.
* if the escaper is the last character, treat it
* normally also. */
if (f[1] != T('\0')) f++;
}
else if (*f == T('$'))
{
if (f[1] == T('{') &&
(f[2] >= T('0') && f[2] <= T('9')))
{
const char_t* tmp;
qse_size_t idx = 0;
tmp = f;
f += 2;
do idx = idx * 10 + (*f++ - T('0'));
while (*f >= T('0') && *f <= T('9'));
if (*f != T('}'))
{
f = tmp;
goto normal;
}
f++;
tmp = str[idx];
while (*tmp != T('\0'))
{
if (b >= end) goto fini;
*b++ = *tmp++;
}
continue;
}
else if (f[1] == T('$')) f++;
}
normal:
if (b >= end) break;
*b++ = *f++;
}
fini:
*b = T('\0');
return b - buf;
}
qse_size_t strxfncpy (
char_t* buf, qse_size_t bsz,
const char_t* fmt, const cstr_t str[])
{
char_t* b = buf;
char_t* end = buf + bsz - 1;
const char_t* f = fmt;
if (bsz <= 0) return 0;
while (*f != T('\0'))
{
if (*f == T('\\'))
{
/* get the escaped character and treat it normally.
* if the escaper is the last character, treat it
* normally also. */
if (f[1] != T('\0')) f++;
}
else if (*f == T('$'))
{
if (f[1] == T('{') &&
(f[2] >= T('0') && f[2] <= T('9')))
{
const char_t* tmp, * tmpend;
qse_size_t idx = 0;
tmp = f;
f += 2;
do idx = idx * 10 + (*f++ - T('0'));
while (*f >= T('0') && *f <= T('9'));
if (*f != T('}'))
{
f = tmp;
goto normal;
}
f++;
tmp = str[idx].ptr;
tmpend = tmp + str[idx].len;
while (tmp < tmpend)
{
if (b >= end) goto fini;
*b++ = *tmp++;
}
continue;
}
else if (f[1] == T('$')) f++;
}
normal:
if (b >= end) break;
*b++ = *f++;
}
fini:
*b = T('\0');
return b - buf;
}

136
qse/lib/cmn/str-fmt.c Normal file
View File

@ -0,0 +1,136 @@
/*
* $Id$
*
Copyright 2006-2012 Chung, Hyung-Hwan.
This file is part of QSE.
QSE is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
QSE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with QSE. If not, see <http://www.gnu.org/licenses/>.
*/
#include <qse/cmn/str.h>
#include "fmt.h"
struct wbuf_t
{
qse_wchar_t* ptr;
qse_size_t len;
qse_size_t capa;
};
struct mbuf_t
{
qse_mchar_t* ptr;
qse_size_t len;
qse_size_t capa;
};
typedef struct wbuf_t wbuf_t;
typedef struct mbuf_t mbuf_t;
static int put_wchar_to_wbuf (qse_wchar_t c, void* arg)
{
qse_fmtout_t* fo = (qse_fmtout_t*)arg;
wbuf_t* buf = (wbuf_t*)fo->ctx;
if (buf->len < buf->capa) buf->ptr[buf->len++] = c;
return 1;
}
static int put_mchar_to_mbuf (qse_mchar_t c, void* arg)
{
qse_fmtout_t* fo = (qse_fmtout_t*)arg;
mbuf_t* buf = (mbuf_t*)fo->ctx;
if (buf->len < buf->capa) buf->ptr[buf->len++] = c;
return 1;
}
static int put_wchar_to_mbuf (qse_wchar_t c, void* arg)
{
return 1;
}
static int put_mchar_to_wbuf (qse_mchar_t c, void* arg)
{
return 1;
}
/* ----------------------------------- */
#undef T
#undef char_t
#undef buf_t
#undef fmtout
#undef output_char
#undef output_ochar
#undef strfmt
#undef strxfmt
#define T(x) QSE_MT(x)
#define char_t qse_mchar_t
#define buf_t mbuf_t
#define fmtout qse_mfmtout
#define output_mchar put_mchar_to_mbuf
#define output_wchar put_wchar_to_mbuf
#define strfmt qse_mbsfmt
#define strxfmt qse_mbsxfmt
#include "str-fmt.h"
/* ----------------------------------- */
#undef T
#undef char_t
#undef buf_t
#undef fmtout
#undef output_mchar
#undef output_wchar
#undef strfmt
#undef strxfmt
#define T(x) QSE_WT(x)
#define char_t qse_wchar_t
#define buf_t wbuf_t
#define fmtout qse_wfmtout
#define output_mchar put_mchar_to_wbuf
#define output_wchar put_wchar_to_wbuf
#define strfmtx qse_wcsfmt
#define strxfmtx qse_wcsxfmt
#include "str-fmt.h"
/* ----------------------------------- */
#undef T
#undef char_t
#undef buf_t
#undef fmtout
#undef output_mchar
#undef output_wchar
#undef strfmt
#undef strxfmt
#define T(x) QSE_T(x)
#define char_t qse_char_t
#if defined(QSE_CHAR_IS_MCHAR)
# define buf_t mbuf_t
# define output_mchar put_mchar_to_mbuf
# define output_wchar put_wchar_to_mbuf
# define fmtout qse_mfmtout
#else
# define buf_t wbuf_t
# define output_mchar put_mchar_to_wbuf
# define output_wchar put_wchar_to_wbuf
# define fmtout qse_wfmtout
#endif
#define strfmt qse_strfmt
#define strxfmt qse_strxfmt
#include "str-fmt.h"

79
qse/lib/cmn/str-fmt.h Normal file
View File

@ -0,0 +1,79 @@
/*
* $Id$
*
Copyright 2006-2012 Chung, Hyung-Hwan.
This file is part of QSE.
QSE is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
QSE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with QSE. If not, see <http://www.gnu.org/licenses/>.
*/
qse_size_t strfmt (char_t* buf, const char_t* fmt, ...)
{
buf_t b;
va_list ap;
qse_fmtout_t fo;
b.ptr = buf;
b.len = 0;
b.capa = QSE_TYPE_MAX(qse_ssize_t);
fo.limit = QSE_TYPE_MAX(qse_size_t) - 1;
fo.ctx = &b;
fo.put_mchar = output_mchar;
fo.put_wchar = output_wchar;
/* no error must be returned by fmtout since
* the callback function never fails. */
va_start (ap, fmt);
fmtout (fmt, &fo, ap);
va_end (ap);
b.ptr[b.len] = T('\0');
return fo.count;
}
qse_size_t strxfmt (char_t* buf, qse_size_t len, const char_t* fmt, ...)
{
buf_t b;
va_list ap;
qse_fmtout_t fo;
b.ptr = buf;
b.len = 0;
if (len > QSE_TYPE_MAX(qse_ssize_t))
b.capa = QSE_TYPE_MAX(qse_ssize_t);
else if (len > 0)
b.capa = len - 1;
else
b.capa = 0;
fo.limit = QSE_TYPE_MAX(qse_size_t) - 1;
fo.ctx = &b;
fo.put_mchar = output_mchar;
fo.put_wchar = output_wchar;
/* no error must be returned by fmtout since
* the callback function never fails. */
va_start (ap, fmt);
fmtout (fmt, &fo, ap);
va_end (ap);
if (len > 0) b.ptr[b.len] = T('\0');
return fo.count;
}

View File

@ -669,6 +669,11 @@ static int server_open (qse_httpd_t* httpd, qse_httpd_server_t* server)
setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (void*)&flag, QSE_SIZEOF(flag)); setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (void*)&flag, QSE_SIZEOF(flag));
#endif #endif
#if defined(SO_REUSEPORT)
flag = 1;
setsockopt (fd, SOL_SOCKET, SO_REUSEPORT, (void*)&flag, QSE_SIZEOF(flag));
#endif
/* TODO: linux. use capset() to set required capabilities just in case */ /* TODO: linux. use capset() to set required capabilities just in case */
#if defined(IP_TRANSPARENT) #if defined(IP_TRANSPARENT)
/* remove the ip routing restriction that a packet can only /* remove the ip routing restriction that a packet can only