qse/samples/cmn/tre01.c

105 lines
2.1 KiB
C
Raw Normal View History

2011-09-01 09:43:46 +00:00
#include <qse/cmn/tre.h>
#include <qse/cmn/mem.h>
2011-10-14 22:57:41 +00:00
#include <qse/cmn/path.h>
2012-01-03 14:41:15 +00:00
#include <qse/cmn/main.h>
#include <qse/cmn/mbwc.h>
2016-04-29 03:55:42 +00:00
#include <qse/si/sio.h>
2011-09-01 09:43:46 +00:00
2012-01-03 14:41:15 +00:00
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
2012-10-15 09:46:29 +00:00
static int test_main (int argc, qse_char_t* argv[])
2011-09-01 09:43:46 +00:00
{
qse_tre_t tre;
unsigned int nsubmat;
qse_tre_match_t* mat = QSE_NULL;
if (argc != 3)
{
qse_printf (QSE_T("USAGE: %s pattern string\n"),
qse_basename(argv[0]));
return -1;
}
2011-09-01 09:43:46 +00:00
2011-12-28 14:26:02 +00:00
qse_tre_init (&tre, QSE_MMGR_GETDFL());
2011-09-01 09:43:46 +00:00
if (qse_tre_comp (&tre, argv[1], &nsubmat, QSE_TRE_EXTENDED) <= -1)
2011-09-01 09:43:46 +00:00
{
qse_printf (QSE_T("ERROR: Cannot compile pattern [%s] - %s\n"), argv[1], qse_tre_geterrmsg(&tre));
2011-09-01 09:43:46 +00:00
goto oops;
}
if (nsubmat > 0)
2011-09-01 09:43:46 +00:00
{
mat = QSE_MMGR_ALLOC (qse_tre_getmmgr(&tre), QSE_SIZEOF(*mat) * nsubmat);
if (mat == QSE_NULL)
{
qse_printf (QSE_T("ERROR: Cannot allocate submatch array\n"));
goto oops;
}
}
if (qse_tre_exec(&tre, argv[2], mat, nsubmat, 0) <= -1)
{
if (QSE_TRE_ERRNUM(&tre) == QSE_TRE_ENOMATCH) qse_printf (QSE_T("Match: NO\n"));
else
{
qse_printf (QSE_T("ERROR: Cannot not match pattern - %s\n"), qse_tre_geterrmsg(&tre));
goto oops;
}
2011-09-01 09:43:46 +00:00
}
else
{
unsigned int i;
qse_printf (QSE_T("Match: YES\n"));
for (i = 0; i < nsubmat; i++)
{
if (mat[i].rm_so == -1) break;
qse_printf (QSE_T("SUBMATCH[%u] = [%.*s]\n"), i,
(int)(mat[i].rm_eo - mat[i].rm_so), &argv[2][mat[i].rm_so]);
}
2011-09-01 09:43:46 +00:00
}
if (mat) QSE_MMGR_FREE (qse_tre_getmmgr(&tre), mat);
2011-09-01 09:43:46 +00:00
qse_tre_fini (&tre);
return 0;
oops:
if (mat) QSE_MMGR_FREE (qse_tre_getmmgr(&tre), mat);
2011-09-01 09:43:46 +00:00
qse_tre_fini (&tre);
return -1;
}
2012-10-15 09:46:29 +00:00
int qse_main (int argc, qse_achar_t* argv[])
2011-09-01 09:43:46 +00:00
{
2015-03-11 06:12:48 +00:00
int x;
2012-01-03 14:41:15 +00:00
#if defined(_WIN32)
2012-10-15 09:46:29 +00:00
char locale[100];
2015-03-11 06:12:48 +00:00
UINT codepage = GetConsoleOutputCP();
2012-01-03 14:41:15 +00:00
if (codepage == CP_UTF8)
{
/*SetConsoleOUtputCP (CP_UTF8);*/
2012-10-15 09:36:39 +00:00
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
2012-01-03 14:41:15 +00:00
}
else
{
2015-03-11 06:12:48 +00:00
sprintf (locale, ".%u", (unsigned int)codepage);
setlocale (LC_ALL, locale);
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
2012-01-03 14:41:15 +00:00
}
#else
setlocale (LC_ALL, "");
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
2012-01-03 14:41:15 +00:00
#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;
2011-09-01 09:43:46 +00:00
}