simple_env

API Reference

SIMPLE_ENV

SCOOP-compatible environment variable access via Win32 API.

Creation

Feature Signature Description
default_create Create instance. No arguments needed.

Access

Feature Signature Description
get (a_name: READABLE_STRING_GENERAL): detachable STRING_32 Get value of environment variable. Returns Void if not set.
item alias [] (a_name: READABLE_STRING_GENERAL): detachable STRING_32 Array-style access. Alias for get.

Contract: get

get (a_name: READABLE_STRING_GENERAL): detachable STRING_32
    require
        name_not_empty: not a_name.is_empty

Status Report

Feature Signature Description
has (a_name: READABLE_STRING_GENERAL): BOOLEAN Does environment variable exist?
last_operation_succeeded : BOOLEAN Did the last set/unset operation succeed?

Contract: has

has (a_name: READABLE_STRING_GENERAL): BOOLEAN
    require
        name_not_empty: not a_name.is_empty

Modification

Feature Signature Description
set (a_name, a_value: READABLE_STRING_GENERAL) Set environment variable to value.
put (a_value, a_name: READABLE_STRING_GENERAL) Set variable (HASH_TABLE convention: value first).
unset (a_name: READABLE_STRING_GENERAL) Remove environment variable.

Contract: set

set (a_name, a_value: READABLE_STRING_GENERAL)
    require
        name_not_empty: not a_name.is_empty
    ensure
        variable_set: last_operation_succeeded implies attached get (a_name)

Contract: unset

unset (a_name: READABLE_STRING_GENERAL)
    require
        name_not_empty: not a_name.is_empty
    ensure
        variable_removed: last_operation_succeeded implies not has (a_name)

Expansion

Feature Signature Description
expand (a_string: READABLE_STRING_GENERAL): STRING_32 Expand %VAR% references in string.

Contract: expand

expand (a_string: READABLE_STRING_GENERAL): STRING_32
    require
        string_not_empty: not a_string.is_empty

Example

-- Input: "%USERPROFILE%\Documents"
-- Output: "C:\Users\John\Documents"

-- Unknown variables are left unchanged:
-- Input: "%UNKNOWN%"
-- Output: "%UNKNOWN%"

Enumeration

Feature Signature Description
all_names : ARRAYED_LIST [STRING_32] All environment variable names in current process.
names_with_prefix (a_prefix: READABLE_STRING_GENERAL): ARRAYED_LIST [STRING_32] All variable names starting with prefix (case-insensitive).

Contract: all_names

all_names: ARRAYED_LIST [STRING_32]
    ensure
        result_attached: Result /= Void

Contract: names_with_prefix

names_with_prefix (a_prefix: READABLE_STRING_GENERAL): ARRAYED_LIST [STRING_32]
    require
        prefix_not_empty: not a_prefix.is_empty
    ensure
        result_attached: Result /= Void

Usage Examples

Getting Variables

local
    env: SIMPLE_ENV
do
    create env

    -- Using get with attached
    if attached env.get ("PATH") as path then
        print (path)
    end

    -- Using bracket notation
    if attached env ["TEMP"] as temp then
        print (temp)
    end
end

Setting Variables

local
    env: SIMPLE_ENV
do
    create env

    env.set ("MY_APP_CONFIG", "/etc/myapp/config")

    if env.last_operation_succeeded then
        print ("Configuration path set")
    end
end

Expanding Paths

local
    env: SIMPLE_ENV
    config_path: STRING_32
do
    create env

    -- Expand Windows path variables
    config_path := env.expand ("%APPDATA%\MyApp\config.json")
    -- Result: "C:\Users\John\AppData\Roaming\MyApp\config.json"
end

Enumerating Variables

local
    env: SIMPLE_ENV
do
    create env

    -- List all variables
    across env.all_names as name loop
        if attached env.get (name) as value then
            print (name + "=" + value + "%N")
        end
    end

    -- List only SIMPLE_* variables
    across env.names_with_prefix ("SIMPLE_") as lib loop
        print (lib + "%N")
    end
end

Return Types

Type Used By Notes
detachable STRING_32 get, item Void if variable doesn't exist
STRING_32 expand Always returns a string (original if no expansion)
BOOLEAN has, last_operation_succeeded True/False status
ARRAYED_LIST [STRING_32] all_names, names_with_prefix Never Void, may be empty

Error Conditions

Operation Failure Condition Result
get Variable doesn't exist Returns Void
set Insufficient permissions or system limit last_operation_succeeded = False
unset Variable doesn't exist or permission denied last_operation_succeeded = False
expand Unknown variable in string Returns original %VAR% unchanged