Module preferences

Provides functions to save and load preferences.

Preferences are stored as a key-value-pair. The key and the value must be a string.

The preferences can be saved to a file and loaded again. Each entry is written into a separate line. The line separator is '\n'. Key and value are separated by a '='. When loading a file, every character before the first '=' character is considered as part of the key. All characters between the first '=' and the end of the line are considered as part of the value. Therefore, keys must not contain a '=' character and values must not contain a '\n' character.

Info:

  • Copyright: 2022 Patrick Lang
  • Author: Patrick Lang

Functions

escapeString (str) Creates a valid preference key based on the provided string.
set (key, value) Sets a preference value.
get (key, defaultValue) Gets a preference value.
remove (key) Removes a preference value.
clear () Removes all preferences.
save (filePath) Saves the preferences to the specified file.
load (filePath) Loads the preferences from the specified file.


Functions

escapeString (str)
Creates a valid preference key based on the provided string. Replaces all '=' characters with an underscore.

Parameters:

  • str string The string to convert.

Returns:

    string The escaped preference key.
set (key, value)
Sets a preference value. Providing a nil value removes the preference with the given key.

Parameters:

  • key string The key of the value.
  • value optional string The preference value.

Raises:

An error is thrown if the key contains a '=' or '\n', or the value contains a '\n'.
get (key, defaultValue)
Gets a preference value.

Parameters:

  • key string The key of the value.
  • defaultValue optional string The default value to use if there is no preference with this key. Can be nil.

Returns:

    string The value for the preference key, or the default value, if there is no preference with this key.
remove (key)
Removes a preference value.

Parameters:

  • key string The key of the value to remove.
clear ()
Removes all preferences.
save (filePath)
Saves the preferences to the specified file. If the file could not be opened for writing, then the preferences are not saved.

Parameters:

  • filePath string The path to the target file.

Returns:

    bool True if the preferences has been saved, otherwise false.
load (filePath)
Loads the preferences from the specified file. If the file does not exist, then no preferences are loaded. The actual preferences are not reset before loading the file. Already existing preferences are overwritten.

Parameters:

  • filePath string The path to the file which contains the preferences.

Returns:

    bool True if the preferences has been loaded, otherwise false.
generated by LDoc 1.4.6 Last updated 2022-05-28 12:58:52