1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
## .. importdoc:: paths.nim, dirs.nim
include system/inclrtl
import std/envvars
import std/private/ospaths2
proc getHomeDir*(): string {.rtl, extern: "nos$1",
tags: [ReadEnvEffect, ReadIOEffect].} =
## Returns the home directory of the current user.
##
## This proc is wrapped by the `expandTilde proc`_
## for the convenience of processing paths coming from user configuration files.
##
## See also:
## * `getDataDir proc`_
## * `getConfigDir proc`_
## * `getTempDir proc`_
## * `expandTilde proc`_
## * `getCurrentDir proc`_
## * `setCurrentDir proc`_
runnableExamples:
import std/os
assert getHomeDir() == expandTilde("~")
when defined(windows): return getEnv("USERPROFILE") & "\\"
else: return getEnv("HOME") & "/"
proc getDataDir*(): string {.rtl, extern: "nos$1"
tags: [ReadEnvEffect, ReadIOEffect].} =
## Returns the data directory of the current user for applications.
##
## On non-Windows OSs, this proc conforms to the XDG Base Directory
## spec. Thus, this proc returns the value of the `XDG_DATA_HOME` environment
## variable if it is set, otherwise it returns the default configuration
## directory ("~/.local/share" or "~/Library/Application Support" on macOS).
##
## See also:
## * `getHomeDir proc`_
## * `getConfigDir proc`_
## * `getTempDir proc`_
## * `expandTilde proc`_
## * `getCurrentDir proc`_
## * `setCurrentDir proc`_
when defined(windows):
result = getEnv("APPDATA")
elif defined(macosx):
result = getEnv("XDG_DATA_HOME", getEnv("HOME") / "Library" / "Application Support")
else:
result = getEnv("XDG_DATA_HOME", getEnv("HOME") / ".local" / "share")
result.normalizePathEnd(trailingSep = true)
proc getConfigDir*(): string {.rtl, extern: "nos$1",
tags: [ReadEnvEffect, ReadIOEffect].} =
## Returns the config directory of the current user for applications.
##
## On non-Windows OSs, this proc conforms to the XDG Base Directory
## spec. Thus, this proc returns the value of the `XDG_CONFIG_HOME` environment
## variable if it is set, otherwise it returns the default configuration
## directory ("~/.config/").
##
## An OS-dependent trailing slash is always present at the end of the
## returned string: `\\` on Windows and `/` on all other OSs.
##
## See also:
## * `getHomeDir proc`_
## * `getDataDir proc`_
## * `getTempDir proc`_
## * `expandTilde proc`_
## * `getCurrentDir proc`_
## * `setCurrentDir proc`_
when defined(windows):
result = getEnv("APPDATA")
else:
result = getEnv("XDG_CONFIG_HOME", getEnv("HOME") / ".config")
result.normalizePathEnd(trailingSep = true)
proc getCacheDir*(): string =
## Returns the cache directory of the current user for applications.
##
## This makes use of the following environment variables:
##
## * On Windows: `getEnv("LOCALAPPDATA")`
##
## * On macOS: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")`
##
## * On other platforms: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")`
##
## **See also:**
## * `getHomeDir proc`_
## * `getTempDir proc`_
## * `getConfigDir proc`_
## * `getDataDir proc`_
# follows https://crates.io/crates/platform-dirs
when defined(windows):
result = getEnv("LOCALAPPDATA")
elif defined(osx):
result = getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")
else:
result = getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")
result.normalizePathEnd(false)
proc getCacheDir*(app: string): string =
## Returns the cache directory for an application `app`.
##
## * On Windows, this uses: `getCacheDir() / app / "cache"`
## * On other platforms, this uses: `getCacheDir() / app`
when defined(windows):
getCacheDir() / app / "cache"
else:
getCacheDir() / app
when defined(windows):
type DWORD = uint32
when defined(nimPreviewSlimSystem):
import std/widestrs
proc getTempPath(
nBufferLength: DWORD, lpBuffer: WideCString
): DWORD {.stdcall, dynlib: "kernel32.dll", importc: "GetTempPathW".} =
## Retrieves the path of the directory designated for temporary files.
template getEnvImpl(result: var string, tempDirList: openArray[string]) =
for dir in tempDirList:
if existsEnv(dir):
result = getEnv(dir)
break
template getTempDirImpl(result: var string) =
when defined(windows):
getEnvImpl(result, ["TMP", "TEMP", "USERPROFILE"])
else:
getEnvImpl(result, ["TMPDIR", "TEMP", "TMP", "TEMPDIR"])
proc getTempDir*(): string {.rtl, extern: "nos$1",
tags: [ReadEnvEffect, ReadIOEffect].} =
## Returns the temporary directory of the current user for applications to
## save temporary files in.
##
## On Windows, it calls [GetTempPath](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppathw).
## On Posix based platforms, it will check `TMPDIR`, `TEMP`, `TMP` and `TEMPDIR` environment variables in order.
## On all platforms, `/tmp` will be returned if the procs fails.
##
## You can override this implementation
## by adding `-d:tempDir=mytempname` to your compiler invocation.
##
## **Note:** This proc does not check whether the returned path exists.
##
## See also:
## * `getHomeDir proc`_
## * `getConfigDir proc`_
## * `expandTilde proc`_
## * `getCurrentDir proc`_
## * `setCurrentDir proc`_
const tempDirDefault = "/tmp"
when defined(tempDir):
const tempDir {.strdefine.}: string = tempDirDefault
result = tempDir
else:
when nimvm:
getTempDirImpl(result)
else:
when defined(windows):
let size = getTempPath(0, nil)
# If the function fails, the return value is zero.
if size > 0:
let buffer = newWideCString(size.int)
if getTempPath(size, buffer) > 0:
result = $buffer
elif defined(android): result = "/data/local/tmp"
else:
getTempDirImpl(result)
if result.len == 0:
result = tempDirDefault
normalizePathEnd(result, trailingSep=true)
|