search.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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
  93. * iovector */
  94. size_t *iovector;
  95. #else
  96. int iovector[MC_SEARCH__NUM_REPLACE_ARGS * 2];
  97. #endif
  98. #endif /* SEARCH_TYPE_PCRE */
  99. /* private data */
  100. struct
  101. {
  102. GPtrArray *conditions;
  103. gboolean result;
  104. } prepared;
  105. /* original search string */
  106. struct
  107. {
  108. GString *str;
  109. #ifdef HAVE_CHARSET
  110. gchar *charset;
  111. #endif
  112. } original;
  113. /* error code after search */
  114. mc_search_error_t error;
  115. gchar *error_str;
  116. } mc_search_t;
  117. typedef struct mc_search_type_str_struct
  118. {
  119. const char *str;
  120. mc_search_type_t type;
  121. } mc_search_type_str_t;
  122. /*** global variables defined in .c file *********************************************************/
  123. /* Error messages */
  124. extern const char *STR_E_NOTFOUND;
  125. extern const char *STR_E_UNKNOWN_TYPE;
  126. extern const char *STR_E_RPL_NOT_EQ_TO_FOUND;
  127. extern const char *STR_E_RPL_INVALID_TOKEN;
  128. /*** declarations of public functions ************************************************************/
  129. mc_search_t *mc_search_new (const gchar *original, const gchar *original_charset);
  130. mc_search_t *mc_search_new_len (const gchar *original, gsize original_len,
  131. const gchar *original_charset);
  132. void mc_search_free (mc_search_t *lc_mc_search);
  133. gboolean mc_search_prepare (mc_search_t *mc_search);
  134. gboolean mc_search_run (mc_search_t *mc_search, const void *user_data, gsize start_search,
  135. gsize end_search, gsize *found_len);
  136. gboolean mc_search_is_type_avail (mc_search_type_t search_type);
  137. const mc_search_type_str_t *mc_search_types_list_get (size_t *num);
  138. GString *mc_search_prepare_replace_str (mc_search_t *mc_search, GString *replace_str);
  139. char *mc_search_prepare_replace_str2 (mc_search_t *lc_mc_search, const char *replace_str);
  140. gboolean mc_search_is_fixed_search_str (const mc_search_t *lc_mc_search);
  141. gchar **mc_search_get_types_strings_array (size_t *num);
  142. gboolean mc_search (const gchar *pattern, const gchar *pattern_charset, const gchar *str,
  143. mc_search_type_t type);
  144. int mc_search_getstart_result_by_num (mc_search_t *lc_mc_search, int lc_index);
  145. int mc_search_getend_result_by_num (mc_search_t *lc_mc_search, int lc_index);
  146. /* *INDENT-OFF* */
  147. void mc_search_set_error (mc_search_t *lc_mc_search, mc_search_error_t code, const gchar *format,
  148. ...) G_GNUC_PRINTF (3, 4);
  149. /* *INDENT-ON* */
  150. #endif /* MC__SEARCH_H */