diff options
Diffstat (limited to 'sample')
-rw-r--r-- | sample/Makefile.am | 8 | ||||
-rw-r--r-- | sample/callback_each_match.c | 168 | ||||
-rw-r--r-- | sample/count.c | 6 | ||||
-rw-r--r-- | sample/listcap.c | 2 |
4 files changed, 177 insertions, 7 deletions
diff --git a/sample/Makefile.am b/sample/Makefile.am index c2c4596..681cd2a 100644 --- a/sample/Makefile.am +++ b/sample/Makefile.am @@ -4,13 +4,13 @@ lib_onig = ../src/libonig.la LDADD = $(lib_onig) AM_CFLAGS = -Wall -AM_LDFLAGS = -L$(prefix)/lib +AM_LDFLAGS = -L$(libdir) AM_CPPFLAGS = -I$(top_srcdir)/src if ENABLE_POSIX_API -TESTS = encode listcap names posix simple sql syntax user_property callout echo count bug_fix regset scan +TESTS = encode listcap names posix simple sql syntax user_property callout echo count bug_fix regset scan callback_each_match else -TESTS = encode listcap names simple sql syntax user_property callout echo count bug_fix regset scan +TESTS = encode listcap names simple sql syntax user_property callout echo count bug_fix regset scan callback_each_match endif check_PROGRAMS = $(TESTS) @@ -29,6 +29,7 @@ count_SOURCES = count.c bug_fix = bug_fix.c regset_SOURCES = regset.c scan_SOURCES = scan.c +callback_each_match_SOURCES = callback_each_match.c sampledir = . @@ -49,3 +50,4 @@ endif $(sampledir)/bug_fix $(sampledir)/regset $(sampledir)/scan + $(sampledir)/callback_each_match diff --git a/sample/callback_each_match.c b/sample/callback_each_match.c new file mode 100644 index 0000000..10ed56d --- /dev/null +++ b/sample/callback_each_match.c @@ -0,0 +1,168 @@ +/* + * callback_each_match.c + */ +#include <stdio.h> +#include <string.h> +#include "oniguruma.h" + +static int +each_match_callback(const UChar* str, const UChar* end, + const UChar* match_start, OnigRegion* region, void* user_data) +{ +#if 1 + fprintf(stdout, "each_match_callback:\n"); + fprintf(stdout, " match at: %ld - %d: %p\n", match_start - str, region->end[0], + user_data); + fprintf(stdout, " region[0]: %d - %d\n", region->beg[0], region->end[0]); +#else + int i; + i = region->beg[0]; + fputc('<', stdout); + while (i < region->end[0]) { + fputc((int )str[i], stdout); + i++; + } + fputc('>', stdout); +#endif + +#if 0 + /* terminate match/search if returns error code < 0 */ + return ONIG_ABORT; +#endif + + return ONIG_NORMAL; +} + +static int +search(UChar* pattern, UChar* str, OnigOptionType options, OnigOptionType runtime_options) +{ + int r; + unsigned char *start, *range, *end; + regex_t* reg; + OnigErrorInfo einfo; + OnigRegion *region; + OnigMatchParam* mp; + void* user_data; + + r = onig_new(®, pattern, pattern + strlen((char* )pattern), + options, ONIG_ENCODING_ASCII, ONIG_SYNTAX_DEFAULT, &einfo); + if (r != ONIG_NORMAL) { + char s[ONIG_MAX_ERROR_MESSAGE_LEN]; + onig_error_code_to_str((UChar* )s, r, &einfo); + fprintf(stderr, "ERROR: %s\n", s); + return -1; + } + + region = onig_region_new(); + + end = str + strlen((char* )str); + start = str; + range = end; + mp = onig_new_match_param(); + if (mp == 0) return -2; + + user_data = (void* )0x1234; + onig_set_callout_user_data_of_match_param(mp, user_data); + + r = onig_search_with_param(reg, str, end, start, range, region, + runtime_options, mp); + onig_free_match_param(mp); + if (r >= 0) { + /* If ONIG_OPTION_CALLBACK_EACH_MATCH is used with + ONIG_OPTION_FIND_LONGEST, it may also return positive value. */ + fprintf(stdout, "\nr: %d\n", r); + } + else if (r == ONIG_MISMATCH) { + /* always return ONIG_MISMATCH if ONIG_OPTION_CALLBACK_EACH_MATCH */ + fprintf(stdout, "\n"); + } + else { /* error */ + char s[ONIG_MAX_ERROR_MESSAGE_LEN]; + onig_error_code_to_str((UChar* )s, r); + fprintf(stderr, "ERROR: %s\n", s); + onig_region_free(region, 1 /* 1:free self, 0:free contents only */); + onig_free(reg); + onig_end(); + return -1; + } + + return 0; +} + +static int +match(UChar* pattern, UChar* str, UChar* at, OnigOptionType options, OnigOptionType runtime_options) +{ + int r; + unsigned char *start, *range, *end; + regex_t* reg; + OnigErrorInfo einfo; + OnigRegion *region; + OnigMatchParam* mp; + void* user_data; + + r = onig_new(®, pattern, pattern + strlen((char* )pattern), + options, ONIG_ENCODING_ASCII, ONIG_SYNTAX_DEFAULT, &einfo); + if (r != ONIG_NORMAL) { + char s[ONIG_MAX_ERROR_MESSAGE_LEN]; + onig_error_code_to_str((UChar* )s, r, &einfo); + fprintf(stderr, "ERROR: %s\n", s); + return -1; + } + + region = onig_region_new(); + + end = str + strlen((char* )str); + start = str; + range = end; + mp = onig_new_match_param(); + if (mp == 0) return -2; + + user_data = (void* )0x1234; + onig_set_callout_user_data_of_match_param(mp, user_data); + + r = onig_match_with_param(reg, str, end, at, region, runtime_options, mp); + onig_free_match_param(mp); + if (r >= 0) { + /* If ONIG_OPTION_CALLBACK_EACH_MATCH is used with + ONIG_OPTION_FIND_LONGEST, it may also return positive value. */ + fprintf(stdout, "\nr: %d\n", r); + } + else if (r == ONIG_MISMATCH) { + /* always return ONIG_MISMATCH if ONIG_OPTION_CALLBACK_EACH_MATCH */ + fprintf(stdout, "\n"); + } + else { /* error */ + char s[ONIG_MAX_ERROR_MESSAGE_LEN]; + onig_error_code_to_str((UChar* )s, r); + fprintf(stderr, "ERROR: %s\n", s); + onig_region_free(region, 1 /* 1:free self, 0:free contents only */); + onig_free(reg); + onig_end(); + return -1; + } + + return 0; +} + +extern int main(int argc, char* argv[]) +{ + OnigEncoding use_encs[1]; + + static UChar* pattern = (UChar* )"a(.*)\\Kb|[e-f]+"; + static UChar* str = (UChar* )"zzzzafffb"; + + use_encs[0] = ONIG_ENCODING_ASCII; + onig_initialize(use_encs, sizeof(use_encs)/sizeof(use_encs[0])); + onig_set_callback_each_match(each_match_callback); + + fprintf(stdout, "<search>\n"); + search(pattern, str, ONIG_OPTION_NONE, ONIG_OPTION_CALLBACK_EACH_MATCH); + fprintf(stdout, "<search with FIND_LONGEST>\n"); + search(pattern, str, ONIG_OPTION_FIND_LONGEST, ONIG_OPTION_CALLBACK_EACH_MATCH); + + fprintf(stdout, "<match>\n"); + match(pattern, str, str + 5, ONIG_OPTION_NONE, ONIG_OPTION_CALLBACK_EACH_MATCH); + + onig_end(); + return 0; +} diff --git a/sample/count.c b/sample/count.c index 2b67db7..904101c 100644 --- a/sample/count.c +++ b/sample/count.c @@ -59,10 +59,10 @@ test(OnigEncoding enc, OnigMatchParam* mp, char* in_pattern, char* in_str) tag_len = ulen(enc, tag); slot = 0; - r = onig_get_callout_data_by_tag(reg, mp, (UChar* )tag, (UChar* )tag + tag_len, - slot, 0, &val); + r = onig_get_callout_data_by_tag_dont_clear_old(reg, mp, (UChar* )tag, + (UChar* )tag + tag_len, slot, 0, &val); if (r < ONIG_NORMAL) goto err; - else if (r > ONIG_NORMAL) { + else if (r == ONIG_VALUE_IS_NOT_SET) { fprintf(stdout, "COUNT[x]: NO DATA\n"); } else { diff --git a/sample/listcap.c b/sample/listcap.c index c0d3014..8072842 100644 --- a/sample/listcap.c +++ b/sample/listcap.c @@ -103,7 +103,7 @@ extern int main(int argc, char* argv[]) use_encs[0] = ONIG_ENCODING_ASCII; onig_initialize(use_encs, sizeof(use_encs)/sizeof(use_encs[0])); - /* enable capture hostory */ + /* enable capture history */ onig_copy_syntax(&syn, ONIG_SYNTAX_DEFAULT); onig_set_syntax_op2(&syn, onig_get_syntax_op2(&syn) | ONIG_SYN_OP2_ATMARK_CAPTURE_HISTORY); |