qse/samples/cmn/rex01.c

99 lines
1.9 KiB
C
Raw Normal View History

#include <qse/cmn/rex.h>
2011-12-28 14:26:02 +00:00
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/main.h>
2012-01-03 14:41:15 +00:00
#include <qse/cmn/mbwc.h>
2011-10-14 22:57:41 +00:00
#include <qse/cmn/path.h>
2016-04-29 03:55:42 +00:00
#include <qse/si/sio.h>
2012-01-03 14:41:15 +00:00
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
static int rex_main (int argc, qse_char_t* argv[])
{
qse_rex_t* rex;
qse_rex_node_t* start;
2014-07-08 14:30:42 +00:00
qse_cstr_t str;
qse_cstr_t matstr;
2009-11-28 07:46:49 +00:00
int n;
2009-11-28 01:01:43 +00:00
if (argc != 3)
{
qse_printf (QSE_T("USAGE: %s pattern string\n"),
qse_basename(argv[0]));
return -1;
}
2011-12-28 14:26:02 +00:00
rex = qse_rex_open (QSE_MMGR_GETDFL(), 0, QSE_NULL);
if (rex == QSE_NULL)
{
qse_printf (QSE_T("ERROR: cannot open rex\n"));
return -1;
}
2015-03-11 06:12:48 +00:00
qse_rex_setopt (rex, QSE_REX_STRICT);
2009-12-10 05:35:54 +00:00
start = qse_rex_comp (rex, argv[1], qse_strlen(argv[1]));
if (start == QSE_NULL)
{
qse_printf (QSE_T("ERROR: cannot compile - %s\n"),
qse_rex_geterrmsg(rex));
qse_rex_close (rex);
return -1;
}
2009-11-28 07:46:49 +00:00
str.ptr = argv[2];
str.len = qse_strlen(argv[2]);
qse_printf (QSE_T("compile ok\n"));
2009-11-28 07:46:49 +00:00
n = qse_rex_exec (rex, &str, &str, &matstr);
if (n <= -1)
{
qse_printf (QSE_T("ERROR: cannot execute - %s\n"),
qse_rex_geterrmsg(rex));
qse_rex_close (rex);
return -1;
}
2009-11-28 07:46:49 +00:00
if (n >= 1)
{
qse_printf (QSE_T("MATCH: [%.*s] beginning from char #%d\n"),
(int)matstr.len, matstr.ptr,
2010-05-10 07:44:39 +00:00
(int)(matstr.ptr - str.ptr + 1));
2009-11-28 07:46:49 +00:00
}
qse_rex_close (rex);
return 0;
}
int qse_main (int argc, qse_achar_t* argv[])
{
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
2015-03-11 06:12:48 +00:00
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, rex_main);
2017-09-16 08:54:25 +00:00
qse_close_stdsios ();
2015-03-11 06:12:48 +00:00
return x;
}