qse/samples/cmn/uri01.c

144 lines
3.6 KiB
C
Raw Permalink Normal View History

#include <qse/cmn/uri.h>
#include <qse/cmn/main.h>
#include <qse/cmn/mbwc.h>
2016-04-29 03:55:42 +00:00
#include <qse/si/sio.h>
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
static void xxx (const qse_wchar_t* ptr, qse_size_t len)
{
if (ptr)
{
qse_size_t i;
qse_printf (QSE_T(" ["));
for (i = 0; i < len; i++)
qse_printf (QSE_T("%hc"), ptr[i]);
qse_printf (QSE_T("] "));
}
else
{
qse_printf (QSE_T(" (null) "));
}
}
2012-10-15 09:46:29 +00:00
static int test_main (int argc, qse_char_t* argv[])
{
static const qse_wchar_t* wcs[] =
{
QSE_WT("http://www.google.com"),
QSE_WT("http://www.google.com:80"),
QSE_WT("http://abc@www.google.com:80"),
QSE_WT("http://abc:def@www.google.com:80"),
QSE_WT("http://abc:def@www.google.com:80/"),
QSE_WT("http://abc:def@www.google.com:80/?#"),
QSE_WT("http://abc:def@www.google.com:80/abcdef/ghi?kkk=23#fragment"),
QSE_WT("http://abc:def@www.google.com:80/abcdef/ghi#fragment#fragment"),
QSE_WT("http://abc:def@www@.google.com:80/abcdef/ghi#fragment#fragment"),
QSE_WT("http://@@@@@abc:def@www@.google.com:80/abcdef/ghi#fragment#fragment")
};
qse_size_t i;
qse_uri_t uri;
for (i = 0; i < QSE_COUNTOF(wcs); i++)
{
qse_printf (QSE_T("[%ls] => "), wcs[i]);
if (qse_wcstouri (wcs[i], &uri, 0) <= -1)
{
qse_printf (QSE_T("[ERROR]"));
}
else
{
xxx (uri.scheme.ptr, uri.scheme.len);
xxx (uri.auth.user.ptr, uri.auth.user.len);
xxx (uri.auth.pass.ptr, uri.auth.pass.len);
xxx (uri.host.ptr, uri.host.len);
xxx (uri.port.ptr, uri.port.len);
xxx (uri.path.ptr, uri.path.len);
xxx (uri.query.ptr, uri.query.len);
xxx (uri.frag.ptr, uri.frag.len);
}
qse_printf (QSE_T("\n"));
}
qse_printf (QSE_T("================================================\n"));
for (i = 0; i < QSE_COUNTOF(wcs); i++)
{
qse_printf (QSE_T("[%ls] => "), wcs[i]);
if (qse_wcstouri (wcs[i], &uri, QSE_WCSTOURI_NOQUERY | QSE_WCSTOURI_NOAUTH) <= -1)
{
qse_printf (QSE_T("[ERROR]"));
}
else
{
xxx (uri.scheme.ptr, uri.scheme.len);
xxx (uri.auth.user.ptr, uri.auth.user.len);
xxx (uri.auth.pass.ptr, uri.auth.pass.len);
xxx (uri.host.ptr, uri.host.len);
xxx (uri.port.ptr, uri.port.len);
xxx (uri.path.ptr, uri.path.len);
xxx (uri.query.ptr, uri.query.len);
xxx (uri.frag.ptr, uri.frag.len);
}
qse_printf (QSE_T("\n"));
}
qse_printf (QSE_T("================================================\n"));
for (i = 0; i < QSE_COUNTOF(wcs); i++)
{
qse_printf (QSE_T("[%ls] => "), wcs[i]);
if (qse_wcstouri (wcs[i], &uri, QSE_WCSTOURI_NOQUERY | QSE_WCSTOURI_NOAUTH | QSE_WCSTOURI_NOFRAG) <= -1)
{
qse_printf (QSE_T("[ERROR]"));
}
else
{
xxx (uri.scheme.ptr, uri.scheme.len);
xxx (uri.auth.user.ptr, uri.auth.user.len);
xxx (uri.auth.pass.ptr, uri.auth.pass.len);
xxx (uri.host.ptr, uri.host.len);
xxx (uri.port.ptr, uri.port.len);
xxx (uri.path.ptr, uri.path.len);
xxx (uri.query.ptr, uri.query.len);
xxx (uri.frag.ptr, uri.frag.len);
}
qse_printf (QSE_T("\n"));
}
qse_printf (QSE_T("================================================\n"));
return 0;
}
2012-10-15 09:46:29 +00:00
int qse_main (int argc, qse_achar_t* argv[])
{
2015-03-11 06:12:48 +00:00
int x;
#if defined(_WIN32)
2015-03-11 06:12:48 +00:00
char locale[100];
UINT codepage = GetConsoleOutputCP();
if (codepage == CP_UTF8)
{
/*SetConsoleOUtputCP (CP_UTF8);*/
2012-10-15 09:36:39 +00:00
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
}
else
{
2015-03-11 06:12:48 +00:00
sprintf (locale, ".%u", (unsigned int)codepage);
setlocale (LC_ALL, locale);
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
}
#else
setlocale (LC_ALL, "");
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
#endif
2017-09-16 08:54:25 +00:00
qse_open_stdsios ();
x = qse_run_main (argc, argv, test_main);
2017-09-16 08:54:25 +00:00
qse_close_stdsios ();
2015-03-11 06:12:48 +00:00
return x;
}