path_unix.odin 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //+build linux, darwin, freebsd
  2. package filepath
  3. when ODIN_OS == "darwin" {
  4. foreign import libc "System.framework"
  5. } else {
  6. foreign import libc "system:c"
  7. }
  8. import "core:strings"
  9. SEPARATOR :: '/';
  10. SEPARATOR_STRING :: `/`;
  11. LIST_SEPARATOR :: ':';
  12. is_reserved_name :: proc(path: string) -> bool {
  13. return false;
  14. }
  15. is_abs :: proc(path: string) -> bool {
  16. return strings.has_prefix(path, "/");
  17. }
  18. abs :: proc(path: string, allocator := context.allocator) -> (string, bool) {
  19. rel := path;
  20. if rel == "" {
  21. rel = ".";
  22. }
  23. rel_cstr := strings.clone_to_cstring(rel, context.temp_allocator);
  24. path_ptr := realpath(rel_cstr, nil);
  25. if path_ptr == nil {
  26. return "", __error()^ == 0;
  27. }
  28. defer _unix_free(path_ptr);
  29. path_cstr := cstring(path_ptr);
  30. path_str := strings.clone(string(path_cstr), allocator);
  31. return path_str, true;
  32. }
  33. join :: proc(elems: ..string, allocator := context.allocator) -> string {
  34. for e, i in elems {
  35. if e != "" {
  36. p := strings.join(elems[i:], SEPARATOR_STRING, context.temp_allocator);
  37. return clean(p, allocator);
  38. }
  39. }
  40. return "";
  41. }
  42. @(private)
  43. foreign libc {
  44. realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr ---
  45. @(link_name="free") _unix_free :: proc(ptr: rawptr) ---
  46. }
  47. when ODIN_OS == "darwin" {
  48. @(private)
  49. foreign libc {
  50. @(link_name="__error") __error :: proc() -> ^i32 ---
  51. }
  52. } else {
  53. @(private)
  54. foreign libc {
  55. @(link_name="__errno_location") __error :: proc() -> ^i32 ---
  56. }
  57. }