user.odin 1.6 KB

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