search.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. enum mc_search_cbret_t
  52. {
  53. MC_SEARCH_CB_OK = 0,
  54. MC_SEARCH_CB_INVALID = -1,
  55. MC_SEARCH_CB_ABORT = -2,
  56. MC_SEARCH_CB_SKIP = -3,
  57. MC_SEARCH_CB_NOTFOUND = -4
  58. };
  59. /*** structures declarations (and typedefs of structures)*****************************************/
  60. typedef struct mc_search_struct
  61. {
  62. /* public input data */
  63. #ifdef HAVE_CHARSET
  64. /* search in all charsets */
  65. gboolean is_all_charsets;
  66. #endif
  67. /* case sensitive search */
  68. gboolean is_case_sensitive;
  69. /* search only once. Is this for replace? */
  70. gboolean is_once_only;
  71. /* search only whole words (from begin to end). Used only with NORMAL search type */
  72. gboolean whole_words;
  73. /* search entire string (from begin to end). Used only with GLOB search type */
  74. gboolean is_entire_line;
  75. /* function, used for getting data. NULL if not used */
  76. mc_search_fn search_fn;
  77. /* function, used for updatin current search status. NULL if not used */
  78. mc_update_fn update_fn;
  79. /* type of search */
  80. mc_search_type_t search_type;
  81. /* public output data */
  82. /* some data for normal */
  83. off_t normal_offset;
  84. off_t start_buffer;
  85. /* some data for regexp */
  86. int num_results;
  87. gboolean is_utf8;
  88. mc_search_matchinfo_t *regex_match_info;
  89. GString *regex_buffer;
  90. #ifdef SEARCH_TYPE_PCRE
  91. #ifdef HAVE_PCRE2
  92. /* pcre2 will provide a pointer to a match_data structure that can be manipulated like an iovector */
  93. size_t *iovector;
  94. #else
  95. int iovector[MC_SEARCH__NUM_REPLACE_ARGS * 2];
  96. #endif
  97. #endif /* SEARCH_TYPE_PCRE */
  98. /* private data */
  99. struct
  100. {
  101. GPtrArray *conditions;
  102. gboolean result;
  103. } prepared;
  104. /* original search string */
  105. struct
  106. {
  107. GString *str;
  108. #ifdef HAVE_CHARSET
  109. gchar *charset;
  110. #endif
  111. } original;
  112. /* error code after search */
  113. mc_search_error_t error;
  114. gchar *error_str;
  115. } mc_search_t;
  116. typedef struct mc_search_type_str_struct
  117. {
  118. const char *str;
  119. mc_search_type_t type;
  120. } mc_search_type_str_t;
  121. /*** global variables defined in .c file *********************************************************/
  122. /* Error messages */
  123. extern const char *STR_E_NOTFOUND;
  124. extern const char *STR_E_UNKNOWN_TYPE;
  125. extern const char *STR_E_RPL_NOT_EQ_TO_FOUND;
  126. extern const char *STR_E_RPL_INVALID_TOKEN;
  127. /*** declarations of public functions ************************************************************/
  128. mc_search_t *mc_search_new (const gchar * original, const gchar * original_charset);
  129. mc_search_t *mc_search_new_len (const gchar * original, gsize original_len,
  130. const gchar * original_charset);
  131. void mc_search_free (mc_search_t * lc_mc_search);
  132. gboolean mc_search_prepare (mc_search_t * mc_search);
  133. gboolean mc_search_run (mc_search_t * mc_search, const void *user_data, gsize start_search,
  134. gsize end_search, gsize * found_len);
  135. gboolean mc_search_is_type_avail (mc_search_type_t search_type);
  136. const mc_search_type_str_t *mc_search_types_list_get (size_t *num);
  137. GString *mc_search_prepare_replace_str (mc_search_t * mc_search, GString * replace_str);
  138. char *mc_search_prepare_replace_str2 (mc_search_t * lc_mc_search, const char *replace_str);
  139. gboolean mc_search_is_fixed_search_str (const mc_search_t * lc_mc_search);
  140. gchar **mc_search_get_types_strings_array (size_t *num);
  141. gboolean mc_search (const gchar * pattern, const gchar * pattern_charset, const gchar * str,
  142. mc_search_type_t type);
  143. int mc_search_getstart_result_by_num (mc_search_t * lc_mc_search, int lc_index);
  144. int mc_search_getend_result_by_num (mc_search_t * lc_mc_search, int lc_index);
  145. /* *INDENT-OFF* */
  146. void mc_search_set_error (mc_search_t * lc_mc_search, mc_search_error_t code, const gchar * format, ...)
  147. G_GNUC_PRINTF (3, 4);
  148. /* *INDENT-ON* */
  149. #endif /* MC__SEARCH_H */