user.odin 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package os2
  2. import "core:strings"
  3. import "base:runtime"
  4. user_cache_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
  5. #partial switch ODIN_OS {
  6. case .Windows:
  7. dir = get_env("LocalAppData", allocator)
  8. if dir != "" {
  9. dir = strings.clone(dir, allocator) or_return
  10. }
  11. case .Darwin:
  12. dir = get_env("HOME", allocator)
  13. if dir != "" {
  14. dir = strings.concatenate({dir, "/Library/Caches"}, allocator) or_return
  15. }
  16. case: // All other UNIX systems
  17. dir = get_env("XDG_CACHE_HOME", allocator)
  18. if dir == "" {
  19. dir = get_env("HOME", allocator)
  20. if dir == "" {
  21. return
  22. }
  23. dir = strings.concatenate({dir, "/.cache"}, allocator) or_return
  24. }
  25. }
  26. if dir == "" {
  27. err = .Invalid_Path
  28. }
  29. return
  30. }
  31. user_config_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
  32. #partial switch ODIN_OS {
  33. case .Windows:
  34. dir = get_env("AppData", allocator)
  35. if dir != "" {
  36. dir = strings.clone(dir, allocator) or_return
  37. }
  38. case .Darwin:
  39. dir = get_env("HOME", allocator)
  40. if dir != "" {
  41. dir = strings.concatenate({dir, "/Library/Application Support"}, allocator) or_return
  42. }
  43. case: // All other UNIX systems
  44. dir = get_env("XDG_CACHE_HOME", allocator)
  45. if dir == "" {
  46. dir = get_env("HOME", allocator)
  47. if dir == "" {
  48. return
  49. }
  50. dir = strings.concatenate({dir, "/.config"}, allocator) or_return
  51. }
  52. }
  53. if dir == "" {
  54. err = .Invalid_Path
  55. }
  56. return
  57. }
  58. user_home_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
  59. env := "HOME"
  60. #partial switch ODIN_OS {
  61. case .Windows:
  62. env = "USERPROFILE"
  63. }
  64. if v := get_env(env, allocator); v != "" {
  65. return v, nil
  66. }
  67. return "", .Invalid_Path
  68. }