path_posixfs.odin 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #+private
  2. #+build linux, darwin, netbsd, freebsd, openbsd, wasi
  3. package os2
  4. // This implementation is for all systems that have POSIX-compliant filesystem paths.
  5. _are_paths_identical :: proc(a, b: string) -> (identical: bool) {
  6. return a == b
  7. }
  8. _clean_path_handle_start :: proc(path: string, buffer: []u8) -> (rooted: bool, start: int) {
  9. // Preserve rooted paths.
  10. if _is_path_separator(path[0]) {
  11. rooted = true
  12. buffer[0] = _Path_Separator
  13. start = 1
  14. }
  15. return
  16. }
  17. _is_absolute_path :: proc(path: string) -> bool {
  18. return len(path) > 0 && _is_path_separator(path[0])
  19. }
  20. _get_relative_path_handle_start :: proc(base, target: string) -> bool {
  21. base_rooted := len(base) > 0 && _is_path_separator(base[0])
  22. target_rooted := len(target) > 0 && _is_path_separator(target[0])
  23. return base_rooted == target_rooted
  24. }
  25. _get_common_path_len :: proc(base, target: string) -> int {
  26. i := 0
  27. end := min(len(base), len(target))
  28. for j in 0..=end {
  29. if j == end || _is_path_separator(base[j]) {
  30. if base[i:j] == target[i:j] {
  31. i = j
  32. } else {
  33. break
  34. }
  35. }
  36. }
  37. return i
  38. }
  39. _split_path :: proc(path: string) -> (dir, file: string) {
  40. i := len(path) - 1
  41. for i >= 0 && !_is_path_separator(path[i]) {
  42. i -= 1
  43. }
  44. if i == 0 {
  45. return path[:i+1], path[i+1:]
  46. } else if i > 0 {
  47. return path[:i], path[i+1:]
  48. }
  49. return "", path
  50. }