Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

The Configuration-File Library
[Helper Components]

Library that provides a method for accessing and writing config files. More...


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_tconfig_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().


Detailed Description

Library that provides a method for accessing and writing config files.

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

Define Documentation

#define _GNU_SOURCE
 

#define CONFIG_FILE_MAX_PAIRS   64
 

#define CONFIG_FILE_MAX_RESIZE   32
 


Typedef Documentation

typedef struct key_value_pair key_value_pair_t
 


Function Documentation

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().

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.

Parameters:
pairs An array of key_value_pair_t
pairs_loaded The number of key_value_pair_t in pairs.
See also:
config_file_read()
Returns:
This function returns a pointer to a list of key_value_pair_t. These are the key-value pairs that are pulled from the specified file.

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.

Iterates through pairs comparing key to the key-value pairs. If a match is found the function returns the value associated with that key.

Parameters:
pairs An array of key_value_pair_t
key A pointer to the key to be searched for in pairs
pairs_loaded The number of key_value_pair_t in pairs
Returns:
This function returns a pointer to the value associated with key if key is found in pairs. If key is not found in pairs, NULL is returned.

key_value_pair_t * config_file_read char *  filename,
int *  pairs_loaded
 

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.

Parameters:
filename This is a character pointer that points to the full path of the configuration file.
pairs_loaded This returns the number of key_value_pair_t that were loaded.
See also:
config_file_write()
Returns:
This function returns a pointer to a list of key_value_pair_t. These are the key-value pairs that are pulled from filename.

int config_file_write char *  filename,
key_value_pair_t pairs,
int  pairs_to_write
 

Writes the key-value pairs to filename.

This function opens filename and iterates through pairs writing each element to the file in the format:

<key>=<value>

Parameters:
filename A character pointer that points to the full path of the configuration file.
pairs A pointer to a list of key_value_pair_t to write to filename
pairs_to_write The number of key_value_pair_t in pairs.
See also:
config_file_read()
Returns:
returns 0 on success and -1 on failure.


Generated on Thu Dec 16 23:07:32 2004 for alsad by doxygen 1.3.6