Documentation



SYNOPSIS

#include <conffile/conffile.h>

int new_conffile(conffile_t **conf);
void free_conffile(conffile_t **conf);
int load_from_file(char *filename, conffile_t **conf);
int save_to_file(char *filename, conffile_t *conf);
int set_key(char *key, char *value, conffile_t **conf);
char* get_value(char *key, conffile_t *conf);
int remove_key(char *key, conffile_t **conf);

DESCRIPTION

The libconffile provides the functions cited above to handle with configuration files in a easy way. Configuration files has this format:

   #---- begin configuration file ----

   my_key0 = value0

   # This is a comment
   my_key1 = value1

   anotherkey=other value
   random = one_more_value

   #---- end configuration file ----
Lines started by # are comments and a key is used to store a value. Functions can read and change the value of any key present in the configuration file.

FUNCTIONS


int  new_conffile(conffile_t **conf);
        This function allocates memory for new configuration.

        new_conffile() return 0 on success, or error flag ENOMEM
        otherwise.


void free_conffile(conffile_t **conf);
        This function frees the memory allocated by new_conffile().

        free_conffile() returns no value.


int  load_from_file(char *filename, conffile_t **conf);
        Load all keys from configuration file.

        load_from_file() return 0 on success, error flag otherwise, see errno(3).


int  save_to_file(char *filename, conffile_t *conf);
        Save all configuration into a file. If filename is NULL, use the same file
        opened by load_from_file() function.

        save_to_file() return 0 on success, error flag otherwise, see errno(3).


int  set_key(char *key, char *value, conffile_t **conf);
        Set or create a configuration key with a value.

        set_key() return 0 on success, -1 otherwise.


char* get_value(char *key, conffile_t *conf);
        Return the value of a key

        get_value return the value of key or NULL if the key not found.


int  remove_key(char *key, conffile_t **conf);
        Remove key from configuration.

        remove_key return 0 on success, -1 otherwise.


BUGS

Bugs reports to <rene@renesp.com.br>

EXAMPLE


/**
 * A simple application to demonstrates the use of libconffile
 *
 * To compile: gcc hello.c -o hello -lconffile
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conffile/conffile.h>

/**
 * Main
 */
int main(int argc, char **argv)
{
        int inumber    = 0;
        double dnumber = 0.0;
        char *home, *ptr;
        char path[1024];
        char str[1024];
        conffile_t *myconf;

        // First, initialize myconf struct (always do it)
        if(new_conffile(&myconf) != 0) {
                perror("Error on allocate memory!\n");
                return(EXIT_FAILURE);
        }

        // Try to load configuration file from home user directory
        if( (home = getenv("HOME")) != NULL ) {
                strcpy(path, home);
                strcat(path, "/.myconffile.conf");
        } else {
                strcpy(path, "./.myconffile.conf");
        }

        // Load file
        if(load_from_file(path, &myconf) == 0) {
                printf("Configuration file loaded!\n");
        } else {
                printf("Configuration file not found\n");

                // File not found, set keys
                set_key("intnum", "200", &myconf);
                set_key("dbnum", "22.500", &myconf);
                set_key("phrase", "Hello World!", &myconf);
        }

        // Get keys
        if((ptr = get_value("intnum", &myconf)) != NULL)
                inumber = atoi(ptr);

        if((ptr = get_value("dbnum", &myconf)) != NULL)
                dnumber = atof(ptr);

        if((ptr = get_value("phrase", &myconf)) != NULL)
                strcpy(str, ptr);

        // Show keys
        printf("\nValue of intnum: %d\n", inumber);
        printf("\nValue of dbnum:  %f\n", dnumber);
        printf("\nValue of str:    %s\n", str);

        // Save configuration file
        if(save_to_file(path, myconf) != 0) {
                perror("Error on saving configuration file!\n");
        }

        printf("\nBye Bye!\n\n");

        // Free memory allocated by new_conffile (always do it too)
        free_conffile(&myconf);
        return(EXIT_SUCCESS);
}

AUTHOR

libconffile was written by Renê S. Pinto <rene@renesp.com.br>