Added Per Ola Kristensson's ANSI C application settings management

More information here:
http://pokristensson.com/settings.html
This commit is contained in:
Per Ola Kristensson 2012-07-04 02:39:48 +02:00 committed by CeRiAl
parent ef8cda3b2d
commit cd1a34c253
4 changed files with 2331 additions and 0 deletions

955
src/settings.c Normal file
View file

@ -0,0 +1,955 @@
/*
* settings version 1.0.1
*
* ANSI C implementation for managing application settings.
*
* Version History:
* 1.0.0 (2009) - Initial release
* 1.0.1 (2010) - Fixed small memory leak in settings_delete
* (Thanks to Edwin van den Oetelaar)
* 1.0.2 (2011) - Adapted code for new strmap API
*
* settings.c
*
* Copyright (c) 2009-2011 Per Ola Kristensson.
*
* Per Ola Kristensson <pok21@cam.ac.uk>
* Inference Group, Department of Physics
* University of Cambridge
* Cavendish Laboratory
* JJ Thomson Avenue
* CB3 0HE Cambridge
* United Kingdom
*
* settings is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* settings is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with settings. If not, see <http://www.gnu.org/licenses/>.
*/
#include "settings.h"
#define MAX_SECTIONCHARS 256
#define MAX_KEYCHARS 256
#define MAX_VALUECHARS 256
#define MAX_LINECHARS (MAX_KEYCHARS + MAX_VALUECHARS + 10)
#define COMMENT_CHAR '#'
#define SECTION_START_CHAR '['
#define SECTION_END_CHAR ']'
#define KEY_VALUE_SEPARATOR_CHAR '='
#define DEFAULT_STRMAP_CAPACITY 256
typedef struct Section Section;
typedef struct ParseState ParseState;
typedef enum ConvertMode ConvertMode;
struct Settings {
Section *sections;
unsigned int section_count;
};
struct Section {
char *name;
StrMap *map;
};
struct ParseState {
char *current_section;
unsigned int current_section_n;
int has_section;
};
enum ConvertMode {
CONVERT_MODE_INT,
CONVERT_MODE_LONG,
CONVERT_MODE_DOUBLE,
};
static void trim_str(const char *str, char *out_buf);
static int parse_str(Settings *settings, char *str, ParseState *parse_state);
static int is_blank_char(char c);
static int is_blank_str(const char *str);
static int is_comment_str(const char *str);
static int is_section_str(const char *str);
static int is_key_value_str(const char *str);
static int is_key_without_value_str(const char *str);
static const char * get_token(char *str, char delim, char **last);
static int get_section_from_str(const char *str, char *out_buf, unsigned int out_buf_n);
static int get_key_value_from_str(const char *str, char *out_buf1, unsigned int out_buf1_n, char *out_buf2, unsigned int out_buf2_n);
static int get_key_without_value_from_str(const char *str, char *out_buf, unsigned int out_buf_n);
static int get_converted_value(const Settings *settings, const char *section, const char *key, ConvertMode mode, void *out);
static int get_converted_tuple(const Settings *settings, const char *section, const char *key, char delim, ConvertMode mode, void *out, unsigned int n_out);
static Section * get_section(Section *sections, unsigned int n, const char *name);
static void enum_map(const char *key, const char *value, const void *obj);
Settings * settings_new()
{
Settings *settings;
settings = malloc(sizeof(Settings));
if (settings == NULL) {
return NULL;
}
settings->section_count = 0;
settings->sections = NULL;
return settings;
}
void settings_delete(Settings *settings)
{
unsigned int i, n;
Section *section;
if (settings == NULL) {
return;
}
section = settings->sections;
n = settings->section_count;
i = 0;
while (i < n) {
sm_delete(section->map);
if (section->name != NULL) {
free(section->name);
}
section++;
i++;
}
free(settings->sections);
free(settings);
}
Settings * settings_open(FILE *stream)
{
Settings *settings;
char buf[MAX_LINECHARS];
char trimmed_buf[MAX_LINECHARS];
char section_buf[MAX_LINECHARS];
ParseState parse_state;
if (stream == NULL) {
return NULL;
}
settings = settings_new();
if (settings == NULL) {
return NULL;
}
parse_state.current_section = section_buf;
parse_state.current_section_n = sizeof(section_buf);
parse_state.has_section = 0;
trim_str("", trimmed_buf);
while (fgets(buf, MAX_LINECHARS, stream) != NULL) {
trim_str(buf, trimmed_buf);
if (!parse_str(settings, trimmed_buf, &parse_state)) {
return NULL;
}
}
return settings;
}
int settings_save(const Settings *settings, FILE *stream)
{
unsigned int i, n;
Section *section;
char buf[MAX_LINECHARS];
if (settings == NULL) {
return 0;
}
if (stream == NULL) {
return 0;
}
section = settings->sections;
n = settings->section_count;
i = 0;
while (i < n) {
sprintf(buf, "[%s]\n", section->name);
fputs(buf, stream);
sm_enum(section->map, enum_map, stream);
section++;
i++;
fputs("\n", stream);
}
return 0;
}
int settings_get(const Settings *settings, const char *section, const char *key, char *out_buf, unsigned int n_out_buf)
{
Section *s;
if (settings == NULL) {
return 0;
}
s = get_section(settings->sections, settings->section_count, section);
if (s == NULL) {
return 0;
}
return sm_get(s->map, key, out_buf, n_out_buf);
}
int settings_get_int(const Settings *settings, const char *section, const char *key)
{
int i;
if (get_converted_value(settings, section, key, CONVERT_MODE_INT, &i)) {
return i;
}
return 0;
}
long settings_get_long(const Settings *settings, const char *section, const char *key)
{
long l;
if (get_converted_value(settings, section, key, CONVERT_MODE_LONG, &l)) {
return l;
}
return 0;
}
double settings_get_double(const Settings *settings, const char *section, const char *key)
{
double d;
if (get_converted_value(settings, section, key, CONVERT_MODE_DOUBLE, &d)) {
return d;
}
return 0;
}
int settings_get_int_tuple(const Settings *settings, const char *section, const char *key, int *out, unsigned int n_out)
{
return get_converted_tuple(settings, section, key, ',', CONVERT_MODE_INT, out, n_out);
}
long settings_get_long_tuple(const Settings *settings, const char *section, const char *key, long *out, unsigned int n_out)
{
return get_converted_tuple(settings, section, key, ',', CONVERT_MODE_LONG, out, n_out);
}
double settings_get_double_tuple(const Settings *settings, const char *section, const char *key, double *out, unsigned int n_out)
{
return get_converted_tuple(settings, section, key, ',', CONVERT_MODE_DOUBLE, out, n_out);
}
int settings_set(Settings *settings, const char *section, const char *key, const char *value)
{
Section *s;
if (settings == NULL) {
return 0;
}
if (section == NULL || key == NULL || value == NULL) {
return 0;
}
if (strlen(section) == 0) {
return 0;
}
/* Get a pointer to the section */
s = get_section(settings->sections, settings->section_count, section);
if (s == NULL) {
/* The section is not created---create it */
s = realloc(settings->sections, (settings->section_count + 1) * sizeof(Section));
if (s == NULL) {
return 0;
}
settings->sections = s;
settings->section_count++;
s = &(settings->sections[settings->section_count - 1]);
s->map = sm_new(DEFAULT_STRMAP_CAPACITY);
if (s->map == NULL) {
free(s);
return 0;
}
s->name = malloc((strlen(section) + 1) * sizeof(char));
if (s->name == NULL) {
sm_delete(s->map);
free(s);
return 0;
}
strcpy(s->name, section);
}
return sm_put(s->map, key, value);
}
int settings_section_get_count(const Settings *settings, const char *section)
{
Section *sect;
if (settings == NULL) {
return 0;
}
sect = get_section(settings->sections, settings->section_count, section);
if (sect == NULL) {
return 0;
}
return sm_get_count(sect->map);
}
int settings_section_enum(const Settings *settings, const char *section, settings_section_enum_func enum_func, const void *obj)
{
Section *sect;
sect = get_section(settings->sections, settings->section_count, section);
if (sect == NULL) {
return 0;
}
return sm_enum(sect->map, enum_func, obj);
}
/* Copies a trimmed variant without leading and trailing blank characters
* of the input string into the output buffer. The output buffer is assumed
* to be large enough to contain the entire input string.
*/
static void trim_str(const char *str, char *out_buf)
{
unsigned int len;
const char *s0;
while (*str != '\0' && is_blank_char(*str)) {
str++;
}
s0 = str;
len = 0;
while (*str != '\0') {
len++;
str++;
}
if (len > 0) {
str--;
}
while (is_blank_char(*str)) {
str--;
len--;
}
memcpy(out_buf, s0, len);
out_buf[len] = '\0';
}
/* Parses a single input string and updates the provided settings object.
* The given parse state may be updated following a call. It is assumed this
* function is called in repeated succession for each input line read. The
* provided parse state should be initialized to the following before this
* function is called for the first time for an intended parse:
*
* parse_state->current_section: a pre-allocated character buffer this function
* can read and write to
* parse_state->current_section_n: sizeof(parse_state->current_section)
* parse_state->has_section: 0 (false)
*/
static int parse_str(Settings *settings, char *str, ParseState *parse_state)
{
char buf[MAX_LINECHARS];
char buf1[MAX_LINECHARS];
char buf2[MAX_LINECHARS];
int result;
if (*str == '\0') {
return 1;
}
else if (is_blank_str(str)) {
return 1;
}
else if (is_comment_str(str)) {
return 1;
}
else if (is_section_str(str)) {
result = get_section_from_str(str, buf, sizeof(buf));
if (!result) {
return 0;
}
if (strlen(buf) + 1 > parse_state->current_section_n) {
return 0;
}
strcpy(parse_state->current_section, buf);
parse_state->has_section = 1;
return 1;
}
else if (is_key_value_str(str)) {
result = get_key_value_from_str(str, buf1, sizeof(buf1), buf2, sizeof(buf2));
if (!result) {
return 0;
}
if (!parse_state->has_section) {
return 0;
}
return settings_set(settings, parse_state->current_section, buf1, buf2);
}
else if (is_key_without_value_str(str)) {
result = get_key_without_value_from_str(str, buf, sizeof(buf));
if (!result) {
return 0;
}
if (!parse_state->has_section) {
return 0;
}
return settings_set(settings, parse_state->current_section, buf, "");
}
else {
return 0;
}
}
/* Returns true if the input character is blank,
* false otherwise.
*/
static int is_blank_char(char c)
{
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
}
/* Returns true if the input string is blank,
* false otherwise.
*/
static int is_blank_str(const char *str)
{
while (*str != '\0') {
if (!is_blank_char(*str)) {
return 0;
}
str++;
}
return 1;
}
/* Returns true if the input string denotes a comment,
* false otherwise.
*/
static int is_comment_str(const char *str)
{
if (*str == COMMENT_CHAR) {
/* To be a comment the first character must be the
* comment character.
*/
return 1;
}
return 0;
}
/* Returns true if the input string denotes a section name,
* false otherwise.
*/
static int is_section_str(const char *str)
{
if (*str != SECTION_START_CHAR) {
/* The first character must be the section start character */
return 0;
}
while (*str != '\0' && *str != SECTION_END_CHAR) {
str++;
}
if (*str != SECTION_END_CHAR) {
/* The section end character must be present somewhere thereafter */
return 0;
}
return 1;
}
/* Returns true if the input string denotes a key-value pair,
* false otherwise.
*/
static int is_key_value_str(const char *str)
{
if (*str == KEY_VALUE_SEPARATOR_CHAR) {
/* It is illegal to start with the key-value separator */
return 0;
}
while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) {
str++;
}
if (*str != KEY_VALUE_SEPARATOR_CHAR) {
/* The key-value separator must be present after the key part */
return 0;
}
return 1;
}
/* Returns true if the input string denotes a key without a value,
* false otherwise.
*/
static int is_key_without_value_str(const char *str)
{
if (*str == KEY_VALUE_SEPARATOR_CHAR) {
/* It is illegal to start with the key-value separator */
return 0;
}
while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) {
str++;
}
if (*str == KEY_VALUE_SEPARATOR_CHAR) {
/* The key-value separator must not be present after the key part */
return 0;
}
return 1;
}
/*
* Parses a section name from an input string. The input string is assumed to
* already have been identified as a valid input string denoting a section name.
*/
static int get_section_from_str(const char *str, char *out_buf, unsigned int out_buf_n)
{
unsigned int count;
count = 0;
/* Jump past the section begin character */
str++;
while (*str != '\0' && *str != SECTION_END_CHAR) {
/* Read in the section name into the output buffer */
if (count == out_buf_n) {
return 0;
}
*out_buf = *str;
out_buf++;
str++;
count++;
}
/* Terminate the output buffer */
if (count == out_buf_n) {
return 0;
}
*out_buf = '\0';
return 1;
}
/*
* Parses a key and value from an input string. The input string is assumed to
* already have been identified as a valid input string denoting a key-value pair.
*/
static int get_key_value_from_str(const char *str, char *out_buf1, unsigned int out_buf1_n, char *out_buf2, unsigned int out_buf2_n)
{
unsigned int count1;
unsigned int count2;
count1 = 0;
count2 = 0;
/* Read the key value from the input string and write it sequentially
* to the first output buffer by walking the input string until we either hit
* the null-terminator or the key-value separator.
*/
while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) {
/* Ensure the first output buffer is large enough. */
if (count1 == out_buf1_n) {
return 0;
}
/* Copy the character to the first output buffer */
*out_buf1 = *str;
out_buf1++;
str++;
count1++;
}
/* Terminate the first output buffer */
if (count1 == out_buf1_n) {
return 0;
}
*out_buf1 = '\0';
/* Now trace the first input buffer backwards until we hit a non-blank character */
while (is_blank_char(*(out_buf1 - 1))) {
out_buf1--;
}
*out_buf1 = '\0';
/* Try to proceed one more character, past the last read key-value
* delimiter, in the input string.
*/
if (*str != '\0') {
str++;
}
/* Now find start of the value in the input string by walking the input
* string until we either hit the null-terminator or a blank character.
*/
while (*str != '\0' && is_blank_char(*str)) {
str++;
}
while (*str != '\0') {
/* Fail if there is a possibility that we are overwriting the second
* input buffer.
*/
if (count2 == out_buf2_n) {
return 0;
}
/* Copy the character to the second output buffer */
*out_buf2 = *str;
out_buf2++;
str++;
count2++;
}
/* Terminate the second output buffer */
if (count2 == out_buf2_n) {
return 0;
}
*out_buf2 = '\0';
return 1;
}
/*
* Parses a key from an input string. The input string is assumed to already
* have been identified as a valid input string denoting a key without a value.
*/
static int get_key_without_value_from_str(const char *str, char *out_buf, unsigned int out_buf_n)
{
unsigned int count;
count = 0;
/* Now read the key value from the input string and write it sequentially
* to the output buffer by walking the input string until we either hit
* the null-terminator or the key-value separator.
*/
while (*str != '\0') {
/* Ensure the output buffer is large enough. */
if (count == out_buf_n) {
return 0;
}
/* Copy the character to the input buffer */
*out_buf = *str;
out_buf++;
str++;
count++;
}
/* Terminate the output buffer */
if (count == out_buf_n) {
return 0;
}
*out_buf = '\0';
return 1;
}
/* Returns a pointer to the next token in the input string delimited
* by the specified delimiter or null if no such token exist. The provided
* last pointer will be changed to point one position after the pointed
* token. The currently ouputted token will be null-terminated.
*
* An idiom for tokenizing a (in this case, comma-separated) string is:
*
* char test_string[] = "Token1,Token2,Token3";
* char token[255];
* char *str;
*
* str = test_string;
* while ((token = get_token(str, ',', &str) != NULL) {
* printf("token: %s", token);
* }
*/
static const char * get_token(char *str, char delim, char **last)
{
char *s0;
s0 = str;
/* If we hit the null-terminator the string
* is exhausted and another token does not
* exist.
*/
if (*str == '\0') {
return NULL;
}
/* Walk the string until we encounter a
* null-terminator or the delimiter.
*/
while (*str != '\0' && *str != delim) {
str++;
}
/* Terminate the return token, if necessary */
if (*str != '\0') {
*str = '\0';
str++;
}
*last = str;
return s0;
}
/* Returns a converted value pointed to by the provided key in the given section.
* The mode specifies which conversion takes place and dictates what value out
* is pointing to. The value out is pointing to will be replaced by the converted
* value assuming conversion is succesful. The function returns 1 if conversion
* is succsessful and 0 if the convertion could not be carried out.
*/
static int get_converted_value(const Settings *settings, const char *section, const char *key, ConvertMode mode, void *out)
{
char value[MAX_VALUECHARS];
if (!settings_get(settings, section, key, value, MAX_VALUECHARS)) {
return 0;
}
switch (mode) {
case CONVERT_MODE_INT:
*((int *)out) = atoi(value);
return 1;
case CONVERT_MODE_LONG:
*((long *)out) = atol(value);
return 1;
case CONVERT_MODE_DOUBLE:
*((double *)out) = atof(value);
return 1;
}
return 0;
}
/* Returns a converted tuple pointed to by the provided key in the given section.
* The tuple is created by splitting the value by the supplied delimiter and then
* converting each token after the split according to the specified mode.
* The array out is pointing to will be replaced by the converted tuple
* assuming conversion is succesful. The function returns 1 if conversion
* is succsessful and 0 if the convertion could not be carried out.
*/
static int get_converted_tuple(const Settings *settings, const char *section, const char *key, char delim, ConvertMode mode, void *out, unsigned int n_out)
{
unsigned int count;
const char *token;
static char value[MAX_VALUECHARS];
char *v;
if (out == NULL) {
return 0;
}
if (n_out == 0) {
return 0;
}
if (!settings_get(settings, section, key, value, MAX_VALUECHARS)) {
return 0;
}
v = value;
count = 0;
/* Walk over all tokens in the value, and convert them and assign them
* to the output array as specified by the mode.
*/
while ((token = get_token(v, delim, &v)) != NULL && count < n_out) {
switch (mode) {
case CONVERT_MODE_INT:
((int *)out)[count] = atoi(token);
break;
case CONVERT_MODE_LONG:
((long *)out)[count] = atol(token);
break;
case CONVERT_MODE_DOUBLE:
((double *)out)[count] = atof(token);
break;
default:
return 0;
}
count++;
}
return 1;
}
/* Returns a pointer to the section or null if the named section does not
* exist.
*/
static Section * get_section(Section *sections, unsigned int n, const char *name)
{
unsigned int i;
Section *section;
if (name == NULL) {
return NULL;
}
section = sections;
i = 0;
while (i < n) {
if (strcmp(section->name, name) == 0) {
return section;
}
section++;
i++;
}
return NULL;
}
/* Callback function that is passed into the enumeration function in the
* string map. It casts the passed into object into a FILE pointer and
* writes out the key and value to the file.
*/
static void enum_map(const char *key, const char *value, const void *obj)
{
FILE *stream;
char buf[MAX_LINECHARS];
if (key == NULL || value == NULL) {
return;
}
if (obj == NULL) {
return;
}
stream = (FILE *)obj;
if (strlen(key) < MAX_KEYCHARS && strlen(value) < MAX_VALUECHARS) {
sprintf(buf, "%s%c%s\n", key, KEY_VALUE_SEPARATOR_CHAR, value);
fputs(buf, stream);
}
}
/*
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
*/

507
src/settings.h Normal file
View file

@ -0,0 +1,507 @@
/*
* settings version 1.0.0
*
* ANSI C implementation for managing application settings.
*
* settings.h
*
* Copyright (c) 2009 Per Ola Kristensson.
*
* Per Ola Kristensson <pok21@cam.ac.uk>
* Inference Group, Department of Physics
* University of Cambridge
* Cavendish Laboratory
* JJ Thomson Avenue
* CB3 0HE Cambridge
* United Kingdom
*
* settings is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* settings is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with settings. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _SETTINGS_H_
#define _SETTINGS_H_
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "strmap.h"
#ifdef __cplusplus
extern "C"
{
#endif
typedef struct Settings Settings;
/*
* This callback function is called once per key-value when enumerating
* all keys inside a section.
*
* Parameters:
*
* key: A pointer to a null-terminated C string. The string must not
* be modified by the client.
*
* value: A pointer to a null-terminated C string. The string must
* not be modified by the client.
*
* obj: A pointer to a client-specific object. This parameter may be
* null.
*
* Return value: None.
*/
typedef void(*settings_section_enum_func)(const char *key, const char *value, const void *obj);
/*
* Creates a settings object.
*
* Return value: A pointer to a settings object,
* or null if a new settings object could not be allocated.
*/
Settings * settings_new();
/*
* Releases all memory held by a settings object.
*
* Parameters:
*
* settings: A pointer to a settings object. This parameter cannot be null.
* If the supplied settings object has been previously released, the
* behaviour of this function is undefined.
*
* Return value: None.
*/
void settings_delete(Settings *settings);
/*
* Constructs a settings object by loading settings in textual form
* from the given stream.
*
* Parameters:
*
* settings: A pointer to a settings object. This parameter cannot be null.
*
* stream: A pointer to a stream. This parameter cannot be null.
*
* Return value: A pointer to a settings object,
* or null if an error occurred.
*/
Settings * settings_open(FILE *stream);
/*
* Saves the current settings object in textual form to the given stream.
*
* Parameters:
*
* settings: A pointer to a settings object. This parameter cannot be null.
*
* stream: A pointer to a stream. This parameter cannot be null.
*
* Return value: 1 if the operation succeeded, 0 otherwise.
*/
int settings_save(const Settings *settings, FILE *stream);
/*
* Returns the value associated with the supplied key in the
* provided section.
*
* Parameters:
*
* settings: A pointer to a settings object. This parameter cannot be null.
*
* section: A pointer to a null-terminated C string. This parameter cannot
* be null.
*
* key: A pointer to a null-terminated C string. This parameter cannot
* be null.
*
* out_buf: A pointer to an output buffer which will contain the value,
* if it exists and fits into the buffer.
*
* n_out_buf: The size of the output buffer in bytes.
*
* Return value: If out_buf is set to null and n_out_buf is set to 0 the return
* value will be the number of bytes required to store the value (if it exists)
* and its null-terminator. For all other parameter configurations the return value
* is 1 if an associated value was found and completely copied into the output buffer,
* 0 otherwise.
*/
int settings_get(const Settings *settings, const char *section, const char *key, char *out_buf, unsigned int n_out_buf);
/*
* Returns the integer value associated with the supplied key in the
* provided section.
*
* Parameters:
*
* settings: A pointer to a settings object. This parameter cannot be null.
*
* section: A pointer to a null-terminated C string. This parameter cannot
* be null.
*
* key: A pointer to a null-terminated C string. This parameter cannot
* be null.
*
* Return value: The integer value associated to the provided section and
* key, or 0 if no such value exists.
*/
int settings_get_int(const Settings *settings, const char *section, const char *key);
/*
* Returns the long integer value associated with the supplied key in the
* provided section.
*
* Parameters:
*
* settings: A pointer to a settings object. This parameter cannot be null.
*
* section: A pointer to a null-terminated C string. This parameter cannot
* be null.
*
* key: A pointer to a null-terminated C string. This parameter cannot
* be null.
*
* Return value: The long integer value associated to the provided section and
* key, or 0 if no such value exists.
*/
long settings_get_long(const Settings *settings, const char *section, const char *key);
/*
* Returns the double value associated with the supplied key in the
* provided section.
*
* Parameters:
*
* settings: A pointer to a settings object. This parameter cannot be null.
*
* section: A pointer to a null-terminated C string. This parameter cannot
* be null.
*
* key: A pointer to a null-terminated C string. This parameter cannot
* be null.
*
* Return value: The double value associated to the provided section and
* key, or 0 if no such value exists.
*/
double settings_get_double(const Settings *settings, const char *section, const char *key);
/*
* Returns the integer tuple associated with the supplied key in the
* provided section.
*
* Parameters:
*
* settings: A pointer to a settings object. This parameter cannot be null.
*
* section: A pointer to a null-terminated C string. This parameter cannot
* be null.
*
* key: A pointer to a null-terminated C string. This parameter cannot
* be null.
*
* out: A pointer to an output buffer.
*
* n_out: The maximum number of elements the output buffer can hold.
*
* Return value: 1 if the entire tuple was copied into the output buffer,
* 0 otherwise.
*/
int settings_get_int_tuple(const Settings *settings, const char *section, const char *key, int *out, unsigned int n_out);
/*
* Returns the long tuple associated with the supplied key in the
* provided section.
*
* Parameters:
*
* settings: A pointer to a settings object. This parameter cannot be null.
*
* section: A pointer to a null-terminated C string. This parameter cannot
* be null.
*
* key: A pointer to a null-terminated C string. This parameter cannot
* be null.
*
* out: A pointer to an output buffer.
*
* n_out: The maximum number of elements the output buffer can hold.
*
* Return value: 1 if the entire tuple was copied into the output buffer,
* 0 otherwise.
*/
long settings_get_long_tuple(const Settings *settings, const char *section, const char *key, long *out, unsigned int n_out);
/*
* Returns the double tuple associated with the supplied key in the
* provided section.
*
* Parameters:
*
* settings: A pointer to a settings object. This parameter cannot be null.
*
* section: A pointer to a null-terminated C string. This parameter cannot
* be null.
*
* key: A pointer to a null-terminated C string. This parameter cannot
* be null.
*
* out: A pointer to an output buffer.
*
* n_out: The maximum number of elements the output buffer can hold.
*
* Return value: 1 if the entire tuple was copied into the output buffer,
* 0 otherwise.
*/
double settings_get_double_tuple(const Settings *settings, const char *section, const char *key, double *out, unsigned int n_out);
/*
* Associates a value with the supplied key in the provided section.
* If the key is already associated with a value, the previous value
* is replaced.
*
* Parameters:
*
* settings: A pointer to a settings object. This parameter cannot be null.
*
* section: A pointer to a null-terminated C string. This parameter cannot
* be null. The string must have a string length > 0. The string will
* be copied.
*
* key: A pointer to a null-terminated C string. This parameter
* cannot be null. The string must have a string length > 0. The
* string will be copied.
*
* value: A pointer to a null-terminated C string. This parameter
* cannot be null. The string must have a string length > 0. The
* string will be copied.
*
* Return value: 1 if the association succeeded, 0 otherwise.
*/
int settings_set(Settings *setting, const char *section, const char *key, const char *value);
/*
* Returns the number of associations between keys and values that exist
* in the provided section.
*
* Parameters:
*
* settings: A pointer to a settings object. This parameter cannot be null.
*
* section: A pointer to a null-terminated C string. This parameter cannot
* be null.
*
* Return value: The number of associations between keys and values in
* the provided section.
*/
int settings_section_get_count(const Settings *settings, const char *section);
/*
* Enumerates all associations between keys and values in the provided
* section.
*
* Parameters:
*
* settings: A pointer to a settings object. This parameter cannot be null.
*
* section: A pointer to a null-terminated C string. This parameter cannot
* be null.
*
* enum_func: A pointer to a callback function that will be
* called by this procedure once for every key associated
* with a value. This parameter cannot be null.
*
* obj: A pointer to a client-specific object. This parameter will be
* passed back to the client's callback function. This parameter can
* be null.
*
* Return value: 1 if enumeration completed, 0 otherwise.
*/
int settings_section_enum(const Settings *settings, const char *section, settings_section_enum_func enum_func, const void *obj);
#ifdef __cplusplus
}
#endif
#endif
/*
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
*/

514
src/strmap.c Normal file
View file

@ -0,0 +1,514 @@
/*
* strmap version 2.0.0
*
* ANSI C hash table for strings.
*
* Version history:
* 1.0.0 - initial release
* 2.0.0 - changed function prefix from strmap to sm to ensure
* ANSI C compatibility
*
* strmap.c
*
* Copyright (c) 2009, 2011 Per Ola Kristensson.
*
* Per Ola Kristensson <pok21@cam.ac.uk>
* Inference Group, Department of Physics
* University of Cambridge
* Cavendish Laboratory
* JJ Thomson Avenue
* CB3 0HE Cambridge
* United Kingdom
*
* strmap is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* strmap is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with strmap. If not, see <http://www.gnu.org/licenses/>.
*/
#include "strmap.h"
typedef struct Pair Pair;
typedef struct Bucket Bucket;
struct Pair {
char *key;
char *value;
};
struct Bucket {
unsigned int count;
Pair *pairs;
};
struct StrMap {
unsigned int count;
Bucket *buckets;
};
static Pair * get_pair(Bucket *bucket, const char *key);
static unsigned long hash(const char *str);
StrMap * sm_new(unsigned int capacity)
{
StrMap *map;
map = malloc(sizeof(StrMap));
if (map == NULL) {
return NULL;
}
map->count = capacity;
map->buckets = malloc(map->count * sizeof(Bucket));
if (map->buckets == NULL) {
free(map);
return NULL;
}
memset(map->buckets, 0, map->count * sizeof(Bucket));
return map;
}
void sm_delete(StrMap *map)
{
unsigned int i, j, n, m;
Bucket *bucket;
Pair *pair;
if (map == NULL) {
return;
}
n = map->count;
bucket = map->buckets;
i = 0;
while (i < n) {
m = bucket->count;
pair = bucket->pairs;
j = 0;
while(j < m) {
free(pair->key);
free(pair->value);
pair++;
j++;
}
free(bucket->pairs);
bucket++;
i++;
}
free(map->buckets);
free(map);
}
int sm_get(const StrMap *map, const char *key, char *out_buf, unsigned int n_out_buf)
{
unsigned int index;
Bucket *bucket;
Pair *pair;
if (map == NULL) {
return 0;
}
if (key == NULL) {
return 0;
}
index = hash(key) % map->count;
bucket = &(map->buckets[index]);
pair = get_pair(bucket, key);
if (pair == NULL) {
return 0;
}
if (out_buf == NULL && n_out_buf == 0) {
return strlen(pair->value) + 1;
}
if (out_buf == NULL) {
return 0;
}
if (strlen(pair->value) >= n_out_buf) {
return 0;
}
strcpy(out_buf, pair->value);
return 1;
}
int sm_exists(const StrMap *map, const char *key)
{
unsigned int index;
Bucket *bucket;
Pair *pair;
if (map == NULL) {
return 0;
}
if (key == NULL) {
return 0;
}
index = hash(key) % map->count;
bucket = &(map->buckets[index]);
pair = get_pair(bucket, key);
if (pair == NULL) {
return 0;
}
return 1;
}
int sm_put(StrMap *map, const char *key, const char *value)
{
unsigned int key_len, value_len, index;
Bucket *bucket;
Pair *tmp_pairs, *pair;
char *tmp_value;
char *new_key, *new_value;
if (map == NULL) {
return 0;
}
if (key == NULL || value == NULL) {
return 0;
}
key_len = strlen(key);
value_len = strlen(value);
/* Get a pointer to the bucket the key string hashes to */
index = hash(key) % map->count;
bucket = &(map->buckets[index]);
/* Check if we can handle insertion by simply replacing
* an existing value in a key-value pair in the bucket.
*/
if ((pair = get_pair(bucket, key)) != NULL) {
/* The bucket contains a pair that matches the provided key,
* change the value for that pair to the new value.
*/
if (strlen(pair->value) < value_len) {
/* If the new value is larger than the old value, re-allocate
* space for the new larger value.
*/
tmp_value = realloc(pair->value, (value_len + 1) * sizeof(char));
if (tmp_value == NULL) {
return 0;
}
pair->value = tmp_value;
}
/* Copy the new value into the pair that matches the key */
strcpy(pair->value, value);
return 1;
}
/* Allocate space for a new key and value */
new_key = malloc((key_len + 1) * sizeof(char));
if (new_key == NULL) {
return 0;
}
new_value = malloc((value_len + 1) * sizeof(char));
if (new_value == NULL) {
free(new_key);
return 0;
}
/* Create a key-value pair */
if (bucket->count == 0) {
/* The bucket is empty, lazily allocate space for a single
* key-value pair.
*/
bucket->pairs = malloc(sizeof(Pair));
if (bucket->pairs == NULL) {
free(new_key);
free(new_value);
return 0;
}
bucket->count = 1;
}
else {
/* The bucket wasn't empty but no pair existed that matches the provided
* key, so create a new key-value pair.
*/
tmp_pairs = realloc(bucket->pairs, (bucket->count + 1) * sizeof(Pair));
if (tmp_pairs == NULL) {
free(new_key);
free(new_value);
return 0;
}
bucket->pairs = tmp_pairs;
bucket->count++;
}
/* Get the last pair in the chain for the bucket */
pair = &(bucket->pairs[bucket->count - 1]);
pair->key = new_key;
pair->value = new_value;
/* Copy the key and its value into the key-value pair */
strcpy(pair->key, key);
strcpy(pair->value, value);
return 1;
}
int sm_get_count(const StrMap *map)
{
unsigned int i, j, n, m;
unsigned int count;
Bucket *bucket;
Pair *pair;
if (map == NULL) {
return 0;
}
bucket = map->buckets;
n = map->count;
i = 0;
count = 0;
while (i < n) {
pair = bucket->pairs;
m = bucket->count;
j = 0;
while (j < m) {
count++;
pair++;
j++;
}
bucket++;
i++;
}
return count;
}
int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj)
{
unsigned int i, j, n, m;
Bucket *bucket;
Pair *pair;
if (map == NULL) {
return 0;
}
if (enum_func == NULL) {
return 0;
}
bucket = map->buckets;
n = map->count;
i = 0;
while (i < n) {
pair = bucket->pairs;
m = bucket->count;
j = 0;
while (j < m) {
enum_func(pair->key, pair->value, obj);
pair++;
j++;
}
bucket++;
i++;
}
return 1;
}
/*
* Returns a pair from the bucket that matches the provided key,
* or null if no such pair exist.
*/
static Pair * get_pair(Bucket *bucket, const char *key)
{
unsigned int i, n;
Pair *pair;
n = bucket->count;
if (n == 0) {
return NULL;
}
pair = bucket->pairs;
i = 0;
while (i < n) {
if (pair->key != NULL && pair->value != NULL) {
if (strcmp(pair->key, key) == 0) {
return pair;
}
}
pair++;
i++;
}
return NULL;
}
/*
* Returns a hash code for the provided string.
*/
static unsigned long hash(const char *str)
{
unsigned long hash = 5381;
int c;
while (c = *str++) {
hash = ((hash << 5) + hash) + c;
}
return hash;
}
/*
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
*/

355
src/strmap.h Normal file
View file

@ -0,0 +1,355 @@
/*
* strmap version 2.0.0
*
* ANSI C hash table for strings.
*
* Version history:
* 1.0.0 - initial release
* 2.0.0 - changed function prefix from strmap to sm to ensure
* ANSI C compatibility
*
* strmap.h
*
* Copyright (c) 2009, 2011 Per Ola Kristensson.
*
* Per Ola Kristensson <pok21@cam.ac.uk>
* Inference Group, Department of Physics
* University of Cambridge
* Cavendish Laboratory
* JJ Thomson Avenue
* CB3 0HE Cambridge
* United Kingdom
*
* strmap is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* strmap is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with strmap. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _STRMAP_H_
#define _STRMAP_H_
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdlib.h>
#include <string.h>
typedef struct StrMap StrMap;
/*
* This callback function is called once per key-value when enumerating
* all keys associated to values.
*
* Parameters:
*
* key: A pointer to a null-terminated C string. The string must not
* be modified by the client.
*
* value: A pointer to a null-terminated C string. The string must
* not be modified by the client.
*
* obj: A pointer to a client-specific object. This parameter may be
* null.
*
* Return value: None.
*/
typedef void(*sm_enum_func)(const char *key, const char *value, const void *obj);
/*
* Creates a string map.
*
* Parameters:
*
* capacity: The number of top-level slots this string map
* should allocate. This parameter must be > 0.
*
* Return value: A pointer to a string map object,
* or null if a new string map could not be allocated.
*/
StrMap * sm_new(unsigned int capacity);
/*
* Releases all memory held by a string map object.
*
* Parameters:
*
* map: A pointer to a string map. This parameter cannot be null.
* If the supplied string map has been previously released, the
* behaviour of this function is undefined.
*
* Return value: None.
*/
void sm_delete(StrMap *map);
/*
* Returns the value associated with the supplied key.
*
* Parameters:
*
* map: A pointer to a string map. This parameter cannot be null.
*
* key: A pointer to a null-terminated C string. This parameter cannot
* be null.
*
* out_buf: A pointer to an output buffer which will contain the value,
* if it exists and fits into the buffer.
*
* n_out_buf: The size of the output buffer in bytes.
*
* Return value: If out_buf is set to null and n_out_buf is set to 0 the return
* value will be the number of bytes required to store the value (if it exists)
* and its null-terminator. For all other parameter configurations the return value
* is 1 if an associated value was found and completely copied into the output buffer,
* 0 otherwise.
*/
int sm_get(const StrMap *map, const char *key, char *out_buf, unsigned int n_out_buf);
/*
* Queries the existence of a key.
*
* Parameters:
*
* map: A pointer to a string map. This parameter cannot be null.
*
* key: A pointer to a null-terminated C string. This parameter cannot
* be null.
*
* Return value: 1 if the key exists, 0 otherwise.
*/
int sm_exists(const StrMap *map, const char *key);
/*
* Associates a value with the supplied key. If the key is already
* associated with a value, the previous value is replaced.
*
* Parameters:
*
* map: A pointer to a string map. This parameter cannot be null.
*
* key: A pointer to a null-terminated C string. This parameter
* cannot be null. The string must have a string length > 0. The
* string will be copied.
*
* value: A pointer to a null-terminated C string. This parameter
* cannot be null. The string must have a string length > 0. The
* string will be copied.
*
* Return value: 1 if the association succeeded, 0 otherwise.
*/
int sm_put(StrMap *map, const char *key, const char *value);
/*
* Returns the number of associations between keys and values.
*
* Parameters:
*
* map: A pointer to a string map. This parameter cannot be null.
*
* Return value: The number of associations between keys and values.
*/
int sm_get_count(const StrMap *map);
/*
* Enumerates all associations between keys and values.
*
* Parameters:
*
* map: A pointer to a string map. This parameter cannot be null.
*
* enum_func: A pointer to a callback function that will be
* called by this procedure once for every key associated
* with a value. This parameter cannot be null.
*
* obj: A pointer to a client-specific object. This parameter will be
* passed back to the client's callback function. This parameter can
* be null.
*
* Return value: 1 if enumeration completed, 0 otherwise.
*/
int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj);
#ifdef __cplusplus
}
#endif
#endif
/*
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
*/