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 enum mc_search_cbret_t
64 {
65 MC_SEARCH_CB_OK = 0,
66 MC_SEARCH_CB_INVALID = -1,
67 MC_SEARCH_CB_ABORT = -2,
68 MC_SEARCH_CB_SKIP = -3,
69 MC_SEARCH_CB_NOTFOUND = -4
70 };
71
72
73
74 typedef struct mc_search_struct
75 {
76
77
78 #ifdef HAVE_CHARSET
79
80 gboolean is_all_charsets;
81 #endif
82
83
84 gboolean is_case_sensitive;
85
86
87 gboolean is_once_only;
88
89
90 gboolean whole_words;
91
92
93 gboolean is_entire_line;
94
95
96 mc_search_fn search_fn;
97
98
99 mc_update_fn update_fn;
100
101
102 mc_search_type_t search_type;
103
104
105
106
107 off_t normal_offset;
108
109 off_t start_buffer;
110
111 int num_results;
112 gboolean is_utf8;
113 mc_search_matchinfo_t *regex_match_info;
114 GString *regex_buffer;
115 #ifdef SEARCH_TYPE_PCRE
116 #ifdef HAVE_PCRE2
117
118 size_t *iovector;
119 #else
120 int iovector[MC_SEARCH__NUM_REPLACE_ARGS * 2];
121 #endif
122 #endif
123
124
125
126 struct
127 {
128 GPtrArray *conditions;
129 gboolean result;
130 } prepared;
131
132
133 struct
134 {
135 GString *str;
136 #ifdef HAVE_CHARSET
137 gchar *charset;
138 #endif
139 } original;
140
141
142 mc_search_error_t error;
143 gchar *error_str;
144 } mc_search_t;
145
146 typedef struct mc_search_type_str_struct
147 {
148 const char *str;
149 mc_search_type_t type;
150 } mc_search_type_str_t;
151
152
153
154
155 extern const char *STR_E_NOTFOUND;
156 extern const char *STR_E_UNKNOWN_TYPE;
157 extern const char *STR_E_RPL_NOT_EQ_TO_FOUND;
158 extern const char *STR_E_RPL_INVALID_TOKEN;
159
160
161
162 mc_search_t *mc_search_new (const gchar * original, const gchar * original_charset);
163
164 mc_search_t *mc_search_new_len (const gchar * original, gsize original_len,
165 const gchar * original_charset);
166
167 void mc_search_free (mc_search_t * lc_mc_search);
168
169 gboolean mc_search_prepare (mc_search_t * mc_search);
170
171 gboolean mc_search_run (mc_search_t * mc_search, const void *user_data, gsize start_search,
172 gsize end_search, gsize * found_len);
173
174 gboolean mc_search_is_type_avail (mc_search_type_t search_type);
175
176 const mc_search_type_str_t *mc_search_types_list_get (size_t *num);
177
178 GString *mc_search_prepare_replace_str (mc_search_t * mc_search, GString * replace_str);
179 char *mc_search_prepare_replace_str2 (mc_search_t * lc_mc_search, const char *replace_str);
180
181 gboolean mc_search_is_fixed_search_str (const mc_search_t * lc_mc_search);
182
183 gchar **mc_search_get_types_strings_array (size_t *num);
184
185 gboolean mc_search (const gchar * pattern, const gchar * pattern_charset, const gchar * str,
186 mc_search_type_t type);
187
188 int mc_search_getstart_result_by_num (mc_search_t * lc_mc_search, int lc_index);
189 int mc_search_getend_result_by_num (mc_search_t * lc_mc_search, int lc_index);
190
191
192 void mc_search_set_error (mc_search_t * lc_mc_search, mc_search_error_t code, const gchar * format, ...)
193 G_GNUC_PRINTF (3, 4);
194
195
196 #endif