2012-02-26 02:23:47 +00:00
|
|
|
#include <qse/cmn/ipad.h>
|
|
|
|
#include <qse/cmn/main.h>
|
|
|
|
#include <qse/cmn/mbwc.h>
|
2016-04-29 03:55:42 +00:00
|
|
|
#include <qse/si/sio.h>
|
2012-02-26 02:23:47 +00:00
|
|
|
|
|
|
|
#include <locale.h>
|
|
|
|
#if defined(_WIN32)
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2012-11-18 15:19:51 +00:00
|
|
|
static int test_ip4ad (void)
|
2012-02-26 02:23:47 +00:00
|
|
|
{
|
2012-11-18 15:19:51 +00:00
|
|
|
qse_ip4ad_t ip4ad;
|
2012-02-26 02:23:47 +00:00
|
|
|
qse_char_t buf[32];
|
|
|
|
qse_mchar_t mbsbuf[32];
|
|
|
|
qse_wchar_t wcsbuf[32];
|
|
|
|
qse_size_t i;
|
|
|
|
|
|
|
|
static qse_char_t* ipstr[] =
|
|
|
|
{
|
|
|
|
QSE_T("192.168.1.1"),
|
|
|
|
QSE_T("255.255.255.255"),
|
|
|
|
QSE_T("4.3.0.0"),
|
|
|
|
QSE_T("4.3.0.0X"),
|
|
|
|
QSE_T("65.1234.11.34"),
|
|
|
|
QSE_T("65.123.11.34"),
|
|
|
|
QSE_T("1.1.1.1")
|
|
|
|
};
|
|
|
|
|
|
|
|
static qse_mchar_t* ipstr_mbs[] =
|
|
|
|
{
|
|
|
|
QSE_MT("192.168.1.1"),
|
|
|
|
QSE_MT("255.255.255.255"),
|
|
|
|
QSE_MT("4.3.0.0"),
|
|
|
|
QSE_MT("4.3.0.0X"),
|
|
|
|
QSE_MT("65.1234.11.34"),
|
|
|
|
QSE_MT("65.123.11.34"),
|
|
|
|
QSE_MT("1.1.1.1")
|
|
|
|
};
|
|
|
|
|
|
|
|
static qse_wchar_t* ipstr_wcs[] =
|
|
|
|
{
|
|
|
|
QSE_WT("192.168.1.1"),
|
|
|
|
QSE_WT("255.255.255.255"),
|
|
|
|
QSE_WT("4.3.0.0"),
|
|
|
|
QSE_WT("4.3.0.0X"),
|
|
|
|
QSE_WT("65.1234.11.34"),
|
|
|
|
QSE_WT("65.123.11.34"),
|
|
|
|
QSE_WT("1.1.1.1")
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; i < QSE_COUNTOF(ipstr); i++)
|
|
|
|
{
|
2012-11-18 15:19:51 +00:00
|
|
|
if (qse_strtoip4ad (ipstr[i], &ip4ad) <= -1)
|
2012-02-26 02:23:47 +00:00
|
|
|
{
|
|
|
|
qse_printf (QSE_T("Failed to convert %s\n"), ipstr[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-18 15:19:51 +00:00
|
|
|
qse_ip4adtostr (&ip4ad, buf, QSE_COUNTOF(buf));
|
2012-02-26 02:23:47 +00:00
|
|
|
qse_printf (QSE_T("Converted [%s] to [%s]\n"), ipstr[i], buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qse_printf (QSE_T("-------------------\n"));
|
|
|
|
for (i = 0; i < QSE_COUNTOF(ipstr_mbs); i++)
|
|
|
|
{
|
2012-11-18 15:19:51 +00:00
|
|
|
if (qse_mbstoip4ad (ipstr_mbs[i], &ip4ad) <= -1)
|
2012-02-26 02:23:47 +00:00
|
|
|
{
|
|
|
|
qse_printf (QSE_T("Failed to convert %hs\n"), ipstr_mbs[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-18 15:19:51 +00:00
|
|
|
qse_ip4adtombs (&ip4ad, mbsbuf, QSE_COUNTOF(mbsbuf));
|
2012-02-26 02:23:47 +00:00
|
|
|
qse_printf (QSE_T("Converted [%hs] to [%hs]\n"), ipstr[i], mbsbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qse_printf (QSE_T("-------------------\n"));
|
|
|
|
for (i = 0; i < QSE_COUNTOF(ipstr_wcs); i++)
|
|
|
|
{
|
2012-11-18 15:19:51 +00:00
|
|
|
if (qse_wcstoip4ad (ipstr_wcs[i], &ip4ad) <= -1)
|
2012-02-26 02:23:47 +00:00
|
|
|
{
|
|
|
|
qse_printf (QSE_T("Failed to convert %ls\n"), ipstr_wcs[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-18 15:19:51 +00:00
|
|
|
qse_ip4adtowcs (&ip4ad, wcsbuf, QSE_COUNTOF(wcsbuf));
|
2012-02-26 02:23:47 +00:00
|
|
|
qse_printf (QSE_T("Converted [%ls] to [%ls]\n"), ipstr[i], wcsbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-18 15:19:51 +00:00
|
|
|
static int test_ip6ad (void)
|
2012-02-27 15:44:57 +00:00
|
|
|
{
|
2012-11-18 15:19:51 +00:00
|
|
|
qse_ip6ad_t ip6ad;
|
2012-02-27 15:44:57 +00:00
|
|
|
qse_char_t buf[32];
|
|
|
|
qse_mchar_t mbsbuf[32];
|
|
|
|
qse_wchar_t wcsbuf[32];
|
|
|
|
qse_size_t i;
|
|
|
|
|
|
|
|
static qse_char_t* ipstr[] =
|
|
|
|
{
|
|
|
|
QSE_T("::"),
|
|
|
|
QSE_T("::1"),
|
|
|
|
QSE_T("fe80::f27b:cbff:fea3:f40c"),
|
|
|
|
QSE_T("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
|
|
|
|
QSE_T("2001:db8:1234:ffff:ffff:ffff:ffff:ffff"),
|
|
|
|
QSE_T("::ffff:0:0"),
|
|
|
|
QSE_T("::ffff:192.168.1.1")
|
|
|
|
};
|
|
|
|
|
|
|
|
static qse_mchar_t* ipstr_mbs[] =
|
|
|
|
{
|
|
|
|
QSE_MT("::"),
|
|
|
|
QSE_MT("::1"),
|
|
|
|
QSE_MT("fe80::f27b:cbff:fea3:f40c"),
|
|
|
|
QSE_MT("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
|
|
|
|
QSE_MT("2001:db8:1234:ffff:ffff:ffff:ffff:ffff"),
|
|
|
|
QSE_MT("::ffff:0:0"),
|
|
|
|
QSE_MT("::ffff:192.168.1.1")
|
|
|
|
};
|
|
|
|
|
|
|
|
static qse_wchar_t* ipstr_wcs[] =
|
|
|
|
{
|
|
|
|
QSE_WT("::"),
|
|
|
|
QSE_WT("::1"),
|
|
|
|
QSE_WT("fe80::f27b:cbff:fea3:f40c"),
|
|
|
|
QSE_WT("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
|
|
|
|
QSE_WT("2001:db8:1234:ffff:ffff:ffff:ffff:ffff"),
|
|
|
|
QSE_WT("::ffff:0:0"),
|
|
|
|
QSE_WT("::ffff:192.168.1.1")
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; i < QSE_COUNTOF(ipstr); i++)
|
|
|
|
{
|
2012-11-18 15:19:51 +00:00
|
|
|
if (qse_strtoip6ad (ipstr[i], &ip6ad) <= -1)
|
2012-02-27 15:44:57 +00:00
|
|
|
{
|
|
|
|
qse_printf (QSE_T("Failed to convert %s\n"), ipstr[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-18 15:19:51 +00:00
|
|
|
qse_ip6adtostr (&ip6ad, buf, QSE_COUNTOF(buf));
|
2012-02-27 15:44:57 +00:00
|
|
|
qse_printf (QSE_T("Converted [%s] to [%s]\n"), ipstr[i], buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qse_printf (QSE_T("-------------------\n"));
|
|
|
|
for (i = 0; i < QSE_COUNTOF(ipstr_mbs); i++)
|
|
|
|
{
|
2012-11-18 15:19:51 +00:00
|
|
|
if (qse_mbstoip6ad (ipstr_mbs[i], &ip6ad) <= -1)
|
2012-02-27 15:44:57 +00:00
|
|
|
{
|
|
|
|
qse_printf (QSE_T("Failed to convert %hs\n"), ipstr_mbs[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-18 15:19:51 +00:00
|
|
|
qse_ip6adtombs (&ip6ad, mbsbuf, QSE_COUNTOF(mbsbuf));
|
2012-02-27 15:44:57 +00:00
|
|
|
qse_printf (QSE_T("Converted [%hs] to [%hs]\n"), ipstr_mbs[i], mbsbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qse_printf (QSE_T("-------------------\n"));
|
|
|
|
for (i = 0; i < QSE_COUNTOF(ipstr_wcs); i++)
|
|
|
|
{
|
2012-11-18 15:19:51 +00:00
|
|
|
if (qse_wcstoip6ad (ipstr_wcs[i], &ip6ad) <= -1)
|
2012-02-27 15:44:57 +00:00
|
|
|
{
|
|
|
|
qse_printf (QSE_T("Failed to convert %ls\n"), ipstr_wcs[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-18 15:19:51 +00:00
|
|
|
qse_ip6adtowcs (&ip6ad, wcsbuf, QSE_COUNTOF(wcsbuf));
|
2012-02-27 15:44:57 +00:00
|
|
|
qse_printf (QSE_T("Converted [%ls] to [%ls]\n"), ipstr_wcs[i], wcsbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-15 09:46:29 +00:00
|
|
|
static int test_main (int argc, qse_char_t* argv[])
|
2012-02-27 15:44:57 +00:00
|
|
|
{
|
2012-11-18 15:19:51 +00:00
|
|
|
test_ip4ad ();
|
2012-02-27 15:44:57 +00:00
|
|
|
qse_printf (QSE_T("==============\n"));
|
2012-11-18 15:19:51 +00:00
|
|
|
test_ip6ad ();
|
2012-02-27 15:44:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-15 09:46:29 +00:00
|
|
|
int qse_main (int argc, qse_achar_t* argv[])
|
2012-02-26 02:23:47 +00:00
|
|
|
{
|
2015-03-11 06:12:48 +00:00
|
|
|
int x;
|
2012-02-26 02:23:47 +00:00
|
|
|
#if defined(_WIN32)
|
2015-03-11 06:12:48 +00:00
|
|
|
char locale[100];
|
|
|
|
UINT codepage = GetConsoleOutputCP();
|
2012-02-26 02:23:47 +00:00
|
|
|
if (codepage == CP_UTF8)
|
|
|
|
{
|
|
|
|
/*SetConsoleOUtputCP (CP_UTF8);*/
|
2012-10-15 09:36:39 +00:00
|
|
|
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
|
2012-02-26 02:23:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-11 06:12:48 +00:00
|
|
|
sprintf (locale, ".%u", (unsigned int)codepage);
|
|
|
|
setlocale (LC_ALL, locale);
|
2015-04-27 08:37:57 +00:00
|
|
|
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
|
2012-02-26 02:23:47 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
setlocale (LC_ALL, "");
|
2015-04-27 08:37:57 +00:00
|
|
|
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
|
2012-02-26 02:23:47 +00:00
|
|
|
#endif
|
2017-09-16 08:54:25 +00:00
|
|
|
qse_open_stdsios ();
|
2018-09-13 03:16:23 +00:00
|
|
|
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;
|
2012-02-26 02:23:47 +00:00
|
|
|
}
|
|
|
|
|