path_unix.odin 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #+build linux, darwin, freebsd, openbsd, netbsd
  2. package filepath
  3. import "base:runtime"
  4. import "core:strings"
  5. import "core:sys/posix"
  6. SEPARATOR :: '/'
  7. SEPARATOR_STRING :: `/`
  8. LIST_SEPARATOR :: ':'
  9. is_reserved_name :: proc(path: string) -> bool {
  10. return false
  11. }
  12. is_abs :: proc(path: string) -> bool {
  13. return strings.has_prefix(path, "/")
  14. }
  15. abs :: proc(path: string, allocator := context.allocator) -> (string, bool) {
  16. rel := path
  17. if rel == "" {
  18. rel = "."
  19. }
  20. rel_cstr := strings.clone_to_cstring(rel, context.temp_allocator)
  21. path_ptr := posix.realpath(rel_cstr, nil)
  22. if path_ptr == nil {
  23. return "", posix.errno() == nil
  24. }
  25. defer posix.free(path_ptr)
  26. path_str := strings.clone(string(path_ptr), allocator)
  27. return path_str, true
  28. }
  29. join :: proc(elems: []string, allocator := context.allocator) -> (joined: string, err: runtime.Allocator_Error) #optional_allocator_error {
  30. for e, i in elems {
  31. if e != "" {
  32. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == allocator)
  33. p := strings.join(elems[i:], SEPARATOR_STRING, context.temp_allocator) or_return
  34. return clean(p, allocator)
  35. }
  36. }
  37. return "", nil
  38. }