diff --git a/qse/cmd/http/httpd.c b/qse/cmd/http/httpd.c index 09c50fa6..25ba43e0 100644 --- a/qse/cmd/http/httpd.c +++ b/qse/cmd/http/httpd.c @@ -1,7 +1,6 @@ #include #include -#include #include #include #include @@ -48,6 +47,9 @@ # include #endif +#define QSE_STDOUT qse_getstdout() +#define QSE_STDERR qse_getstderr() + /* --------------------------------------------------------------------- */ 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); if (cfg->xcfg[i] == QSE_NULL) { - /*qse_printf (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 - %s\n"), qse_httpd_geterrmsg (httpd));*/ + qse_putstrf (QSE_T("ERROR in copying\n")); 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); 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; } @@ -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) { - qse_printf (QSE_T("ERROR: memory failure in copying index\n")); + qse_putstrf (QSE_T("ERROR: memory failure in copying index\n")); 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)); 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; } @@ -917,7 +919,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg) if (!cgi->spec) { 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; } 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); - 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; } } @@ -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)); 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; } @@ -993,7 +995,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg) if (!auth_rule->spec) { 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; } } @@ -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)); 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; } @@ -1041,7 +1043,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg) if (!mime->spec) { 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; } @@ -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); - qse_printf (QSE_T("ERROR: memory failure in copying mime\n")); + qse_putstrf (QSE_T("ERROR: memory failure in copying mime\n")); 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)); 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; } @@ -1108,7 +1110,7 @@ static int load_loccfg (qse_httpd_t* httpd, qse_xli_list_t* list, loccfg_t* cfg) if (!acc->spec) { 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; } } @@ -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 */ 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 @@ -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); if (server_xtn->scfg[i] == QSE_NULL) { - /*qse_printf (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 - %s\n"), qse_httpd_geterrmsg (httpd));*/ + qse_putstrf (QSE_T("ERROR in copying\n")); 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) { /* 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; } @@ -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) { /* 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; } @@ -1351,7 +1353,7 @@ static qse_httpd_server_t* attach_server (qse_httpd_t* httpd, int num, qse_xli_l if (xserver == QSE_NULL) { /* 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; } @@ -1474,7 +1476,7 @@ static int open_config_file (qse_httpd_t* httpd) httpd_xtn->xli = qse_xli_openstd (0, 0); 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; } @@ -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) { - 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); httpd_xtn->xli = QSE_NULL; return -1; @@ -1505,12 +1507,12 @@ static int open_config_file (qse_httpd_t* httpd) 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)); } 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); @@ -1609,7 +1611,7 @@ static int load_config (qse_httpd_t* httpd) 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 { @@ -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) { - 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; } } @@ -1629,7 +1631,7 @@ static int load_config (qse_httpd_t* httpd) 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; } @@ -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) { - 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; } } @@ -1713,27 +1715,27 @@ static void logact_httpd (qse_httpd_t* httpd, const qse_httpd_act_t* act) switch (act->code) { 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; 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; case QSE_HTTPD_ACCEPT_CLIENT: 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->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: 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; case QSE_HTTPD_READERR_CLIENT: 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; } } @@ -1741,23 +1743,23 @@ static void logact_httpd (qse_httpd_t* httpd, const qse_httpd_act_t* act) /* --------------------------------------------------------------------- */ 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]); - qse_fprintf (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("USAGE: %s [options] -c 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_fprintf (out, QSE_T(" -h/--help show this message\n")); - qse_fprintf (out, QSE_T(" --version show version\n")); - qse_fprintf (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_fprintf (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("options as follows:\n")); + qse_sio_putstrf (out, QSE_T(" -h/--help show this message\n")); + qse_sio_putstrf (out, QSE_T(" --version show version\n")); + qse_sio_putstrf (out, QSE_T(" -c/--config-file file specify a configuration file\n")); + qse_sio_putstrf (out, QSE_T(" -d/--daemon run in the background\n")); + qse_sio_putstrf (out, QSE_T(" -n string specify a process name if supported\n")); + qse_sio_putstrf (out, QSE_T(" -x output debugging messages\n")); } 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; case QSE_T('?'): - qse_fprintf (QSE_STDERR, + qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: bad option - %c\n"), opt.opt ); goto wrongusage; case QSE_T(':'): - qse_fprintf (QSE_STDERR, + qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: bad parameter for %c\n"), 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)); 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; } @@ -1896,7 +1898,7 @@ static int httpd_main (int argc, qse_char_t* argv[]) { 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; } } @@ -1927,7 +1929,7 @@ static int httpd_main (int argc, qse_char_t* argv[]) restore_signal_handlers (); 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: if (httpd) qse_httpd_close (httpd); @@ -1942,6 +1944,13 @@ int qse_main (int argc, qse_achar_t* argv[]) char locale[100]; UINT codepage; WSADATA wsadata; +#else + /* nothing */ +#endif + + qse_openstdsios (); + +#if defined(_WIN32) codepage = GetConsoleOutputCP(); if (codepage == CP_UTF8) @@ -1960,7 +1969,7 @@ int qse_main (int argc, qse_achar_t* argv[]) 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; } @@ -1989,5 +1998,6 @@ int qse_main (int argc, qse_achar_t* argv[]) WSACleanup (); #endif + qse_closestdsios (); return ret; } diff --git a/qse/cmd/sed/sed.c b/qse/cmd/sed/sed.c index a3e4faec..f820429d 100644 --- a/qse/cmd/sed/sed.c +++ b/qse/cmd/sed/sed.c @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #include @@ -55,6 +54,9 @@ # include #endif +#define QSE_STDOUT qse_getstdout() +#define QSE_STDERR qse_getstderr() + static struct { qse_sed_iostd_t* io; @@ -140,46 +142,46 @@ static qse_mmgr_t xma_mmgr = 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]); - qse_fprintf (out, QSE_T("USAGE: %s [options] script [file]\n"), b); - qse_fprintf (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("USAGE: %s [options] script [file]\n"), b); + qse_sio_putstrf (out, QSE_T(" %s [options] -f script-file [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_fprintf (out, QSE_T(" -h/--help show this message\n")); - qse_fprintf (out, QSE_T(" --version show version\n")); - qse_fprintf (out, QSE_T(" -n disable auto-print\n")); - qse_fprintf (out, QSE_T(" -e script specify a script\n")); - qse_fprintf (out, QSE_T(" -f file specify a script file\n")); - qse_fprintf (out, QSE_T(" -o file specify an output file\n")); - qse_fprintf (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_fprintf (out, QSE_T(" expression\n")); - qse_fprintf (out, QSE_T(" -i perform in-place editing. imply -s\n")); - qse_fprintf (out, QSE_T(" -s process input files separately\n")); - qse_fprintf (out, QSE_T(" -a perform strict address and label check\n")); - qse_fprintf (out, QSE_T(" -b allow extended address formats\n")); - qse_fprintf (out, QSE_T(" ,,,<0,/regex/>\n")); - qse_fprintf (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_fprintf (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("options as follows:\n")); + qse_sio_putstrf (out, QSE_T(" -h/--help show this message\n")); + qse_sio_putstrf (out, QSE_T(" --version show version\n")); + qse_sio_putstrf (out, QSE_T(" -n disable auto-print\n")); + qse_sio_putstrf (out, QSE_T(" -e script specify a script\n")); + qse_sio_putstrf (out, QSE_T(" -f file specify a script file\n")); + qse_sio_putstrf (out, QSE_T(" -o file specify an output file\n")); + qse_sio_putstrf (out, QSE_T(" -r use the extended regular expression\n")); + qse_sio_putstrf (out, QSE_T(" -R enable non-standard extensions to the regular\n")); + qse_sio_putstrf (out, QSE_T(" expression\n")); + qse_sio_putstrf (out, QSE_T(" -i perform in-place editing. imply -s\n")); + qse_sio_putstrf (out, QSE_T(" -s process input files separately\n")); + qse_sio_putstrf (out, QSE_T(" -a perform strict address and label check\n")); + qse_sio_putstrf (out, QSE_T(" -b allow extended address formats\n")); + qse_sio_putstrf (out, QSE_T(" ,,,<0,/regex/>\n")); + qse_sio_putstrf (out, QSE_T(" -x allow text on the same line as c, a, i\n")); + qse_sio_putstrf (out, QSE_T(" -y ensure a newline at text end\n")); + qse_sio_putstrf (out, QSE_T(" -m number specify the maximum amount of memory to use in bytes\n")); + qse_sio_putstrf (out, QSE_T(" -w expand file wildcards\n")); #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 #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 #if defined(QSE_CHAR_IS_WCHAR) - qse_fprintf (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_fprintf (out, QSE_T(" --outfile-encoding string specify output file encoding name\n")); + qse_sio_putstrf (out, QSE_T(" --script-encoding string specify script file encoding name\n")); + qse_sio_putstrf (out, QSE_T(" --infile-encoding string specify input file encoding name\n")); + qse_sio_putstrf (out, QSE_T(" --outfile-encoding string specify output file encoding name\n")); #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)); 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; } @@ -266,7 +268,7 @@ static int handle_args (int argc, qse_char_t* argv[]) goto oops; case QSE_T('?'): - qse_fprintf (QSE_STDERR, + qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: bad option - %c\n"), opt.opt ); @@ -274,7 +276,7 @@ static int handle_args (int argc, qse_char_t* argv[]) goto oops; case QSE_T(':'): - qse_fprintf (QSE_STDERR, + qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: bad parameter for %c\n"), opt.opt ); @@ -368,7 +370,7 @@ static int handle_args (int argc, qse_char_t* argv[]) g_script_cmgr = qse_findcmgr (opt.arg); 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; } } @@ -377,7 +379,7 @@ static int handle_args (int argc, qse_char_t* argv[]) g_infile_cmgr = qse_findcmgr (opt.arg); 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; } } @@ -386,7 +388,7 @@ static int handle_args (int argc, qse_char_t* argv[]) g_outfile_cmgr = qse_findcmgr (opt.arg); 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; } } @@ -425,7 +427,7 @@ void print_exec_error (qse_sed_t* sed) const qse_sed_loc_t* errloc = qse_sed_geterrloc(sed); 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_sed_geterrmsg(sed), (unsigned long)errloc->line, @@ -434,7 +436,7 @@ void print_exec_error (qse_sed_t* sed) } else { - qse_fprintf (QSE_STDERR, + qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: cannot execute - %s\n"), 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) { case QSE_SED_TRACER_READ: - /*qse_fprintf (QSE_STDERR, QSE_T("reading...\n"));*/ + /*qse_sio_putstrf (QSE_STDERR, QSE_T("reading...\n"));*/ break; case QSE_SED_TRACER_WRITE: - /*qse_fprintf (QSE_STDERR, QSE_T("wrting...\n"));*/ + /*qse_sio_putstrf (QSE_STDERR, QSE_T("wrting...\n"));*/ break; /* TODO: use function to get hold space and pattern space and print them */ 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("<>")), (unsigned long)cmd->loc.line, 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; 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("<>")), (unsigned long)cmd->loc.line, 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); 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; } mmgr = &xma_mmgr; @@ -720,13 +722,13 @@ static int sed_main (int argc, qse_char_t* argv[]) fs = qse_fs_open (mmgr, 0); 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; } 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")); goto oops; } @@ -735,7 +737,7 @@ static int sed_main (int argc, qse_char_t* argv[]) sed = qse_sed_openstdwithmmgr (mmgr, 0); 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; } @@ -757,14 +759,14 @@ static int sed_main (int argc, qse_char_t* argv[]) { /* i dont' use QSE_SED_IOSTD_SIO for input */ QSE_ASSERT (g_script.io[script_count].type == QSE_SED_IOSTD_STR); - qse_sprintf (exprbuf, QSE_COUNTOF(exprbuf), - QSE_T("expression #%lu"), (unsigned long)script_count); + qse_fmtuintmax (exprbuf, QSE_COUNTOF(exprbuf), + script_count, 10, -1, QSE_T('\0'), QSE_T("expression #")); target = exprbuf; } 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"), target, qse_sed_geterrmsg(sed), @@ -774,7 +776,7 @@ static int sed_main (int argc, qse_char_t* argv[]) } else { - qse_fprintf (QSE_STDERR, + qse_sio_putstrf (QSE_STDERR, QSE_T("ERROR: cannot compile %s - %s\n"), target, 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) { - 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; } @@ -828,7 +830,7 @@ static int sed_main (int argc, qse_char_t* argv[]) /* perform wild-card expansions for non-unix platforms */ 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; } @@ -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)); 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; } @@ -870,7 +872,7 @@ static int sed_main (int argc, qse_char_t* argv[]) { 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); 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)); 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; } 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) { - 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); QSE_MMGR_FREE (qse_sed_getmmgr(sed), tmpl_tmpfile); goto oops; @@ -943,14 +945,14 @@ static int sed_main (int argc, qse_char_t* argv[]) /* perform wild-card expansions for non-unix platforms */ 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; } in = QSE_MMGR_ALLOC (qse_sed_getmmgr(sed), QSE_SIZEOF(*in) * (xarg.size + 1)); 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; } @@ -1008,13 +1010,13 @@ oops: #if defined(QSE_BUILD_DEBUG) if (g_failmalloc > 0) { - qse_fprintf (QSE_STDERR, QSE_T("\n")); - qse_fprintf (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("\n")); + qse_sio_putstrf (QSE_STDERR, QSE_T("-[MALLOC COUNTS]---------------------------------------\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_free_count, (unsigned long)debug_mmgr_realloc_count); - qse_fprintf (QSE_STDERR, QSE_T("-------------------------------------------------------\n")); + qse_sio_putstrf (QSE_STDERR, QSE_T("-------------------------------------------------------\n")); } #endif return ret; @@ -1022,9 +1024,18 @@ oops: int qse_main (int argc, qse_achar_t* argv[]) { + int ret; #if defined(_WIN32) char locale[100]; - UINT codepage = GetConsoleOutputCP(); + UINT codepage; +#else + /* nothing */ +#endif + + qse_openstdsios (); + +#if defined(_WIN32) + codepage = GetConsoleOutputCP(); if (codepage == CP_UTF8) { /*SetConsoleOUtputCP (CP_UTF8);*/ @@ -1043,6 +1054,9 @@ int qse_main (int argc, qse_achar_t* argv[]) qse_setdflcmgrbyid (QSE_CMGR_SLMB); #endif - return qse_runmain (argc, argv, sed_main); + ret = qse_runmain (argc, argv, sed_main); + + qse_closestdsios (); + return ret; } diff --git a/qse/include/qse/cmn/str.h b/qse/include/qse/cmn/str.h index 5a33fe41..c41a696e 100644 --- a/qse/include/qse/cmn/str.h +++ b/qse/include/qse/cmn/str.h @@ -685,6 +685,46 @@ QSE_EXPORT qse_size_t qse_wcsxfncpy ( # define qse_strxfncpy(buf,bsz,fmt,str) qse_wcsxfncpy(buf,bsz,fmt,str) #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 * \a bsz by substituting new values for ${} segments within it. The actual diff --git a/qse/lib/cmn/Makefile.am b/qse/lib/cmn/Makefile.am index c3e75bea..3812411d 100644 --- a/qse/lib/cmn/Makefile.am +++ b/qse/lib/cmn/Makefile.am @@ -11,9 +11,11 @@ noinst_HEADERS = \ cp950.h \ fmt.h \ fmt-intmax.h \ - fmt-print.h \ + fmt-out.h \ fs.h \ mem.h \ + str-fcpy.h \ + str-fmt.h \ str-join.h \ str-subst.h \ syscall.h \ @@ -40,7 +42,7 @@ libqsecmn_la_SOURCES = \ fio.c \ fma.c \ fmt-intmax.c \ - fmt-print.c \ + fmt-out.c \ fs.c \ fs-err.c \ fs-move.c \ @@ -84,6 +86,7 @@ libqsecmn_la_SOURCES = \ str-end.c \ str-excl.c \ str-fcpy.c \ + str-fmt.c \ str-fnmat.c \ str-incl.c \ str-join.c \ diff --git a/qse/lib/cmn/Makefile.in b/qse/lib/cmn/Makefile.in index 8c478bac..b5ebf96d 100644 --- a/qse/lib/cmn/Makefile.in +++ b/qse/lib/cmn/Makefile.in @@ -87,38 +87,38 @@ am__DEPENDENCIES_1 = libqsecmn_la_DEPENDENCIES = $(am__DEPENDENCIES_1) 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 \ - 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 \ 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 \ 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-end.c str-excl.c str-fcpy.c str-fnmat.c str-incl.c \ - str-join.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-spn.c str-str.c str-subst.c \ - str-tok.c str-trm.c str-word.c task.c time.c tio.c tre.c \ - tre-ast.c tre-compile.c tre-match-backtrack.c \ + str-end.c str-excl.c str-fcpy.c str-fmt.c str-fnmat.c \ + str-incl.c str-join.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-spn.c str-str.c \ + str-subst.c str-tok.c str-trm.c str-word.c task.c time.c tio.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 \ xma.c uni.c cp949.c cp950.c @ENABLE_BUNDLED_UNICODE_TRUE@am__objects_1 = uni.lo @ENABLE_XCMGRS_TRUE@am__objects_2 = cp949.lo cp950.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 \ - htb.lo fio.lo fma.lo fmt-intmax.lo fmt-print.lo fs.lo \ - fs-err.lo fs-move.lo glob.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 oht.lo opt.lo path-basename.lo \ + htb.lo fio.lo fma.lo fmt-intmax.lo fmt-out.lo fs.lo fs-err.lo \ + fs-move.lo glob.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 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 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-dynw.lo str-end.lo str-excl.lo str-fcpy.lo str-fnmat.lo \ - str-incl.lo str-join.lo str-len.lo str-pac.lo str-pbrk.lo \ - str-put.lo str-rev.lo str-rot.lo str-set.lo str-spl.lo \ - str-spn.lo str-str.lo str-subst.lo str-tok.lo str-trm.lo \ - str-word.lo task.lo time.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 xma.lo \ - $(am__objects_1) $(am__objects_2) + str-dynw.lo str-end.lo str-excl.lo str-fcpy.lo str-fmt.lo \ + str-fnmat.lo str-incl.lo str-join.lo str-len.lo str-pac.lo \ + str-pbrk.lo str-put.lo str-rev.lo str-rot.lo str-set.lo \ + str-spl.lo str-spn.lo str-str.lo str-subst.lo str-tok.lo \ + str-trm.lo str-word.lo task.lo time.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 \ + xma.lo $(am__objects_1) $(am__objects_2) libqsecmn_la_OBJECTS = $(am_libqsecmn_la_OBJECTS) AM_V_lt = $(am__v_lt_@AM_V@) am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) @@ -369,9 +369,11 @@ noinst_HEADERS = \ cp950.h \ fmt.h \ fmt-intmax.h \ - fmt-print.h \ + fmt-out.h \ fs.h \ mem.h \ + str-fcpy.h \ + str-fmt.h \ str-join.h \ str-subst.h \ syscall.h \ @@ -385,17 +387,17 @@ noinst_HEADERS = \ 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 \ - 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 \ 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 \ 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-end.c str-excl.c str-fcpy.c str-fnmat.c str-incl.c \ - str-join.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-spn.c str-str.c str-subst.c \ - str-tok.c str-trm.c str-word.c task.c time.c tio.c tre.c \ - tre-ast.c tre-compile.c tre-match-backtrack.c \ + str-end.c str-excl.c str-fcpy.c str-fmt.c str-fnmat.c \ + str-incl.c str-join.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-spn.c str-str.c \ + str-subst.c str-tok.c str-trm.c str-word.c task.c time.c tio.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 \ xma.c $(am__append_1) $(am__append_2) 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)/fma.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-move.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-excl.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-incl.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str-join.Plo@am__quote@ diff --git a/qse/lib/cmn/fmt-print.c b/qse/lib/cmn/fmt-out.c similarity index 67% rename from qse/lib/cmn/fmt-print.c rename to qse/lib/cmn/fmt-out.c index 9b303360..01ed0009 100644 --- a/qse/lib/cmn/fmt-print.c +++ b/qse/lib/cmn/fmt-out.c @@ -24,6 +24,11 @@ #include #include #include "mem.h" +#include "fmt.h" + +#include /* for snrintf() */ +/* TODO: remove stdio.h once snprintf gets replaced by own +floting-point conversion implementation*/ /* number of bits in a byte */ #define NBBY 8 @@ -106,44 +111,6 @@ static const qse_wchar_t* w_hex2ascii = /* ------------------------------------------------------------------ */ -#include /* 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 uchar_t #undef ochar_t @@ -152,7 +119,9 @@ static int put_mchar (qse_mchar_t c, void *arg) #undef toupper #undef hex2ascii #undef sprintn -#undef xprintf +#undef put_char +#undef put_ochar +#undef fmtout #define char_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 toupper QSE_TOUPPER #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]) -#include "fmt-print.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); -} +#include "fmt-out.h" /* ------------------------------------------------------------------ */ @@ -192,7 +148,9 @@ qse_ssize_t qse_mvprintf (const char_t* fmt, va_list ap) #undef toupper #undef hex2ascii #undef sprintn -#undef xprintf +#undef put_char +#undef put_ochar +#undef fmtout #define char_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 toupper QSE_TOWUPPER #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]) -#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); -} diff --git a/qse/lib/cmn/fmt-print.h b/qse/lib/cmn/fmt-out.h similarity index 96% rename from qse/lib/cmn/fmt-print.h rename to qse/lib/cmn/fmt-out.h index c931eb9f..139ad823 100644 --- a/qse/lib/cmn/fmt-print.h +++ b/qse/lib/cmn/fmt-out.h @@ -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_OCHAR -/* TODO: error check */ #define PUT_CHAR(c) do { \ - if (put_char (c, arg) <= -1) goto oops; \ - outcnt++; \ + if (data->put_char (c, data->ctx) <= -1) goto oops; \ + data->count++; \ } while (0) #define PUT_OCHAR(c) do { \ - if (put_ochar (c, arg) <= -1) goto oops; \ - outcnt++; \ + if (data->put_ochar (c, data->ctx) <= -1) goto oops; \ + data->count++; \ } 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]; 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; qse_uintmax_t num = 0; int stop = 0; - qse_ssize_t outcnt = 0; + + data->count = 0; 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) { if (ch == T('\0')) goto done; - PUT_CHAR(ch); + PUT_CHAR (ch); } percent = fmt - 1; @@ -308,21 +308,21 @@ reswitch: case T('n'): 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 */ - *(va_arg(ap, qse_size_t*)) = outcnt; + *(va_arg(ap, qse_size_t*)) = data->count; #if (QSE_SIZEOF_LONG_LONG > 0) else if (lm_flag & LF_Q) /* ll */ - *(va_arg(ap, long long int*)) = outcnt; + *(va_arg(ap, long long int*)) = data->count; #endif 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 */ - *(va_arg(ap, short int*)) = outcnt; + *(va_arg(ap, short int*)) = data->count; else if (lm_flag & LF_C) /* hh */ - *(va_arg(ap, char*)) = outcnt; + *(va_arg(ap, char*)) = data->count; else - *(va_arg(ap, int*)) = outcnt; + *(va_arg(ap, int*)) = data->count; break; @@ -733,7 +733,7 @@ done: QSE_MMGR_FREE (QSE_MMGR_GETDFL(), fltfmt.ptr); if (fltout.ptr != fltout.sbuf) QSE_MMGR_FREE (QSE_MMGR_GETDFL(), fltout.ptr); - return outcnt; + return 0; oops: if (fltfmt.ptr != fltfmt.sbuf) diff --git a/qse/lib/cmn/fmt.h b/qse/lib/cmn/fmt.h index 73e50a41..48254441 100644 --- a/qse/lib/cmn/fmt.h +++ b/qse/lib/cmn/fmt.h @@ -24,36 +24,49 @@ #include #include -typedef int (*qse_printf_mchar_t) ( +typedef int (*qse_fmtout_mchar_t) ( qse_mchar_t c, void* ctx ); -typedef int (*qse_printf_wchar_t) ( - qse_mchar_t c, +typedef int (*qse_fmtout_wchar_t) ( + qse_wchar_t c, 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 -extern { +extern "C" { #endif -qse_ssize_t qse_mxprintf ( +int qse_mfmtout ( const qse_mchar_t* fmt, - qse_printf_mchar_t put_mchar, - qse_printf_wchar_t put_wchar, - void* arg, + qse_fmtout_t* data, va_list ap ); -qse_ssize_t qse_wxprintf ( +int qse_wfmtout ( const qse_wchar_t* fmt, - qse_printf_wchar_t put_wchar, - qse_printf_mchar_t put_mchar, - void* arg, + qse_fmtout_t* data, 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 } #endif diff --git a/qse/lib/cmn/sio.c b/qse/lib/cmn/sio.c index 9e2ff41e..d520f44d 100644 --- a/qse/lib/cmn/sio.c +++ b/qse/lib/cmn/sio.c @@ -636,12 +636,12 @@ qse_ssize_t qse_sio_putwcsn ( 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); } -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); } @@ -650,59 +650,90 @@ qse_ssize_t qse_sio_putmbsf (qse_sio_t* sio, const qse_mchar_t* fmt, ...) { va_list ap; 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); - x = qse_mxprintf (fmt, put_mchar, put_wchar, sio, ap); + x = qse_mfmtout (fmt, &fo, 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, ...) { 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); - x = qse_wxprintf (fmt, put_wchar, put_mchar, sio, ap); + x = qse_wfmtout (fmt, &fo, 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, ...) { 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); -#if defined(QSE_CHAR_IS_MCHAR) - x = qse_mxprintf (fmt, put_mchar, put_wchar, sio, ap); -#else - x = qse_wxprintf (fmt, put_wchar, put_mchar, sio, ap); -#endif + x = qse_fmtout (fmt, &fo, 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) { - 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) { - 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) { -#if defined(QSE_CHAR_IS_MCHAR) - return qse_mxprintf (fmt, put_mchar, put_wchar, sio, ap); -#else - return qse_wxprintf (fmt, put_wchar, put_mchar, sio, ap); -#endif + 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_fmtout (fmt, &fo, ap) <= -1)? -1: fo.count; } 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, ...) { 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); - x = qse_mxprintf (fmt, put_mchar, put_wchar, sio_stdout, ap); + x = qse_mfmtout (fmt, &fo, ap); va_end (ap); - return x; + return (x <= -1)? -1: fo.count; } qse_ssize_t qse_putwcsf (const qse_wchar_t* fmt, ...) { 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); - x = qse_wxprintf (fmt, put_wchar, put_mchar, sio_stdout, ap); + x = qse_wfmtout (fmt, &fo, ap); va_end (ap); - return x; + return (x <= -1)? -1: fo.count; } qse_ssize_t qse_putstrf (const qse_char_t* fmt, ...) { 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); -#if defined(QSE_CHAR_IS_MCHAR) - x = qse_mxprintf (fmt, put_mchar, put_wchar, sio_stdout, ap); -#else - x = qse_wxprintf (fmt, put_wchar, put_mchar, sio_stdout, ap); -#endif + x = qse_fmtout (fmt, &fo, 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) { - 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) { - 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) { -#if defined(QSE_CHAR_IS_MCHAR) - return qse_mxprintf (fmt, put_mchar, put_wchar, sio_stdout, ap); -#else - return qse_wxprintf (fmt, put_wchar, put_mchar, sio_stdout, ap); -#endif + 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_fmtout (fmt, &fo, ap) <= -1)? -1: fo.count; } diff --git a/qse/lib/cmn/str-fcpy.c b/qse/lib/cmn/str-fcpy.c index 8334119e..760a9bdb 100644 --- a/qse/lib/cmn/str-fcpy.c +++ b/qse/lib/cmn/str-fcpy.c @@ -20,452 +20,39 @@ #include -qse_size_t qse_mbsfcpy ( - qse_mchar_t* buf, const qse_mchar_t* fmt, const qse_mchar_t* str[]) -{ - qse_mchar_t* b = buf; - const qse_mchar_t* f = fmt; +#undef T +#undef char_t +#undef cstr_t +#undef strfcpy +#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; -} diff --git a/qse/lib/cmn/str-fcpy.h b/qse/lib/cmn/str-fcpy.h new file mode 100644 index 00000000..2e7134bf --- /dev/null +++ b/qse/lib/cmn/str-fcpy.h @@ -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 . + */ + +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; +} diff --git a/qse/lib/cmn/str-fmt.c b/qse/lib/cmn/str-fmt.c new file mode 100644 index 00000000..4034a984 --- /dev/null +++ b/qse/lib/cmn/str-fmt.c @@ -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 . + */ + +#include +#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" diff --git a/qse/lib/cmn/str-fmt.h b/qse/lib/cmn/str-fmt.h new file mode 100644 index 00000000..9517b396 --- /dev/null +++ b/qse/lib/cmn/str-fmt.h @@ -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 . + */ + + +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; +} + diff --git a/qse/lib/http/httpd-std.c b/qse/lib/http/httpd-std.c index 59361561..3f37bcfd 100644 --- a/qse/lib/http/httpd-std.c +++ b/qse/lib/http/httpd-std.c @@ -668,6 +668,11 @@ static int server_open (qse_httpd_t* httpd, qse_httpd_server_t* server) flag = 1; setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (void*)&flag, QSE_SIZEOF(flag)); #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 */ #if defined(IP_TRANSPARENT)