search.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include <pcre.h>
  8. #endif
  9. /*** typedefs(not structures) and defined constants **********************************************/
  10. typedef enum mc_search_cbret_t mc_search_cbret_t;
  11. typedef mc_search_cbret_t (*mc_search_fn) (const void *user_data, gsize char_offset,
  12. int *current_char);
  13. typedef mc_search_cbret_t (*mc_update_fn) (const void *user_data, gsize char_offset);
  14. #define MC_SEARCH__NUM_REPLACE_ARGS 64
  15. #ifdef SEARCH_TYPE_GLIB
  16. #define mc_search_matchinfo_t GMatchInfo
  17. #else
  18. #define mc_search_matchinfo_t pcre_extra
  19. #endif
  20. /*** enums ***************************************************************************************/
  21. typedef enum
  22. {
  23. MC_SEARCH_E_OK = 0,
  24. MC_SEARCH_E_INPUT,
  25. MC_SEARCH_E_REGEX_COMPILE,
  26. MC_SEARCH_E_REGEX,
  27. MC_SEARCH_E_REGEX_REPLACE,
  28. MC_SEARCH_E_NOTFOUND,
  29. MC_SEARCH_E_ABORT
  30. } mc_search_error_t;
  31. typedef enum
  32. {
  33. MC_SEARCH_T_INVALID = -1,
  34. MC_SEARCH_T_NORMAL,
  35. MC_SEARCH_T_REGEX,
  36. MC_SEARCH_T_HEX,
  37. MC_SEARCH_T_GLOB
  38. } mc_search_type_t;
  39. enum mc_search_cbret_t
  40. {
  41. MC_SEARCH_CB_OK = 0,
  42. MC_SEARCH_CB_INVALID = -1,
  43. MC_SEARCH_CB_ABORT = -2,
  44. MC_SEARCH_CB_SKIP = -3,
  45. MC_SEARCH_CB_NOTFOUND = -4
  46. };
  47. /*** structures declarations (and typedefs of structures)*****************************************/
  48. typedef struct mc_search_struct
  49. {
  50. /* public input data */
  51. #ifdef HAVE_CHARSET
  52. /* search in all charsets */
  53. gboolean is_all_charsets;
  54. #endif
  55. /* case sensitive search */
  56. gboolean is_case_sensitive;
  57. /* search only once. Is this for replace? */
  58. gboolean is_once_only;
  59. /* search only whole words (from begin to end). Used only with NORMAL search type */
  60. gboolean whole_words;
  61. /* search entire string (from begin to end). Used only with GLOB search type */
  62. gboolean is_entire_line;
  63. /* function, used for getting data. NULL if not used */
  64. mc_search_fn search_fn;
  65. /* function, used for updatin current search status. NULL if not used */
  66. mc_update_fn update_fn;
  67. /* type of search */
  68. mc_search_type_t search_type;
  69. /* public output data */
  70. /* some data for normal */
  71. off_t normal_offset;
  72. off_t start_buffer;
  73. /* some data for regexp */
  74. int num_results;
  75. gboolean is_utf8;
  76. mc_search_matchinfo_t *regex_match_info;
  77. GString *regex_buffer;
  78. #ifdef SEARCH_TYPE_PCRE
  79. int iovector[MC_SEARCH__NUM_REPLACE_ARGS * 2];
  80. #endif /* SEARCH_TYPE_PCRE */
  81. /* private data */
  82. /* prepared conditions */
  83. GPtrArray *conditions;
  84. /* original search string */
  85. gchar *original;
  86. gsize original_len;
  87. #ifdef HAVE_CHARSET
  88. gchar *original_charset;
  89. #endif
  90. /* error code after search */
  91. mc_search_error_t error;
  92. gchar *error_str;
  93. } mc_search_t;
  94. typedef struct mc_search_type_str_struct
  95. {
  96. const char *str;
  97. mc_search_type_t type;
  98. } mc_search_type_str_t;
  99. /*** global variables defined in .c file *********************************************************/
  100. /* Error messages */
  101. extern const char *STR_E_NOTFOUND;
  102. extern const char *STR_E_UNKNOWN_TYPE;
  103. extern const char *STR_E_RPL_NOT_EQ_TO_FOUND;
  104. extern const char *STR_E_RPL_INVALID_TOKEN;
  105. /*** declarations of public functions ************************************************************/
  106. mc_search_t *mc_search_new (const gchar * original, const gchar * original_charset);
  107. mc_search_t *mc_search_new_len (const gchar * original, gsize original_len,
  108. const gchar * original_charset);
  109. void mc_search_free (mc_search_t * lc_mc_search);
  110. gboolean mc_search_prepare (mc_search_t * mc_search);
  111. gboolean mc_search_run (mc_search_t * mc_search, const void *user_data, gsize start_search,
  112. gsize end_search, gsize * found_len);
  113. gboolean mc_search_is_type_avail (mc_search_type_t);
  114. const mc_search_type_str_t *mc_search_types_list_get (size_t * num);
  115. GString *mc_search_prepare_replace_str (mc_search_t * mc_search, GString * replace_str);
  116. char *mc_search_prepare_replace_str2 (mc_search_t * lc_mc_search, const char *replace_str);
  117. gboolean mc_search_is_fixed_search_str (mc_search_t *);
  118. gchar **mc_search_get_types_strings_array (size_t * num);
  119. gboolean mc_search (const gchar * pattern, const gchar * pattern_charset, const gchar * str,
  120. mc_search_type_t type);
  121. int mc_search_getstart_result_by_num (mc_search_t *, int);
  122. int mc_search_getend_result_by_num (mc_search_t *, int);
  123. /* *INDENT-OFF* */
  124. void mc_search_set_error (mc_search_t * lc_mc_search, mc_search_error_t code, const gchar * format, ...)
  125. G_GNUC_PRINTF (3, 4);
  126. /* *INDENT-ON* */
  127. #endif