scoop:scoop.platform:0.1.0
src/env/environment.scoop
Selected production source · src/env/environment.scoop
1
package scoop.platform.env3
import scoop.collections.LinkedHashMap4
import scoop.collections.Map5
import scoop.collections.toMap6
import scoop.platform.fs.*7
import scoop.platform.system.OsString9
/**10
* Returns an independent value snapshot, distinguishing absence from an empty string.11
* Names and values retain the operating system's native encoding.12
*/13
public 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
*/20
public 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
@Unsafe29
{30
return name.withPlatformString {31
pointer -> @Unsafe32
{33
return@withPlatformString nativeEnvironmentValue(pointer).takeOptionalString(34
"environment variable",35
Some(name)36
)37
}38
}39
}40
}42
/**43
* Returns an immutable snapshot with the original spelling of environment names. Map key44
* equality remains OsString equality, including on Windows.45
*/46
public fun environment(): Map<OsString, OsString> {47
val result = LinkedHashMap<OsString, OsString>()48
@Unsafe49
{50
val bytes = nativeEnvironment().takeString("environment").toByteArray()51
var start = 0L52
while (start < bytes.size) {53
var end = start54
while (end < bytes.size && bytes[end] != 0)55
end += 1L56
var separator = start57
while (separator < end && bytes[separator] != 61)58
separator += 1L59
if (separator > start && separator < end) {60
val first = start61
val split = separator62
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 + 1L69
}70
return result.toMap()71
}72
}74
/** Returns the actual absolute path of the process executable, independent of argv and cwd. */75
public fun executablePath(): Path {76
@Unsafe77
{78
return nativeExecutablePath().takePath("executable path")79
}80
}82
@Extern(name = "scoop_platform_executable_path_result")83
private fun nativeExecutablePath(): NativeOsString85
/** Queries the actual current working directory, without consulting PWD. */86
public fun currentDirectory(): Path {87
@Unsafe88
{89
return nativeCurrentDirectory().takePath("current directory")90
}91
}93
/**94
* Queries the user's home directory. Windows prefers USERPROFILE then the process user's95
* profile; iOS uses its application container. Other POSIX targets prefer HOME then the96
* current uid's passwd record.97
*/98
public fun homeDirectory(): Path? {99
@Unsafe100
{101
return when (nativeHomeDirectory().takeOptionalString("home directory")) {102
case Some(val value) -> Some(Path.fromOsString(value))103
case None -> None104
}105
}106
}108
@Extern(name = "scoop_platform_environment_value_result")109
private fun nativeEnvironmentValue(name: Ptr<Byte>): NativeOsString111
@Extern(name = "scoop_platform_environment_result")112
private fun nativeEnvironment(): NativeOsString114
@Extern(name = "scoop_platform_current_directory_result")115
private fun nativeCurrentDirectory(): NativeOsString117
@Extern(name = "scoop_platform_home_directory_result")118
private fun nativeHomeDirectory(): NativeOsString