]> granicus.if.org Git - onig/blob - sample/listcap.c
remove include windows.h from oniguruma.h
[onig] / sample / listcap.c
1 /*
2  * listcap.c
3  *
4  * capture history (?@...) sample.
5  */
6 #include <stdio.h>
7 #include <string.h>
8 #include "oniguruma.h"
9
10 static int
11 node_callback(int group, int beg, int end, int level, int at, void* arg)
12 {
13   int i;
14
15   if (at != ONIG_TRAVERSE_CALLBACK_AT_FIRST)
16     return -1; /* error */
17
18   /* indent */
19   for (i = 0; i < level * 2; i++)
20     fputc(' ', stderr);
21
22   fprintf(stderr, "%d: (%d-%d)\n", group, beg, end);
23   return 0;
24 }
25
26 extern int ex(unsigned char* str, unsigned char* pattern,
27               OnigSyntaxType* syntax)
28 {
29   int r;
30   unsigned char *start, *range, *end;
31   regex_t* reg;
32   OnigErrorInfo einfo;
33   OnigRegion *region;
34
35   r = onig_new(&reg, pattern, pattern + strlen((char* )pattern),
36                ONIG_OPTION_DEFAULT, ONIG_ENCODING_ASCII, syntax, &einfo);
37   if (r != ONIG_NORMAL) {
38     char s[ONIG_MAX_ERROR_MESSAGE_LEN];
39     onig_error_code_to_str(s, r, &einfo);
40     fprintf(stderr, "ERROR: %s\n", s);
41     return -1;
42   }
43
44   fprintf(stderr, "number of captures: %d\n", onig_number_of_captures(reg));
45   fprintf(stderr, "number of capture histories: %d\n",
46           onig_number_of_capture_histories(reg));
47
48   region = onig_region_new();
49
50   end   = str + strlen((char* )str);
51   start = str;
52   range = end;
53   r = onig_search(reg, str, end, start, range, region, ONIG_OPTION_NONE);
54   if (r >= 0) {
55     int i;
56
57     fprintf(stderr, "match at %d\n", r);
58     for (i = 0; i < region->num_regs; i++) {
59       fprintf(stderr, "%d: (%d-%d)\n", i, region->beg[i], region->end[i]);
60     }
61     fprintf(stderr, "\n");
62
63     r = onig_capture_tree_traverse(region, ONIG_TRAVERSE_CALLBACK_AT_FIRST,
64                                    node_callback, (void* )0);
65   }
66   else if (r == ONIG_MISMATCH) {
67     fprintf(stderr, "search fail\n");
68   }
69   else { /* error */
70     char s[ONIG_MAX_ERROR_MESSAGE_LEN];
71     onig_error_code_to_str(s, r);
72     return -1;
73   }
74
75   onig_region_free(region, 1 /* 1:free self, 0:free contents only */);
76   onig_free(reg);
77   return 0;
78 }
79
80
81 extern int main(int argc, char* argv[])
82 {
83   int r;
84   OnigSyntaxType syn;
85
86   static UChar* str1 = (UChar* )"((())())";
87   static UChar* pattern1
88     = (UChar* )"\\g<p>(?@<p>\\(\\g<s>\\)){0}(?@<s>(?:\\g<p>)*|){0}";
89
90   static UChar* str2     = (UChar* )"x00x00x00";
91   static UChar* pattern2 = (UChar* )"(?@x(?@\\d+))+";
92
93   static UChar* str3     = (UChar* )"0123";
94   static UChar* pattern3 = (UChar* )"(?@.)(?@.)(?@.)(?@.)";
95
96   OnigEncoding use_encs[] = { ONIG_ENCODING_ASCII };
97   onig_initialize(use_encs, sizeof(use_encs)/sizeof(use_encs[0]));
98
99  /* enable capture hostory */
100   onig_copy_syntax(&syn, ONIG_SYNTAX_DEFAULT);
101   onig_set_syntax_op2(&syn, 
102        onig_get_syntax_op2(&syn) | ONIG_SYN_OP2_ATMARK_CAPTURE_HISTORY);
103
104   r = ex(str1, pattern1, &syn);
105   r = ex(str2, pattern2, &syn);
106   r = ex(str3, pattern3, &syn);
107
108   onig_end();
109   return r;
110 }