root/lib/search.h

/* [previous][next][first][last][top][bottom][index][help]  */

INCLUDED FROM


   1 #ifndef MC__SEARCH_H
   2 #define MC__SEARCH_H
   3 
   4 #include <config.h>
   5 
   6 #include "lib/global.h"  // <glib.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 /*** typedefs(not structures) and defined constants **********************************************/
  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, off_t char_offset,
  23                                            int *current_char);
  24 typedef mc_search_cbret_t (*mc_update_fn) (const void *user_data, off_t 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 /* 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
  40 
  41 /*** enums ***************************************************************************************/
  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  * enum to store search conditions check results.
  65  * (whether the search condition has BOL (^) or EOL ($) regexp characters).
  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 /*** structures declarations (and typedefs of structures)*****************************************/
  85 
  86 typedef struct mc_search_struct
  87 {
  88     // public input data
  89 
  90 #ifdef HAVE_CHARSET
  91     // search in all charsets
  92     gboolean is_all_charsets;
  93 #endif
  94 
  95     // case sensitive search
  96     gboolean is_case_sensitive;
  97 
  98     // search only once.  Is this for replace?
  99     gboolean is_once_only;
 100 
 101     // search only whole words (from begin to end). Used only with NORMAL search type
 102     gboolean whole_words;
 103 
 104     // search entire string (from begin to end). Used only with GLOB search type
 105     gboolean is_entire_line;
 106 
 107     // function, used for getting data. NULL if not used
 108     mc_search_fn search_fn;
 109 
 110     // function, used for updatin current search status. NULL if not used
 111     mc_update_fn update_fn;
 112 
 113     // type of search
 114     mc_search_type_t search_type;
 115 
 116     // public output data
 117 
 118     // some data for normal
 119     off_t normal_offset;
 120 
 121     off_t start_buffer;
 122     // some data for regexp
 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     // pcre2 will provide a pointer to a match_data structure that can be manipulated like an
 130     // iovector
 131     size_t *iovector;
 132 #    else
 133     int iovector[MC_SEARCH__NUM_REPLACE_ARGS * 2];
 134 #    endif
 135 #endif
 136 
 137     // private data
 138 
 139     struct
 140     {
 141         GPtrArray *conditions;
 142         gboolean result;
 143     } prepared;
 144 
 145     // original search string
 146     struct
 147     {
 148         GString *str;
 149 #ifdef HAVE_CHARSET
 150         gchar *charset;
 151 #endif
 152     } original;
 153 
 154     // error code after search
 155     mc_search_error_t error;
 156     gchar *error_str;
 157 } mc_search_t;
 158 
 159 typedef struct mc_search_type_str_struct
 160 {
 161     const char *str;
 162     mc_search_type_t type;
 163 } mc_search_type_str_t;
 164 
 165 /*** global variables defined in .c file *********************************************************/
 166 
 167 /* Error messages */
 168 extern const char *STR_E_NOTFOUND;
 169 extern const char *STR_E_UNKNOWN_TYPE;
 170 extern const char *STR_E_RPL_NOT_EQ_TO_FOUND;
 171 extern const char *STR_E_RPL_INVALID_TOKEN;
 172 
 173 /*** declarations of public functions ************************************************************/
 174 
 175 mc_search_t *mc_search_new (const gchar *original, const gchar *original_charset);
 176 
 177 mc_search_t *mc_search_new_len (const gchar *original, gsize original_len,
 178                                 const gchar *original_charset);
 179 
 180 void mc_search_free (mc_search_t *lc_mc_search);
 181 
 182 gboolean mc_search_prepare (mc_search_t *mc_search);
 183 
 184 gboolean mc_search_run (mc_search_t *mc_search, const void *user_data, off_t start_search,
 185                         off_t end_search, gsize *found_len);
 186 
 187 gboolean mc_search_is_type_avail (mc_search_type_t search_type);
 188 
 189 const mc_search_type_str_t *mc_search_types_list_get (size_t *num);
 190 
 191 GString *mc_search_prepare_replace_str (mc_search_t *mc_search, GString *replace_str);
 192 char *mc_search_prepare_replace_str2 (mc_search_t *lc_mc_search, const char *replace_str);
 193 
 194 gboolean mc_search_is_fixed_search_str (const mc_search_t *lc_mc_search);
 195 
 196 gchar **mc_search_get_types_strings_array (size_t *num);
 197 
 198 gboolean mc_search (const gchar *pattern, const gchar *pattern_charset, const gchar *str,
 199                     mc_search_type_t type);
 200 
 201 mc_search_line_t mc_search_get_line_type (const mc_search_t *search);
 202 
 203 int mc_search_getstart_result_by_num (mc_search_t *lc_mc_search, int lc_index);
 204 int mc_search_getend_result_by_num (mc_search_t *lc_mc_search, int lc_index);
 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 #endif

/* [previous][next][first][last][top][bottom][index][help]  */