Files | |
file | config_file.c |
This is the implementation of the Configuration-File Library. | |
file | config_file.h |
This is the header for the Configuration-File Library. | |
Data Structures | |
struct | key_value_pair |
Storage structure for a key value pair. More... | |
Defines | |
#define | _GNU_SOURCE |
#define | CONFIG_FILE_MAX_PAIRS 64 |
#define | CONFIG_FILE_MAX_RESIZE 32 |
Typedefs | |
typedef key_value_pair | key_value_pair_t |
Functions | |
key_value_pair_t * | config_file_read (char *filename, int *loaded_pairs) |
Reads the requested filename and returns the key-value pairs from within. | |
int | config_file_write (char *filename, key_value_pair_t *pairs, int pairs_to_write) |
Writes the key-value pairs to filename. | |
char * | config_file_get_value (key_value_pair_t *pairs, char *key, int pairs_loaded) |
Searches pairs for key and returns the value of that key. | |
void | config_file_destroy (key_value_pair_t *pairs, int pairs_loaded) |
This function frees all of the memory that has been allocated with config_file_read(). |
It is often important for a program to input data at runtime. This task is often achieved by using a configuration file. The functions provided in this library allow a programmer to pull key-value pairs from a configuration file and putting them into memory. Once in memory, the list of key-value pairs can be searched by key to locate a value. If the list is modified, it can be written back to a configuration file. Most configuration files use the format <key>=<value>.
This library provides the same basic format but is less flexible than other applications in that extra spaces and new line characters will cause the key-value pairs to be loaded improperly. A programmer should strictly adhere to the following format when creating key value pair files:
<beginning of file><key_1>=<value_1><new line character> <key_2>=<value_2><new line character> . . . <key_n>=<value_n><end of file>
Any string of characters (except strings than contain an equals sign) with a length greater or equal to 1 can be used as a key or value.
Here is an example:
public_key_dir=/home/drwarr0/workspace/masters/pubkeys/ username=drwarr0 private_key_file=/home/drwarr0/workspace/masters/.ssh/drwarr0.priv max_channel_conns=2
|
|
|
|
|
|
|
|
|
This function frees all of the memory that has been allocated with config_file_read(). This function iterates through the list of key-value pairs and frees the keys and values. The memory allocated for the array of key_value_pair_t is also freed.
|
|
Searches pairs for key and returns the value of that key. Iterates through pairs comparing key to the key-value pairs. If a match is found the function returns the value associated with that key.
|
|
Reads the requested filename and returns the key-value pairs from within. This function opens filename and allocates memory for CONFIG_FILE_MAX_PAIRS key_value_pair_t. The function then iterates through the file pulling key-value pairs into the array of memory allocated for key_value_pair_t. The buffer is resized if CONFIG_FILE_MAX_PAIRS is exceded. CONFIG_FILE_MAX_RESIZE * CONFIG_FILE_MAX_PAIRS is equal to the maximum number of key_value_pair_t that can be returned with this function call.
|
|
Writes the key-value pairs to filename. This function opens filename and iterates through pairs writing each element to the file in the format:
|