fnmatch.odin 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #+build darwin, linux, openbsd, freebsd, netbsd
  2. package posix
  3. import "core:c"
  4. when ODIN_OS == .Darwin {
  5. foreign import lib "system:System.framework"
  6. } else {
  7. foreign import lib "system:c"
  8. }
  9. // fnmatch.h - filename-matching types
  10. foreign lib {
  11. /*
  12. Match patterns as described in XCU [[ Patterns Matching a Single Character; https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13_01 ]]
  13. // and [[ Patterns Matching Multiple Characters; https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13_02 ]].
  14. It checks the string specified by the string argument to see if it matches the pattern specified by the pattern argument.
  15. Returns: 0 when matched. if there is no match, fnmatch() shall return FNM_NOMATCH. Non-zero on other errors.
  16. Example:
  17. assert(posix.fnmatch("*.odin", "foo.odin", {}) == 0)
  18. assert(posix.fnmatch("*.txt", "foo.odin", {}) == posix.FNM_NOMATCH)
  19. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/fnmatch.html ]]
  20. */
  21. fnmatch :: proc(pattern: cstring, string: cstring, flags: FNM_Flags) -> c.int ---
  22. }
  23. FNM_Flag_Bits :: enum c.int {
  24. // A <slash> character ( '/' ) in string shall be explicitly matched by a <slash> in pattern;
  25. // it shall not be matched by either the <asterisk> or <question-mark> special characters,
  26. // nor by a bracket expression.
  27. PATHNAME = log2(FNM_PATHNAME),
  28. // A leading <period> ( '.' ) in string shall match a <period> in pattern;
  29. // as described by rule 2 in XCU [[ Patterns Used for Filename Expansion; https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13_03 ]]
  30. // where the location of "leading" is indicated by the value of PATHNAME:
  31. // 1. If PATHNAME is set, a <period> is "leading" if it is the first character in string or if it immediately follows a <slash>.
  32. // 2. If PATHNAME is not set, a <period> is "leading" only if it is the first character of string.
  33. PERIOD = log2(FNM_PERIOD),
  34. // A <backslash> character shall be treated as an ordinary character.
  35. NOESCAPE = log2(FNM_NOESCAPE),
  36. }
  37. FNM_Flags :: bit_set[FNM_Flag_Bits; c.int]
  38. when ODIN_OS == .Darwin || ODIN_OS == .FreeBSD || ODIN_OS == .NetBSD || ODIN_OS == .OpenBSD {
  39. FNM_NOMATCH :: 1
  40. FNM_PATHNAME :: 0x02
  41. FNM_PERIOD :: 0x04
  42. FNM_NOESCAPE :: 0x01
  43. } else when ODIN_OS == .Linux {
  44. FNM_NOMATCH :: 1
  45. FNM_PATHNAME :: 0x01
  46. FNM_NOESCAPE :: 0x02
  47. FNM_PERIOD :: 0x04
  48. }