mceditor: refactoring.
[midnight-commander.git] / lib / search.h
blob07372c2592ed375176a511402d6e6118a3e2bcf0
1 #ifndef MC__SEARCH_H
2 #define MC__SEARCH_H
4 #include <config.h>
6 #include "lib/global.h" /* <glib.h> */
8 #include <sys/types.h>
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 /* SEARCH_TYPE_PCRE */
18 /*** typedefs(not structures) and defined constants **********************************************/
20 typedef enum mc_search_cbret_t mc_search_cbret_t;
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);
26 #define MC_SEARCH__NUM_REPLACE_ARGS 64
28 #ifdef SEARCH_TYPE_GLIB
29 #define mc_search_matchinfo_t GMatchInfo
30 #else
31 #ifdef HAVE_PCRE2
32 /* no pcre_extra in PCRE2. pcre2_jit_compile (equivalent of pcre_study) handles
33 * all of this internally. but we can use this to hold the pcre2_matches data
34 * until the search is complete */
35 #define mc_search_matchinfo_t pcre2_match_data
36 #else
37 #define mc_search_matchinfo_t pcre_extra
38 #endif
39 #endif
41 /*** enums ***************************************************************************************/
43 typedef enum
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;
54 typedef enum
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;
63 enum mc_search_cbret_t
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
72 /*** structures declarations (and typedefs of structures)*****************************************/
74 typedef struct mc_search_struct
76 /* public input data */
78 #ifdef HAVE_CHARSET
79 /* search in all charsets */
80 gboolean is_all_charsets;
81 #endif
83 /* case sensitive search */
84 gboolean is_case_sensitive;
86 /* search only once. Is this for replace? */
87 gboolean is_once_only;
89 /* search only whole words (from begin to end). Used only with NORMAL search type */
90 gboolean whole_words;
92 /* search entire string (from begin to end). Used only with GLOB search type */
93 gboolean is_entire_line;
95 /* function, used for getting data. NULL if not used */
96 mc_search_fn search_fn;
98 /* function, used for updatin current search status. NULL if not used */
99 mc_update_fn update_fn;
101 /* type of search */
102 mc_search_type_t search_type;
104 /* public output data */
106 /* some data for normal */
107 off_t normal_offset;
109 off_t start_buffer;
110 /* some data for regexp */
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 /* pcre2 will provide a pointer to a match_data structure that can be manipulated like an iovector */
118 size_t *iovector;
119 #else
120 int iovector[MC_SEARCH__NUM_REPLACE_ARGS * 2];
121 #endif
122 #endif /* SEARCH_TYPE_PCRE */
124 /* private data */
126 struct
128 GPtrArray *conditions;
129 gboolean result;
130 } prepared;
132 /* original search string */
133 struct
135 GString *str;
136 #ifdef HAVE_CHARSET
137 gchar *charset;
138 #endif
139 } original;
141 /* error code after search */
142 mc_search_error_t error;
143 gchar *error_str;
144 } mc_search_t;
146 typedef struct mc_search_type_str_struct
148 const char *str;
149 mc_search_type_t type;
150 } mc_search_type_str_t;
152 /*** global variables defined in .c file *********************************************************/
154 /* Error messages */
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;
160 /*** declarations of public functions ************************************************************/
162 mc_search_t *mc_search_new (const gchar * original, const gchar * original_charset);
164 mc_search_t *mc_search_new_len (const gchar * original, gsize original_len,
165 const gchar * original_charset);
167 void mc_search_free (mc_search_t * lc_mc_search);
169 gboolean mc_search_prepare (mc_search_t * mc_search);
171 gboolean mc_search_run (mc_search_t * mc_search, const void *user_data, gsize start_search,
172 gsize end_search, gsize * found_len);
174 gboolean mc_search_is_type_avail (mc_search_type_t search_type);
176 const mc_search_type_str_t *mc_search_types_list_get (size_t * num);
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);
181 gboolean mc_search_is_fixed_search_str (const mc_search_t * lc_mc_search);
183 gchar **mc_search_get_types_strings_array (size_t * num);
185 gboolean mc_search (const gchar * pattern, const gchar * pattern_charset, const gchar * str,
186 mc_search_type_t type);
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);
191 /* *INDENT-OFF* */
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 /* *INDENT-ON* */
196 #endif /* MC__SEARCH_H */