summaryrefslogtreecommitdiff
path: root/sample/callback_each_match.c
blob: 15c39dd295e777255352fd33ffc83db99babb03b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * 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(&reg, 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 *end;
  regex_t* reg;
  OnigErrorInfo einfo;
  OnigRegion *region;
  OnigMatchParam* mp;
  void* user_data;

  r = onig_new(&reg, 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);
  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;
}