qse/qse/samples/cmn/rex01.c

70 lines
1.3 KiB
C
Raw Normal View History

#include <qse/cmn/rex.h>
#include <qse/cmn/str.h>
#include <qse/cmn/main.h>
2011-10-14 22:57:41 +00:00
#include <qse/cmn/path.h>
#include <qse/cmn/stdio.h>
static int rex_main (int argc, qse_char_t* argv[])
{
qse_rex_t* rex;
qse_rex_node_t* start;
2009-11-28 07:46:49 +00:00
qse_cstr_t str;
qse_cstr_t matstr;
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;
}
rex = qse_rex_open (QSE_NULL, 0, QSE_NULL);
if (rex == QSE_NULL)
{
qse_printf (QSE_T("ERROR: cannot open rex\n"));
return -1;
}
2009-12-10 05:35:54 +00:00
qse_rex_setoption (rex, QSE_REX_STRICT);
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[])
{
return qse_runmain (argc, argv, rex_main);
}