You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

444 lines
16 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: shwild/shwild.h
  3. *
  4. * Purpose: Root header file for the shwild library
  5. *
  6. * Created: 17th June 2005
  7. * Updated: 19th December 2011
  8. *
  9. * Home: http://shwild.org/
  10. *
  11. * Copyright (c) 2005-2011, Matthew Wilson and Sean Kelly
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are
  16. * met:
  17. *
  18. * - Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * - Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * - Neither the names of Matthew Wilson and Sean Kelly nor the names of
  24. * any contributors may be used to endorse or promote products derived
  25. * from this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  31. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * ////////////////////////////////////////////////////////////////////// */
  40. /** \file shwild/shwild.h [C/C++] This is the root file of the shwild C-API
  41. */
  42. #ifndef SHWILD_INCL_SHWILD_H_SHWILD
  43. #define SHWILD_INCL_SHWILD_H_SHWILD
  44. /* /////////////////////////////////////////////////////////////////////////
  45. * Version information
  46. */
  47. #ifndef SHWILD_DOCUMENTATION_SKIP_SECTION
  48. # define SHWILD_VER_SHWILD_H_SHWILD_MAJOR 1
  49. # define SHWILD_VER_SHWILD_H_SHWILD_MINOR 2
  50. # define SHWILD_VER_SHWILD_H_SHWILD_REVISION 20
  51. # define SHWILD_VER_SHWILD_H_SHWILD_EDIT 30
  52. #endif /* !SHWILD_DOCUMENTATION_SKIP_SECTION */
  53. /** \def SHWILD_VER_MAJOR
  54. * The major version number of shwild
  55. */
  56. /** \def SHWILD_VER_MINOR
  57. * The minor version number of shwild
  58. */
  59. /** \def SHWILD_VER_REVISION
  60. * The revision version number of shwild
  61. */
  62. /** \def SHWILD_VER
  63. * The current composite version number of shwild
  64. */
  65. #ifndef SHWILD_DOCUMENTATION_SKIP_SECTION
  66. # define SHWILD_VER_0_9_1 0x00090100
  67. # define SHWILD_VER_0_9_2 0x00090200
  68. # define SHWILD_VER_0_9_3 0x00090300
  69. # define SHWILD_VER_0_9_4 0x00090400
  70. # define SHWILD_VER_0_9_5 0x00090500
  71. # define SHWILD_VER_0_9_6 0x00090600
  72. # define SHWILD_VER_0_9_7 0x00090700
  73. # define SHWILD_VER_0_9_8 0x00090800
  74. # define SHWILD_VER_0_9_9 0x00090900
  75. # define SHWILD_VER_0_9_10 0x00090a00
  76. # define SHWILD_VER_0_9_11 0x00090b00
  77. # define SHWILD_VER_0_9_12 0x00090cff
  78. # define SHWILD_VER_0_9_13 0x00090dff
  79. # define SHWILD_VER_0_9_14 0x00090eff
  80. # define SHWILD_VER_0_9_15 0x00090fff
  81. # define SHWILD_VER_0_9_16 0x000910ff
  82. # define SHWILD_VER_0_9_17 0x000911ff
  83. # define SHWILD_VER_0_9_18 0x000912ff
  84. # define SHWILD_VER_0_9_19 0x000913ff
  85. # define SHWILD_VER_0_9_20 0x000914ff
  86. #endif /* !SHWILD_DOCUMENTATION_SKIP_SECTION */
  87. #define SHWILD_VER_MAJOR 0
  88. #define SHWILD_VER_MINOR 9
  89. #define SHWILD_VER_REVISION 20
  90. #define SHWILD_VER SHWILD_VER_0_9_20
  91. /* /////////////////////////////////////////////////////////////////////////
  92. * Includes
  93. */
  94. #include <stddef.h> /* for size_t */
  95. /* /////////////////////////////////////////////////////////////////////////
  96. * Documentation
  97. */
  98. /** \defgroup group__shwild_api shwild API
  99. * The shwild Public API
  100. *
  101. * The types, constants and functions that constitute the shwild API. Users of
  102. * the shwild library will use only these functions to effect pattern creation
  103. * and matching.
  104. */
  105. /** \defgroup group__shwild_api__c_api C API
  106. * \ingroup group__shwild_api
  107. * These types, constants and functions form the core of the shwild API.
  108. */
  109. /* /////////////////////////////////////////////////////////////////////////
  110. * Typedefs
  111. */
  112. /** Handle type for use in declaring opaque handles to compiled patterns
  113. * \ingroup group__shwild_api__c_api
  114. */
  115. struct shwild_handle_t_;
  116. /** Handle to compiled pattern.
  117. * \ingroup group__shwild_api__c_api
  118. *
  119. * Used by shwild_compile_pattern(), shwild_match_pattern() and shwild_destroy_pattern()
  120. */
  121. typedef struct shwild_handle_t_* shwild_handle_t;
  122. /** Length-aware string.
  123. * \ingroup group__shwild_api__c_api
  124. *
  125. * This structure is used within the library to provide higher efficiency. In order
  126. * to maximise efficiency, there are equivalent versions of all API functions that
  127. * allow application code to specify string arguments as string slices, including
  128. * shwild_match_s(), shwild_compile_pattern_s() and shwild_match_pattern_s().
  129. */
  130. struct shwild_slice_t
  131. {
  132. size_t len; /*!< Number of characters in the slice. */
  133. char const* ptr; /*!< Pointer to the first character in the slice. May be NULL if len == 0. */
  134. #ifdef __cplusplus
  135. public:
  136. /** Initialises members to default value */
  137. shwild_slice_t();
  138. /** Copies members from another slice instance */
  139. shwild_slice_t(shwild_slice_t const& rhs);
  140. /** Initialises members from the given parameters
  141. *
  142. * \param n The number of characters in the string to be sliced
  143. * \param s Pointer to the first character in the string to be sliced. May be NULL only if n == 0.
  144. */
  145. shwild_slice_t(size_t n, char const* s);
  146. #endif /* __cplusplus */
  147. };
  148. #ifndef SHWILD_DOCUMENTATION_SKIP_SECTION
  149. typedef struct shwild_slice_t shwild_slice_t;
  150. #endif /* !SHWILD_DOCUMENTATION_SKIP_SECTION */
  151. /* /////////////////////////////////////////////////////////////////////////
  152. * Constants and definitions
  153. */
  154. /** \defgroup group__shwild_api__flags Pattern Control Flags
  155. * \ingroup group__shwild_api
  156. * These flags control the pattern matching behaviour:
  157. *
  158. * 1. Escape character. This is to recognise \ as an escape character, escaping
  159. * any following character.
  160. *
  161. * This is on by default, but may be suppressed by
  162. * SHWILD_F_SUPPRESS_BACKSLASH_ESCAPE.
  163. *
  164. * 2. Range support. This recognises [] as delimiting a range of possible
  165. * characters that may sustitute for a single character.
  166. *
  167. * This is on by default, but may be suppressed by
  168. * SHWILD_F_SUPPRESS_RANGE_SUPPORT.
  169. *
  170. * 3. Range continuum support. This recognises a continuum of characters or
  171. * numbers, e.g. [3-5m-q] === [345mnopq]
  172. *
  173. * This is on by default, but may be suppressed by
  174. * SHWILD_F_SUPPRESS_RANGE_CONTINUUM_SUPPORT.
  175. * @{
  176. */
  177. #define SHWILD_F_SUPPRESS_RANGE_SUPPORT (0x0001) /*!< Suppresses the recognition of ranges. [ and ] are treated as literal characters (and need no escaping). */
  178. #define SHWILD_F_SUPPRESS_BACKSLASH_ESCAPE (0x0002) /*!< Suppresses the use of backslash interpretation as escape. \ is treated as a literal character. */
  179. #define SHWILD_F_SUPPRESS_RANGE_CONTINUUM_SUPPORT (0x0004) /*!< Suppresses the recognition of range continuums, i.e. [0-9]. */
  180. #define SHWILD_F_SUPPRESS_RANGE_CONTINUUM_HIGHLOW_SUPPORT (0x0008) /*!< Suppresses the recognition of reverse range continuums, i.e. [9-0], [M-D]. */
  181. #define SHWILD_F_SUPPRESS_RANGE_CONTINUUM_CROSSCASE_SUPPORT (0x0010) /*!< Suppresses the recognition of cross-case range continuums, i.e. [h-J] === [hijHIJ]. */
  182. #define SHWILD_F_SUPPRESS_RANGE_LITERAL_WILDCARD_SUPPORT (0x0020) /*!< Suppresses the recognition of ? and * as literal inside range. */
  183. #define SHWILD_F_SUPPRESS_RANGE_LEADTRAIL_LITERAL_HYPHEN_SUPPORT (0x0040) /*!< Suppresses the recognition of leading/trailing hyphens as literal inside range. */
  184. #define SHWILD_F_SUPPRESS_RANGE_NOT_SUPPORT (0x0400) /*!< Suppresses the use of a leading ^ to mean not any of the following, i.e. [^0-9] means do not match a digit. */
  185. #define SHWILD_F_IGNORE_CASE (0x0200) /*!< Comparison is case-insensitive. */
  186. #if 0
  187. /* Not currently supported. */
  188. #define SHWILD_F_ALLOW_RANGE_LITERAL_BRACKET_SUPPORT (0x0080) /*!< Treats [ and ] as literal inside range. ] only literal if immediately preceeds closing ]. NOT CURRENTLY SUPPORTED. */
  189. #define SHWILD_F_ALLOW_RANGE_QUANTIFICATION_SUPPORT (0x0100) /*!< Allows quantification of the wildcards, with trailing escaped numbers, as in [a-Z]\2-10. All chars in 0-9- become range specifiers. These are separated from actual pattern digits by []. NOT CURRENTLY SUPPORTED. */
  190. #define SHWILD_F_FNM_PATHNAME_SEMANTICS (0x0800) /*!< Will only match / (and \ on Win32) characters with literals, not via any wildcard substitutions. */
  191. #endif /* 0 */
  192. /** @} */
  193. /** \defgroup group__shwild_api__result_codes shwild Results Codes
  194. * \ingroup group__shwild_api
  195. * These codes represent the
  196. *
  197. * @{
  198. */
  199. #define SHWILD_RC_SUCCESS (0) /*!< Operating completed successfully */
  200. #define SHWILD_RC_ERROR_BASE_ (-2000)
  201. #define SHWILD_RC_ALLOC_ERROR (SHWILD_RC_ERROR_BASE_ - 1) /*!< Memory exhaustion. */
  202. #define SHWILD_RC_PARSE_ERROR (SHWILD_RC_ERROR_BASE_ - 2) /*!< Parsing error. */
  203. #define SHWILD_RC_UNSPECIFIED (SHWILD_RC_ERROR_BASE_ - 3) /*!< Unspecified exception. */
  204. /** @} */
  205. /* /////////////////////////////////////////////////////////////////////////
  206. * C API
  207. */
  208. #ifdef __cplusplus
  209. extern "C"
  210. {
  211. #endif /* __cplusplus */
  212. /** Initialises the shwild API
  213. *
  214. * \return Status indicating whether initialisation was successful
  215. * \retval <0 An error
  216. * \retval >=0 Initialisation was successful
  217. */
  218. int shwild_init(void);
  219. /** Uninitialises the shwild API
  220. *
  221. * \remarks Must be called once for each successful call to shwild_init().
  222. */
  223. void shwild_uninit(void);
  224. /** Matches a string against an shwild pattern
  225. * \ingroup group__shwild_api__c_api
  226. *
  227. * \param pattern The shwild pattern against which matching will be performed
  228. * \param string The string to match against the pattern
  229. * \param flags Flags which moderate the search.
  230. *
  231. * \return Status indicating whether the string matched against the given pattern
  232. * \retval <0 An error code (one of SHWILD_RC_*)
  233. * \retval 0 The string matched the pattern
  234. * \retval non-0 The string did not match the pattern
  235. */
  236. int shwild_match(char const* pattern, char const* string, unsigned flags);
  237. /** Synonym for shwild_match() using length-aware string arguments
  238. * \ingroup group__shwild_api__c_api
  239. */
  240. int shwild_match_s(shwild_slice_t const* pattern, shwild_slice_t const* string, unsigned flags);
  241. /** Compiles a pattern into an efficient form for use in multiple match operations
  242. * \ingroup group__shwild_api__c_api
  243. *
  244. * \param pattern The shwild pattern against which matching will be performed
  245. * \param flags Flags which moderate the search.
  246. * \param phCompiledPattern Pointer to a variable to hold the compiled pattern
  247. *
  248. * \return Status indicating whether the operating completed successfully
  249. * \retval <0 The operation failed
  250. * \retval >=0 The operation succeeded. The value indicates the number of match
  251. * sub-components created to represent the pattern. The compiled pattern must
  252. * be destroyed when it is no longer needed, by shwild_destroy_pattern(), to
  253. * avoid memory leaks.
  254. */
  255. int shwild_compile_pattern(char const* pattern, unsigned flags, shwild_handle_t* phCompiledPattern);
  256. /** Synonym for shwild_compile_pattern() using length-aware string arguments
  257. * \ingroup group__shwild_api__c_api
  258. */
  259. int shwild_compile_pattern_s(shwild_slice_t const* pattern, unsigned flags, shwild_handle_t* phCompiledPattern);
  260. /** Matches a string against against a pre-compiled shwild pattern
  261. * \ingroup group__shwild_api__c_api
  262. *
  263. * \param hCompiledPattern The precompiled shwild pattern against which matching will be performed
  264. * \param string The string to match against the pattern
  265. *
  266. * \return Status indicating whether the string matched against the given pattern
  267. * \retval 0 The string did not match the pattern
  268. * \retval non-0 The string matched against the pattern
  269. */
  270. int shwild_match_pattern(shwild_handle_t hCompiledPattern, char const* string);
  271. /** Synonym for shwild_match_pattern() using length-aware string arguments
  272. * \ingroup group__shwild_api__c_api
  273. */
  274. int shwild_match_pattern_s(shwild_handle_t hCompiledPattern, shwild_slice_t const* string);
  275. /** Releases all resources associated with a pre-compiled shwild pattern
  276. * \ingroup group__shwild_api__c_api
  277. *
  278. * \param hCompiledPattern The pattern to be destroyed.
  279. */
  280. void shwild_destroy_pattern(shwild_handle_t hCompiledPattern);
  281. #ifdef __cplusplus
  282. } /* extern "C" */
  283. #endif /* __cplusplus */
  284. /* /////////////////////////////////////////////////////////////////////////
  285. * Namespace
  286. */
  287. #if !defined(__cplusplus) && \
  288. !defined(SHWILD_DOCUMENTATION_SKIP_SECTION) && \
  289. !defined(SHWILD_NO_NAMESPACE)
  290. # define SHWILD_NO_NAMESPACE
  291. #endif /* __cplusplus, etc. */
  292. #if !defined(SHWILD_NO_NAMESPACE)
  293. /** The shwild/C++ namespace - \c shwild - that contains wrappers for the
  294. * \link group__shwild_api__c_api C API\endlink. */
  295. namespace shwild
  296. {
  297. #endif /* !SHWILD_NO_NAMESPACE */
  298. /* /////////////////////////////////////////////////////////////////////////
  299. * C++ functions
  300. */
  301. #ifdef __cplusplus
  302. /** \defgroup group__shwild_api__cpp_api C++ API
  303. * \ingroup group__shwild_api
  304. * The C++ API provides convenient overloads of the
  305. * \ref group__shwild_api__c_api functions for use in C++, along with the
  306. * Pattern class that provides a convenient interface to compiled patterns.
  307. * @{
  308. */
  309. /** C++ synonym for shwild_slice_t
  310. * \ingroup group__shwild_api__cpp_api
  311. */
  312. typedef shwild_slice_t slice_t;
  313. /** C++ overload synonym for shwild_match()
  314. * \ingroup group__shwild_api__cpp_api
  315. */
  316. inline int match(char const* pattern, char const* string, unsigned flags = 0)
  317. {
  318. return shwild_match(pattern, string, flags);
  319. }
  320. /** C++ overload synonym for shwild_compile_pattern()
  321. * \ingroup group__shwild_api__cpp_api
  322. */
  323. inline int compile_pattern(char const* pattern, unsigned flags, shwild_handle_t* phCompiledPattern)
  324. {
  325. return shwild_compile_pattern(pattern, flags, phCompiledPattern);
  326. }
  327. /** C++ overload synonym for shwild_match_pattern()
  328. * \ingroup group__shwild_api__cpp_api
  329. */
  330. inline int match_pattern(shwild_handle_t hCompiledPattern, char const* string)
  331. {
  332. return shwild_match_pattern(hCompiledPattern, string);
  333. }
  334. /** C++ overload synonym for shwild_destroy_pattern()
  335. * \ingroup group__shwild_api__cpp_api
  336. */
  337. inline void destroy_pattern(shwild_handle_t hCompiledPattern)
  338. {
  339. shwild_destroy_pattern(hCompiledPattern);
  340. }
  341. /** @} */
  342. #endif /* __cplusplus */
  343. /* /////////////////////////////////////////////////////////////////////////
  344. * Namespace
  345. */
  346. #if !defined(SHWILD_NO_NAMESPACE)
  347. } /* namespace shwild */
  348. #endif /* !SHWILD_NO_NAMESPACE */
  349. /* /////////////////////////////////////////////////////////////////////////
  350. * Implementation
  351. */
  352. #ifndef SHWILD_DOCUMENTATION_SKIP_SECTION
  353. # ifdef __cplusplus
  354. # if defined(__BORLANDC__) && \
  355. __BORLANDC__ >= 0x0582
  356. # pragma warn -8026
  357. # endif /* compiler */
  358. inline shwild_slice_t::shwild_slice_t()
  359. : len(0)
  360. , ptr(NULL)
  361. {}
  362. inline shwild_slice_t::shwild_slice_t(shwild_slice_t const& rhs)
  363. : len(rhs.len)
  364. , ptr(rhs.ptr)
  365. {}
  366. inline shwild_slice_t::shwild_slice_t(size_t n, char const* s)
  367. : len(n)
  368. , ptr(s)
  369. {}
  370. # if defined(__BORLANDC__) && \
  371. __BORLANDC__ >= 0x0582
  372. # pragma warn .8026
  373. # endif /* compiler */
  374. # endif /* __cplusplus */
  375. #endif /* !SHWILD_DOCUMENTATION_SKIP_SECTION */
  376. /* ////////////////////////////////////////////////////////////////////// */
  377. #endif /* !SHWILD_INCL_SHWILD_H_SHWILD */
  378. /* ///////////////////////////// end of file //////////////////////////// */