Reformat and normalize licenses and headerss

This commit is purely aestetic/taste, nothing important.

I don't really like this *huge* license at the top of every file
but I guess it must stay so add it to some files where it wasn't
included so they are the same. Slightly reformat them.

Unindent extra indentation found in some headers.

Strip trailing whitespace in files and enabled setting in
editor, which caused a lot of noise in this commit but that should
be the end of it.

Remove the comment at the top of each file that said who created
it and when (it will remain in VCS history for reference and was
always Don who created the files).
This commit is contained in:
Matthew Brush 2013-03-04 15:38:21 -08:00
parent eb89c4bf8e
commit 01d2b274b7
16 changed files with 1036 additions and 983 deletions

View File

@ -1,43 +1,40 @@
/* Creation date: 2005-07-09 21:44:59
* Authors: Don
* Change log:
/*
* cfu.c - This file is part of the libcfu library
*
* Copyright (c) 2005 Don Owens. All rights reserved.
*
* This code is released under the BSD license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* * Neither the name of the author nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Copyright (c) 2005 Don Owens
All rights reserved.
This code is released under the BSD license:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cfu.h"
struct libcfu_item {

View File

@ -1,43 +1,40 @@
/* Creation date: 2005-07-06 07:34:51
* Authors: Don
* Change log:
/*
* cfu.h - This file is part of the libcfu library
*
* Copyright (c) 2005 Don Owens. All rights reserved.
*
* This code is released under the BSD license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* * Neither the name of the author nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Copyright (c) 2005 Don Owens
All rights reserved.
This code is released under the BSD license:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _LIBCFU_H_
#define _LIBCFU_H_

View File

@ -1,44 +1,40 @@
/* Creation date: 2005-07-08 22:23:31
* Authors: Don
* Change log:
/*
* cfuconf.c - This file is part of the libcfu library
*
* Copyright (c) 2005 Don Owens. All rights reserved.
*
* This code is released under the BSD license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* * Neither the name of the author nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Copyright (c) 2005 Don Owens
All rights reserved.
This code is released under the BSD license:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cfu.h"
#include "cfuconf.h"
@ -68,7 +64,7 @@ cfuconf_new() {
conf->type = libcfu_t_conf;
conf->containers = cfuhash_new_with_flags(CFUHASH_IGNORE_CASE);
conf->directives = cfuhash_new_with_flags(CFUHASH_IGNORE_CASE);
return conf;
}
@ -106,7 +102,7 @@ _directive_free_val_list_fn(void *data) {
cfulist_t *val_list = (cfulist_t *)data;
assert(cfu_is_list(val_list));
cfulist_destroy_with_free_fn(val_list, _free_val_fn);
cfulist_destroy_with_free_fn(val_list, _free_val_fn);
}
static void
@ -153,7 +149,7 @@ _get_directive_last_val_list(cfuconf_t *conf, char *directive, cfulist_t **val_l
if (!conf) {
return -1;
}
directives = cfuconf_get_directives(conf);
if (!directives) {
return -1;
@ -168,7 +164,7 @@ _get_directive_last_val_list(cfuconf_t *conf, char *directive, cfulist_t **val_l
return 0;
}
}
return -1;
}
@ -186,7 +182,7 @@ cfuconf_get_directive_one_arg(cfuconf_t *conf, char *directive, char **rvalue) {
} else {
return -1;
}
return -1;
}
@ -218,9 +214,9 @@ cfuconf_get_directive_n_args(cfuconf_t *conf, char *directive, size_t n, ...) {
}
}
va_end(ap);
if (i == n) rv = 0;
}
return rv;
@ -261,7 +257,7 @@ static cfulist_t *
_slurp_file_from_fp(FILE *fp) {
cfulist_t *lines = cfulist_new();
char *buf = NULL;
while ( (buf = cfuconf_get_line(fp)) ) {
cfulist_push(lines, buf);
}
@ -302,7 +298,7 @@ _get_name(char **buf) {
while (*ptr && *ptr != '>' && !_is_whitespace(*ptr)) ptr++;
name = cfustring_dup_c_str_n(name, ptr - name);
*buf = ptr;
return name;
}
@ -320,7 +316,7 @@ _get_value(char **buf) {
while (*ptr && *ptr != '>' && !_is_whitespace(*ptr)) ptr++;
value = cfustring_dup_c_str_n(value, ptr - value);
*buf = ptr;
return value;
}
@ -332,7 +328,7 @@ _dup_c_str_n_drop_escape(const char *str, size_t n, char escape) {
char *ns_ptr = NULL;
char last_char = '\000';
char *end = (char *)str + n;
if (n == 0) return NULL;
ptr = (char *)str;
@ -388,7 +384,7 @@ _get_quoted_value(char **buf, char quote) {
}
if (!*ptr) return NULL;
*buf = ptr;
if (found_escape) {
value = _dup_c_str_n_drop_escape(value, ptr - value, '\\');
} else {
@ -418,7 +414,7 @@ _get_next_value(char **buf) {
}
*buf = ptr;
return value;
}
@ -439,7 +435,7 @@ _get_long_value(char **buf) {
value = cfustring_dup_c_str_n(value, ptr - value - 1);
*buf = ptr;
return value;
}
#endif
@ -469,7 +465,7 @@ _cfuconf_parse_list(cfulist_t *lines, char **error) {
free(line);
continue;
}
if (*ptr == '<') { /* opening or closing container tag */
ptr++;
if (*ptr == '/') {
@ -536,7 +532,7 @@ _cfuconf_parse_list(cfulist_t *lines, char **error) {
this_hash = cfuhash_new_with_flags(CFUHASH_IGNORE_CASE);
cfuhash_put(cur_conf->containers, name, (void *)this_hash);
}
if ( !(new_conf = cfuhash_get(this_hash, value)) ) {
new_conf = cfuconf_new();
new_conf->container_type = name;
@ -546,7 +542,7 @@ _cfuconf_parse_list(cfulist_t *lines, char **error) {
free(name); name = NULL;
free(value); value = NULL;
}
cfulist_push(stack, (void *)cur_conf);
cur_conf = new_conf;
@ -568,10 +564,10 @@ _cfuconf_parse_list(cfulist_t *lines, char **error) {
cfuhash_put(cur_conf->directives, name, (void *)list);
}
free(name); name = NULL;
val_list = cfulist_new();
cfulist_enqueue(list, (void *)val_list);
/* fprintf(stderr, "====> got '%s' =>", name); */
while ( (value = _get_next_value(&ptr) ) ) {
/* fprintf(stderr, " '%s'", value); */
@ -608,7 +604,7 @@ cfuconf_parse_file(char *file_path, cfuconf_t **ret_conf, char **error) {
lines = _slurp_file_from_fp(fp);
fclose(fp); fp = NULL;
if (!lines) return -1;
conf = _cfuconf_parse_list(lines, error);
cfulist_destroy(lines);
*ret_conf = conf;
@ -628,11 +624,11 @@ cfuconf_parse_buffer(char *buffer, cfuconf_t **ret_conf, char **error) {
strings = cfustring_c_str_split(buffer, &num_strings, 0, "\r\n", "\n", NULL);
if (!strings) return -1;
for (i = 0; i < num_strings; i++) {
cfulist_push_string(lines, strings[i]);
}
conf = _cfuconf_parse_list(lines, error);
cfulist_destroy(lines);
*ret_conf = conf;
@ -640,7 +636,7 @@ cfuconf_parse_buffer(char *buffer, cfuconf_t **ret_conf, char **error) {
free(strings);
if (conf) return 0;
return -1;
return -1;
}
@ -699,7 +695,7 @@ print_container(cfuhash_table_t *conf_hash, size_t depth) {
container = cfuhash_get(ht, container_name);
print_container(container, depth + 1);
}
free(container_names);
}
free(keys[i]);
@ -752,7 +748,7 @@ print_directive_list_foreach_fn(void *data, size_t data_size, void *arg) {
cfulist_t *val_list = (cfulist_t *)data;
directive_foreach_ds *ds = (directive_foreach_ds *)arg;
char *str = NULL;
data_size = data_size;
if (!val_list) return 0;
assert(cfu_is_list(val_list));
@ -771,14 +767,14 @@ print_conf_foreach_directive(void *name, size_t key_size, void *data, size_t dat
key_size = key_size;
data_size = data_size;
new_ds->fp = ds->fp;
new_ds->depth = ds->depth;
new_ds->name = name;
assert(cfu_is_list(this_directive_list));
cfulist_foreach(this_directive_list, print_directive_list_foreach_fn, new_ds);
free(new_ds);
return 0;
}

View File

@ -1,43 +1,40 @@
/* Creation date: 2005-07-08 22:24:03
* Authors: Don
* Change log:
/*
* cfuconf.h - This file is part of the libcfu library
*
* Copyright (c) 2005 Don Owens. All rights reserved.
*
* This code is released under the BSD license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* * Neither the name of the author nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Copyright (c) 2005 Don Owens
All rights reserved.
This code is released under the BSD license:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _CFU_CONF_H_
#define _CFU_CONF_H_
@ -48,72 +45,72 @@
CFU_BEGIN_DECLS
struct cfuconf;
typedef struct cfuconf cfuconf_t;
/* Apache-style conf files contain directives and containers.
Directives are simple one line specifications with or without
arguments, e.g.,
struct cfuconf;
typedef struct cfuconf cfuconf_t;
Doit
Expires On
LoadModule my_mod modules/my_mod.so
/* Apache-style conf files contain directives and containers.
Directives are simple one line specifications with or without
arguments, e.g.,
Containers have a type and a name associated with them and they
in turn contain directives and/or containers, e.g.,
Doit
Expires On
LoadModule my_mod modules/my_mod.so
<MyContainer test1>
Expires Off
<DB devdb>
DBHost db.example.com
DBUser test_user
</DB>
</MyContainer>
Containers have a type and a name associated with them and they
in turn contain directives and/or containers, e.g.,
Values may be quoted, e.g.
<MyContainer test1>
Expires Off
<DB devdb>
DBHost db.example.com
DBUser test_user
</DB>
</MyContainer>
DBUser "test user"
Values may be quoted, e.g.
But must be specified on a single line. To escape quotes
within a quoted string, use the '\' character.
DBUser "test user"
*/
But must be specified on a single line. To escape quotes
within a quoted string, use the '\' character.
/* Parse the apache-like conf file specified by file_path,
* returning a pointer to a cfuconf_t structure in conf. Returns
* zero on success, less than zero on error. If an error occurs
* and error is not NULL, it will be set to an error message
* (which must be free()'d by the caller).
*/
extern int cfuconf_parse_file(char *file_path, cfuconf_t **conf, char **error);
*/
/* Same as cfuconf_parse_file(), except assume the contents of the
* file are already in buffer.
*/
extern int cfuconf_parse_buffer(char *buffer, cfuconf_t **conf, char **error);
/* Free all resources used by the cfuconf_t structure */
extern void cfuconf_destroy(cfuconf_t *conf);
/* Parse the apache-like conf file specified by file_path,
* returning a pointer to a cfuconf_t structure in conf. Returns
* zero on success, less than zero on error. If an error occurs
* and error is not NULL, it will be set to an error message
* (which must be free()'d by the caller).
*/
extern int cfuconf_parse_file(char *file_path, cfuconf_t **conf, char **error);
/* Get a hash of containers at the top level of conf */
extern cfuhash_table_t * cfuconf_get_containers(cfuconf_t *conf);
/* Same as cfuconf_parse_file(), except assume the contents of the
* file are already in buffer.
*/
extern int cfuconf_parse_buffer(char *buffer, cfuconf_t **conf, char **error);
/* Get a hash of directives at the to level */
extern cfuhash_table_t * cfuconf_get_directives(cfuconf_t *conf);
/* Free all resources used by the cfuconf_t structure */
extern void cfuconf_destroy(cfuconf_t *conf);
/* Get the value of the given directive, assuming there is only one argument */
extern int cfuconf_get_directive_one_arg(cfuconf_t *conf, char *directive, char **rvalue);
/* Get a hash of containers at the top level of conf */
extern cfuhash_table_t * cfuconf_get_containers(cfuconf_t *conf);
/* Get the value of the given directive, assuming there are two arguments */
extern int cfuconf_get_directive_two_args(cfuconf_t *conf, char *directive, char **rvalue,
char **rvalue2);
/* Get a hash of directives at the to level */
extern cfuhash_table_t * cfuconf_get_directives(cfuconf_t *conf);
/* Get the value of the given directives, with n arguments */
extern int cfuconf_get_directive_n_args(cfuconf_t *conf, char *directive, size_t n, ...);
/* Get the value of the given directive, assuming there is only one argument */
extern int cfuconf_get_directive_one_arg(cfuconf_t *conf, char *directive, char **rvalue);
/* Print out a representation of the parsed configuration */
extern void cfuconf_pretty_print_conf(cfuconf_t *conf, FILE *fp, size_t indent_level);
/* Get the value of the given directive, assuming there are two arguments */
extern int cfuconf_get_directive_two_args(cfuconf_t *conf, char *directive, char **rvalue,
char **rvalue2);
/* Get the value of the given directives, with n arguments */
extern int cfuconf_get_directive_n_args(cfuconf_t *conf, char *directive, size_t n, ...);
/* Print out a representation of the parsed configuration */
extern void cfuconf_pretty_print_conf(cfuconf_t *conf, FILE *fp, size_t indent_level);
CFU_END_DECLS

View File

@ -1,43 +1,40 @@
/* Creation date: 2005-06-24 21:22:40
* Authors: Don
* Change log:
/*
* cfuhash.c - This file is part of the libcfu library
*
* Copyright (c) 2005 Don Owens. All rights reserved.
*
* This code is released under the BSD license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* * Neither the name of the author nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Copyright (c) 2005 Don Owens
All rights reserved.
This code is released under the BSD license:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
@ -155,7 +152,7 @@ hash_value(cfuhash_table_t *ht, const void *key, size_t key_size, size_t num_buc
static cfuhash_table_t *
_cfuhash_new(size_t size, u_int32_t flags) {
cfuhash_table_t *ht;
size = hash_size(size);
ht = (cfuhash_table_t *)malloc(sizeof(cfuhash_table_t));
memset(ht, '\000', sizeof(cfuhash_table_t));
@ -169,11 +166,11 @@ _cfuhash_new(size_t size, u_int32_t flags) {
#ifdef HAVE_PTHREAD_H
pthread_mutex_init(&ht->mutex, NULL);
#endif
ht->hash_func = hash_func;
ht->high = 0.75;
ht->low = 0.25;
return ht;
}
@ -231,7 +228,7 @@ cfuhash_merge(cfuhash_table_t *ht1, cfuhash_table_t *ht2, u_int32_t flags) {
new_ht = _cfuhash_new(cfuhash_num_entries(ht1) + cfuhash_num_entries(ht2), flags);
if (ht1) cfuhash_copy(ht1, new_ht);
if (ht2) cfuhash_copy(ht2, new_ht);
return new_ht;
}
@ -274,7 +271,7 @@ extern int
cfuhash_set_hash_function(cfuhash_table_t *ht, cfuhash_function_t hf) {
/* can't allow changing the hash function if the hash already contains entries */
if (ht->entries) return -1;
ht->hash_func = hf ? hf : hash_func;
return 0;
}
@ -368,7 +365,7 @@ cfuhash_get_data(cfuhash_table_t *ht, const void *key, size_t key_size, void **r
if (key_size == (size_t)(-1)) {
if (key) key_size = strlen(key) + 1;
else key_size = 0;
}
lock_hash(ht);
@ -386,7 +383,7 @@ cfuhash_get_data(cfuhash_table_t *ht, const void *key, size_t key_size, void **r
}
unlock_hash(ht);
return (hr ? 1 : 0);
}
@ -397,7 +394,7 @@ extern void *
cfuhash_get(cfuhash_table_t *ht, const char *key) {
void *r = NULL;
int rv = 0;
rv = cfuhash_get_data(ht, (const void *)key, -1, &r, NULL);
if (rv) return r; /* found */
return NULL;
@ -430,7 +427,7 @@ cfuhash_put_data(cfuhash_table_t *ht, const void *key, size_t key_size, void *da
u_int hv = 0;
cfuhash_entry *he = NULL;
int added_an_entry = 0;
if (key_size == (size_t)(-1)) {
if (key) key_size = strlen(key) + 1;
else key_size = 0;
@ -438,7 +435,7 @@ cfuhash_put_data(cfuhash_table_t *ht, const void *key, size_t key_size, void *da
if (data_size == (size_t)(-1)) {
if (data) data_size = strlen(data) + 1;
else data_size = 0;
}
lock_hash(ht);
@ -461,7 +458,7 @@ cfuhash_put_data(cfuhash_table_t *ht, const void *key, size_t key_size, void *da
added_an_entry = 1;
}
unlock_hash(ht);
unlock_hash(ht);
if (added_an_entry && !(ht->flags & CFUHASH_FROZEN)) {
if ( (float)ht->entries/(float)ht->num_buckets > ht->high ) cfuhash_rehash(ht);
@ -579,7 +576,7 @@ cfuhash_keys_data(cfuhash_table_t *ht, size_t *num_keys, size_t **key_sizes, int
if (key_sizes) key_lengths = (size_t *)calloc(ht->entries, sizeof(size_t));
keys = (void **)calloc(ht->entries, sizeof(void *));
for (bucket = 0; bucket < ht->num_buckets; bucket++) {
if ( (he = ht->buckets[bucket]) ) {
for (; he; he = he->next, entry_index++) {
@ -624,7 +621,7 @@ cfuhash_each_data(cfuhash_table_t *ht, void **key, size_t *key_size, void **data
extern int
cfuhash_next_data(cfuhash_table_t *ht, void **key, size_t *key_size, void **data,
size_t *data_size) {
if (ht->each_chain_entry && ht->each_chain_entry->next) {
ht->each_chain_entry = ht->each_chain_entry->next;
} else {
@ -672,7 +669,7 @@ cfuhash_foreach_remove(cfuhash_table_t *ht, cfuhash_remove_fn_t r_fn, cfuhash_fr
size_t num_removed = 0;
cfuhash_entry **buckets = NULL;
size_t num_buckets = 0;
if (!ht) return 0;
lock_hash(ht);
@ -716,7 +713,7 @@ cfuhash_foreach(cfuhash_table_t *ht, cfuhash_foreach_fn_t fe_fn, void *arg) {
cfuhash_entry **buckets = NULL;
size_t num_buckets = 0;
int rv = 0;
if (!ht) return 0;
lock_hash(ht);
@ -803,7 +800,7 @@ cfuhash_pretty_print(cfuhash_table_t *ht, FILE *fp) {
parg.count = 0;
rv += fprintf(fp, "{\n");
cfuhash_foreach(ht, _pretty_print_foreach, (void *)&parg);
rv += parg.count;

View File

@ -1,43 +1,40 @@
/* Creation date: 2005-06-24 21:22:09
* Authors: Don
* Change log:
/*
* cfuhash.h - This file is part of the libcfu library
*
* Copyright (c) 2005 Don Owens. All rights reserved.
*
* This code is released under the BSD license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* * Neither the name of the author nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Copyright (c) 2005 Don Owens
All rights reserved.
This code is released under the BSD license:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _CFU_HASH_H_
#define _CFU_HASH_H_
@ -47,215 +44,215 @@
CFU_BEGIN_DECLS
/* The hash table itself. */
struct cfuhash_table;
typedef struct cfuhash_table cfuhash_table_t;
/* The hash table itself. */
struct cfuhash_table;
typedef struct cfuhash_table cfuhash_table_t;
/* Prototype for a pointer to a hashing function. */
typedef u_int32_t (*cfuhash_function_t)(const void *key, size_t length);
/* Prototype for a pointer to a free function. */
typedef void (*cfuhash_free_fn_t)(void *data);
/* Prototype for a pointer to a function that determines whether
* or not to remove an entry from the hash.
*/
typedef int (*cfuhash_remove_fn_t)(void *key, size_t key_size, void *data, size_t data_size,
void *arg);
/* Prototype for a pointer to a hashing function. */
typedef u_int32_t (*cfuhash_function_t)(const void *key, size_t length);
/* Prototype for a pointer to a function to be called foreach
* key/value pair in the hash by cfuhash_foreach(). Iteration
* stops if a non-zero value is returned.
*/
typedef int (*cfuhash_foreach_fn_t)(void *key, size_t key_size, void *data, size_t data_size,
void *arg);
/* Creates a new hash table. */
extern cfuhash_table_t * cfuhash_new();
/* Prototype for a pointer to a free function. */
typedef void (*cfuhash_free_fn_t)(void *data);
/* Creates a new hash table with the specified size (number of
* buckets).
*/
extern cfuhash_table_t * cfuhash_new_with_initial_size(size_t size);
/* Prototype for a pointer to a function that determines whether
* or not to remove an entry from the hash.
*/
typedef int (*cfuhash_remove_fn_t)(void *key, size_t key_size, void *data, size_t data_size,
void *arg);
/* Creates a new hash table with the specified flags. Pass zero
* for flags if you want the defaults.
*/
extern cfuhash_table_t * cfuhash_new_with_flags(u_int32_t flags);
/* Same as cfuhash_new() except automatically calls cfuhash_set_free_fn(). */
extern cfuhash_table_t * cfuhash_new_with_free_fn(cfuhash_free_fn_t ff);
/* Prototype for a pointer to a function to be called foreach
* key/value pair in the hash by cfuhash_foreach(). Iteration
* stops if a non-zero value is returned.
*/
typedef int (*cfuhash_foreach_fn_t)(void *key, size_t key_size, void *data, size_t data_size,
void *arg);
/* Copies entries in src to dst */
extern int cfuhash_copy(cfuhash_table_t *src, cfuhash_table_t *dst);
/* Creates a new hash table. */
extern cfuhash_table_t * cfuhash_new();
/* Returns a new hash containing entries from both hash tables.
* For any entries with the same key, the one from ht2 wins.
*/
extern cfuhash_table_t * cfuhash_merge(cfuhash_table_t *ht1, cfuhash_table_t *ht2,
u_int32_t flags);
/* Creates a new hash table with the specified size (number of
* buckets).
*/
extern cfuhash_table_t * cfuhash_new_with_initial_size(size_t size);
/* Sets the hashing function to use when computing which bucket to add
* entries to. It should return a 32-bit unsigned integer. By
* default, Perl's hashing algorithm is used.
*/
extern int cfuhash_set_hash_function(cfuhash_table_t *ht, cfuhash_function_t hf);
/* Creates a new hash table with the specified flags. Pass zero
* for flags if you want the defaults.
*/
extern cfuhash_table_t * cfuhash_new_with_flags(u_int32_t flags);
/* Sets the thresholds for when to rehash. The ratio
* num_entries/buckets is compared against low and high. If it is
* below 'low' or above 'high', the hash will shrink or grow,
* respectively, unless the flags say to do otherwise.
*/
extern int cfuhash_set_thresholds(cfuhash_table_t *ht, float low, float high);
/* Same as cfuhash_new() except automatically calls cfuhash_set_free_fn(). */
extern cfuhash_table_t * cfuhash_new_with_free_fn(cfuhash_free_fn_t ff);
/* Sets the function to use when removing an entry from the hash,
* i.e., when replacing an existing entry, deleting an entry, or
* clearing the hash. It is passed the value of the entry as a
* void *.
*/
extern int cfuhash_set_free_function(cfuhash_table_t * ht, cfuhash_free_fn_t ff);
/* Copies entries in src to dst */
extern int cfuhash_copy(cfuhash_table_t *src, cfuhash_table_t *dst);
/* Returns the hash's flags. See below for flag definitions. */
extern u_int32_t cfuhash_get_flags(cfuhash_table_t *ht);
/* Returns a new hash containing entries from both hash tables.
* For any entries with the same key, the one from ht2 wins.
*/
extern cfuhash_table_t * cfuhash_merge(cfuhash_table_t *ht1, cfuhash_table_t *ht2,
u_int32_t flags);
/* Sets a flag. */
extern u_int32_t cfuhash_set_flag(cfuhash_table_t *ht, u_int32_t flag);
/* Sets the hashing function to use when computing which bucket to add
* entries to. It should return a 32-bit unsigned integer. By
* default, Perl's hashing algorithm is used.
*/
extern int cfuhash_set_hash_function(cfuhash_table_t *ht, cfuhash_function_t hf);
/* Clears a flag. */
extern u_int32_t cfuhash_clear_flag(cfuhash_table_t *ht, u_int32_t new_flag);
/* Sets the thresholds for when to rehash. The ratio
* num_entries/buckets is compared against low and high. If it is
* below 'low' or above 'high', the hash will shrink or grow,
* respectively, unless the flags say to do otherwise.
*/
extern int cfuhash_set_thresholds(cfuhash_table_t *ht, float low, float high);
/* Returns the value for the entry with given key. If key_size is -1,
* key is assumed to be a null-terminated string. If data_size is not
* NULL, the size of the value is placed into data_size.
*/
extern int cfuhash_get_data(cfuhash_table_t *ht, const void *key, size_t key_size,
void **data, size_t *data_size);
/* Sets the function to use when removing an entry from the hash,
* i.e., when replacing an existing entry, deleting an entry, or
* clearing the hash. It is passed the value of the entry as a
* void *.
*/
extern int cfuhash_set_free_function(cfuhash_table_t * ht, cfuhash_free_fn_t ff);
/* Returns 1 if an entry with the given key exists in the hash, 0 otherwise. */
extern int cfuhash_exists_data(cfuhash_table_t *ht, const void *key, size_t key_size);
/* Returns the hash's flags. See below for flag definitions. */
extern u_int32_t cfuhash_get_flags(cfuhash_table_t *ht);
/* Inserts the given data value into the hash and associates it with
* key. If key_size is -1, key is assumed to be a null-terminated
* string. If data_size is -1, it is assumed to be a null-terminated
* string (it's length will be calculated using strlen). If
* data_size is zero, it will be returned as zero when the value is
* requested.
*/
extern int cfuhash_put_data(cfuhash_table_t *ht, const void *key, size_t key_size, void *data,
size_t data_size, void **r);
/* Sets a flag. */
extern u_int32_t cfuhash_set_flag(cfuhash_table_t *ht, u_int32_t flag);
/* Clears the hash table (deletes all entries). */
extern void cfuhash_clear(cfuhash_table_t *ht);
/* Clears a flag. */
extern u_int32_t cfuhash_clear_flag(cfuhash_table_t *ht, u_int32_t new_flag);
/* Deletes the entry in the hash associated with key. If the entry
* existed, it's value will be returned.
*/
extern void * cfuhash_delete_data(cfuhash_table_t *ht, const void *key, size_t key_size);
/* Returns the value for the entry with given key. If key_size is -1,
* key is assumed to be a null-terminated string. If data_size is not
* NULL, the size of the value is placed into data_size.
*/
extern int cfuhash_get_data(cfuhash_table_t *ht, const void *key, size_t key_size,
void **data, size_t *data_size);
/* Returns all the keys from the hash. The number of keys is placed
* into the value pointed to by num_keys. If key_sizes is not NULL,
* it will be set to an array of key sizes. If fast is zero, copies
* of the keys are returned. Otherwise, pointers to the real keys
* will be returned.
*/
extern void **cfuhash_keys_data(cfuhash_table_t *ht, size_t *num_keys, size_t **key_sizes,
int fast);
/* Returns 1 if an entry with the given key exists in the hash, 0 otherwise. */
extern int cfuhash_exists_data(cfuhash_table_t *ht, const void *key, size_t key_size);
/* Initializes a loop over all the key/value pairs in the hash. It
* returns the first key/value pair (see cfuhash_next_data()). 1 is
* returned if there are any entries in the hash. 0 is returned
* otherwise.
*/
extern int cfuhash_each_data(cfuhash_table_t *ht, void **key, size_t *key_size, void **data,
size_t *data_size);
/* Inserts the given data value into the hash and associates it with
* key. If key_size is -1, key is assumed to be a null-terminated
* string. If data_size is -1, it is assumed to be a null-terminated
* string (it's length will be calculated using strlen). If
* data_size is zero, it will be returned as zero when the value is
* requested.
*/
extern int cfuhash_put_data(cfuhash_table_t *ht, const void *key, size_t key_size, void *data,
size_t data_size, void **r);
/* Gets the next key/value pair from the hash. You must initialize
* the loop using cfuhash_each_data() before calling this function.
* If a entry is left to return, 1 is returned from the function. 0
* is returned if there are no more entries in the hash.
*/
extern int cfuhash_next_data(cfuhash_table_t *ht, void **key, size_t *key_size, void **data,
size_t *data_size);
/* Clears the hash table (deletes all entries). */
extern void cfuhash_clear(cfuhash_table_t *ht);
/* Iterates over the key/value pairs in the hash, passing each one
* to r_fn, and removes all entries for which r_fn returns true.
* If ff is not NULL, it is the passed the data to be freed. arg
* is passed to r_fn.
*/
extern size_t cfuhash_foreach_remove(cfuhash_table_t *ht, cfuhash_remove_fn_t r_fn,
cfuhash_free_fn_t ff, void *arg);
/* Deletes the entry in the hash associated with key. If the entry
* existed, it's value will be returned.
*/
extern void * cfuhash_delete_data(cfuhash_table_t *ht, const void *key, size_t key_size);
/* Iterates over the key/value pairs in the hash, passing each one
* to fe_fn, along with arg. This locks the hash, so do not call
* any operations on the hash from within fe_fn unless you really
* know what you're doing. A non-zero return value from fe_fn()
* stops the iteration.
*/
extern size_t cfuhash_foreach(cfuhash_table_t *ht, cfuhash_foreach_fn_t fe_fn, void *arg);
/* Returns all the keys from the hash. The number of keys is placed
* into the value pointed to by num_keys. If key_sizes is not NULL,
* it will be set to an array of key sizes. If fast is zero, copies
* of the keys are returned. Otherwise, pointers to the real keys
* will be returned.
*/
extern void **cfuhash_keys_data(cfuhash_table_t *ht, size_t *num_keys, size_t **key_sizes,
int fast);
/* Frees all resources allocated by the hash. If ff is not NULL, it
* is called for each hash entry with the value of the entry passed as
* its only argument. If ff is not NULL, it overrides any function
* set previously with cfuhash_set_free_function().
*/
extern int cfuhash_destroy(cfuhash_table_t *ht);
/* Initializes a loop over all the key/value pairs in the hash. It
* returns the first key/value pair (see cfuhash_next_data()). 1 is
* returned if there are any entries in the hash. 0 is returned
* otherwise.
*/
extern int cfuhash_each_data(cfuhash_table_t *ht, void **key, size_t *key_size, void **data,
size_t *data_size);
extern int cfuhash_destroy_with_free_fn(cfuhash_table_t *ht, cfuhash_free_fn_t ff);
/* Gets the next key/value pair from the hash. You must initialize
* the loop using cfuhash_each_data() before calling this function.
* If a entry is left to return, 1 is returned from the function. 0
* is returned if there are no more entries in the hash.
*/
extern int cfuhash_next_data(cfuhash_table_t *ht, void **key, size_t *key_size, void **data,
size_t *data_size);
/* Rebuild the hash to better accomodate the number of entries. See
* cfuhash_set_thresholds().
*/
extern int cfuhash_rehash(cfuhash_table_t *ht);
/* Iterates over the key/value pairs in the hash, passing each one
* to r_fn, and removes all entries for which r_fn returns true.
* If ff is not NULL, it is the passed the data to be freed. arg
* is passed to r_fn.
*/
extern size_t cfuhash_foreach_remove(cfuhash_table_t *ht, cfuhash_remove_fn_t r_fn,
cfuhash_free_fn_t ff, void *arg);
/* Returns the number entries in the hash. */
extern size_t cfuhash_num_entries(cfuhash_table_t *ht);
/* Iterates over the key/value pairs in the hash, passing each one
* to fe_fn, along with arg. This locks the hash, so do not call
* any operations on the hash from within fe_fn unless you really
* know what you're doing. A non-zero return value from fe_fn()
* stops the iteration.
*/
extern size_t cfuhash_foreach(cfuhash_table_t *ht, cfuhash_foreach_fn_t fe_fn, void *arg);
/* Returns the number of buckets allocated for the hash. */
extern size_t cfuhash_num_buckets(cfuhash_table_t *ht);
/* Frees all resources allocated by the hash. If ff is not NULL, it
* is called for each hash entry with the value of the entry passed as
* its only argument. If ff is not NULL, it overrides any function
* set previously with cfuhash_set_free_function().
*/
extern int cfuhash_destroy(cfuhash_table_t *ht);
/* Returns the number of buckets actually used out of the total number
* allocated for the hash.
*/
extern size_t cfuhash_num_buckets_used(cfuhash_table_t *ht);
extern int cfuhash_destroy_with_free_fn(cfuhash_table_t *ht, cfuhash_free_fn_t ff);
/* Assumes all the keys and values are null-terminated strings and
* returns a bencoded string representing the hash (see
* http://www.bittorrent.com/protocol.html)
*/
extern char * cfuhash_bencode_strings(cfuhash_table_t *ht);
/* Rebuild the hash to better accomodate the number of entries. See
* cfuhash_set_thresholds().
*/
extern int cfuhash_rehash(cfuhash_table_t *ht);
/* Locks the hash. Use this with the each and next functions for
* concurrency control. Note that the hash is locked automatically
* when doing inserts and deletes, so if you lock the hash and then
* try to insert something into it, you may get into a deadlock,
* depending on your system defaults for how mutexes work.
*/
extern int cfuhash_lock(cfuhash_table_t *ht);
/* Returns the number entries in the hash. */
extern size_t cfuhash_num_entries(cfuhash_table_t *ht);
/* Unlocks the hash. Use this with the each an next functions for
* concurrency control. The caveat for cfuhash_lcok() also applies to
* this function.
*/
extern int cfuhash_unlock(cfuhash_table_t *ht);
/* Returns the number of buckets allocated for the hash. */
extern size_t cfuhash_num_buckets(cfuhash_table_t *ht);
/* Pretty print the hash's key/value pairs to the stream fp. It is
* assumed that all the keys and values are null-terminated strings.
*/
extern int cfuhash_pretty_print(cfuhash_table_t *ht, FILE *fp);
/* Returns the number of buckets actually used out of the total number
* allocated for the hash.
*/
extern size_t cfuhash_num_buckets_used(cfuhash_table_t *ht);
/* These are like the _data versions of these functions, with the
* following exceptions:
* 1) They assume that the key provided is a null-terminated string.
* 2) They don't worry about the size of the data.
* 3) Returned keys or values are the return value of the function.
*/
extern void * cfuhash_get(cfuhash_table_t *ht, const char *key);
extern int cfuhash_exists(cfuhash_table_t *ht, const char *key);
extern void * cfuhash_put(cfuhash_table_t *ht, const char *key, void *data);
extern void * cfuhash_delete(cfuhash_table_t *ht, const char *key);
extern int cfuhash_each(cfuhash_table_t *ht, char **key, void **data);
extern int cfuhash_next(cfuhash_table_t *ht, char **key, void **data);
extern void **cfuhash_keys(cfuhash_table_t *ht, size_t *num_keys, int fast);
/* Assumes all the keys and values are null-terminated strings and
* returns a bencoded string representing the hash (see
* http://www.bittorrent.com/protocol.html)
*/
extern char * cfuhash_bencode_strings(cfuhash_table_t *ht);
/* Locks the hash. Use this with the each and next functions for
* concurrency control. Note that the hash is locked automatically
* when doing inserts and deletes, so if you lock the hash and then
* try to insert something into it, you may get into a deadlock,
* depending on your system defaults for how mutexes work.
*/
extern int cfuhash_lock(cfuhash_table_t *ht);
/* Unlocks the hash. Use this with the each an next functions for
* concurrency control. The caveat for cfuhash_lcok() also applies to
* this function.
*/
extern int cfuhash_unlock(cfuhash_table_t *ht);
/* Pretty print the hash's key/value pairs to the stream fp. It is
* assumed that all the keys and values are null-terminated strings.
*/
extern int cfuhash_pretty_print(cfuhash_table_t *ht, FILE *fp);
/* These are like the _data versions of these functions, with the
* following exceptions:
* 1) They assume that the key provided is a null-terminated string.
* 2) They don't worry about the size of the data.
* 3) Returned keys or values are the return value of the function.
*/
extern void * cfuhash_get(cfuhash_table_t *ht, const char *key);
extern int cfuhash_exists(cfuhash_table_t *ht, const char *key);
extern void * cfuhash_put(cfuhash_table_t *ht, const char *key, void *data);
extern void * cfuhash_delete(cfuhash_table_t *ht, const char *key);
extern int cfuhash_each(cfuhash_table_t *ht, char **key, void **data);
extern int cfuhash_next(cfuhash_table_t *ht, char **key, void **data);
extern void **cfuhash_keys(cfuhash_table_t *ht, size_t *num_keys, int fast);
/* hash table flags */

View File

@ -1,43 +1,40 @@
/* Creation date: 2005-07-02 22:25:39
* Authors: Don
* Change log:
/*
* cfulist.c - This file is part of the libcfu library
*
* Copyright (c) 2005 Don Owens. All rights reserved.
*
* This code is released under the BSD license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* * Neither the name of the author nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Copyright (c) 2005 Don Owens
All rights reserved.
This code is released under the BSD license:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
@ -199,7 +196,7 @@ cfulist_unshift_data(cfulist_t *list, void *data, size_t data_size) {
list->tail = list->entries = entry;
}
list->num_entries++;
unlock_list(list);
return 1;
@ -488,7 +485,7 @@ typedef struct _join_foreach_struct {
static int
_join_foreach_fn(void *data, size_t data_size, void *arg) {
_join_foreach_struct *stuff = (_join_foreach_struct *)arg;
data_size = data_size;
if (stuff->count) cfustring_append(stuff->string, stuff->delimiter);
stuff->count++;

View File

@ -1,43 +1,40 @@
/* Creation date: 2005-07-02 22:25:10
* Authors: Don
* Change log:
/*
* cfulist.h - This file is part of the libcfu library
*
* Copyright (c) 2005 Don Owens. All rights reserved.
*
* This code is released under the BSD license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* * Neither the name of the author nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Copyright (c) 2005 Don Owens
All rights reserved.
This code is released under the BSD license:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _CFU_LIST_
#define _CFU_LIST_
@ -46,113 +43,113 @@
CFU_BEGIN_DECLS
/* The list itself. */
struct cfulist;
typedef struct cfulist cfulist_t;
/* The list itself. */
struct cfulist;
typedef struct cfulist cfulist_t;
/* Function called for each element in the list when passed to
* cfulist_foreach(). A non-zero return value means to stop
* iteration.
*/
typedef int (*cfulist_foreach_fn_t)(void *data, size_t data_size, void *arg);
/* Function called for each element in the list when passed to
* cfulist_foreach(). A non-zero return value means to stop
* iteration.
*/
typedef int (*cfulist_foreach_fn_t)(void *data, size_t data_size, void *arg);
/* Function called for each element in the list when passed to
* cfulist_map(). The return value is used to build a new
* list.
*/
typedef void * (*cfulist_map_fn_t)(void *data, size_t data_size, void *arg,
size_t *new_data_size);
/* Function called for each element in the list when passed to
* cfulist_map(). The return value is used to build a new
* list.
*/
typedef void * (*cfulist_map_fn_t)(void *data, size_t data_size, void *arg,
size_t *new_data_size);
/* Function called to free the data in an element. */
typedef void (*cfulist_free_fn_t)(void *data);
/* Function called to free the data in an element. */
typedef void (*cfulist_free_fn_t)(void *data);
/* Returns a new list. */
extern cfulist_t * cfulist_new();
/* Returns a new list. */
extern cfulist_t * cfulist_new();
/* Same as cfulist_new(), but set a function to be called on each
* element when the list is destroyed.
*/
extern cfulist_t * cfulist_new_with_free_fn(cfulist_free_fn_t free_fn);
/* Same as cfulist_new(), but set a function to be called on each
* element when the list is destroyed.
*/
extern cfulist_t * cfulist_new_with_free_fn(cfulist_free_fn_t free_fn);
/* Returns the number of entries in the list. */
extern size_t cfulist_num_entries(cfulist_t *list);
/* Returns the number of entries in the list. */
extern size_t cfulist_num_entries(cfulist_t *list);
/* Manipulating list entries */
/* Manipulating list entries */
/* Push a value onto the end of the list. */
extern int cfulist_push_data(cfulist_t *list, void *data, size_t data_size);
/* Push a value onto the end of the list. */
extern int cfulist_push_data(cfulist_t *list, void *data, size_t data_size);
/* Pop a value from the end of the list. */
extern int cfulist_pop_data(cfulist_t *list, void **data, size_t *data_size);
/* Pop a value from the end of the list. */
extern int cfulist_pop_data(cfulist_t *list, void **data, size_t *data_size);
/* Add a value at the beginning of the list. */
extern int cfulist_unshift_data(cfulist_t *list, void *data, size_t data_size);
/* Add a value at the beginning of the list. */
extern int cfulist_unshift_data(cfulist_t *list, void *data, size_t data_size);
/* Shift a value off the beginning of the list. */
extern int cfulist_shift_data(cfulist_t *list, void **data, size_t *data_size);
/* Shift a value off the beginning of the list. */
extern int cfulist_shift_data(cfulist_t *list, void **data, size_t *data_size);
/* Add a value at the end of the queue (equivalent to push) */
extern int cfulist_enqueue_data(cfulist_t *list, void *data, size_t data_size);
/* Add a value at the end of the queue (equivalent to push) */
extern int cfulist_enqueue_data(cfulist_t *list, void *data, size_t data_size);
/* Remove the value at the beginning of the list (equivalent to shift). */
extern int cfulist_dequeue_data(cfulist_t *list, void **data, size_t *data_size);
/* Remove the value at the beginning of the list (equivalent to shift). */
extern int cfulist_dequeue_data(cfulist_t *list, void **data, size_t *data_size);
/* Return the last entry from the list (without removing it from
* the list).
*/
extern int cfulist_first_data(cfulist_t *list, void **data, size_t *data_size);
/* Return the last entry from the list (without removing it from
* the list).
*/
extern int cfulist_first_data(cfulist_t *list, void **data, size_t *data_size);
/* Return the last entry from the list (without removing it from
* the list).
*/
extern int cfulist_last_data(cfulist_t *list, void **data, size_t *data_size);
/* Return the last entry from the list (without removing it from
* the list).
*/
extern int cfulist_last_data(cfulist_t *list, void **data, size_t *data_size);
/* Return the nth entry from the list (without removing it from
* the list). n starts at zero.
*/
extern int cfulist_nth_data(cfulist_t *list, void **data, size_t *data_size, size_t n);
/* Return the nth entry from the list (without removing it from
* the list). n starts at zero.
*/
extern int cfulist_nth_data(cfulist_t *list, void **data, size_t *data_size, size_t n);
extern void cfulist_reset_each(cfulist_t *list);
extern int cfulist_each_data(cfulist_t *list, void **data, size_t *data_size);
extern int cfulist_next_data(cfulist_t *list, void **data, size_t *data_size);
extern void cfulist_reset_each(cfulist_t *list);
extern int cfulist_each_data(cfulist_t *list, void **data, size_t *data_size);
extern int cfulist_next_data(cfulist_t *list, void **data, size_t *data_size);
/* Calls fe_fn() for each element in the list. Also passes arg on
* each call. If fe_fn() returns a non-zero value, the iteration
* over the elements stops.
*/
extern size_t cfulist_foreach(cfulist_t *list, cfulist_foreach_fn_t fe_fn, void *arg);
/* Calls fe_fn() for each element in the list. Also passes arg on
* each call. If fe_fn() returns a non-zero value, the iteration
* over the elements stops.
*/
extern size_t cfulist_foreach(cfulist_t *list, cfulist_foreach_fn_t fe_fn, void *arg);
/* Creates a new list from the list passed in. Calls map_fn() on
* each element in the list. The return value is placed in the
* corresponding position in the new list.
*/
extern cfulist_t *cfulist_map(cfulist_t *list, cfulist_map_fn_t map_fn, void *arg);
/* Creates a new list from the list passed in. Calls map_fn() on
* each element in the list. The return value is placed in the
* corresponding position in the new list.
*/
extern cfulist_t *cfulist_map(cfulist_t *list, cfulist_map_fn_t map_fn, void *arg);
/* Free all resources used by the list. */
extern void cfulist_destroy(cfulist_t *list);
extern void cfulist_destroy_with_free_fn(cfulist_t *list, cfulist_free_fn_t free_fn);
/* Free all resources used by the list. */
extern void cfulist_destroy(cfulist_t *list);
extern void cfulist_destroy_with_free_fn(cfulist_t *list, cfulist_free_fn_t free_fn);
/* When you don't care about the size of the data */
/* When you don't care about the size of the data */
extern int cfulist_push(cfulist_t *list, void *data);
extern void * cfulist_pop(cfulist_t *list);
extern int cfulist_unshift(cfulist_t *list, void *data);
extern void * cfulist_shift(cfulist_t *list);
extern int cfulist_enqueue(cfulist_t *list, void *data);
extern void *cfulist_dequeue(cfulist_t *list);
extern int cfulist_push(cfulist_t *list, void *data);
extern void * cfulist_pop(cfulist_t *list);
extern int cfulist_unshift(cfulist_t *list, void *data);
extern void * cfulist_shift(cfulist_t *list);
extern int cfulist_enqueue(cfulist_t *list, void *data);
extern void *cfulist_dequeue(cfulist_t *list);
/* Strings -- assume data is a null-terminated string -- size is
* calculated by strlen(data) + 1
*/
/* Strings -- assume data is a null-terminated string -- size is
* calculated by strlen(data) + 1
*/
extern int cfulist_push_string(cfulist_t *list, char *data);
extern char * cfulist_pop_string(cfulist_t *list);
extern int cfulist_unshift_string(cfulist_t *list, char *data);
extern char * cfulist_shift_string(cfulist_t *list);
extern int cfulist_enqueue_string(cfulist_t *list, char *data);
extern char *cfulist_dequeue_string(cfulist_t *list);
extern int cfulist_push_string(cfulist_t *list, char *data);
extern char * cfulist_pop_string(cfulist_t *list);
extern int cfulist_unshift_string(cfulist_t *list, char *data);
extern char * cfulist_shift_string(cfulist_t *list);
extern int cfulist_enqueue_string(cfulist_t *list, char *data);
extern char *cfulist_dequeue_string(cfulist_t *list);
extern char *cfulist_join(cfulist_t *list, const char *delimiter);
extern char *cfulist_join(cfulist_t *list, const char *delimiter);
CFU_END_DECLS

View File

@ -1,6 +1,38 @@
/* Creation date: 2005-09-04 12:57:25
* Authors: Don
* Change log:
/*
* cfuopt.c - This file is part of the libcfu library
*
* Copyright (c) 2005 Don Owens. All rights reserved.
*
* This code is released under the BSD license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* * Neither the name of the author nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cfu.h"
@ -160,7 +192,7 @@ _add_to_option_map(void *data, size_t data_size, void *arg) {
data_size = data_size;
cfuhash_put(es->context->option_map, (char *)data, (void *)es->entry);
return 0;
}
@ -183,7 +215,7 @@ cfuopt_add_entry(cfuopt_t *context, const char *opt_str, void *arg_data,
entry->param_names = param_list;
cfulist_push(context->option_list, (void *)entry);
entry_struct.entry = entry;
entry_struct.context = context;
@ -204,7 +236,7 @@ check_arg(const char *arg, int *is_long, int *is_short, int *is_data, int *is_en
*value = NULL;
if (!ptr || !*ptr) return;
if (*ptr != '-') {
*is_data = 1;
*parsed_arg = cfustring_dup_c_str(ptr);
@ -295,7 +327,7 @@ cfuopt_parse(cfuopt_t *context, int *argc, char ***argv, char **error) {
if (*argc < 1) return;
context->progname = cfustring_dup_c_str(args[0]);
if (*argc < 2) return;
for (i = 1; i < *argc; i++) {
@ -305,7 +337,7 @@ cfuopt_parse(cfuopt_t *context, int *argc, char ***argv, char **error) {
if (parsed_arg) free(parsed_arg);
parsed_arg = NULL;
check_arg(cur_arg, &is_long_opt, &is_short_opt, &is_data, &is_end_of_opt, &parsed_arg,
&value);
if (is_long_opt || is_short_opt) {
@ -367,7 +399,7 @@ cfuopt_parse(cfuopt_t *context, int *argc, char ***argv, char **error) {
default:
break;
}
}
extra_count = cfulist_num_entries(context->extra);
@ -435,7 +467,7 @@ _cfuopt_pretty_print_foreach(void *key, size_t key_size, void *data, size_t data
arg = arg;
ARG_TYPE_TO_STR(list_entry->arg_type, str);
printf("%s=%p (%s - %s) => %s, \"%s\"\n", name, (void *)list_entry, (list_entry->required ? "required" : "optional"), str, list_entry->description, list_entry->arg_description);
return 0;
}
@ -506,7 +538,7 @@ _find_foreach_fn(void *data, size_t data_size, void *arg) {
desc = cfulist_join(param_full_list, ", ");
data_size = data_size;
cfulist_destroy_with_free_fn(param_full_list, _simple_list_free_fn);
desc_ds->desc = desc;
@ -527,7 +559,7 @@ _find_longest_help_desc(cfuopt_t *context, cfulist_t **desc_list) {
cfulist_foreach(context->option_list, _find_foreach_fn, (void *)&ds);
*desc_list = ds.desc_list;
return ds.max_size;
}
@ -548,7 +580,7 @@ _get_help_lines(void *data, size_t data_size, void *arg) {
data_size = data_size;
left_col = desc_ds->desc;
if (!left_col) return 0;
desc = desc_ds->entry->description;
@ -558,7 +590,7 @@ _get_help_lines(void *data, size_t data_size, void *arg) {
cfulist_push(ds->list, (void *)line);
return 0;
}
static void
@ -577,7 +609,7 @@ cfuopt_get_help_str(cfuopt_t *context) {
char *help_str = NULL;
max_width = _find_longest_help_desc(context, &desc_list);
fmt = cfustring_sprintf_c_str(" %%-%us %%s\n", max_width);
ds.fmt = fmt;
ds.list = cfulist_new();

View File

@ -1,6 +1,38 @@
/* Creation date: 2005-09-04 12:54:41
* Authors: Don
* Change log:
/*
* cfuopt.h - This file is part of the libcfu library
*
* Copyright (c) 2005 Don Owens. All rights reserved.
*
* This code is released under the BSD license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* * Neither the name of the author nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cfu.h>

View File

@ -1,43 +1,40 @@
/* Creation date: 2005-06-26 19:59:10
* Authors: Don
* Change log:
/*
* cfustring.c - This file is part of the libcfu library
*
* Copyright (c) 2005 Don Owens. All rights reserved.
*
* This code is released under the BSD license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* * Neither the name of the author nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Copyright (c) 2005 Don Owens
All rights reserved.
This code is released under the BSD license:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cfu.h"
#include "cfustring.h"
@ -167,7 +164,7 @@ extern char *
cfustring_get_buffer_copy(cfustring_t *cfu_str) {
char *buffer = NULL;
if (!cfu_str->str) return NULL;
buffer = (char *)calloc(cfu_str->used_size, 1);
memcpy(buffer, cfu_str->str, cfu_str->used_size);
return buffer;
@ -222,7 +219,7 @@ _check_sep(char **start, char **seps, size_t num_seps, char **sep_chk_ptrs, int
}
return NULL;
}
for (end = *start; *end; end++) {
for (i = 0; i < num_seps; i++) {
if (*sep_chk_ptrs[i] == *end) {
@ -287,7 +284,7 @@ __cfustring_split_to_raw(cfustring_t *cfu_str, size_t *num_strings, size_t num_s
free(sep_array);
return NULL;
}
while ( (next_str = _check_sep(&end, sep_array, num_seps, sep_chk_ptrs, last)) ) {
if (used_ret_strings == max_ret_strings) {
/* allocate more space */
@ -377,7 +374,7 @@ cfustring_sprintf_c_str(const char *fmt, ...) {
char *str = NULL;
cfustring_t *cfu_str = cfustring_new();
va_start(ap, fmt);
cfustring_vsprintf(cfu_str, fmt, ap);
cfustring_vsprintf(cfu_str, fmt, ap);
va_end(ap);
str = cfustring_get_buffer_copy(cfu_str);
@ -569,7 +566,7 @@ cfustring_vsprintf(cfustring_t *cfu_str, const char *fmt_in, va_list ap) {
start = end = fmt;
continue;
}
} else {
byte_count++;
end = fmt;
@ -618,5 +615,5 @@ cfustring_c_str_split(const char *c_str, size_t *num_strings, size_t limit, ...)
cfustring_destroy(cfu_str);
return rv;
return rv;
}

View File

@ -1,43 +1,40 @@
/* Creation date: 2005-06-26 19:56:34
* Authors: Don
* Change log:
/*
* cfustring.h - This file is part of the libcfu library
*
* Copyright (c) 2005 Don Owens. All rights reserved.
*
* This code is released under the BSD license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* * Neither the name of the author nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Copyright (c) 2005 Don Owens
All rights reserved.
This code is released under the BSD license:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _CFU_STRING_H_
#define _CFU_STRING_H_
@ -47,74 +44,74 @@
CFU_BEGIN_DECLS
struct cfustring;
typedef struct cfustring cfustring_t;
struct cfustring;
typedef struct cfustring cfustring_t;
/* Returns a new String. */
extern cfustring_t * cfustring_new();
/* Returns a new String. */
extern cfustring_t * cfustring_new();
/* Returns a new String, but preallocates a buffer of the given size. */
extern cfustring_t * cfustring_new_with_initial_size(size_t initial_size);
/* Returns a new String, but preallocates a buffer of the given size. */
extern cfustring_t * cfustring_new_with_initial_size(size_t initial_size);
/* Returns a new String initalized with the given string. */
extern cfustring_t * cfustring_new_from_string(const char *string);
/* Returns a new String initalized with the given string. */
extern cfustring_t * cfustring_new_from_string(const char *string);
/* Overwrite anything currently in cfu_str with string. */
extern int cfustring_dup(cfustring_t *cfu_str, const char *string);
/* Overwrite anything currently in cfu_str with string. */
extern int cfustring_dup(cfustring_t *cfu_str, const char *string);
/* Truncate the string. */
extern int cfustring_clear(cfustring_t *cfu_str);
/* Truncate the string. */
extern int cfustring_clear(cfustring_t *cfu_str);
/* Append str to the end of the buffer in cfu_str. */
extern int cfustring_append(cfustring_t *cfu_str, const char *str);
/* Append str to the end of the buffer in cfu_str. */
extern int cfustring_append(cfustring_t *cfu_str, const char *str);
/* Get the buffer used to hold the string. Do not free() it, as it is
* used directly by cfustring and will be destroyed when
* cfustring_destroy() is called.
*/
extern char * cfustring_get_buffer(cfustring_t *cfu_str);
/* Get the buffer used to hold the string. Do not free() it, as it is
* used directly by cfustring and will be destroyed when
* cfustring_destroy() is called.
*/
extern char * cfustring_get_buffer(cfustring_t *cfu_str);
/* Same as cfustring_get_buffer(), except return a copy of the string.
* Caller is responsible for deallocating the buffer with free().
*/
extern char * cfustring_get_buffer_copy(cfustring_t *cfu_str);
/* Same as cfustring_get_buffer(), except return a copy of the string.
* Caller is responsible for deallocating the buffer with free().
*/
extern char * cfustring_get_buffer_copy(cfustring_t *cfu_str);
/* Split cfu_str on one or more delimiting strings, e.g.,
* cfustring_split(cfu_str, 2, 0, "\r\n", "\n"). Use a limit > 0 if
* you want to only get back a certain number of strings and ignore
* any extra delimiters.
*/
extern cfustring_t ** cfustring_split(cfustring_t *cfu_str, size_t *num_strings,
size_t limit, ...);
/* Split cfu_str on one or more delimiting strings, e.g.,
* cfustring_split(cfu_str, 2, 0, "\r\n", "\n"). Use a limit > 0 if
* you want to only get back a certain number of strings and ignore
* any extra delimiters.
*/
extern cfustring_t ** cfustring_split(cfustring_t *cfu_str, size_t *num_strings,
size_t limit, ...);
/* Same as cfustring_split(), except return an array of C-strings.
* Caller is responsible for deallocating the buffers.
*/
extern char ** cfustring_split_to_c_str(cfustring_t *cfu_str, size_t *num_strings,
size_t limit, ...);
/* Same as cfustring_split(), except return an array of C-strings.
* Caller is responsible for deallocating the buffers.
*/
extern char ** cfustring_split_to_c_str(cfustring_t *cfu_str, size_t *num_strings,
size_t limit, ...);
/* Free all resources allocated by cfu_str. */
extern int cfustring_destroy(cfustring_t *cfu_str);
/* Free all resources allocated by cfu_str. */
extern int cfustring_destroy(cfustring_t *cfu_str);
/* Duplicate the C string str. Caller must free with free(). */
extern char * cfustring_dup_c_str(const char *str);
/* Same as cfustring_dup_c_str(), but only copy at most n chars */
extern char * cfustring_dup_c_str_n(const char *str, size_t n);
/* Duplicate the C string str. Caller must free with free(). */
extern char * cfustring_dup_c_str(const char *str);
/* Like sprintf(), but writes to a self-extending string. */
extern size_t cfustring_sprintf(cfustring_t *cfu_str, const char *fmt, ...);
/* Same as cfustring_dup_c_str(), but only copy at most n chars */
extern char * cfustring_dup_c_str_n(const char *str, size_t n);
/* Like vsprintf(), but writes to a self-extending string. */
extern size_t cfustring_vsprintf(cfustring_t *cfu_str, const char *fmt, va_list ap);
/* Like sprintf(), but writes to a self-extending string. */
extern size_t cfustring_sprintf(cfustring_t *cfu_str, const char *fmt, ...);
/* Similar to sprintf(), but allocates a C string of the
* appropriate size for you and returns it.
*/
extern char * cfustring_sprintf_c_str(const char *fmt, ...);
/* Like vsprintf(), but writes to a self-extending string. */
extern size_t cfustring_vsprintf(cfustring_t *cfu_str, const char *fmt, va_list ap);
/* Like cfustring_split_to_c_str(), but split a char * instead of a cfustring_t *. */
extern char ** cfustring_c_str_split(const char *c_str, size_t *num_strings, size_t limit, ...);
/* Similar to sprintf(), but allocates a C string of the
* appropriate size for you and returns it.
*/
extern char * cfustring_sprintf_c_str(const char *fmt, ...);
/* Like cfustring_split_to_c_str(), but split a char * instead of a cfustring_t *. */
extern char ** cfustring_c_str_split(const char *c_str, size_t *num_strings, size_t limit, ...);
CFU_END_DECLS

View File

@ -1,8 +1,38 @@
/* Creation date: 2005-07-26 08:20:59
* Authors: Don
* Change log:
/*
* cfuthread_queue.c - This file is part of the libcfu library
*
* Copyright (c) 2005 Don Owens. All rights reserved.
*
* This code is released under the BSD license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* * Neither the name of the author nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cfuthread_queue.h"
@ -59,13 +89,13 @@ _run_queue(void *arg) {
}
pthread_cleanup_push((void *)tq->cleanup_fn, tq->cleanup_arg);
while (1) {
pthread_mutex_lock(&tq->mutex);
while (cfulist_num_entries(tq->request_queue) == 0) {
pthread_cond_wait(&tq->cv, &tq->mutex);
}
request = (cfuthread_queue_entry *)cfulist_dequeue(tq->request_queue);
pthread_mutex_unlock(&tq->mutex);
if (!request) continue;
@ -101,7 +131,7 @@ cfuthread_queue_new_with_cleanup(cfuthread_queue_fn_t fn, cfuthread_queue_init_t
if ( (0 != pthread_create(&tq->thread, NULL, _run_queue, (void *)tq)) ) {
return NULL;
}
return tq;
}

View File

@ -1,44 +1,40 @@
/* Creation date: 2005-07-26 08:19:33
* Authors: Don
* Change log:
/*
* cfuthread_queue.h - This file is part of the libcfu library
*
* Copyright (c) 2005 Don Owens. All rights reserved.
*
* This code is released under the BSD license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* * Neither the name of the author nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Copyright (c) 2005 Don Owens
All rights reserved.
This code is released under the BSD license:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _CFUTHREAD_QUEUE_H
#define _CFUTHREAD_QUEUE_H
@ -46,49 +42,49 @@
CFU_BEGIN_DECLS
/* cfuthread_queue provides a way to serialize requests for a
* resource where you want the resource to be accessed from a
* single thread only. For instance, for a database connection
* where making calls in separate threads does not work properly,
* you can use cfuthread_queue. cfuthread_queue_new() creates a
* new thread that waits for something to be added to the queue.
* Once something is added, the thread will process the data by
* calling the function you pass as an argument to the
* cfuthread_queue_new() function.
*/
/* cfuthread_queue provides a way to serialize requests for a
* resource where you want the resource to be accessed from a
* single thread only. For instance, for a database connection
* where making calls in separate threads does not work properly,
* you can use cfuthread_queue. cfuthread_queue_new() creates a
* new thread that waits for something to be added to the queue.
* Once something is added, the thread will process the data by
* calling the function you pass as an argument to the
* cfuthread_queue_new() function.
*/
struct cfuthread_queue;
typedef struct cfuthread_queue cfuthread_queue_t;
struct cfuthread_queue;
typedef struct cfuthread_queue cfuthread_queue_t;
typedef void * (*cfuthread_queue_fn_t)(void *arg);
typedef void (*cfuthread_queue_init_t)(void *arg);
typedef void (*cfuthread_queue_cleanup_t)(void *arg);
typedef void * (*cfuthread_queue_fn_t)(void *arg);
typedef void (*cfuthread_queue_init_t)(void *arg);
typedef void (*cfuthread_queue_cleanup_t)(void *arg);
/* Creates a new thread queue structure that will run the given
* function when a request is received.
*/
extern cfuthread_queue_t * cfuthread_queue_new(cfuthread_queue_fn_t fn);
/* Creates a new thread queue structure that will run the given
* function when a request is received.
*/
extern cfuthread_queue_t * cfuthread_queue_new(cfuthread_queue_fn_t fn);
/* Same as cfuthread_queue_new(), but with an initialization
* function that gets called with the argument init_arg when the
* thread is created, and a cleanup function that gets called with
* the argument cleanup_arg when the thread exits, e.g., when
* cfuthread_queue_destroy() is called.
*/
extern cfuthread_queue_t * cfuthread_queue_new_with_cleanup(cfuthread_queue_fn_t fn,
cfuthread_queue_init_t init_fn, void *init_arg, cfuthread_queue_cleanup_t cleanup_fn,
void *cleanup_arg);
/* Same as cfuthread_queue_new(), but with an initialization
* function that gets called with the argument init_arg when the
* thread is created, and a cleanup function that gets called with
* the argument cleanup_arg when the thread exits, e.g., when
* cfuthread_queue_destroy() is called.
*/
extern cfuthread_queue_t * cfuthread_queue_new_with_cleanup(cfuthread_queue_fn_t fn,
cfuthread_queue_init_t init_fn, void *init_arg, cfuthread_queue_cleanup_t cleanup_fn,
void *cleanup_arg);
/* Add a request to the queue. data will get passed to the
* function fn given to cfuthread_queue_new when it reaches the
* front of the queue.
*/
extern void * cfuthread_queue_make_request(cfuthread_queue_t * tq, void *data);
/* Add a request to the queue. data will get passed to the
* function fn given to cfuthread_queue_new when it reaches the
* front of the queue.
*/
extern void * cfuthread_queue_make_request(cfuthread_queue_t * tq, void *data);
/* Free up resources used by the queue, in addition to canceling
* the thread.
*/
extern void cfuthread_queue_destroy(cfuthread_queue_t *);
/* Free up resources used by the queue, in addition to canceling
* the thread.
*/
extern void cfuthread_queue_destroy(cfuthread_queue_t *);
CFU_END_DECLS

View File

@ -1,43 +1,40 @@
/* Creation date: 2005-06-26 18:11:40
* Authors: Don
* Change log:
/*
* cfutime.c - This file is part of the libcfu library
*
* Copyright (c) 2005 Don Owens. All rights reserved.
*
* This code is released under the BSD license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* * Neither the name of the author nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Copyright (c) 2005 Don Owens
All rights reserved.
This code is released under the BSD license:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cfu.h"
#include "cfutime.h"

View File

@ -1,43 +1,40 @@
/* Creation date: 2005-06-26 18:09:46
* Authors: Don
* Change log:
/*
* cfutime.h - This file is part of the libcfu library
*
* Copyright (c) 2005 Don Owens. All rights reserved.
*
* This code is released under the BSD license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* * Neither the name of the author nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Copyright (c) 2005 Don Owens
All rights reserved.
This code is released under the BSD license:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _CFU_TIME_H_
#define _CFU_TIME_H_
@ -45,23 +42,23 @@
CFU_BEGIN_DECLS
struct cfutime;
typedef struct cfutime cfutime_t;
struct cfutime;
typedef struct cfutime cfutime_t;
/* Return a new cfutime structure. */
extern cfutime_t *cfutime_new();
/* Return a new cfutime structure. */
extern cfutime_t *cfutime_new();
/* Start the timer. */
extern void cfutime_begin(cfutime_t *time);
/* Start the timer. */
extern void cfutime_begin(cfutime_t *time);
/* Stop the timer. */
extern void cfutime_end(cfutime_t *time);
/* Stop the timer. */
extern void cfutime_end(cfutime_t *time);
/* Return the number of seconds elapsed as a double. */
extern double cfutime_elapsed(cfutime_t *time);
/* Return the number of seconds elapsed as a double. */
extern double cfutime_elapsed(cfutime_t *time);
/* Deallocate resources allocated for time. */
extern void cfutime_free(cfutime_t *time);
/* Deallocate resources allocated for time. */
extern void cfutime_free(cfutime_t *time);
CFU_END_DECLS