@@ -40,6 +40,9 @@ export osdirs
40
40
import std/ private/ ossymlinks
41
41
export ossymlinks
42
42
43
+ import std/ private/ osappdirs
44
+ export osappdirs
45
+
43
46
import std/ private/ oscommon
44
47
45
48
include system/ inclrtl
@@ -100,148 +103,7 @@ export envvars
100
103
import std/ private/ osseps
101
104
export osseps
102
105
103
- proc getHomeDir * (): string {.rtl , extern : " nos$1" ,
104
- tags : [ReadEnvEffect , ReadIOEffect ].} =
105
- # # Returns the home directory of the current user.
106
- # #
107
- # # This proc is wrapped by the `expandTilde proc`_
108
- # # for the convenience of processing paths coming from user configuration files.
109
- # #
110
- # # See also:
111
- # # * `getConfigDir proc`_
112
- # # * `getTempDir proc`_
113
- # # * `expandTilde proc`_
114
- # # * `getCurrentDir proc`_
115
- # # * `setCurrentDir proc`_
116
- runnableExamples:
117
- assert getHomeDir () == expandTilde (" ~" )
118
-
119
- when defined (windows): return getEnv (" USERPROFILE" ) & " \\ "
120
- else : return getEnv (" HOME" ) & " /"
121
-
122
- proc getConfigDir * (): string {.rtl , extern : " nos$1" ,
123
- tags : [ReadEnvEffect , ReadIOEffect ].} =
124
- # # Returns the config directory of the current user for applications.
125
- # #
126
- # # On non-Windows OSs, this proc conforms to the XDG Base Directory
127
- # # spec. Thus, this proc returns the value of the `XDG_CONFIG_HOME` environment
128
- # # variable if it is set, otherwise it returns the default configuration
129
- # # directory ("~/.config/").
130
- # #
131
- # # An OS-dependent trailing slash is always present at the end of the
132
- # # returned string: `\\` on Windows and `/` on all other OSs.
133
- # #
134
- # # See also:
135
- # # * `getHomeDir proc`_
136
- # # * `getTempDir proc`_
137
- # # * `expandTilde proc`_
138
- # # * `getCurrentDir proc`_
139
- # # * `setCurrentDir proc`_
140
- when defined (windows):
141
- result = getEnv (" APPDATA" )
142
- else :
143
- result = getEnv (" XDG_CONFIG_HOME" , getEnv (" HOME" ) / " .config" )
144
- result .normalizePathEnd (trailingSep = true )
145
-
146
- proc getCacheDir * (): string =
147
- # # Returns the cache directory of the current user for applications.
148
- # #
149
- # # This makes use of the following environment variables:
150
- # #
151
- # # * On Windows: `getEnv("LOCALAPPDATA")`
152
- # #
153
- # # * On macOS: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")`
154
- # #
155
- # # * On other platforms: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")`
156
- # #
157
- # # **See also:**
158
- # # * `getHomeDir proc`_
159
- # # * `getTempDir proc`_
160
- # # * `getConfigDir proc`_
161
- # follows https://crates.io/crates/platform-dirs
162
- when defined (windows):
163
- result = getEnv (" LOCALAPPDATA" )
164
- elif defined (osx):
165
- result = getEnv (" XDG_CACHE_HOME" , getEnv (" HOME" ) / " Library/Caches" )
166
- else :
167
- result = getEnv (" XDG_CACHE_HOME" , getEnv (" HOME" ) / " .cache" )
168
- result .normalizePathEnd (false )
169
-
170
- proc getCacheDir * (app: string ): string =
171
- # # Returns the cache directory for an application `app`.
172
- # #
173
- # # * On Windows, this uses: `getCacheDir() / app / "cache"`
174
- # # * On other platforms, this uses: `getCacheDir() / app`
175
- when defined (windows):
176
- getCacheDir () / app / " cache"
177
- else :
178
- getCacheDir () / app
179
-
180
-
181
- when defined (windows):
182
- type DWORD = uint32
183
-
184
- when defined (nimPreviewSlimSystem):
185
- import std/ widestrs
186
-
187
- proc getTempPath (
188
- nBufferLength: DWORD , lpBuffer: WideCString
189
- ): DWORD {.stdcall , dynlib : " kernel32.dll" , importc : " GetTempPathW" .} =
190
- # # Retrieves the path of the directory designated for temporary files.
191
-
192
- template getEnvImpl (result: var string , tempDirList: openArray [string ]) =
193
- for dir in tempDirList:
194
- if existsEnv (dir):
195
- result = getEnv (dir)
196
- break
197
-
198
- template getTempDirImpl (result: var string ) =
199
- when defined (windows):
200
- getEnvImpl (result , [" TMP" , " TEMP" , " USERPROFILE" ])
201
- else :
202
- getEnvImpl (result , [" TMPDIR" , " TEMP" , " TMP" , " TEMPDIR" ])
203
106
204
- proc getTempDir * (): string {.rtl , extern : " nos$1" ,
205
- tags : [ReadEnvEffect , ReadIOEffect ].} =
206
- # # Returns the temporary directory of the current user for applications to
207
- # # save temporary files in.
208
- # #
209
- # # On Windows, it calls [GetTempPath](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppathw).
210
- # # On Posix based platforms, it will check `TMPDIR`, `TEMP`, `TMP` and `TEMPDIR` environment variables in order.
211
- # # On all platforms, `/tmp` will be returned if the procs fails.
212
- # #
213
- # # You can override this implementation
214
- # # by adding `-d:tempDir=mytempname` to your compiler invocation.
215
- # #
216
- # # **Note:** This proc does not check whether the returned path exists.
217
- # #
218
- # # See also:
219
- # # * `getHomeDir proc`_
220
- # # * `getConfigDir proc`_
221
- # # * `expandTilde proc`_
222
- # # * `getCurrentDir proc`_
223
- # # * `setCurrentDir proc`_
224
- const tempDirDefault = " /tmp"
225
- when defined (tempDir):
226
- const tempDir {.strdefine .}: string = tempDirDefault
227
- result = tempDir
228
- else :
229
- when nimvm :
230
- getTempDirImpl (result )
231
- else :
232
- when defined (windows):
233
- let size = getTempPath (0 , nil )
234
- # If the function fails, the return value is zero.
235
- if size > 0 :
236
- let buffer = newWideCString (size.int )
237
- if getTempPath (size, buffer) > 0 :
238
- result = $ buffer
239
- elif defined (android): result = " /data/local/tmp"
240
- else :
241
- getTempDirImpl (result )
242
- if result .len == 0 :
243
- result = tempDirDefault
244
- normalizePathEnd (result , trailingSep= true )
245
107
246
108
proc expandTilde * (path: string ): string {.
247
109
tags : [ReadEnvEffect , ReadIOEffect ].} =
0 commit comments