fix indenting

This commit is contained in:
Anthony Minessale 2015-09-04 11:25:56 -05:00
parent 3947d443fa
commit ab4514d1e5
8 changed files with 5577 additions and 5489 deletions

View File

@ -134,11 +134,11 @@
* mpool_resize only. * mpool_resize only.
*/ */
typedef void (*mpool_log_func_t)(const void *mp_p, typedef void (*mpool_log_func_t)(const void *mp_p,
const int func_id, const int func_id,
const unsigned long byte_size, const unsigned long byte_size,
const unsigned long ele_n, const unsigned long ele_n,
const void *old_addr, const void *new_addr, const void *old_addr, const void *new_addr,
const unsigned long old_byte_size); const unsigned long old_byte_size);
#ifdef MPOOL_MAIN #ifdef MPOOL_MAIN
@ -183,7 +183,7 @@ typedef void mpool_t;
*/ */
extern extern
mpool_t *mpool_open(const unsigned int flags, const unsigned int page_size, mpool_t *mpool_open(const unsigned int flags, const unsigned int page_size,
void *start_addr, int *error_p); void *start_addr, int *error_p);
/* /*
* int mpool_close * int mpool_close
@ -251,7 +251,7 @@ int mpool_clear(mpool_t *mp_p);
*/ */
extern extern
void *mpool_alloc(mpool_t *mp_p, const unsigned long byte_size, void *mpool_alloc(mpool_t *mp_p, const unsigned long byte_size,
int *error_p); int *error_p);
/* /*
* void *mpool_calloc * void *mpool_calloc
@ -281,7 +281,7 @@ void *mpool_alloc(mpool_t *mp_p, const unsigned long byte_size,
*/ */
extern extern
void *mpool_calloc(mpool_t *mp_p, const unsigned long ele_n, void *mpool_calloc(mpool_t *mp_p, const unsigned long ele_n,
const unsigned long ele_size, int *error_p); const unsigned long ele_size, int *error_p);
/* /*
* int mpool_free * int mpool_free
@ -341,9 +341,9 @@ int mpool_free(mpool_t *mp_p, void *addr, const unsigned long size);
*/ */
extern extern
void *mpool_resize(mpool_t *mp_p, void *old_addr, void *mpool_resize(mpool_t *mp_p, void *old_addr,
const unsigned long old_byte_size, const unsigned long old_byte_size,
const unsigned long new_byte_size, const unsigned long new_byte_size,
int *error_p); int *error_p);
/* /*
* int mpool_stats * int mpool_stats
@ -381,10 +381,10 @@ void *mpool_resize(mpool_t *mp_p, void *old_addr,
*/ */
extern extern
int mpool_stats(const mpool_t *mp_p, unsigned int *page_size_p, int mpool_stats(const mpool_t *mp_p, unsigned int *page_size_p,
unsigned long *num_alloced_p, unsigned long *num_alloced_p,
unsigned long *user_alloced_p, unsigned long *user_alloced_p,
unsigned long *max_alloced_p, unsigned long *max_alloced_p,
unsigned long *tot_alloced_p); unsigned long *tot_alloced_p);
/* /*
* int mpool_set_log_func * int mpool_set_log_func
@ -461,3 +461,14 @@ const char *mpool_strerror(const int error);
/*<<<<<<<<<< This is end of the auto-generated output from fillproto. */ /*<<<<<<<<<< This is end of the auto-generated output from fillproto. */
#endif /* ! __MPOOL_H__ */ #endif /* ! __MPOOL_H__ */
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
*/

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -83,25 +83,25 @@
* were about 2^68 hashes to choose from. I only tested about a * were about 2^68 hashes to choose from. I only tested about a
* billion of those. * billion of those.
*/ */
#define HASH_MIX(a, b, c) \ #define HASH_MIX(a, b, c) \
do { \ do { \
a -= b; a -= c; a ^= (c >> 13); \ a -= b; a -= c; a ^= (c >> 13); \
b -= c; b -= a; b ^= (a << 8); \ b -= c; b -= a; b ^= (a << 8); \
c -= a; c -= b; c ^= (b >> 13); \ c -= a; c -= b; c ^= (b >> 13); \
a -= b; a -= c; a ^= (c >> 12); \ a -= b; a -= c; a ^= (c >> 12); \
b -= c; b -= a; b ^= (a << 16); \ b -= c; b -= a; b ^= (a << 16); \
c -= a; c -= b; c ^= (b >> 5); \ c -= a; c -= b; c ^= (b >> 5); \
a -= b; a -= c; a ^= (c >> 3); \ a -= b; a -= c; a ^= (c >> 3); \
b -= c; b -= a; b ^= (a << 10); \ b -= c; b -= a; b ^= (a << 10); \
c -= a; c -= b; c ^= (b >> 15); \ c -= a; c -= b; c ^= (b >> 15); \
} while(0) } while(0)
#define SET_POINTER(pnt, val) \ #define SET_POINTER(pnt, val) \
do { \ do { \
if ((pnt) != NULL) { \ if ((pnt) != NULL) { \
(*(pnt)) = (val); \ (*(pnt)) = (val); \
} \ } \
} while(0) } while(0)
/* /*
* The following macros take care of the mmap case. When we are * The following macros take care of the mmap case. When we are
@ -117,9 +117,9 @@
#else #else
#define TABLE_POINTER(tab_p, type, pnt) \ #define TABLE_POINTER(tab_p, type, pnt) \
((tab_p)->ta_mmap == NULL || (pnt) == NULL ? (pnt) : \ ((tab_p)->ta_mmap == NULL || (pnt) == NULL ? (pnt) : \
(type)((char *)((tab_p)->ta_mmap) + (long)(pnt))) (type)((char *)((tab_p)->ta_mmap) + (long)(pnt)))
#endif #endif
@ -127,8 +127,8 @@
* Macros to get at the key and the data pointers * Macros to get at the key and the data pointers
*/ */
#define ENTRY_KEY_BUF(entry_p) ((entry_p)->te_key_buf) #define ENTRY_KEY_BUF(entry_p) ((entry_p)->te_key_buf)
#define ENTRY_DATA_BUF(tab_p, entry_p) \ #define ENTRY_DATA_BUF(tab_p, entry_p) \
(ENTRY_KEY_BUF(entry_p) + (entry_p)->te_key_size) (ENTRY_KEY_BUF(entry_p) + (entry_p)->te_key_size)
/* /*
* Table structures... * Table structures...
@ -142,10 +142,10 @@
* faster. * faster.
*/ */
typedef struct table_shell_st { typedef struct table_shell_st {
unsigned int te_key_size; /* size of data */ unsigned int te_key_size; /* size of data */
unsigned int te_data_size; /* size of data */ unsigned int te_data_size; /* size of data */
struct table_shell_st *te_next_p; /* pointer to next in the list */ struct table_shell_st *te_next_p; /* pointer to next in the list */
/* NOTE: this does not have the te_key_buf field here */ /* NOTE: this does not have the te_key_buf field here */
} table_shell_t; } table_shell_t;
/* /*
@ -157,10 +157,10 @@ typedef struct table_shell_st {
* changed to match. * changed to match.
*/ */
typedef struct table_entry_st { typedef struct table_entry_st {
unsigned int te_key_size; /* size of data */ unsigned int te_key_size; /* size of data */
unsigned int te_data_size; /* size of data */ unsigned int te_data_size; /* size of data */
struct table_entry_st *te_next_p; /* pointer to next in the list */ struct table_entry_st *te_next_p; /* pointer to next in the list */
unsigned char te_key_buf[1]; /* 1st byte of key buf */ unsigned char te_key_buf[1]; /* 1st byte of key buf */
} table_entry_t; } table_entry_t;
/* external structure for debuggers be able to see void */ /* external structure for debuggers be able to see void */
@ -168,20 +168,20 @@ typedef table_entry_t table_entry_ext_t;
/* main table structure */ /* main table structure */
typedef struct table_st { typedef struct table_st {
unsigned int ta_magic; /* magic number */ unsigned int ta_magic; /* magic number */
unsigned int ta_flags; /* table's flags defined in table.h */ unsigned int ta_flags; /* table's flags defined in table.h */
unsigned int ta_bucket_n; /* num of buckets, should be 2^X */ unsigned int ta_bucket_n; /* num of buckets, should be 2^X */
unsigned int ta_entry_n; /* num of entries in all buckets */ unsigned int ta_entry_n; /* num of entries in all buckets */
unsigned int ta_data_align; /* data alignment value */ unsigned int ta_data_align; /* data alignment value */
table_entry_t **ta_buckets; /* array of linked lists */ table_entry_t **ta_buckets; /* array of linked lists */
table_linear_t ta_linear; /* linear tracking */ table_linear_t ta_linear; /* linear tracking */
struct table_st *ta_mmap; /* mmaped table */ struct table_st *ta_mmap; /* mmaped table */
unsigned long ta_file_size; /* size of on-disk space */ unsigned long ta_file_size; /* size of on-disk space */
void *ta_mem_pool; /* pointer to some memory pool */ void *ta_mem_pool; /* pointer to some memory pool */
table_mem_alloc_t ta_alloc_func; /* memory allocation function */ table_mem_alloc_t ta_alloc_func; /* memory allocation function */
table_mem_resize_t ta_resize_func; /* memory resize function */ table_mem_resize_t ta_resize_func; /* memory resize function */
table_mem_free_t ta_free_func; /* memory free function */ table_mem_free_t ta_free_func; /* memory free function */
} table_t; } table_t;
/* external table structure for debuggers */ /* external table structure for debuggers */
@ -189,41 +189,53 @@ typedef table_t table_ext_t;
/* local comparison functions */ /* local comparison functions */
typedef int (*compare_t)(const void *element1_p, const void *element2_p, typedef int (*compare_t)(const void *element1_p, const void *element2_p,
table_compare_t user_compare, table_compare_t user_compare,
const table_t *table_p, int *err_bp); const table_t *table_p, int *err_bp);
/* /*
* to map error to string * to map error to string
*/ */
typedef struct { typedef struct {
int es_error; /* error number */ int es_error; /* error number */
char *es_string; /* assocaited string */ char *es_string; /* assocaited string */
} error_str_t; } error_str_t;
static error_str_t errors[] = { static error_str_t errors[] = {
{ TABLE_ERROR_NONE, "no error" }, { TABLE_ERROR_NONE, "no error" },
{ TABLE_ERROR_PNT, "invalid table pointer" }, { TABLE_ERROR_PNT, "invalid table pointer" },
{ TABLE_ERROR_ARG_NULL, "buffer argument is null" }, { TABLE_ERROR_ARG_NULL, "buffer argument is null" },
{ TABLE_ERROR_SIZE, "incorrect size argument" }, { TABLE_ERROR_SIZE, "incorrect size argument" },
{ TABLE_ERROR_OVERWRITE, "key exists and no overwrite" }, { TABLE_ERROR_OVERWRITE, "key exists and no overwrite" },
{ TABLE_ERROR_NOT_FOUND, "key does not exist" }, { TABLE_ERROR_NOT_FOUND, "key does not exist" },
{ TABLE_ERROR_ALLOC, "error allocating memory" }, { TABLE_ERROR_ALLOC, "error allocating memory" },
{ TABLE_ERROR_LINEAR, "linear access not in progress" }, { TABLE_ERROR_LINEAR, "linear access not in progress" },
{ TABLE_ERROR_OPEN, "could not open file" }, { TABLE_ERROR_OPEN, "could not open file" },
{ TABLE_ERROR_SEEK, "could not seek to position in file" }, { TABLE_ERROR_SEEK, "could not seek to position in file" },
{ TABLE_ERROR_READ, "could not read from file" }, { TABLE_ERROR_READ, "could not read from file" },
{ TABLE_ERROR_WRITE, "could not write to file" }, { TABLE_ERROR_WRITE, "could not write to file" },
{ TABLE_ERROR_MMAP_NONE, "no mmap support compiled in library" }, { TABLE_ERROR_MMAP_NONE, "no mmap support compiled in library" },
{ TABLE_ERROR_MMAP, "could not mmap the file" }, { TABLE_ERROR_MMAP, "could not mmap the file" },
{ TABLE_ERROR_MMAP_OP, "operation not valid on mmap files" }, { TABLE_ERROR_MMAP_OP, "operation not valid on mmap files" },
{ TABLE_ERROR_EMPTY, "table is empty" }, { TABLE_ERROR_EMPTY, "table is empty" },
{ TABLE_ERROR_NOT_EMPTY, "table contains data" }, { TABLE_ERROR_NOT_EMPTY, "table contains data" },
{ TABLE_ERROR_ALIGNMENT, "invalid alignment value" }, { TABLE_ERROR_ALIGNMENT, "invalid alignment value" },
{ TABLE_ERROR_COMPARE, "problems with internal comparison" }, { TABLE_ERROR_COMPARE, "problems with internal comparison" },
{ TABLE_ERROR_FREE, "memory free error" }, { TABLE_ERROR_FREE, "memory free error" },
{ 0 } { 0 }
}; };
#define INVALID_ERROR "invalid error code" #define INVALID_ERROR "invalid error code"
#endif /* ! __TABLE_LOC_H__ */ #endif /* ! __TABLE_LOC_H__ */
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
*/

File diff suppressed because it is too large Load Diff

View File

@ -26,12 +26,12 @@
#include <errno.h> /* for setting errno */ #include <errno.h> /* for setting errno */
#include <sys/types.h> #include <sys/types.h>
#ifndef _WIN32 #ifndef _WIN32
/* not in Windows! */ /* not in Windows! */
# include <unistd.h> # include <unistd.h>
# include <stdint.h> # include <stdint.h>
#endif #endif
#ifndef SIMCLIST_NO_DUMPRESTORE #ifndef SIMCLIST_NO_DUMPRESTORE
/* includes for dump/restore */ /* includes for dump/restore */
# include <time.h> # include <time.h>
# include <sys/uio.h> /* for READ_ERRCHECK() and write() */ # include <sys/uio.h> /* for READ_ERRCHECK() and write() */
# include <fcntl.h> /* for open() etc */ # include <fcntl.h> /* for open() etc */
@ -88,31 +88,31 @@ typedef INT64 int64_t;
/* define some commodity macros for Dump/Restore functionality */ /* define some commodity macros for Dump/Restore functionality */
#ifndef SIMCLIST_NO_DUMPRESTORE #ifndef SIMCLIST_NO_DUMPRESTORE
/* write() decorated with error checking logic */ /* write() decorated with error checking logic */
#define WRITE_ERRCHECK(fd, msgbuf, msglen) do { \ #define WRITE_ERRCHECK(fd, msgbuf, msglen) do { \
if (write(fd, msgbuf, msglen) < 0) return -1; \ if (write(fd, msgbuf, msglen) < 0) return -1; \
} while (0); } while (0);
/* READ_ERRCHECK() decorated with error checking logic */ /* READ_ERRCHECK() decorated with error checking logic */
#define READ_ERRCHECK(fd, msgbuf, msglen) do { \ #define READ_ERRCHECK(fd, msgbuf, msglen) do { \
if (read(fd, msgbuf, msglen) != msglen) { \ if (read(fd, msgbuf, msglen) != msglen) { \
/*errno = EPROTO;*/ \ /*errno = EPROTO;*/ \
return -1; \ return -1; \
} \ } \
} while (0); } while (0);
/* convert 64bit integers from host to network format */ /* convert 64bit integers from host to network format */
#define hton64(x) (\ #define hton64(x) ( \
htons(1) == 1 ? \ htons(1) == 1 ? \
(uint64_t)x /* big endian */ \ (uint64_t)x /* big endian */ \
: /* little endian */ \ : /* little endian */ \
((uint64_t)((((uint64_t)(x) & 0xff00000000000000ULL) >> 56) | \ ((uint64_t)((((uint64_t)(x) & 0xff00000000000000ULL) >> 56) | \
(((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \ (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
(((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \ (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
(((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \ (((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
(((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \ (((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \
(((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \ (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
(((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \ (((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
(((uint64_t)(x) & 0x00000000000000ffULL) << 56))) \ (((uint64_t)(x) & 0x00000000000000ffULL) << 56))) \
) )
/* convert 64bit integers from network to host format */ /* convert 64bit integers from network to host format */
#define ntoh64(x) (hton64(x)) #define ntoh64(x) (hton64(x))
@ -206,12 +206,12 @@ static int list_attrOk(const list_t *restrict l);
/* do not inline, this is recursive */ /* do not inline, this is recursive */
static void list_sort_quicksort(list_t *restrict l, int versus, static void list_sort_quicksort(list_t *restrict l, int versus,
unsigned int first, struct list_entry_s *fel, unsigned int first, struct list_entry_s *fel,
unsigned int last, struct list_entry_s *lel); unsigned int last, struct list_entry_s *lel);
static inline void list_sort_selectionsort(list_t *restrict l, int versus, static inline void list_sort_selectionsort(list_t *restrict l, int versus,
unsigned int first, struct list_entry_s *fel, unsigned int first, struct list_entry_s *fel,
unsigned int last, struct list_entry_s *lel); unsigned int last, struct list_entry_s *lel);
static void *list_get_minmax(const list_t *restrict l, int versus); static void *list_get_minmax(const list_t *restrict l, int versus);
@ -792,8 +792,8 @@ static void *list_sort_quicksort_threadwrapper(void *wrapped_params) {
#endif #endif
static inline void list_sort_selectionsort(list_t *restrict l, int versus, static inline void list_sort_selectionsort(list_t *restrict l, int versus,
unsigned int first, struct list_entry_s *fel, unsigned int first, struct list_entry_s *fel,
unsigned int last, struct list_entry_s *lel) { unsigned int last, struct list_entry_s *lel) {
struct list_entry_s *cursor, *toswap, *firstunsorted; struct list_entry_s *cursor, *toswap, *firstunsorted;
void *tmpdata; void *tmpdata;
@ -813,8 +813,8 @@ static inline void list_sort_selectionsort(list_t *restrict l, int versus,
} }
static void list_sort_quicksort(list_t *restrict l, int versus, static void list_sort_quicksort(list_t *restrict l, int versus,
unsigned int first, struct list_entry_s *fel, unsigned int first, struct list_entry_s *fel,
unsigned int last, struct list_entry_s *lel) { unsigned int last, struct list_entry_s *lel) {
unsigned int pivotid; unsigned int pivotid;
unsigned int i; unsigned int i;
register struct list_entry_s *pivot; register struct list_entry_s *pivot;
@ -1094,15 +1094,15 @@ int list_dump_filedescriptor(const list_t *restrict l, int fd, size_t *restrict
/**** DUMP FORMAT **** /**** DUMP FORMAT ****
[ ver timestamp | totlen numels elemlen hash | DATA ] [ ver timestamp | totlen numels elemlen hash | DATA ]
where DATA can be: where DATA can be:
@ for constant-size list (element size is constant; elemlen > 0) @ for constant-size list (element size is constant; elemlen > 0)
[ elem elem ... elem ] [ elem elem ... elem ]
@ for other lists (element size dictated by element_meter each time; elemlen <= 0) @ for other lists (element size dictated by element_meter each time; elemlen <= 0)
[ size elem size elem ... size elem ] [ size elem size elem ... size elem ]
all integers are encoded in NETWORK BYTE FORMAT all integers are encoded in NETWORK BYTE FORMAT
*****/ *****/
@ -1327,10 +1327,10 @@ int list_restore_filedescriptor(list_t *restrict l, int fd, size_t *restrict len
/* possibly verify the list consistency */ /* possibly verify the list consistency */
/* wrt hash */ /* wrt hash */
/* don't do that /* don't do that
if (header.listhash != 0 && header.listhash != list_hash(l)) { if (header.listhash != 0 && header.listhash != list_hash(l)) {
errno = ECANCELED; errno = ECANCELED;
return -1; return -1;
} }
*/ */
/* wrt header */ /* wrt header */
@ -1487,14 +1487,14 @@ static int list_repOk(const list_t *restrict l) {
struct list_entry_s *s; struct list_entry_s *s;
ok = (l != NULL) && ( ok = (l != NULL) && (
/* head/tail checks */ /* head/tail checks */
(l->head_sentinel != NULL && l->tail_sentinel != NULL) && (l->head_sentinel != NULL && l->tail_sentinel != NULL) &&
(l->head_sentinel != l->tail_sentinel) && (l->head_sentinel->prev == NULL && l->tail_sentinel->next == NULL) && (l->head_sentinel != l->tail_sentinel) && (l->head_sentinel->prev == NULL && l->tail_sentinel->next == NULL) &&
/* empty list */ /* empty list */
(l->numels > 0 || (l->mid == NULL && l->head_sentinel->next == l->tail_sentinel && l->tail_sentinel->prev == l->head_sentinel)) && (l->numels > 0 || (l->mid == NULL && l->head_sentinel->next == l->tail_sentinel && l->tail_sentinel->prev == l->head_sentinel)) &&
/* spare elements checks */ /* spare elements checks */
l->spareelsnum <= SIMCLIST_MAX_SPARE_ELEMS l->spareelsnum <= SIMCLIST_MAX_SPARE_ELEMS
); );
if (!ok) return 0; if (!ok) return 0;
@ -1523,3 +1523,13 @@ static int list_attrOk(const list_t *restrict l) {
#endif #endif
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
*/

File diff suppressed because it is too large Load Diff

View File

@ -28,7 +28,7 @@
#include "table.h" #include "table.h"
static char *rcs_id = static char *rcs_id =
"$Id: table_util.c,v 1.5 2000/03/09 03:30:42 gray Exp $"; "$Id: table_util.c,v 1.5 2000/03/09 03:30:42 gray Exp $";
#define WRITE_MODE 0640 /* mode to write out table */ #define WRITE_MODE 0640 /* mode to write out table */
#define SPECIAL_CHARS "e\033^^\"\"''\\\\n\nr\rt\tb\bf\fa\007" #define SPECIAL_CHARS "e\033^^\"\"''\\\\n\nr\rt\tb\bf\fa\007"
@ -61,67 +61,67 @@ static char *rcs_id =
* out_size - size of the output buffer. * out_size - size of the output buffer.
*/ */
int expand_chars(const void *buf, const int buf_size, int expand_chars(const void *buf, const int buf_size,
char *out, const int out_size) char *out, const int out_size)
{ {
int buf_c; int buf_c;
const unsigned char *buf_p, *spec_p; const unsigned char *buf_p, *spec_p;
char *max_p, *out_p = out; char *max_p, *out_p = out;
/* setup our max pointer */ /* setup our max pointer */
max_p = out + out_size; max_p = out + out_size;
/* run through the input buffer, counting the characters as we go */ /* run through the input buffer, counting the characters as we go */
for (buf_c = 0, buf_p = (const unsigned char *)buf;; buf_c++, buf_p++) { for (buf_c = 0, buf_p = (const unsigned char *)buf;; buf_c++, buf_p++) {
/* did we reach the end of the buffer? */ /* did we reach the end of the buffer? */
if (buf_size < 0) { if (buf_size < 0) {
if (*buf_p == '\0') { if (*buf_p == '\0') {
break; break;
} }
} }
else { else {
if (buf_c >= buf_size) { if (buf_c >= buf_size) {
break; break;
} }
} }
/* search for special characters */ /* search for special characters */
for (spec_p = (unsigned char *)SPECIAL_CHARS + 1; for (spec_p = (unsigned char *)SPECIAL_CHARS + 1;
*(spec_p - 1) != '\0'; *(spec_p - 1) != '\0';
spec_p += 2) { spec_p += 2) {
if (*spec_p == *buf_p) { if (*spec_p == *buf_p) {
break; break;
} }
} }
/* did we find one? */ /* did we find one? */
if (*(spec_p - 1) != '\0') { if (*(spec_p - 1) != '\0') {
if (out_p + 2 >= max_p) { if (out_p + 2 >= max_p) {
break; break;
} }
(void)sprintf(out_p, "\\%c", *(spec_p - 1)); (void)sprintf(out_p, "\\%c", *(spec_p - 1));
out_p += 2; out_p += 2;
continue; continue;
} }
/* print out any 7-bit printable characters */ /* print out any 7-bit printable characters */
if (*buf_p < 128 && isprint(*buf_p)) { if (*buf_p < 128 && isprint(*buf_p)) {
if (out_p + 1 >= max_p) { if (out_p + 1 >= max_p) {
break; break;
} }
*out_p = *(char *)buf_p; *out_p = *(char *)buf_p;
out_p += 1; out_p += 1;
} }
else { else {
if (out_p + 4 >= max_p) { if (out_p + 4 >= max_p) {
break; break;
} }
(void)sprintf(out_p, "\\%03o", *buf_p); (void)sprintf(out_p, "\\%03o", *buf_p);
out_p += 4; out_p += 4;
} }
} }
return out_p - out; return out_p - out;
} }
/* /*
@ -141,22 +141,22 @@ int expand_chars(const void *buf, const int buf_size,
*/ */
static void dump_table(table_t *tab_p) static void dump_table(table_t *tab_p)
{ {
char buf[10240]; char buf[10240];
void *key_p, *data_p; void *key_p, *data_p;
int ret, key_size, data_size, len, entry_c; int ret, key_size, data_size, len, entry_c;
for (ret = table_first(tab_p, (void **)&key_p, &key_size, for (ret = table_first(tab_p, (void **)&key_p, &key_size,
(void **)&data_p, &data_size), entry_c = 0; (void **)&data_p, &data_size), entry_c = 0;
ret == TABLE_ERROR_NONE; ret == TABLE_ERROR_NONE;
ret = table_next(tab_p, (void **)&key_p, &key_size, ret = table_next(tab_p, (void **)&key_p, &key_size,
(void **)&data_p, &data_size), entry_c++) { (void **)&data_p, &data_size), entry_c++) {
/* expand the key */ /* expand the key */
len = expand_chars(key_p, key_size, buf, sizeof(buf)); len = expand_chars(key_p, key_size, buf, sizeof(buf));
(void)printf("%d: key '%.*s' (%d), ", entry_c, len, buf, len); (void)printf("%d: key '%.*s' (%d), ", entry_c, len, buf, len);
/* now dump the data */ /* now dump the data */
len = expand_chars(data_p, data_size, buf, sizeof(buf)); len = expand_chars(data_p, data_size, buf, sizeof(buf));
(void)printf("data '%.*s' (%d)\n", len, buf, len); (void)printf("data '%.*s' (%d)\n", len, buf, len);
} }
} }
/* /*
@ -176,120 +176,131 @@ static void dump_table(table_t *tab_p)
*/ */
static void usage(void) static void usage(void)
{ {
(void)fprintf(stderr, (void)fprintf(stderr,
"Usage: table_util\n" "Usage: table_util\n"
" [-b number] or --buckets num buckets to adjust table\n" " [-b number] or --buckets num buckets to adjust table\n"
" [-o file] or --out-file output filename\n" " [-o file] or --out-file output filename\n"
" [-v] or --verbose verbose messages\n" " [-v] or --verbose verbose messages\n"
" file input table filename\n"); " file input table filename\n");
exit(1); exit(1);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
table_t *tab_p; table_t *tab_p;
char do_write = 0, verbose = 0; char do_write = 0, verbose = 0;
char *out_file = NULL, *in_file; char *out_file = NULL, *in_file;
int ret, entry_n, bucket_n, num_buckets = 0; int ret, entry_n, bucket_n, num_buckets = 0;
/* process the args */ /* process the args */
for (argc--, argv++; argc > 0 && **argv == '-'; argc--, argv++) { for (argc--, argv++; argc > 0 && **argv == '-'; argc--, argv++) {
switch (*(*argv + 1)) { switch (*(*argv + 1)) {
case 'b': case 'b':
argc--, argv++; argc--, argv++;
if (argc == 0) { if (argc == 0) {
usage(); usage();
} }
num_buckets = atoi(*argv); num_buckets = atoi(*argv);
break; break;
case 'o': case 'o':
argc--, argv++; argc--, argv++;
if (argc == 0) { if (argc == 0) {
usage(); usage();
} }
out_file = *argv; out_file = *argv;
break; break;
case 'v': case 'v':
verbose = 1; verbose = 1;
break; break;
default: default:
usage(); usage();
break; break;
} }
} }
if (argc != 1) { if (argc != 1) {
usage(); usage();
} }
/* take the last argument as the input file */ /* take the last argument as the input file */
in_file = *argv; in_file = *argv;
/* read in the table from disk */ /* read in the table from disk */
tab_p = table_read(in_file, &ret); tab_p = table_read(in_file, &ret);
if (tab_p == NULL) { if (tab_p == NULL) {
(void)fprintf(stderr, "table_util: unable to table_read from '%s': %s\n", (void)fprintf(stderr, "table_util: unable to table_read from '%s': %s\n",
in_file, table_strerror(ret)); in_file, table_strerror(ret));
exit(1); exit(1);
} }
/* get info about the table */ /* get info about the table */
ret = table_info(tab_p, &bucket_n, &entry_n); ret = table_info(tab_p, &bucket_n, &entry_n);
if (ret != TABLE_ERROR_NONE) { if (ret != TABLE_ERROR_NONE) {
(void)fprintf(stderr, (void)fprintf(stderr,
"table_util: unable to get info on table in '%s': %s\n", "table_util: unable to get info on table in '%s': %s\n",
in_file, table_strerror(ret)); in_file, table_strerror(ret));
exit(1); exit(1);
} }
(void)printf("Read table of %d buckets and %d entries from '%s'\n", (void)printf("Read table of %d buckets and %d entries from '%s'\n",
bucket_n, entry_n, in_file); bucket_n, entry_n, in_file);
if (verbose) { if (verbose) {
dump_table(tab_p); dump_table(tab_p);
} }
if (num_buckets > 0) { if (num_buckets > 0) {
/* adjust the table's buckets */ /* adjust the table's buckets */
ret = table_adjust(tab_p, num_buckets); ret = table_adjust(tab_p, num_buckets);
if (ret != TABLE_ERROR_NONE) { if (ret != TABLE_ERROR_NONE) {
(void)fprintf(stderr, (void)fprintf(stderr,
"table_util: unable to adjust table to %d buckets: %s\n", "table_util: unable to adjust table to %d buckets: %s\n",
num_buckets, table_strerror(ret)); num_buckets, table_strerror(ret));
exit(1); exit(1);
} }
do_write = 1; do_write = 1;
} }
/* did we modify the table at all */ /* did we modify the table at all */
if (do_write) { if (do_write) {
if (out_file == NULL) { if (out_file == NULL) {
out_file = in_file; out_file = in_file;
} }
/* write out our table */ /* write out our table */
ret = table_write(tab_p, out_file, WRITE_MODE); ret = table_write(tab_p, out_file, WRITE_MODE);
if (ret != TABLE_ERROR_NONE) { if (ret != TABLE_ERROR_NONE) {
(void)fprintf(stderr, "table_util: unable to write table to '%s': %s\n", (void)fprintf(stderr, "table_util: unable to write table to '%s': %s\n",
out_file, table_strerror(ret)); out_file, table_strerror(ret));
exit(1); exit(1);
} }
(void)printf("Wrote table to '%s'\n", out_file); (void)printf("Wrote table to '%s'\n", out_file);
} }
/* free the table */ /* free the table */
ret = table_free(tab_p); ret = table_free(tab_p);
if (ret != TABLE_ERROR_NONE) { if (ret != TABLE_ERROR_NONE) {
(void)fprintf(stderr, "table_util: unable to free table: %s\n", (void)fprintf(stderr, "table_util: unable to free table: %s\n",
table_strerror(ret)); table_strerror(ret));
/* NOTE: not a critical error */ /* NOTE: not a critical error */
} }
exit(0); exit(0);
} }
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
*/