2
0

fnmatch.odin 2.3 KB

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