search.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #ifndef MC__SEARCH_H
  2. #define MC__SEARCH_H
  3. #include <config.h>
  4. #include "lib/global.h" /* <glib.h> */
  5. #include <sys/types.h>
  6. #ifdef SEARCH_TYPE_PCRE
  7. #ifdef HAVE_PCRE2
  8. #define PCRE2_CODE_UNIT_WIDTH 8
  9. #include <pcre2.h>
  10. #else
  11. #include <pcre.h>
  12. #endif
  13. #endif /* SEARCH_TYPE_PCRE */
  14. /*** typedefs(not structures) and defined constants **********************************************/
  15. typedef enum mc_search_cbret_t mc_search_cbret_t;
  16. typedef mc_search_cbret_t (*mc_search_fn) (const void *user_data, gsize char_offset,
  17. int *current_char);
  18. typedef mc_search_cbret_t (*mc_update_fn) (const void *user_data, gsize char_offset);
  19. #define MC_SEARCH__NUM_REPLACE_ARGS 64
  20. #ifdef SEARCH_TYPE_GLIB
  21. #define mc_search_matchinfo_t GMatchInfo
  22. #else
  23. #ifdef HAVE_PCRE2
  24. /* no pcre_extra in PCRE2. pcre2_jit_compile (equivalent of pcre_study) handles
  25. * all of this internally. but we can use this to hold the pcre2_matches data
  26. * until the search is complete */
  27. #define mc_search_matchinfo_t pcre2_match_data
  28. #else
  29. #define mc_search_matchinfo_t pcre_extra
  30. #endif
  31. #endif
  32. /*** enums ***************************************************************************************/
  33. typedef enum
  34. {
  35. MC_SEARCH_E_OK = 0,
  36. MC_SEARCH_E_INPUT,
  37. MC_SEARCH_E_REGEX_COMPILE,
  38. MC_SEARCH_E_REGEX,
  39. MC_SEARCH_E_REGEX_REPLACE,
  40. MC_SEARCH_E_NOTFOUND,
  41. MC_SEARCH_E_ABORT
  42. } mc_search_error_t;
  43. typedef enum
  44. {
  45. MC_SEARCH_T_INVALID = -1,
  46. MC_SEARCH_T_NORMAL,
  47. MC_SEARCH_T_REGEX,
  48. MC_SEARCH_T_HEX,
  49. MC_SEARCH_T_GLOB
  50. } mc_search_type_t;
  51. /**
  52. * enum to store search conditions check results.
  53. * (whether the search condition has BOL (^) or EOL ($) regexp characters).
  54. */
  55. typedef enum
  56. {
  57. MC_SEARCH_LINE_NONE = 0,
  58. MC_SEARCH_LINE_BEGIN = 1 << 0,
  59. MC_SEARCH_LINE_END = 1 << 1,
  60. MC_SEARCH_LINE_ENTIRE = MC_SEARCH_LINE_BEGIN | MC_SEARCH_LINE_END
  61. } mc_search_line_t;
  62. enum mc_search_cbret_t
  63. {
  64. MC_SEARCH_CB_OK = 0,
  65. MC_SEARCH_CB_INVALID = -1,
  66. MC_SEARCH_CB_ABORT = -2,
  67. MC_SEARCH_CB_SKIP = -3,
  68. MC_SEARCH_CB_NOTFOUND = -4
  69. };
  70. /*** structures declarations (and typedefs of structures)*****************************************/
  71. typedef struct mc_search_struct
  72. {
  73. /* public input data */
  74. #ifdef HAVE_CHARSET
  75. /* search in all charsets */
  76. gboolean is_all_charsets;
  77. #endif
  78. /* case sensitive search */
  79. gboolean is_case_sensitive;
  80. /* search only once. Is this for replace? */
  81. gboolean is_once_only;
  82. /* search only whole words (from begin to end). Used only with NORMAL search type */
  83. gboolean whole_words;
  84. /* search entire string (from begin to end). Used only with GLOB search type */
  85. gboolean is_entire_line;
  86. /* function, used for getting data. NULL if not used */
  87. mc_search_fn search_fn;
  88. /* function, used for updatin current search status. NULL if not used */
  89. mc_update_fn update_fn;
  90. /* type of search */
  91. mc_search_type_t search_type;
  92. /* public output data */
  93. /* some data for normal */
  94. off_t normal_offset;
  95. off_t start_buffer;
  96. /* some data for regexp */
  97. int num_results;
  98. gboolean is_utf8;
  99. mc_search_matchinfo_t *regex_match_info;
  100. GString *regex_buffer;
  101. #ifdef SEARCH_TYPE_PCRE
  102. #ifdef HAVE_PCRE2
  103. /* pcre2 will provide a pointer to a match_data structure that can be manipulated like an iovector */
  104. size_t *iovector;
  105. #else
  106. int iovector[MC_SEARCH__NUM_REPLACE_ARGS * 2];
  107. #endif
  108. #endif /* SEARCH_TYPE_PCRE */
  109. /* private data */
  110. struct
  111. {
  112. GPtrArray *conditions;
  113. gboolean result;
  114. } prepared;
  115. /* original search string */
  116. struct
  117. {
  118. GString *str;
  119. #ifdef HAVE_CHARSET
  120. gchar *charset;
  121. #endif
  122. } original;
  123. /* error code after search */
  124. mc_search_error_t error;
  125. gchar *error_str;
  126. } mc_search_t;
  127. typedef struct mc_search_type_str_struct
  128. {
  129. const char *str;
  130. mc_search_type_t type;
  131. } mc_search_type_str_t;
  132. /*** global variables defined in .c file *********************************************************/
  133. /* Error messages */
  134. extern const char *STR_E_NOTFOUND;
  135. extern const char *STR_E_UNKNOWN_TYPE;
  136. extern const char *STR_E_RPL_NOT_EQ_TO_FOUND;
  137. extern const char *STR_E_RPL_INVALID_TOKEN;
  138. /*** declarations of public functions ************************************************************/
  139. mc_search_t *mc_search_new (const gchar * original, const gchar * original_charset);
  140. mc_search_t *mc_search_new_len (const gchar * original, gsize original_len,
  141. const gchar * original_charset);
  142. void mc_search_free (mc_search_t * lc_mc_search);
  143. gboolean mc_search_prepare (mc_search_t * mc_search);
  144. gboolean mc_search_run (mc_search_t * mc_search, const void *user_data, gsize start_search,
  145. gsize end_search, gsize * found_len);
  146. gboolean mc_search_is_type_avail (mc_search_type_t search_type);
  147. const mc_search_type_str_t *mc_search_types_list_get (size_t *num);
  148. GString *mc_search_prepare_replace_str (mc_search_t * mc_search, GString * replace_str);
  149. char *mc_search_prepare_replace_str2 (mc_search_t * lc_mc_search, const char *replace_str);
  150. gboolean mc_search_is_fixed_search_str (const mc_search_t * lc_mc_search);
  151. gchar **mc_search_get_types_strings_array (size_t *num);
  152. gboolean mc_search (const gchar * pattern, const gchar * pattern_charset, const gchar * str,
  153. mc_search_type_t type);
  154. mc_search_line_t mc_search_get_line_type (const mc_search_t *search);
  155. int mc_search_getstart_result_by_num (mc_search_t * lc_mc_search, int lc_index);
  156. int mc_search_getend_result_by_num (mc_search_t * lc_mc_search, int lc_index);
  157. /* *INDENT-OFF* */
  158. void mc_search_set_error (mc_search_t * lc_mc_search, mc_search_error_t code, const gchar * format, ...)
  159. G_GNUC_PRINTF (3, 4);
  160. /* *INDENT-ON* */
  161. #endif /* MC__SEARCH_H */