Scoop API

scoop:scoop.platform:0.1.0

src/env/environment.scoop

Selected production source · src/env/environment.scoop

1package scoop.platform.env
3import scoop.collections.LinkedHashMap
4import scoop.collections.Map
5import scoop.collections.toMap
6import scoop.platform.fs.*
7import scoop.platform.system.OsString
9/**
10 * Returns an independent value snapshot, distinguishing absence from an empty string.
11 * Names and values retain the operating system's native encoding.
12 */
13public fun environmentVariable(name: String): OsString? =
14 environmentVariable(OsString.fromString(name))
16/**
17 * Returns an independent value snapshot, distinguishing absence from an empty string.
18 * Windows name lookup follows the system's case-insensitive rules.
19 */
20public fun environmentVariable(name: OsString): OsString? {
21 val bytes = name.toByteArray()
22 if (bytes.size == 0L)
23 throw IllegalArgumentException(Some("empty environment name"))
24 for (byte in bytes) {
25 if (byte == 0 || byte == 61)
26 throw IllegalArgumentException(Some("invalid environment name"))
27 }
28 @Unsafe
29 {
30 return name.withPlatformString {
31 pointer -> @Unsafe
32 {
33 return@withPlatformString nativeEnvironmentValue(pointer).takeOptionalString(
34 "environment variable",
35 Some(name)
36 )
37 }
38 }
39 }
42/**
43 * Returns an immutable snapshot with the original spelling of environment names. Map key
44 * equality remains OsString equality, including on Windows.
45 */
46public fun environment(): Map<OsString, OsString> {
47 val result = LinkedHashMap<OsString, OsString>()
48 @Unsafe
49 {
50 val bytes = nativeEnvironment().takeString("environment").toByteArray()
51 var start = 0L
52 while (start < bytes.size) {
53 var end = start
54 while (end < bytes.size && bytes[end] != 0)
55 end += 1L
56 var separator = start
57 while (separator < end && bytes[separator] != 61)
58 separator += 1L
59 if (separator > start && separator < end) {
60 val first = start
61 val split = separator
62 val name = OsString.fromByteArray(Array<Byte>(split - first) { bytes[first + it] })
63 val value =
64 OsString.fromByteArray(Array<Byte>(end - split - 1L) { bytes[split + 1L + it] })
65 if (!result.containsKey(name))
66 result.put(name, value)
67 }
68 start = end + 1L
69 }
70 return result.toMap()
71 }
74/** Returns the actual absolute path of the process executable, independent of argv and cwd. */
75public fun executablePath(): Path {
76 @Unsafe
77 {
78 return nativeExecutablePath().takePath("executable path")
79 }
82@Extern(name = "scoop_platform_executable_path_result")
83private fun nativeExecutablePath(): NativeOsString
85/** Queries the actual current working directory, without consulting PWD. */
86public fun currentDirectory(): Path {
87 @Unsafe
88 {
89 return nativeCurrentDirectory().takePath("current directory")
90 }
93/**
94 * Queries the user's home directory. Windows prefers USERPROFILE then the process user's
95 * profile; iOS uses its application container. Other POSIX targets prefer HOME then the
96 * current uid's passwd record.
97 */
98public fun homeDirectory(): Path? {
99 @Unsafe
100 {
101 return when (nativeHomeDirectory().takeOptionalString("home directory")) {
102 case Some(val value) -> Some(Path.fromOsString(value))
103 case None -> None
104 }
105 }
108@Extern(name = "scoop_platform_environment_value_result")
109private fun nativeEnvironmentValue(name: Ptr<Byte>): NativeOsString
111@Extern(name = "scoop_platform_environment_result")
112private fun nativeEnvironment(): NativeOsString
114@Extern(name = "scoop_platform_current_directory_result")
115private fun nativeCurrentDirectory(): NativeOsString
117@Extern(name = "scoop_platform_home_directory_result")
118private fun nativeHomeDirectory(): NativeOsString