1 #ifndef MC__SEARCH_H
2 #define MC__SEARCH_H
3
4 #include <config.h>
5
6 #include "lib/global.h"
7
8 #include <sys/types.h>
9
10 #ifdef SEARCH_TYPE_PCRE
11 #ifdef HAVE_PCRE2
12 #define PCRE2_CODE_UNIT_WIDTH 8
13 #include <pcre2.h>
14 #else
15 #include <pcre.h>
16 #endif
17 #endif
18
19
20 typedef enum mc_search_cbret_t mc_search_cbret_t;
21
22 typedef mc_search_cbret_t (*mc_search_fn) (const void *user_data, gsize char_offset,
23 int *current_char);
24 typedef mc_search_cbret_t (*mc_update_fn) (const void *user_data, gsize char_offset);
25
26 #define MC_SEARCH__NUM_REPLACE_ARGS 64
27
28 #ifdef SEARCH_TYPE_GLIB
29 #define mc_search_matchinfo_t GMatchInfo
30 #else
31 #ifdef HAVE_PCRE2
32
33
34
35 #define mc_search_matchinfo_t pcre2_match_data
36 #else
37 #define mc_search_matchinfo_t pcre_extra
38 #endif
39 #endif
40
41
42
43 typedef enum
44 {
45 MC_SEARCH_E_OK = 0,
46 MC_SEARCH_E_INPUT,
47 MC_SEARCH_E_REGEX_COMPILE,
48 MC_SEARCH_E_REGEX,
49 MC_SEARCH_E_REGEX_REPLACE,
50 MC_SEARCH_E_NOTFOUND,
51 MC_SEARCH_E_ABORT
52 } mc_search_error_t;
53
54 typedef enum
55 {
56 MC_SEARCH_T_INVALID = -1,
57 MC_SEARCH_T_NORMAL,
58 MC_SEARCH_T_REGEX,
59 MC_SEARCH_T_HEX,
60 MC_SEARCH_T_GLOB
61 } mc_search_type_t;
62
63
64
65
66
67 typedef enum
68 {
69 MC_SEARCH_LINE_NONE = 0,
70 MC_SEARCH_LINE_BEGIN = 1 << 0,
71 MC_SEARCH_LINE_END = 1 << 1,
72 MC_SEARCH_LINE_ENTIRE = MC_SEARCH_LINE_BEGIN | MC_SEARCH_LINE_END
73 } mc_search_line_t;
74
75 enum mc_search_cbret_t
76 {
77 MC_SEARCH_CB_OK = 0,
78 MC_SEARCH_CB_INVALID = -1,
79 MC_SEARCH_CB_ABORT = -2,
80 MC_SEARCH_CB_SKIP = -3,
81 MC_SEARCH_CB_NOTFOUND = -4
82 };
83
84
85
86 typedef struct mc_search_struct
87 {
88
89
90 #ifdef HAVE_CHARSET
91
92 gboolean is_all_charsets;
93 #endif
94
95
96 gboolean is_case_sensitive;
97
98
99 gboolean is_once_only;
100
101
102 gboolean whole_words;
103
104
105 gboolean is_entire_line;
106
107
108 mc_search_fn search_fn;
109
110
111 mc_update_fn update_fn;
112
113
114 mc_search_type_t search_type;
115
116
117
118
119 off_t normal_offset;
120
121 off_t start_buffer;
122
123 int num_results;
124 gboolean is_utf8;
125 mc_search_matchinfo_t *regex_match_info;
126 GString *regex_buffer;
127 #ifdef SEARCH_TYPE_PCRE
128 #ifdef HAVE_PCRE2
129
130 size_t *iovector;
131 #else
132 int iovector[MC_SEARCH__NUM_REPLACE_ARGS * 2];
133 #endif
134 #endif
135
136
137
138 struct
139 {
140 GPtrArray *conditions;
141 gboolean result;
142 } prepared;
143
144
145 struct
146 {
147 GString *str;
148 #ifdef HAVE_CHARSET
149 gchar *charset;
150 #endif
151 } original;
152
153
154 mc_search_error_t error;
155 gchar *error_str;
156 } mc_search_t;
157
158 typedef struct mc_search_type_str_struct
159 {
160 const char *str;
161 mc_search_type_t type;
162 } mc_search_type_str_t;
163
164
165
166
167 extern const char *STR_E_NOTFOUND;
168 extern const char *STR_E_UNKNOWN_TYPE;
169 extern const char *STR_E_RPL_NOT_EQ_TO_FOUND;
170 extern const char *STR_E_RPL_INVALID_TOKEN;
171
172
173
174 mc_search_t *mc_search_new (const gchar * original, const gchar * original_charset);
175
176 mc_search_t *mc_search_new_len (const gchar * original, gsize original_len,
177 const gchar * original_charset);
178
179 void mc_search_free (mc_search_t * lc_mc_search);
180
181 gboolean mc_search_prepare (mc_search_t * mc_search);
182
183 gboolean mc_search_run (mc_search_t * mc_search, const void *user_data, gsize start_search,
184 gsize end_search, gsize * found_len);
185
186 gboolean mc_search_is_type_avail (mc_search_type_t search_type);
187
188 const mc_search_type_str_t *mc_search_types_list_get (size_t *num);
189
190 GString *mc_search_prepare_replace_str (mc_search_t * mc_search, GString * replace_str);
191 char *mc_search_prepare_replace_str2 (mc_search_t * lc_mc_search, const char *replace_str);
192
193 gboolean mc_search_is_fixed_search_str (const mc_search_t * lc_mc_search);
194
195 gchar **mc_search_get_types_strings_array (size_t *num);
196
197 gboolean mc_search (const gchar * pattern, const gchar * pattern_charset, const gchar * str,
198 mc_search_type_t type);
199
200 mc_search_line_t mc_search_get_line_type (const mc_search_t *search);
201
202 int mc_search_getstart_result_by_num (mc_search_t * lc_mc_search, int lc_index);
203 int mc_search_getend_result_by_num (mc_search_t * lc_mc_search, int lc_index);
204
205
206 void mc_search_set_error (mc_search_t * lc_mc_search, mc_search_error_t code, const gchar * format, ...)
207 G_GNUC_PRINTF (3, 4);
208
209
210 #endif