dclib-regex.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /***************************************************************************
  2. * *
  3. * _____ ____ *
  4. * | __ \ / __ \ _ _ _____ *
  5. * | | \ \ / / \_\ | | | | _ \ *
  6. * | | \ \| | | | | | |_| | *
  7. * | | | || | | | | | ___/ *
  8. * | | / /| | __ | | | | _ \ *
  9. * | |__/ / \ \__/ / | |___| | |_| | *
  10. * |_____/ \____/ |_____|_|_____/ *
  11. * *
  12. * Wiimms source code library *
  13. * *
  14. ***************************************************************************
  15. * *
  16. * Copyright (c) 2012-2022 by Dirk Clemens <wiimm@wiimm.de> *
  17. * *
  18. ***************************************************************************
  19. * *
  20. * This library is free software; you can redistribute it and/or modify *
  21. * it under the terms of the GNU General Public License as published by *
  22. * the Free Software Foundation; either version 2 of the License, or *
  23. * (at your option) any later version. *
  24. * *
  25. * This library is distributed in the hope that it will be useful, *
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  28. * GNU General Public License for more details. *
  29. * *
  30. * See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt *
  31. * *
  32. ***************************************************************************/
  33. #ifndef DCLIB_REGEX_H
  34. #define DCLIB_REGEX_H 1
  35. #if DCLIB_USE_PCRE
  36. #include <pcre.h>
  37. #define DC_REGEX_TYPE pcre
  38. #else
  39. #ifndef DCLIB_USE_REGEX
  40. #define DCLIB_USE_REGEX 1
  41. #endif
  42. #if DCLIB_USE_REGEX
  43. #include <regex.h>
  44. #define DC_REGEX_TYPE regex_t
  45. #else
  46. #error Nn Regex Support!
  47. #endif
  48. #endif
  49. #include "dclib-types.h"
  50. #include "dclib-debug.h"
  51. #include "dclib-basics.h"
  52. //
  53. ///////////////////////////////////////////////////////////////////////////////
  54. /////////////// struct Regex_t ///////////////
  55. ///////////////////////////////////////////////////////////////////////////////
  56. // [[RegexReplace_t]]
  57. typedef struct RegexReplace_t
  58. {
  59. mem_t str; // first: string to replace (maybe 0 bytes)
  60. int ref; // second: back reference to copy (if >=0)
  61. }
  62. RegexReplace_t;
  63. ///////////////////////////////////////////////////////////////////////////////
  64. // [[RegexElem_t]]
  65. typedef struct RegexElem_t
  66. {
  67. bool valid; // true: successfull compiled
  68. bool global; // true: replace all ('g')
  69. bool icase; // true: ignore case ('i')
  70. #if DCLIB_USE_PCRE
  71. DC_REGEX_TYPE *regex; // not NULL: compiled regular expression
  72. #else
  73. DC_REGEX_TYPE regex; // compiled regular expression
  74. #endif
  75. int opt; // compile options
  76. ccp pattern; // pattern string, alloced
  77. mem_t replace; // replace string, alloced
  78. RegexReplace_t *repl; // replace data, use 'replace' as reference
  79. uint repl_used; // number of used elements in 'repl'
  80. uint repl_size; // number of available elements in 'repl'
  81. }
  82. RegexElem_t;
  83. ///////////////////////////////////////////////////////////////////////////////
  84. // [[RegexMain_t]]
  85. typedef struct Regex_t
  86. {
  87. bool valid; // true: succesfull initialized
  88. RegexElem_t *re_list; // list; either 're_pool' or alloced
  89. RegexElem_t re_pool[3]; // pool for fast access
  90. uint re_used; // number of used elements in 're_list'
  91. uint re_size; // number of available elements in 're_list'
  92. }
  93. Regex_t;
  94. ///////////////////////////////////////////////////////////////////////////////
  95. ///////////////////////////////////////////////////////////////////////////////
  96. void InitializeRegex ( Regex_t *re );
  97. void ResetRegex ( Regex_t *re );
  98. enumError ScanRegex ( Regex_t *re, bool init_re, ccp regex );
  99. int ReplaceRegex
  100. (
  101. Regex_t *re, // valid Regex_t
  102. FastBuf_t *res, // return buffer, cleared
  103. ccp src,
  104. int src_len // -1: use strlen()
  105. );
  106. //
  107. ///////////////////////////////////////////////////////////////////////////////
  108. /////////////// E N D ///////////////
  109. ///////////////////////////////////////////////////////////////////////////////
  110. #endif // DCLIB_REGEX_H