Configuration File Reading, pure C code

Source: Internet
Author: User

/***************************** Strlist. h ***************************/
# Ifndef ds_string_list_h _
# Define ds_string_list_h _
Struct strlist_node {
Char key _ [64]; // store the data as key
Char value _ [64]; // The Value
Struct strlist_node * next;
// Struct strlist * Prev;
};
Struct strlist {
Int size _;
Struct strlist_node * head _;
};
Struct strlist * strlist_create ();
Void strlist_destroy (struct strlist * List );
Int strlist_add (struct strlist * List, char const * D );
Int strlist_add2 (struct strlist * List, char const * Key, char const * value );
Int strlist_remove (struct strlist * List, const char * Key );
Int strlist_size (struct strlist * List );
Void strlist_clear (struct strlist * List );
/// Get value of special item, if it is not existed return null
Char const * strlist_value (struct strlist * List, char const * Key );
# Ifdef _ debug
Void strlist_print ();
# Endif
# Endif
</P>
<P>
/**************************** Strlist. c ******************************/
# Include <stdlib. h>
# Include <string. h>
# Include <stdio. h>
# Include "strlist. H"
# Include "common. H" // define trace macro
Struct strlist * strlist_create ()
{
Struct strlist * List = (struct strlist *) malloc (sizeof (struct strlist ));
List-> size _ = 0;
List-> head _ = NULL;
Return list;
}
Void strlist_destroy (struct strlist * List)
{
Strlist_clear (list );
Free (list );
}
Int strlist_add (struct strlist * List, char const * D)
{
Char * Key = NULL;
Char * ret = NULL;
If (null = List)
Return 0;

Ret = strchr (D ,':');
If (ret = d | ret = NULL | * (++ RET) = 0) // invalid datum format ignore it
Return 0;
Key = (char *) malloc (ret-D );
Strncpy (Key, D, RET-d-1 );
Strlist_remove (list, key );
Struct strlist_node * node = (struct strlist_node *) malloc (sizeof (struct strlist_node ));
Memset (node-> key _, 0, 64 );
Memset (node-> value _, 0, 64 );
Strncpy (node-> key _, key, RET-d-1 );
Strcpy (node-> value _, RET );
Tracen ("nadd key: % snand value: % Sn", node-> key _, node-> value _);
Node-> next = NULL;
Free (key );

If (null = List-> head _)
{
List-> head _ = node;
List-> size _ = 1;
Return list-> size _;
}
Node-> next = List-> head _;
List-> head _ = node;
++ (List-> size _);
Return list-> size _;
}
Int strlist_add2 (struct strlist * List, char const * Key, char const * value)
{
If (null = List)
Return 0;
Strlist_remove (list, key );

Struct strlist_node * node = (struct strlist_node *) malloc (sizeof (struct strlist_node ));
Memset (node-> key _, 0, 64 );
Memset (node-> value _, 0, 64 );
Strcpy (node-> key _, key );
Strcpy (node-> value _, value );
Node-> next = NULL;

If (null = List-> head _)
{
List-> head _ = node;
List-> size _ = 1;
Return list-> size _;
}
Node-> next = List-> head _;
List-> head _ = node;
++ (List-> size _);
Return list-> size _;
}
Int strlist_remove (struct strlist * List, char const * key)
{
If (null = List | null = List-> head _)
Return-1;
Struct strlist_node * Work = List-> head _;
Struct strlist_node * Prev = work;
While (work! = NULL)
{
If (strcmp (work-> key _, key) = 0)
{
Break;
}
Prev = work;
Work = work-> next;
}

If (null = work)
Return-1;
Prev-> next = work-> next;
If (Work = List-> head _)
List-> head _ = work-> next;

Free (work );

Return -- (list-> size _);
}
Int strlist_size (struct strlist * List)
{
Return list-> size _;
}
Void strlist_clear (struct strlist * List)
{
Struct strlist_node * Work = List-> head _;
While (work! = NULL)
{
Trace;
Struct strlist_node * P = work;
Work = work-> next;
Free (P );
}
List-> head _ = NULL;
List-> size _ = 0;
}
Char const * strlist_value (struct strlist * List, char const * key)
{
If (null = List | null = List-> head _)
Return NULL;
Struct strlist_node * Work = List-> head _;
While (work! = NULL)
{
If (strcmp (work-> key _, key) = 0)
{
Return work-> value _;
}
Work = work-> next;
}

Return NULL;
}
# Ifdef _ debug
Void strlist_print (struct strlist * List)
{
Trace;
If (null = List | null = List-> head _)
Return;
Fprintf (stderr, "list's size is % DN", list-> size _);
Struct strlist_node * Work = List-> head _;
While (work! = NULL)
{
Fprintf (stderr, "% s = % Sn", work-> key _, work-> value _);
Work = work-> next;
}
}
# Endif
</P>
The following is the part of the parsing file.
<P>
/******************* Read_config.h ******************* */
# Ifndef ds_config_handle_h _
# Define ds_config_handle_h _
# Include <stdlib. h>
Int config_load (char const * filename );
Void config_clean ();
Char const * config_string (char const * Section, char const * Key );
Int config_integer (char const * Section, char const * Key );
Float config_float (char const * Section, char const * Key );
// Int config_save (char const * filename );
# Endif // ds_config_handle_h _
</P>
<P>
/*********************** Read_config.c *************** *********/
# Include "config_reader.h"
# Include "strlist. H"
# Include <stdio. h>
# Include <string. h>
# Include "common. H"
Struct strlist * config_list = NULL;
Char section [32];
Int process_line (char key [], char value [], char const * line );
Int config_load (char const * filename)
{
Char * Buf _ = NULL;
Int read _ = 0;
Int size _= 256;
Char * config_pos;
Char * config_offset;
Char line [100];
Char key [32];
Char value [64];
Int COUNT = 0;

If (null = filename)
Return-1;

File * file = fopen (filename, "R ");
If (null = file)
Return-1;
/// Read file into memory
Do {
If (null! = Buf _)
Free (BUF _);
Size _ * = 2;
Buf _ = (char *) malloc (size _);
Rewind (File );
Read _ = fread (BUF _, 1, size _, file );
} While (read _> = size _);
Trace1 ("read the file finished ");
Fclose (File );

Config_pos = Buf _;
Config_offset = Buf _;
/// Parse the config file now
Config_list = strlist_create ();

While (* config_offset! = 0)
{
If (* config_pos = 'n' | * config_pos = 0)
{
Memset (line, 0, sizeof (line ));
Strncpy (line, config_offset, config_pos-config_offset );

Trace1 (line );
If (process_line (Key, value, line ))
{
Trace;
++ Count;
Char TK [64];
Sprintf (TK, "% s-% s", section, key );
Strlist_add2 (config_list, TK, value );
}

Config_offset = config_pos + 1;
}
++ Config_pos;
}
Free (BUF _);

# Ifdef _ debug
Strlist_print (config_list );
# Endif
Return count;
}
Int process_line (char key [], char value [], char const * line)
{
Char Buf [100];
Char * R;
Char * W;
Memset (BUF, 0, sizeof (BUF ));
Strcpy (BUF, line );
Memset (Key, 0, 32 );
Memset (value, 0, 64 );

R = Buf;
While (* r! = 0)
{
If (* r = ';')
Break;
++ R;
}
While (* r! = 0)
* R ++ = 0;
R = Buf;
W = key;
While (* r! = 0)
{
If (* r = ')
Break;
// If (* r = ';' & W = Key) // if the first non space character is ';', the line is comment
// Return 0;
If (* r! = '')
* W ++ = * R;
++ R;
}
Trace1 (key );
-- W;

If (* Key = '[' & * (Key + strlen (key)-1) = ']')
{
Memset (section, 0, 64 );
W = key;
Strncpy (section, key + 1, strlen (key)-2 );
Return 0;
}
++ R;
While (* r! = 0)
{
If (* r = '')
++ R;
Else
Break;
}
Strcpy (value, R );
If (strlen (key)> 0 & strlen (value)> 0)
Return 1;
Return 0;
}
Void config_clean ()
{
Strlist_destroy (config_list );
}
Char const * config_string (char const * Section, char const * key)
{
Char TK [64];
Sprintf (TK, "% s-% s", section, key );
Return strlist_value (config_list, TK );
}
Int config_integer (char const * Section, char const * key)
{
Char TK [64];
Sprintf (TK, "% s-% s", section, key );
Return atoi (strlist_value (config_list, TK ));
}
Float config_float (char const * Section, char const * key)
{
Char TK [64];
Sprintf (TK, "% s-% s", section, key );
Return atof (strlist_value (config_list, TK ));
}
// Int config_save (char const * filename)
//{

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.