!++ !config.sqh ! ! Copyright (C) 2003-2004 Ray Ontko & Company ! ! ! ! This include file handles a set of name-value pairs. They can easily ! be saved and restored using a text file. ! !-- #define EMPTY_VALUE '==Empty==' #define CONFIG_FILE_NO 1001 #ifndef CONFIG_FILE_NAME #define CONFIG_FILE_NAME 'default.conf' #endif #ifndef MAX_CONFIG_SIZE #define MAX_CONFIG_SIZE 200 #endif begin-setup create-array name=config_array size={MAX_CONFIG_SIZE} field=name:char field=value:char end-setup !------------------------------------------------------------------------ !++ !- begin-procedure config_find($name, :$value, :#index) ! Returns the value associated with $name. #index will be -1 ! if the key, $name, is not found. ! !-- begin-procedure config_find($name, :$value, :#index) uppercase $name move -1 to #index move '' to $value move 0 to #count while #count < #_config_array get $comp_name $comp_value from config_array(#count) name value if $comp_name = $name move #count to #index move $comp_value to $value break end-if add 1 to #count end-while end-procedure config_find !------------------------------------------------------------------------ !++ !- begin-procedure config_set($set_name, $set_value) ! Sets the value associated with $set_name to $set_value. ! !-- begin-procedure config_set($set_name, $set_value) uppercase $set_name !find the aray_index if this name was already set do config_find($set_name, $old_value, #array_index) if #array_index = -1 ! find an empty slot (if a previously set variable has been unset do config_find({EMPTY_VALUE}, $old_value, #array_index) if #array_index = -1 move #_config_array to #array_index add 1 to #_config_array end-if put $set_name $set_value into config_array(#array_index) name value else put $set_value into config_array(#array_index) value end-if end-procedure config_set !------------------------------------------------------------------------ !++ !- begin-procedure config_unset($unset_name) ! Removes the values associated with $unset_name ! !-- begin-procedure config_unset($unset_name) uppercase $unset_name do config_find($unset_name, $sub_value, #array_index) if #array_index != -1 put {EMPTY_VALUE} into config_array(#array_index) name end-if end-procedure config_unset !------------------------------------------------------------------------ !++ !- begin-procedure config_set_default($set_name, $set_value) ! Will create a new value for $set_name, but will not overwrite ! an existing one. ! !-- begin-procedure config_set_default($set_name, $set_value) uppercase $set_name do config_find($set_name, $old_value, #array_index) if #array_index = -1 put $set_name $set_value into config_array(#_config_array) name value add 1 to #_config_array end-if end-procedure config_set_default !------------------------------------------------------------------------ !++ !- begin-procedure config_set_number($set_name, #set_value) ! This procedure is just a shorthand way of setting a number value. ! !-- begin-procedure config_set_number($set_name, #set_value) move #set_value to $set_value do config_set($set_name, $set_value) end-procedure config_set_number !------------------------------------------------------------------------ !++ !- begin-procedure config_set_default_number($set_name, #set_value) ! This procedure is just a shorthand way of setting a number value. ! !-- begin-procedure config_set_default_number($set_name, #set_value) move #set_value to $set_value do config_set_default($set_name, $set_value) end-procedure config_set_default_number !------------------------------------------------------------------------ !++ !- begin-procedure config_find_number($name, :#value, :#index) ! This procedure is just a shorthand way of recalling a number value. ! !-- begin-procedure config_find_number($name, :#value, :#index) do config_find($name, $value, #index) move $value to #value end-procedure config_find_number !------------------------------------------------------------------------ !++ !- begin-procedure config_load local ! Loads configuration from the file. ! !-- begin-procedure config_load local if exists({CONFIG_FILE_NAME}) = 0 open {CONFIG_FILE_NAME} as {CONFIG_FILE_NO} for-reading record=200 while 1 read {CONFIG_FILE_NO} into $line_text:200 if #_end-file break end-if if substr($line_text, 1, 1) != '#' !comment character let #ndx = instr($line_text, '=', 1) if #ndx ! '=' found let $name = substr($line_text, 1, #ndx - 1) let $value = substr($line_text, #ndx + 1, 999) else move $line_text to $name move '' to $value end-if do config_set($name, $value) end-if end-while close {CONFIG_FILE_NO} end-if move 1 to #_config_loaded end-procedure config_load local !------------------------------------------------------------------------ !++ !- begin-procedure config_save($comment) ! Saves configuration to the file. ! !-- begin-procedure config_save($comment) open {CONFIG_FILE_NAME} as {CONFIG_FILE_NO} for-writing record=200 write {CONFIG_FILE_NO} from '# saved file: ' $comment move 0 to #count while #count < #_config_array get $comp_name $comp_value from config_array(#count) name value if $comp_name != {EMPTY_VALUE} string $comp_name $comp_value by '=' into $line_text write {CONFIG_FILE_NO} from $line_text end-if add 1 to #count end-while close {CONFIG_FILE_NO} end-procedure config_save