Make C89 compatible using some preprocessor trickery

Also using C99-snprintf file from:
  http://www.jhweiss.de/software/snprintf.html

For when in strict C89 compiler mode or when snprintf() or vsnprintf()
aren't for whatever reason available.

Use CFU_INLINE macro defined in cfu.h to make a decent attempt at
getting inline function support when in C89 mode.
This commit is contained in:
Matthew Brush 2013-03-04 17:37:24 -08:00
parent 0d7d3bf9c8
commit a054a4de71
9 changed files with 2156 additions and 23 deletions

View File

@ -15,7 +15,7 @@ AM_PROG_CC_C_O
AC_HEADER_STDC AC_HEADER_STDC
AC_HEADER_ASSERT AC_HEADER_ASSERT
AC_HEADER_TIME AC_HEADER_TIME
AC_CHECK_HEADERS([sys/time.h]) AC_CHECK_HEADERS([stdarg.h sys/time.h])
# Checks for typedefs, structures, and compiler characteristics. # Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST AC_C_CONST
@ -25,7 +25,7 @@ AC_TYPE_SIZE_T
# Checks for library functions. # Checks for library functions.
AC_FUNC_MALLOC AC_FUNC_MALLOC
AC_FUNC_MEMCMP AC_FUNC_MEMCMP
AC_CHECK_FUNCS([gettimeofday memset strcasecmp strncasecmp]) AC_CHECK_FUNCS([gettimeofday memset snprintf strcasecmp strncasecmp vsnprintf])
# Check for pthread support # Check for pthread support
AC_CHECK_LIB([pthread], AC_CHECK_LIB([pthread],

View File

@ -1,7 +1,7 @@
lib_LTLIBRARIES = libcfu.la lib_LTLIBRARIES = libcfu.la
libcfu_la_SOURCES = cfuhash.c cfutime.c cfustring.c cfulist.c \ libcfu_la_SOURCES = cfuhash.c cfutime.c cfustring.c cfulist.c \
cfuconf.c cfu.c cfuopt.c cfuconf.c cfu.c cfuopt.c snprintf.c
libcfuincdir = $(includedir)/cfu libcfuincdir = $(includedir)/cfu

View File

@ -39,11 +39,19 @@
#define LIBCFU_H_ #define LIBCFU_H_
#ifdef __cplusplus #ifdef __cplusplus
#define CFU_BEGIN_DECLS extern "C" { # define CFU_BEGIN_DECLS extern "C" {
#define CFU_END_DECLS } # define CFU_END_DECLS }
# define CFU_INLINE inline
#else #else
#define CFU_BEGIN_DECLS # define CFU_BEGIN_DECLS
#define CFU_END_DECLS # define CFU_END_DECLS
# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
# define CFU_INLINE inline
# elif defined(__GNUC__)
# define CFU_INLINE __inline__
# else
# define CFU_INLINE
# endif
#endif #endif
CFU_BEGIN_DECLS CFU_BEGIN_DECLS

View File

@ -284,7 +284,7 @@ _slurp_file_from_fp(FILE *fp) {
return lines; return lines;
} }
static inline int static CFU_INLINE int
_is_whitespace(char c) { _is_whitespace(char c) {
if (c == ' ' || c == '\t' || c == '\r' || c == '\n') { if (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
return 1; return 1;
@ -292,7 +292,7 @@ _is_whitespace(char c) {
return 0; return 0;
} }
static inline char * static CFU_INLINE char *
_eat_whitespace(char *buf) { _eat_whitespace(char *buf) {
char *ptr = buf; char *ptr = buf;

View File

@ -45,6 +45,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <ctype.h> #include <ctype.h>
#include <assert.h> #include <assert.h>
@ -73,6 +74,11 @@ strncasecmp(const char *s1, const char *s2, size_t n)
} }
#endif #endif
#ifndef HAVE_SNPRINTF
int rpl_snprintf(char *, size_t, const char *, ...);
# define snprintf rpl_snprintf
#endif
typedef struct cfuhash_event_flags { typedef struct cfuhash_event_flags {
int resized:1; int resized:1;
@ -132,14 +138,14 @@ hash_size(unsigned int s) {
return i; return i;
} }
static inline void * static CFU_INLINE void *
hash_key_dup(const void *key, size_t key_size) { hash_key_dup(const void *key, size_t key_size) {
void *new_key = malloc(key_size); void *new_key = malloc(key_size);
memcpy(new_key, key, key_size); memcpy(new_key, key, key_size);
return new_key; return new_key;
} }
static inline void * static CFU_INLINE void *
hash_key_dup_lower_case(const void *key, size_t key_size) { hash_key_dup_lower_case(const void *key, size_t key_size) {
char *new_key = (char *)hash_key_dup(key, key_size); char *new_key = (char *)hash_key_dup(key, key_size);
size_t i = 0; size_t i = 0;
@ -148,7 +154,7 @@ hash_key_dup_lower_case(const void *key, size_t key_size) {
} }
/* returns the index into the buckets array */ /* returns the index into the buckets array */
static inline unsigned int static CFU_INLINE unsigned int
hash_value(cfuhash_table_t *ht, const void *key, size_t key_size, size_t num_buckets) { hash_value(cfuhash_table_t *ht, const void *key, size_t key_size, size_t num_buckets) {
unsigned int hv = 0; unsigned int hv = 0;
@ -303,7 +309,7 @@ cfuhash_set_free_function(cfuhash_table_t * ht, cfuhash_free_fn_t ff) {
return 0; return 0;
} }
static inline void static CFU_INLINE void
lock_hash(cfuhash_table_t *ht) { lock_hash(cfuhash_table_t *ht) {
if (!ht) return; if (!ht) return;
if (ht->flags & CFUHASH_NO_LOCKING) return; if (ht->flags & CFUHASH_NO_LOCKING) return;
@ -312,7 +318,7 @@ lock_hash(cfuhash_table_t *ht) {
#endif #endif
} }
static inline void static CFU_INLINE void
unlock_hash(cfuhash_table_t *ht) { unlock_hash(cfuhash_table_t *ht) {
if (!ht) return; if (!ht) return;
if (ht->flags & CFUHASH_NO_LOCKING) return; if (ht->flags & CFUHASH_NO_LOCKING) return;
@ -340,7 +346,7 @@ cfuhash_unlock(cfuhash_table_t *ht) {
/* see if this key matches the one in the hash entry */ /* see if this key matches the one in the hash entry */
/* uses the convention that zero means a match, like memcmp */ /* uses the convention that zero means a match, like memcmp */
static inline int static CFU_INLINE int
hash_cmp(const void *key, size_t key_size, cfuhash_entry *he, unsigned int case_insensitive) { hash_cmp(const void *key, size_t key_size, cfuhash_entry *he, unsigned int case_insensitive) {
if (key_size != he->key_size) return 1; if (key_size != he->key_size) return 1;
if (key == he->key) return 0; if (key == he->key) return 0;
@ -350,7 +356,7 @@ hash_cmp(const void *key, size_t key_size, cfuhash_entry *he, unsigned int case_
return memcmp(key, he->key, key_size); return memcmp(key, he->key, key_size);
} }
static inline cfuhash_entry * static CFU_INLINE cfuhash_entry *
hash_add_entry(cfuhash_table_t *ht, unsigned int hv, const void *key, size_t key_size, hash_add_entry(cfuhash_table_t *ht, unsigned int hv, const void *key, size_t key_size,
void *data, size_t data_size) { void *data, size_t data_size) {
cfuhash_entry *he = (cfuhash_entry *)calloc(1, sizeof(cfuhash_entry)); cfuhash_entry *he = (cfuhash_entry *)calloc(1, sizeof(cfuhash_entry));

View File

@ -93,21 +93,21 @@ cfulist_num_entries(cfulist_t *list) {
return list->num_entries; return list->num_entries;
} }
static inline void static CFU_INLINE void
lock_list(cfulist_t *list) { lock_list(cfulist_t *list) {
#ifdef HAVE_PTHREAD_H #ifdef HAVE_PTHREAD_H
pthread_mutex_lock(&list->mutex); pthread_mutex_lock(&list->mutex);
#endif #endif
} }
static inline void static CFU_INLINE void
unlock_list(cfulist_t *list) { unlock_list(cfulist_t *list) {
#ifdef HAVE_PTHREAD_H #ifdef HAVE_PTHREAD_H
pthread_mutex_unlock(&list->mutex); pthread_mutex_unlock(&list->mutex);
#endif #endif
} }
static inline cfulist_entry * static CFU_INLINE cfulist_entry *
new_list_entry() { new_list_entry() {
return (cfulist_entry *)calloc(1, sizeof(cfulist_entry)); return (cfulist_entry *)calloc(1, sizeof(cfulist_entry));
} }

View File

@ -71,8 +71,6 @@ typedef struct cfuopt_list_entry {
cfulist_t *param_names; cfulist_t *param_names;
} cfuopt_list_entry_t; } cfuopt_list_entry_t;
#define CFU_OPT_ALLOC(type, count) (type *)calloc(count, sizeof(type));
extern cfuopt_t * extern cfuopt_t *
cfuopt_new() { cfuopt_new() {
cfuopt_t *context = (cfuopt_t *)calloc(1, sizeof(cfuopt_t)); cfuopt_t *context = (cfuopt_t *)calloc(1, sizeof(cfuopt_t));
@ -199,7 +197,7 @@ _add_to_option_map(void *data, size_t data_size, void *arg) {
extern void extern void
cfuopt_add_entry(cfuopt_t *context, const char *opt_str, void *arg_data, cfuopt_add_entry(cfuopt_t *context, const char *opt_str, void *arg_data,
const char *description, const char *arg_description) { const char *description, const char *arg_description) {
cfuopt_list_entry_t *entry = CFU_OPT_ALLOC(cfuopt_list_entry_t, 1); cfuopt_list_entry_t *entry = calloc(1, sizeof(cfuopt_list_entry_t));
cfulist_t *param_list = NULL; cfulist_t *param_list = NULL;
cfuopt_arg_t arg_type = cfuopt_arg_invalid; cfuopt_arg_t arg_type = cfuopt_arg_invalid;
struct _add_entry_struct entry_struct; struct _add_entry_struct entry_struct;
@ -530,7 +528,7 @@ _find_foreach_fn(void *data, size_t data_size, void *arg) {
size_t this_size = 0; size_t this_size = 0;
cfulist_t *param_full_list = NULL; cfulist_t *param_full_list = NULL;
char *desc = NULL; char *desc = NULL;
_cfuopt_desc *desc_ds = CFU_OPT_ALLOC(_cfuopt_desc, 1); _cfuopt_desc *desc_ds = calloc(1, sizeof(_cfuopt_desc));
if (!data) return 0; if (!data) return 0;

View File

@ -35,6 +35,10 @@
* OF THE POSSIBILITY OF SUCH DAMAGE. * OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "cfu.h" #include "cfu.h"
#include "cfustring.h" #include "cfustring.h"
@ -45,6 +49,11 @@
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#ifndef HAVE_VSNPRINTF
int rpl_vsnprintf(char *, size_t, const char *, va_list);
# define vsnprintf rpl_vsnprintf
#endif
struct cfustring { struct cfustring {
libcfu_type type; libcfu_type type;
size_t max_size; size_t max_size;

2112
src/snprintf.c Normal file

File diff suppressed because it is too large Load Diff