diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 20:22:03 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 20:22:03 +0000 |
commit | ffccd5b2b05243e7976db80f90f453dccfae9886 (patch) | |
tree | 39a43152d27f7390d8f7a6fb276fa6887f87c6e8 /lib/search.h | |
parent | Initial commit. (diff) | |
download | mc-ffccd5b2b05243e7976db80f90f453dccfae9886.tar.xz mc-ffccd5b2b05243e7976db80f90f453dccfae9886.zip |
Adding upstream version 3:4.8.30.upstream/3%4.8.30
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/search.h')
-rw-r--r-- | lib/search.h | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/lib/search.h b/lib/search.h new file mode 100644 index 0000000..07372c2 --- /dev/null +++ b/lib/search.h @@ -0,0 +1,196 @@ +#ifndef MC__SEARCH_H +#define MC__SEARCH_H + +#include <config.h> + +#include "lib/global.h" /* <glib.h> */ + +#include <sys/types.h> + +#ifdef SEARCH_TYPE_PCRE +#ifdef HAVE_PCRE2 +#define PCRE2_CODE_UNIT_WIDTH 8 +#include <pcre2.h> +#else +#include <pcre.h> +#endif +#endif /* SEARCH_TYPE_PCRE */ +/*** typedefs(not structures) and defined constants **********************************************/ + +typedef enum mc_search_cbret_t mc_search_cbret_t; + +typedef mc_search_cbret_t (*mc_search_fn) (const void *user_data, gsize char_offset, + int *current_char); +typedef mc_search_cbret_t (*mc_update_fn) (const void *user_data, gsize char_offset); + +#define MC_SEARCH__NUM_REPLACE_ARGS 64 + +#ifdef SEARCH_TYPE_GLIB +#define mc_search_matchinfo_t GMatchInfo +#else +#ifdef HAVE_PCRE2 +/* no pcre_extra in PCRE2. pcre2_jit_compile (equivalent of pcre_study) handles + * all of this internally. but we can use this to hold the pcre2_matches data + * until the search is complete */ +#define mc_search_matchinfo_t pcre2_match_data +#else +#define mc_search_matchinfo_t pcre_extra +#endif +#endif + +/*** enums ***************************************************************************************/ + +typedef enum +{ + MC_SEARCH_E_OK = 0, + MC_SEARCH_E_INPUT, + MC_SEARCH_E_REGEX_COMPILE, + MC_SEARCH_E_REGEX, + MC_SEARCH_E_REGEX_REPLACE, + MC_SEARCH_E_NOTFOUND, + MC_SEARCH_E_ABORT +} mc_search_error_t; + +typedef enum +{ + MC_SEARCH_T_INVALID = -1, + MC_SEARCH_T_NORMAL, + MC_SEARCH_T_REGEX, + MC_SEARCH_T_HEX, + MC_SEARCH_T_GLOB +} mc_search_type_t; + +enum mc_search_cbret_t +{ + MC_SEARCH_CB_OK = 0, + MC_SEARCH_CB_INVALID = -1, + MC_SEARCH_CB_ABORT = -2, + MC_SEARCH_CB_SKIP = -3, + MC_SEARCH_CB_NOTFOUND = -4 +}; + +/*** structures declarations (and typedefs of structures)*****************************************/ + +typedef struct mc_search_struct +{ + /* public input data */ + +#ifdef HAVE_CHARSET + /* search in all charsets */ + gboolean is_all_charsets; +#endif + + /* case sensitive search */ + gboolean is_case_sensitive; + + /* search only once. Is this for replace? */ + gboolean is_once_only; + + /* search only whole words (from begin to end). Used only with NORMAL search type */ + gboolean whole_words; + + /* search entire string (from begin to end). Used only with GLOB search type */ + gboolean is_entire_line; + + /* function, used for getting data. NULL if not used */ + mc_search_fn search_fn; + + /* function, used for updatin current search status. NULL if not used */ + mc_update_fn update_fn; + + /* type of search */ + mc_search_type_t search_type; + + /* public output data */ + + /* some data for normal */ + off_t normal_offset; + + off_t start_buffer; + /* some data for regexp */ + int num_results; + gboolean is_utf8; + mc_search_matchinfo_t *regex_match_info; + GString *regex_buffer; +#ifdef SEARCH_TYPE_PCRE +#ifdef HAVE_PCRE2 + /* pcre2 will provide a pointer to a match_data structure that can be manipulated like an iovector */ + size_t *iovector; +#else + int iovector[MC_SEARCH__NUM_REPLACE_ARGS * 2]; +#endif +#endif /* SEARCH_TYPE_PCRE */ + + /* private data */ + + struct + { + GPtrArray *conditions; + gboolean result; + } prepared; + + /* original search string */ + struct + { + GString *str; +#ifdef HAVE_CHARSET + gchar *charset; +#endif + } original; + + /* error code after search */ + mc_search_error_t error; + gchar *error_str; +} mc_search_t; + +typedef struct mc_search_type_str_struct +{ + const char *str; + mc_search_type_t type; +} mc_search_type_str_t; + +/*** global variables defined in .c file *********************************************************/ + +/* Error messages */ +extern const char *STR_E_NOTFOUND; +extern const char *STR_E_UNKNOWN_TYPE; +extern const char *STR_E_RPL_NOT_EQ_TO_FOUND; +extern const char *STR_E_RPL_INVALID_TOKEN; + +/*** declarations of public functions ************************************************************/ + +mc_search_t *mc_search_new (const gchar * original, const gchar * original_charset); + +mc_search_t *mc_search_new_len (const gchar * original, gsize original_len, + const gchar * original_charset); + +void mc_search_free (mc_search_t * lc_mc_search); + +gboolean mc_search_prepare (mc_search_t * mc_search); + +gboolean mc_search_run (mc_search_t * mc_search, const void *user_data, gsize start_search, + gsize end_search, gsize * found_len); + +gboolean mc_search_is_type_avail (mc_search_type_t search_type); + +const mc_search_type_str_t *mc_search_types_list_get (size_t * num); + +GString *mc_search_prepare_replace_str (mc_search_t * mc_search, GString * replace_str); +char *mc_search_prepare_replace_str2 (mc_search_t * lc_mc_search, const char *replace_str); + +gboolean mc_search_is_fixed_search_str (const mc_search_t * lc_mc_search); + +gchar **mc_search_get_types_strings_array (size_t * num); + +gboolean mc_search (const gchar * pattern, const gchar * pattern_charset, const gchar * str, + mc_search_type_t type); + +int mc_search_getstart_result_by_num (mc_search_t * lc_mc_search, int lc_index); +int mc_search_getend_result_by_num (mc_search_t * lc_mc_search, int lc_index); + +/* *INDENT-OFF* */ +void mc_search_set_error (mc_search_t * lc_mc_search, mc_search_error_t code, const gchar * format, ...) + G_GNUC_PRINTF (3, 4); +/* *INDENT-ON* */ + +#endif /* MC__SEARCH_H */ |