提交 3ddaf0f2 authored 作者: Mike Jerris's avatar Mike Jerris

cleanup new libks deleted files

上级 9fd0b6bd
PWD=$(shell pwd)
INCS=-I$(PWD)/src/include
DEBUG=-g -ggdb
BASE_FLAGS=$(INCS) $(DEBUG) -I$(LIBEDIT_DIR)/src/ -fPIC
PICKY=-O2
CFLAGS=$(BASE_FLAGS) $(PICKY)
CXXFLAGS=$(BASE_FLAGS)
MYLIB=libks.a
LIBS=-lncurses -lks -lpthread -lm
LDFLAGS=-L.
OBJS=src/ks.o src/ks_threadmutex.o src/ks_config.o src/ks_json.o src/ks_buffer.o src/mpool.o src/table.o src/table_util.o src/simclist.o
SRC=src/ks.c src/ks_json.c src/ks_threadmutex.c src/ks_config.c src/ks_json.c src/ks_buffer.c src/mpool.c src/table.c src/table_util.c src/simclist.c
HEADERS=src/include/ks_config.h src/include/ks.h src/include/ks_threadmutex.h src/include/ks_json.h src/include/ks_buffer.h src/include/mpool.h src/include/mpool_loc.h src/include/table.h src/include/table_loc.h src/include/simclist.h
SOLINK=-shared -Xlinker -x
all: $(MYLIB)
$(MYLIB): $(OBJS) $(HEADERS) $(SRC)
ar rcs $(MYLIB) $(OBJS)
ranlib $(MYLIB)
%.o: %.c $(HEADERS)
$(CC) $(CC_CFLAGS) $(CFLAGS) $(CXFLAGS) -c $< -o $@
test-clean:
rm -f test/testpools
clean: test-clean
rm -f *.o src/*.o libks.a *~ src/*~ src/include/*~
test/testpools: $(MYLIB) test/testpools.c
$(CC) $(CXFLAGS) test/testpools.c -Isrc/include/ libks.a -o test/testpools
test-all: test/testpools
const char *cc = ".========================================================================================================.\n| ____ _____ ____ _ ____ _ _ _____ |\n| / ___|___ _ __ ___ ___ |_ _|__ / ___| |_ _ ___ / ___|___ _ __ ( ) |___ / |\n| | | / _ \\| '_ ` _ \\ / _ \\ | |/ _ \\ | | | | | | |/ _ \\ | / _ \\| '_ \\ |/| | |_ \\ |\n| | |__| (_) | | | | | | __/ | | (_) | | |___| | |_| | __/ |__| (_) | | | | | |___) | |\n| \\____\\___/|_| |_| |_|\\___| |_|\\___/ \\____|_|\\__,_|\\___|\\____\\___/|_| |_| |_|____/ |\n| |\n| ____ _ _ _ _ ____ _ |\n| / ___| |__ (_) ___ __ _ __ _ ___ | | | / ___| / \\ |\n| | | | '_ \\| |/ __/ _` |/ _` |/ _ \\ | | | \\___ \\ / _ \\ |\n| | |___| | | | | (_| (_| | (_| | (_) | | |_| |___) / ___ \\ |\n| \\____|_| |_|_|\\___\\__,_|\\__, |\\___( ) \\___/|____/_/ \\_\\ |\n| |___/ |/ |\n| _ _ __ _ _ ___ _ _ ____ ___ _ _____ |\n| / \\ _ _ __ _ _ _ ___| |_ / /_ | |_| |__ ( _ )| |_| |__ |___ \\ / _ \\/ |___ / |\n| / _ \\| | | |/ _` | | | / __| __| | '_ \\| __| '_ \\ _____ / _ \\| __| '_ \\ __) | | | | | |_ \\ |\n| / ___ \\ |_| | (_| | |_| \\__ \\ |_ | (_) | |_| | | | |_____| | (_) | |_| | | | / __/| |_| | |___) | |\n| /_/ \\_\\__,_|\\__, |\\__,_|___/\\__| \\___/ \\__|_| |_| \\___/ \\__|_| |_| |_____|\\___/|_|____/ |\n| |___/ |\n| _ |\n| __ ____ ____ __ ___| |_ _ ___ ___ ___ _ __ ___ ___ _ __ ___ |\n| \\ \\ /\\ / /\\ \\ /\\ / /\\ \\ /\\ / / / __| | | | |/ _ \\/ __/ _ \\| '_ \\ / __/ _ \\| '_ ` _ \\ |\n| \\ V V / \\ V V / \\ V V / _ | (__| | |_| | __/ (_| (_) | | | | _ | (_| (_) | | | | | | |\n| \\_/\\_/ \\_/\\_/ \\_/\\_/ (_) \\___|_|\\__,_|\\___|\\___\\___/|_| |_| (_) \\___\\___/|_| |_| |_| |\n| |\n.========================================================================================================.\n";
差异被折叠。
/*
* Memory pool local defines.
*
* Copyright 1996 by Gray Watson.
*
* This file is part of the mpool package.
*
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies, and that the name of Gray Watson not be used in advertising
* or publicity pertaining to distribution of the document or software
* without specific, written prior permission.
*
* Gray Watson makes no representations about the suitability of the
* software described herein for any purpose. It is provided "as is"
* without express or implied warranty.
*
* The author may be reached via http://256.com/gray/
*
* $Id: mpool_loc.h,v 1.2 2005/05/20 20:08:54 gray Exp $
*/
#ifndef __MPOOL_LOC_H__
#define __MPOOL_LOC_H__
#define MPOOL_MAGIC 0xABACABA /* magic for struct */
#define BLOCK_MAGIC 0xB1B1007 /* magic for blocks */
#define FENCE_MAGIC0 (unsigned char)(0xFAU) /* 1st magic mem byte */
#define FENCE_MAGIC1 (unsigned char)(0xD3U) /* 2nd magic mem byte */
#define FENCE_SIZE 2 /* fence space */
#define MIN_ALLOCATION (sizeof(mpool_free_t)) /* min alloc */
#define MAX_FREE_SEARCH 10240 /* max size to search */
#define MAX_FREE_LIST_SEARCH 100 /* max looking for free mem */
/*
* bitflag tools for Variable and a Flag
*/
#define BIT_FLAG(x) (1 << (x))
#define BIT_SET(v,f) (v) |= (f)
#define BIT_CLEAR(v,f) (v) &= ~(f)
#define BIT_IS_SET(v,f) ((v) & (f))
#define BIT_TOGGLE(v,f) (v) ^= (f)
#define SET_POINTER(pnt, val) \
do { \
if ((pnt) != NULL) { \
(*(pnt)) = (val); \
} \
} while(0)
#define BLOCK_FLAG_USED BIT_FLAG(0) /* block is used */
#define BLOCK_FLAG_FREE BIT_FLAG(1) /* block is free */
#define DEFAULT_PAGE_MULT 16 /* pagesize = this * getpagesize*/
/* How many pages SIZE bytes resides in. We add in the block header. */
#define PAGES_IN_SIZE(mp_p, size) (((size) + sizeof(mpool_block_t) + \
(mp_p)->mp_page_size - 1) / \
(mp_p)->mp_page_size)
#define SIZE_OF_PAGES(mp_p, page_n) ((page_n) * (mp_p)->mp_page_size)
#define MAX_BITS 30 /* we only can allocate 1gb chunks */
#define MAX_BLOCK_USER_MEMORY(mp_p) ((mp_p)->mp_page_size - \
sizeof(mpool_block_t))
#define FIRST_ADDR_IN_BLOCK(block_p) (void *)((char *)(block_p) + \
sizeof(mpool_block_t))
#define MEMORY_IN_BLOCK(block_p) ((char *)(block_p)->mb_bounds_p - \
((char *)(block_p) + \
sizeof(mpool_block_t)))
typedef struct {
unsigned int mp_magic; /* magic number for struct */
unsigned int mp_flags; /* flags for the struct */
unsigned int mp_mmflags; /* flags for mmap */
unsigned long mp_alloc_c; /* number of allocations */
unsigned long mp_user_alloc; /* user bytes allocated */
unsigned long mp_max_alloc; /* maximum user bytes allocated */
unsigned int mp_page_c; /* number of pages allocated */
unsigned int mp_max_pages; /* maximum number of pages to use */
unsigned int mp_page_size; /* page-size of our system */
int mp_fd; /* fd for /dev/zero if mmap-ing */
off_t mp_top; /* top of our allocations in fd */
mpool_log_func_t mp_log_func; /* log callback function */
void *mp_addr; /* current address for mmaping */
void *mp_min_p; /* min address in pool for checks */
void *mp_bounds_p; /* max address in pool for checks */
struct mpool_block_st *mp_first_p; /* first memory block we are using */
struct mpool_block_st *mp_last_p; /* last memory block we are using */
struct mpool_block_st *mp_free[MAX_BITS + 1]; /* free lists based on size */
unsigned int mp_magic2; /* upper magic for overwrite sanity */
} mpool_t;
/* for debuggers to be able to interrogate the generic type in the .h file */
typedef mpool_t mpool_ext_t;
/*
* Block header structure. This structure *MUST* be long-word
* aligned.
*/
typedef struct mpool_block_st {
unsigned int mb_magic; /* magic number for block header */
void *mb_bounds_p; /* block boundary location */
struct mpool_block_st *mb_next_p; /* linked list next pointer */
unsigned int mb_magic2; /* upper magic for overwrite sanity */
} mpool_block_t;
/*
* Free list structure.
*/
typedef struct {
void *mf_next_p; /* pointer to the next free address */
unsigned long mf_size; /* size of the free block */
} mpool_free_t;
#endif /* ! __MPOOL_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:
*/
差异被折叠。
/*
* local defines for the table module
*
* Copyright 2000 by Gray Watson.
*
* This file is part of the table package.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose and without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies,
* and that the name of Gray Watson not be used in advertising or
* publicity pertaining to distribution of the document or software
* without specific, written prior permission.
*
* Gray Watson makes no representations about the suitability of the
* software described herein for any purpose. It is provided "as is"
* without express or implied warranty.
*
* The author may be reached via http://256.com/gray/
*
* $Id: table_loc.h,v 1.11 2000/03/09 03:30:42 gray Exp $
*/
#ifndef __TABLE_LOC_H__
#define __TABLE_LOC_H__
#ifndef unix
#define NO_MMAP
#endif
#ifndef BITSPERBYTE
#define BITSPERBYTE 8
#endif
#ifndef BITS
#define BITS(type) (BITSPERBYTE * (int)sizeof(type))
#endif
#define TABLE_MAGIC 0xBADF00D /* very magic magicness */
#define LINEAR_MAGIC 0xAD00D00 /* magic value for linear struct */
#define DEFAULT_SIZE 1024 /* default table size */
#define MAX_ALIGNMENT 128 /* max alignment value */
/*
* Maximum number of splits. This should mean that these routines can
* handle at least 2^128 different values (that's _quite_ a few). And
* then you can always increase the value.
*/
#define MAX_QSORT_SPLITS 128
/*
* Maximum number of entries that must be in list for it to be
* partitioned. If there are fewer elements then just do our
* insertion sort.
*/
#define MAX_QSORT_MANY 8
/*
* Macros.
*/
/* returns 1 when we should grow or shrink the table */
#define SHOULD_TABLE_GROW(tab) ((tab)->ta_entry_n > (tab)->ta_bucket_n * 2)
#define SHOULD_TABLE_SHRINK(tab) ((tab)->ta_entry_n < (tab)->ta_bucket_n / 2)
/*
* void HASH_MIX
*
* DESCRIPTION:
*
* Mix 3 32-bit values reversibly. For every delta with one or two
* bits set, and the deltas of all three high bits or all three low
* bits, whether the original value of a,b,c is almost all zero or is
* uniformly distributed.
*
* If HASH_MIX() is run forward or backward, at least 32 bits in a,b,c
* have at least 1/4 probability of changing. If mix() is run
* forward, every bit of c will change between 1/3 and 2/3 of the
* time. (Well, 22/100 and 78/100 for some 2-bit deltas.)
*
* HASH_MIX() takes 36 machine instructions, but only 18 cycles on a
* superscalar machine (like a Pentium or a Sparc). No faster mixer
* seems to work, that's the result of my brute-force search. There
* were about 2^68 hashes to choose from. I only tested about a
* billion of those.
*/
#define HASH_MIX(a, b, c) \
do { \
a -= b; a -= c; a ^= (c >> 13); \
b -= c; b -= a; b ^= (a << 8); \
c -= a; c -= b; c ^= (b >> 13); \
a -= b; a -= c; a ^= (c >> 12); \
b -= c; b -= a; b ^= (a << 16); \
c -= a; c -= b; c ^= (b >> 5); \
a -= b; a -= c; a ^= (c >> 3); \
b -= c; b -= a; b ^= (a << 10); \
c -= a; c -= b; c ^= (b >> 15); \
} while(0)
#define SET_POINTER(pnt, val) \
do { \
if ((pnt) != NULL) { \
(*(pnt)) = (val); \
} \
} while(0)
/*
* The following macros take care of the mmap case. When we are
* mmaping a table from a disk file, all of the pointers in the table
* structures are replaced with offsets into the file. The following
* macro, for each pointer, adds the starting address of the mmaped
* section onto each pointer/offset turning it back into a legitimate
* pointer.
*/
#ifdef NO_MMAP
#define TABLE_POINTER(table, type, pnt) (pnt)
#else
#define TABLE_POINTER(tab_p, type, pnt) \
((tab_p)->ta_mmap == NULL || (pnt) == NULL ? (pnt) : \
(type)((char *)((tab_p)->ta_mmap) + (long)(pnt)))
#endif
/*
* Macros to get at the key and the data pointers
*/
#define ENTRY_KEY_BUF(entry_p) ((entry_p)->te_key_buf)
#define ENTRY_DATA_BUF(tab_p, entry_p) \
(ENTRY_KEY_BUF(entry_p) + (entry_p)->te_key_size)
/*
* Table structures...
*/
/*
* HACK: this should be equiv as the table_entry_t without the key_buf
* char. We use this with the ENTRY_SIZE() macro above which solves
* the problem with the lack of the [0] GNU hack. We use the
* table_entry_t structure to better map the memory and make things
* faster.
*/
typedef struct table_shell_st {
unsigned int te_key_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 */
/* NOTE: this does not have the te_key_buf field here */
} table_shell_t;
/*
* Elements in the bucket linked-lists. The key[1] is the start of
* the key with the rest of the key and all of the data information
* packed in memory directly after the end of this structure.
*
* NOTE: if this structure is changed, the table_shell_t must be
* changed to match.
*/
typedef struct table_entry_st {
unsigned int te_key_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 */
unsigned char te_key_buf[1]; /* 1st byte of key buf */
} table_entry_t;
/* external structure for debuggers be able to see void */
typedef table_entry_t table_entry_ext_t;
/* main table structure */
typedef struct table_st {
unsigned int ta_magic; /* magic number */
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_entry_n; /* num of entries in all buckets */
unsigned int ta_data_align; /* data alignment value */
table_entry_t **ta_buckets; /* array of linked lists */
table_linear_t ta_linear; /* linear tracking */
struct table_st *ta_mmap; /* mmaped table */
unsigned long ta_file_size; /* size of on-disk space */
void *ta_mem_pool; /* pointer to some memory pool */
table_mem_alloc_t ta_alloc_func; /* memory allocation function */
table_mem_resize_t ta_resize_func; /* memory resize function */
table_mem_free_t ta_free_func; /* memory free function */
} table_t;
/* external table structure for debuggers */
typedef table_t table_ext_t;
/* local comparison functions */
typedef int (*compare_t)(const void *element1_p, const void *element2_p,
table_compare_t user_compare,
const table_t *table_p, int *err_bp);
/*
* to map error to string
*/
typedef struct {
int es_error; /* error number */
char *es_string; /* assocaited string */
} error_str_t;
static error_str_t errors[] = {
{ TABLE_ERROR_NONE, "no error" },
{ TABLE_ERROR_PNT, "invalid table pointer" },
{ TABLE_ERROR_ARG_NULL, "buffer argument is null" },
{ TABLE_ERROR_SIZE, "incorrect size argument" },
{ TABLE_ERROR_OVERWRITE, "key exists and no overwrite" },
{ TABLE_ERROR_NOT_FOUND, "key does not exist" },
{ TABLE_ERROR_ALLOC, "error allocating memory" },
{ TABLE_ERROR_LINEAR, "linear access not in progress" },
{ TABLE_ERROR_OPEN, "could not open file" },
{ TABLE_ERROR_SEEK, "could not seek to position in file" },
{ TABLE_ERROR_READ, "could not read from file" },
{ TABLE_ERROR_WRITE, "could not write to file" },
{ TABLE_ERROR_MMAP_NONE, "no mmap support compiled in library" },
{ TABLE_ERROR_MMAP, "could not mmap the file" },
{ TABLE_ERROR_MMAP_OP, "operation not valid on mmap files" },
{ TABLE_ERROR_EMPTY, "table is empty" },
{ TABLE_ERROR_NOT_EMPTY, "table contains data" },
{ TABLE_ERROR_ALIGNMENT, "invalid alignment value" },
{ TABLE_ERROR_COMPARE, "problems with internal comparison" },
{ TABLE_ERROR_FREE, "memory free error" },
{ 0 }
};
#define INVALID_ERROR "invalid error code"
#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:
*/
/*
* Cross Platform Thread/Mutex abstraction
* Copyright(C) 2007 Michael Jerris
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so.
*
* This work is provided under this license on an "as is" basis, without warranty of any kind,
* either expressed or implied, including, without limitation, warranties that the covered code
* is free of defects, merchantable, fit for a particular purpose or non-infringing. The entire
* risk as to the quality and performance of the covered code is with you. Should any covered
* code prove defective in any respect, you (not the initial developer or any other contributor)
* assume the cost of any necessary servicing, repair or correction. This disclaimer of warranty
* constitutes an essential part of this license. No use of any covered code is authorized hereunder
* except under this disclaimer.
*
*/
#ifdef WIN32
/* required for TryEnterCriticalSection definition. Must be defined before windows.h include */
#define _WIN32_WINNT 0x0400
#endif
#include "ks.h"
#include "ks_threadmutex.h"
#ifdef WIN32
#include <process.h>
#define KS_THREAD_CALLING_CONVENTION __stdcall
struct ks_mutex {
CRITICAL_SECTION mutex;
};
#else
#include <pthread.h>
#define KS_THREAD_CALLING_CONVENTION
struct ks_mutex {
pthread_mutex_t mutex;
};
#endif
struct ks_thread {
#ifdef WIN32
void *handle;
#else
pthread_t handle;
#endif
void *private_data;
ks_thread_function_t function;
size_t stack_size;
#ifndef WIN32
pthread_attr_t attribute;
#endif
};
size_t thread_default_stacksize = 240 * 1024;
void ks_thread_override_default_stacksize(size_t size)
{
thread_default_stacksize = size;
}
static void * KS_THREAD_CALLING_CONVENTION thread_launch(void *args)
{
void *exit_val;
ks_thread_t *thread = (ks_thread_t *)args;
exit_val = thread->function(thread, thread->private_data);
#ifndef WIN32
pthread_attr_destroy(&thread->attribute);
#endif
free(thread);
return exit_val;
}
KS_DECLARE(ks_status_t) ks_thread_create_detached(ks_thread_function_t func, void *data)
{
return ks_thread_create_detached_ex(func, data, thread_default_stacksize);
}
ks_status_t ks_thread_create_detached_ex(ks_thread_function_t func, void *data, size_t stack_size)
{
ks_thread_t *thread = NULL;
ks_status_t status = KS_FAIL;
if (!func || !(thread = (ks_thread_t *)malloc(sizeof(ks_thread_t)))) {
goto done;
}
thread->private_data = data;
thread->function = func;
thread->stack_size = stack_size;
#if defined(WIN32)
thread->handle = (void *)_beginthreadex(NULL, (unsigned)thread->stack_size, (unsigned int (__stdcall *)(void *))thread_launch, thread, 0, NULL);
if (!thread->handle) {
goto fail;
}
CloseHandle(thread->handle);
status = KS_SUCCESS;
goto done;
#else
if (pthread_attr_init(&thread->attribute) != 0) goto fail;
if (pthread_attr_setdetachstate(&thread->attribute, PTHREAD_CREATE_DETACHED) != 0) goto failpthread;
if (thread->stack_size && pthread_attr_setstacksize(&thread->attribute, thread->stack_size) != 0) goto failpthread;
if (pthread_create(&thread->handle, &thread->attribute, thread_launch, thread) != 0) goto failpthread;
status = KS_SUCCESS;
goto done;
failpthread:
pthread_attr_destroy(&thread->attribute);
#endif
fail:
if (thread) {
free(thread);
}
done:
return status;
}
KS_DECLARE(ks_status_t) ks_mutex_create(ks_mutex_t **mutex)
{
ks_status_t status = KS_FAIL;
#ifndef WIN32
pthread_mutexattr_t attr;
#endif
ks_mutex_t *check = NULL;
check = (ks_mutex_t *)malloc(sizeof(**mutex));
if (!check)
goto done;
#ifdef WIN32
InitializeCriticalSection(&check->mutex);
#else
if (pthread_mutexattr_init(&attr))
goto done;
if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE))
goto fail;
if (pthread_mutex_init(&check->mutex, &attr))
goto fail;
goto success;
fail:
pthread_mutexattr_destroy(&attr);
goto done;
success:
#endif
*mutex = check;
status = KS_SUCCESS;
done:
return status;
}
KS_DECLARE(ks_status_t) ks_mutex_destroy(ks_mutex_t **mutex)
{
ks_mutex_t *mp = *mutex;
*mutex = NULL;
if (!mp) {
return KS_FAIL;
}
#ifdef WIN32
DeleteCriticalSection(&mp->mutex);
#else
if (pthread_mutex_destroy(&mp->mutex))
return KS_FAIL;
#endif
free(mp);
return KS_SUCCESS;
}
KS_DECLARE(ks_status_t) ks_mutex_lock(ks_mutex_t *mutex)
{
#ifdef WIN32
EnterCriticalSection(&mutex->mutex);
#else
if (pthread_mutex_lock(&mutex->mutex))
return KS_FAIL;
#endif
return KS_SUCCESS;
}
KS_DECLARE(ks_status_t) ks_mutex_trylock(ks_mutex_t *mutex)
{
#ifdef WIN32
if (!TryEnterCriticalSection(&mutex->mutex))
return KS_FAIL;
#else
if (pthread_mutex_trylock(&mutex->mutex))
return KS_FAIL;
#endif
return KS_SUCCESS;
}
KS_DECLARE(ks_status_t) ks_mutex_unlock(ks_mutex_t *mutex)
{
#ifdef WIN32
LeaveCriticalSection(&mutex->mutex);
#else
if (pthread_mutex_unlock(&mutex->mutex))
return KS_FAIL;
#endif
return KS_SUCCESS;
}
/* 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:
*/
差异被折叠。
差异被折叠。
/*
* Hash table utility program.
*
* Copyright 2000 by Gray Watson
*
* This file is part of the table package.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose and without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies,
* and that the name of Gray Watson not be used in advertising or
* publicity pertaining to distribution of the document or software
* without specific, written prior permission.
*
* Gray Watson makes no representations about the suitability of the
* software described herein for any purpose. It is provided "as is"
* without express or implied warranty.
*
* The author may be reached via http://256.com/gray/
*
* $Id: table_util.c,v 1.5 2000/03/09 03:30:42 gray Exp $
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "table.h"
static char *rcs_id =
"$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 SPECIAL_CHARS "e\033^^\"\"''\\\\n\nr\rt\tb\bf\fa\007"
/*
* expand_chars
*
* DESCRIPTION:
*
* Copies a buffer into a output buffer while translates
* non-printables into %03o octal values. If it can, it will also
* translate certain \ characters (\r, \n, etc.) into \\%c. The
* routine is useful for printing out binary values.
*
* NOTE: It does _not_ add a \0 at the end of the output buffer.
*
* RETURNS:
*
* Returns the number of characters added to the output buffer.
*
* ARGUMENTS:
*
* buf - the buffer to convert.
*
* buf_size - size of the buffer. If < 0 then it will expand till it
* sees a \0 character.
*
* out - destination buffer for the convertion.
*
* out_size - size of the output buffer.
*/
int expand_chars(const void *buf, const int buf_size,
char *out, const int out_size)
{
int buf_c;
const unsigned char *buf_p, *spec_p;
char *max_p, *out_p = out;
/* setup our max pointer */
max_p = out + out_size;
/* 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++) {
/* did we reach the end of the buffer? */
if (buf_size < 0) {
if (*buf_p == '\0') {
break;
}
}
else {
if (buf_c >= buf_size) {
break;
}
}
/* search for special characters */
for (spec_p = (unsigned char *)SPECIAL_CHARS + 1;
*(spec_p - 1) != '\0';
spec_p += 2) {
if (*spec_p == *buf_p) {
break;
}
}
/* did we find one? */
if (*(spec_p - 1) != '\0') {
if (out_p + 2 >= max_p) {
break;
}
(void)sprintf(out_p, "\\%c", *(spec_p - 1));
out_p += 2;
continue;
}
/* print out any 7-bit printable characters */
if (*buf_p < 128 && isprint(*buf_p)) {
if (out_p + 1 >= max_p) {
break;
}
*out_p = *(char *)buf_p;
out_p += 1;
}
else {
if (out_p + 4 >= max_p) {
break;
}
(void)sprintf(out_p, "\\%03o", *buf_p);
out_p += 4;
}
}
return out_p - out;
}
/*
* dump_table
*
* DESCRIPTION:
*
* Dump a table file to the screen.
*
* RETURNS:
*
* None.
*
* ARGUMENTS:
*
* tab_p - a table pointer that we are dumping.
*/
static void dump_table(table_t *tab_p)
{
char buf[10240];
void *key_p, *data_p;
int ret, key_size, data_size, len, entry_c;
for (ret = table_first(tab_p, (void **)&key_p, &key_size,
(void **)&data_p, &data_size), entry_c = 0;
ret == TABLE_ERROR_NONE;
ret = table_next(tab_p, (void **)&key_p, &key_size,
(void **)&data_p, &data_size), entry_c++) {
/* expand the key */
len = expand_chars(key_p, key_size, buf, sizeof(buf));
(void)printf("%d: key '%.*s' (%d), ", entry_c, len, buf, len);
/* now dump the data */
len = expand_chars(data_p, data_size, buf, sizeof(buf));
(void)printf("data '%.*s' (%d)\n", len, buf, len);
}
}
/*
* usage
*
* DESCRIPTION:
*
* Print the usage message to stderr.
*
* RETURNS:
*
* None.
*
* ARGUMENTS:
*
* tab_p - a table pointer that we are dumping.
*/
static void usage(void)
{
(void)fprintf(stderr,
"Usage: table_util\n"
" [-b number] or --buckets num buckets to adjust table\n"
" [-o file] or --out-file output filename\n"
" [-v] or --verbose verbose messages\n"
" file input table filename\n");
exit(1);
}
int main(int argc, char **argv)
{
table_t *tab_p;
char do_write = 0, verbose = 0;
char *out_file = NULL, *in_file;
int ret, entry_n, bucket_n, num_buckets = 0;
/* process the args */
for (argc--, argv++; argc > 0 && **argv == '-'; argc--, argv++) {
switch (*(*argv + 1)) {
case 'b':
argc--, argv++;
if (argc == 0) {
usage();
}
num_buckets = atoi(*argv);
break;
case 'o':
argc--, argv++;
if (argc == 0) {
usage();
}
out_file = *argv;
break;
case 'v':
verbose = 1;
break;
default:
usage();
break;
}
}
if (argc != 1) {
usage();
}
/* take the last argument as the input file */
in_file = *argv;
/* read in the table from disk */
tab_p = table_read(in_file, &ret);
if (tab_p == NULL) {
(void)fprintf(stderr, "table_util: unable to table_read from '%s': %s\n",
in_file, table_strerror(ret));
exit(1);
}
/* get info about the table */
ret = table_info(tab_p, &bucket_n, &entry_n);
if (ret != TABLE_ERROR_NONE) {
(void)fprintf(stderr,
"table_util: unable to get info on table in '%s': %s\n",
in_file, table_strerror(ret));
exit(1);
}
(void)printf("Read table of %d buckets and %d entries from '%s'\n",
bucket_n, entry_n, in_file);
if (verbose) {
dump_table(tab_p);
}
if (num_buckets > 0) {
/* adjust the table's buckets */
ret = table_adjust(tab_p, num_buckets);
if (ret != TABLE_ERROR_NONE) {
(void)fprintf(stderr,
"table_util: unable to adjust table to %d buckets: %s\n",
num_buckets, table_strerror(ret));
exit(1);
}
do_write = 1;
}
/* did we modify the table at all */
if (do_write) {
if (out_file == NULL) {
out_file = in_file;
}
/* write out our table */
ret = table_write(tab_p, out_file, WRITE_MODE);
if (ret != TABLE_ERROR_NONE) {
(void)fprintf(stderr, "table_util: unable to write table to '%s': %s\n",
out_file, table_strerror(ret));
exit(1);
}
(void)printf("Wrote table to '%s'\n", out_file);
}
/* free the table */
ret = table_free(tab_p);
if (ret != TABLE_ERROR_NONE) {
(void)fprintf(stderr, "table_util: unable to free table: %s\n",
table_strerror(ret));
/* NOTE: not a critical error */
}
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:
*/
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{766F7FF4-CF39-4CDF-ABDC-4E9C88568F1F}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>testpools</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\src\include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\src\include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\src\include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\src\include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\testpools.c" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\libks.vcxproj">
<Project>{70d178d8-1100-4152-86c0-809a91cff832}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\testpools.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论