提交 af824783 authored 作者: Anthony Minessale's avatar Anthony Minessale

indent pass MODAPP-202

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@11335 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 d88a0fbe
......@@ -23,71 +23,73 @@
#include "bits.h"
#include "arraylist.h"
struct array_list*
array_list_new(array_list_free_fn *free_fn)
struct array_list *array_list_new(array_list_free_fn * free_fn)
{
struct array_list *this;
struct array_list *this;
if(!(this = calloc(1, sizeof(struct array_list)))) return NULL;
this->size = ARRAY_LIST_DEFAULT_SIZE;
this->length = 0;
this->free_fn = free_fn;
if(!(this->array = calloc(sizeof(void*), this->size))) {
free(this);
return NULL;
}
return this;
if (!(this = calloc(1, sizeof(struct array_list))))
return NULL;
this->size = ARRAY_LIST_DEFAULT_SIZE;
this->length = 0;
this->free_fn = free_fn;
if (!(this->array = calloc(sizeof(void *), this->size))) {
free(this);
return NULL;
}
return this;
}
extern void
array_list_free(struct array_list *this)
extern void array_list_free(struct array_list *this)
{
int i;
for(i = 0; i < this->length; i++)
if(this->array[i]) this->free_fn(this->array[i]);
free(this->array);
free(this);
int i;
for (i = 0; i < this->length; i++)
if (this->array[i])
this->free_fn(this->array[i]);
free(this->array);
free(this);
}
void*
array_list_get_idx(struct array_list *this, int i)
void *array_list_get_idx(struct array_list *this, int i)
{
if(i >= this->length) return NULL;
return this->array[i];
if (i >= this->length)
return NULL;
return this->array[i];
}
static int array_list_expand_internal(struct array_list *this, int max)
{
void *t;
int new_size;
void *t;
int new_size;
if(max < this->size) return 0;
new_size = max(this->size << 1, max);
if(!(t = realloc(this->array, new_size*sizeof(void*)))) return -1;
this->array = t;
(void)memset(this->array + this->size, 0, (new_size-this->size)*sizeof(void*));
this->size = new_size;
return 0;
if (max < this->size)
return 0;
new_size = max(this->size << 1, max);
if (!(t = realloc(this->array, new_size * sizeof(void *))))
return -1;
this->array = t;
(void) memset(this->array + this->size, 0, (new_size - this->size) * sizeof(void *));
this->size = new_size;
return 0;
}
int
array_list_put_idx(struct array_list *this, int idx, void *data)
int array_list_put_idx(struct array_list *this, int idx, void *data)
{
if(array_list_expand_internal(this, idx)) return -1;
if(this->array[idx]) this->free_fn(this->array[idx]);
this->array[idx] = data;
if(this->length <= idx) this->length = idx + 1;
return 0;
if (array_list_expand_internal(this, idx))
return -1;
if (this->array[idx])
this->free_fn(this->array[idx]);
this->array[idx] = data;
if (this->length <= idx)
this->length = idx + 1;
return 0;
}
int
array_list_add(struct array_list *this, void *data)
int array_list_add(struct array_list *this, void *data)
{
return array_list_put_idx(this, this->length, data);
return array_list_put_idx(this, this->length, data);
}
int
array_list_length(struct array_list *this)
int array_list_length(struct array_list *this)
{
return this->length;
return this->length;
}
......@@ -16,30 +16,23 @@
typedef void (array_list_free_fn) (void *data);
struct array_list
{
void **array;
int length;
int size;
array_list_free_fn *free_fn;
struct array_list {
void **array;
int length;
int size;
array_list_free_fn *free_fn;
};
extern struct array_list*
array_list_new(array_list_free_fn *free_fn);
extern struct array_list *array_list_new(array_list_free_fn * free_fn);
extern void
array_list_free(struct array_list *al);
extern void array_list_free(struct array_list *al);
extern void*
array_list_get_idx(struct array_list *al, int i);
extern void *array_list_get_idx(struct array_list *al, int i);
extern int
array_list_put_idx(struct array_list *al, int i, void *data);
extern int array_list_put_idx(struct array_list *al, int i, void *data);
extern int
array_list_add(struct array_list *al, void *data);
extern int array_list_add(struct array_list *al, void *data);
extern int
array_list_length(struct array_list *al);
extern int array_list_length(struct array_list *al);
#endif
......@@ -33,48 +33,55 @@
static int _syslog = 0;
static int _debug = 0;
void mc_set_debug(int debug) { _debug = debug; }
int mc_get_debug() { return _debug; }
void mc_set_debug(int debug)
{
_debug = debug;
}
int mc_get_debug()
{
return _debug;
}
extern void mc_set_syslog(int syslog)
{
_syslog = syslog;
_syslog = syslog;
}
void mc_abort(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
va_list ap;
va_start(ap, msg);
#if HAVE_VSYSLOG
if(_syslog) {
vsyslog(LOG_ERR, msg, ap);
} else
if (_syslog) {
vsyslog(LOG_ERR, msg, ap);
} else
#endif
vprintf(msg, ap);
exit(1);
vprintf(msg, ap);
exit(1);
}
void mc_debug(const char *msg, ...)
{
va_list ap;
if(_debug) {
va_start(ap, msg);
va_list ap;
if (_debug) {
va_start(ap, msg);
#if HAVE_VSYSLOG
if(_syslog) {
vsyslog(LOG_DEBUG, msg, ap);
} else
if (_syslog) {
vsyslog(LOG_DEBUG, msg, ap);
} else
#endif
vprintf(msg, ap);
}
vprintf(msg, ap);
}
}
void mc_error(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
va_list ap;
va_start(ap, msg);
#if HAVE_VSYSLOG
if(_syslog) {
if (_syslog) {
vsyslog(LOG_ERR, msg, ap);
} else
#endif
......@@ -83,12 +90,12 @@ void mc_error(const char *msg, ...)
void mc_info(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
va_list ap;
va_start(ap, msg);
#if HAVE_VSYSLOG
if(_syslog) {
if (_syslog) {
vsyslog(LOG_INFO, msg, ap);
} else
} else
#endif
vfprintf(stderr, msg, ap);
}
......@@ -74,65 +74,65 @@ do {\
#define STATUS_CODE_LEN 4
typedef enum tokens {
VERSION,
STATUS_CODE,
PHRASE,
HEADER,
NEWLINE,
SYNTAX_ERROR
VERSION,
STATUS_CODE,
PHRASE,
HEADER,
NEWLINE,
SYNTAX_ERROR
} token_t;
typedef enum accept_states {
ASWR,
ASNR,
NOAS
ASWR,
ASNR,
NOAS
} accept_state_t;
typedef struct state_machines {
accept_state_t (*state)(char, struct state_machines *);
int pos;
char *buf;
ssize_t buf_len;
int stop;
token_t token;
accept_state_t(*state) (char, struct state_machines *);
int pos;
char *buf;
ssize_t buf_len;
int stop;
token_t token;
} state_machine_t;
typedef enum http_methods {
GET,
HEAD,
POST,
DELETE,
PUT
GET,
HEAD,
POST,
DELETE,
PUT
} http_method_t;
typedef struct http_headers {
char *field_name;
char *value;
char *field_name;
char *value;
} http_header_t;
typedef struct http_requests {
http_method_t method;
char *version;
char *url;
http_header_t *headers;
ssize_t header_len;
char *body;
ssize_t body_len;
http_method_t method;
char *version;
char *url;
http_header_t *headers;
ssize_t header_len;
char *body;
ssize_t body_len;
} http_request_t;
typedef struct http_responses {
char *version;
int status_code;
char *phrase;
http_header_t *headers;
ssize_t header_len;
char *body;
int body_len;
char *version;
int status_code;
char *phrase;
http_header_t *headers;
ssize_t header_len;
char *body;
int body_len;
} http_response_t;
token_t get_next_token(state_machine_t *sm);
int http_parse_response(char *buf, ssize_t buf_len, http_response_t *response);
int http_req(http_request_t *req, http_response_t *res);
void free_http_response(http_response_t *response);
token_t get_next_token(state_machine_t * sm);
int http_parse_response(char *buf, ssize_t buf_len, http_response_t * response);
int http_req(http_request_t * req, http_response_t * res);
void free_http_response(http_response_t * response);
#endif
......@@ -27,5 +27,4 @@ extern "C" {
#ifdef __cplusplus
}
#endif
#endif
......@@ -35,13 +35,13 @@ struct json_object_iter;
/* supported object types */
enum json_type {
json_type_null,
json_type_boolean,
json_type_double,
json_type_int,
json_type_object,
json_type_array,
json_type_string
json_type_null,
json_type_boolean,
json_type_double,
json_type_int,
json_type_object,
json_type_array,
json_type_string
};
/* reference counting functions */
......@@ -50,7 +50,7 @@ enum json_type {
* Increment the reference count of json_object
* @param obj the json_object instance
*/
extern struct json_object* json_object_get(struct json_object *obj);
extern struct json_object *json_object_get(struct json_object *obj);
/**
* Decrement the reference count of json_object and free if it reaches zero
......@@ -90,7 +90,7 @@ extern enum json_type json_object_get_type(struct json_object *obj);
* @param obj the json_object instance
* @returns a string in JSON format
*/
extern char* json_object_to_json_string(struct json_object *obj);
extern char *json_object_to_json_string(struct json_object *obj);
/* object type methods */
......@@ -98,13 +98,13 @@ extern char* json_object_to_json_string(struct json_object *obj);
/** Create a new empty object
* @returns a json_object of type json_type_object
*/
extern struct json_object* json_object_new_object();
extern struct json_object *json_object_new_object();
/** Get the hashtable of a json_object of type json_type_object
* @param obj the json_object instance
* @returns a linkhash
*/
extern struct lh_table* json_object_get_object(struct json_object *obj);
extern struct lh_table *json_object_get_object(struct json_object *obj);
/** Add an object field to a json_object of type json_type_object
*
......@@ -116,16 +116,14 @@ extern struct lh_table* json_object_get_object(struct json_object *obj);
* @param key the object field name (a private copy will be duplicated)
* @param val a json_object or NULL member to associate with the given field
*/
extern void json_object_object_add(struct json_object* obj, char *key,
struct json_object *val);
extern void json_object_object_add(struct json_object *obj, char *key, struct json_object *val);
/** Get the json_object associate with a given object field
* @param obj the json_object instance
* @param key the object field name
* @returns the json_object associated with the given field name
*/
extern struct json_object* json_object_object_get(struct json_object* obj,
char *key);
extern struct json_object *json_object_object_get(struct json_object *obj, char *key);
/** Delete the given json_object field
*
......@@ -134,7 +132,7 @@ extern struct json_object* json_object_object_get(struct json_object* obj,
* @param obj the json_object instance
* @param key the object field name
*/
extern void json_object_object_del(struct json_object* obj, char *key);
extern void json_object_object_del(struct json_object *obj, char *key);
/** Iterate through all keys and values of an object
* @param obj the json_object instance
......@@ -165,13 +163,13 @@ extern void json_object_object_del(struct json_object* obj, char *key);
/** Create a new empty json_object of type json_type_array
* @returns a json_object of type json_type_array
*/
extern struct json_object* json_object_new_array();
extern struct json_object *json_object_new_array();
/** Get the arraylist of a json_object of type json_type_array
* @param obj the json_object instance
* @returns an arraylist
*/
extern struct array_list* json_object_get_array(struct json_object *obj);
extern struct array_list *json_object_get_array(struct json_object *obj);
/** Get the length of a json_object of type json_type_array
* @param obj the json_object instance
......@@ -188,8 +186,7 @@ extern int json_object_array_length(struct json_object *obj);
* @param obj the json_object instance
* @param val the json_object to be added
*/
extern int json_object_array_add(struct json_object *obj,
struct json_object *val);
extern int json_object_array_add(struct json_object *obj, struct json_object *val);
/** Insert or replace an element at a specified index in an array (a json_object of type json_type_array)
*
......@@ -206,16 +203,14 @@ extern int json_object_array_add(struct json_object *obj,
* @param idx the index to insert the element at
* @param val the json_object to be added
*/
extern int json_object_array_put_idx(struct json_object *obj, int idx,
struct json_object *val);
extern int json_object_array_put_idx(struct json_object *obj, int idx, struct json_object *val);
/** Get the element at specificed index of the array (a json_object of type json_type_array)
* @param obj the json_object instance
* @param idx the index to get the element at
* @returns the json_object at the specified index (or NULL)
*/
extern struct json_object* json_object_array_get_idx(struct json_object *obj,
int idx);
extern struct json_object *json_object_array_get_idx(struct json_object *obj, int idx);
/* boolean type methods */
......@@ -223,7 +218,7 @@ extern struct json_object* json_object_array_get_idx(struct json_object *obj,
* @param b a boolean TRUE or FALSE (0 or 1)
* @returns a json_object of type json_type_boolean
*/
extern struct json_object* json_object_new_boolean(boolean b);
extern struct json_object *json_object_new_boolean(boolean b);
/** Get the boolean value of a json_object
*
......@@ -245,7 +240,7 @@ extern boolean json_object_get_boolean(struct json_object *obj);
* @param i the integer
* @returns a json_object of type json_type_int
*/
extern struct json_object* json_object_new_int(int i);
extern struct json_object *json_object_new_int(int i);
/** Get the int value of a json_object
*
......@@ -265,7 +260,7 @@ extern int json_object_get_int(struct json_object *obj);
* @param d the double
* @returns a json_object of type json_type_double
*/
extern struct json_object* json_object_new_double(double d);
extern struct json_object *json_object_new_double(double d);
/** Get the double value of a json_object
*
......@@ -288,9 +283,9 @@ extern double json_object_get_double(struct json_object *obj);
* @param s the string
* @returns a json_object of type json_type_string
*/
extern struct json_object* json_object_new_string(char *s);
extern struct json_object *json_object_new_string(char *s);
extern struct json_object* json_object_new_string_len(char *s, int len);
extern struct json_object *json_object_new_string_len(char *s, int len);
/** Get the string value of a json_object
*
......@@ -303,6 +298,6 @@ extern struct json_object* json_object_new_string_len(char *s, int len);
* @param obj the json_object instance
* @returns a string
*/
extern char* json_object_get_string(struct json_object *obj);
extern char *json_object_get_string(struct json_object *obj);
#endif
......@@ -12,30 +12,27 @@
#ifndef _json_object_private_h_
#define _json_object_private_h_
typedef void (json_object_delete_fn)(struct json_object *o);
typedef int (json_object_to_json_string_fn)(struct json_object *o,
struct printbuf *pb);
typedef void (json_object_delete_fn) (struct json_object * o);
typedef int (json_object_to_json_string_fn) (struct json_object * o, struct printbuf * pb);
struct json_object
{
enum json_type o_type;
json_object_delete_fn *_delete;
json_object_to_json_string_fn *_to_json_string;
int _ref_count;
struct printbuf *_pb;
union data {
boolean c_boolean;
double c_double;
int c_int;
struct lh_table *c_object;
struct array_list *c_array;
char *c_string;
} o;
struct json_object {
enum json_type o_type;
json_object_delete_fn *_delete;
json_object_to_json_string_fn *_to_json_string;
int _ref_count;
struct printbuf *_pb;
union data {
boolean c_boolean;
double c_double;
int c_int;
struct lh_table *c_object;
struct array_list *c_array;
char *c_string;
} o;
};
/* CAW: added for ANSI C iteration correctness */
struct json_object_iter
{
struct json_object_iter {
char *key;
struct json_object *val;
struct lh_entry *entry;
......
......@@ -15,75 +15,72 @@
#include "json_object.h"
enum json_tokener_error {
json_tokener_success,
json_tokener_continue,
json_tokener_error_depth,
json_tokener_error_parse_eof,
json_tokener_error_parse_unexpected,
json_tokener_error_parse_null,
json_tokener_error_parse_boolean,
json_tokener_error_parse_number,
json_tokener_error_parse_array,
json_tokener_error_parse_object_key_name,
json_tokener_error_parse_object_key_sep,
json_tokener_error_parse_object_value_sep,
json_tokener_error_parse_string,
json_tokener_error_parse_comment
json_tokener_success,
json_tokener_continue,
json_tokener_error_depth,
json_tokener_error_parse_eof,
json_tokener_error_parse_unexpected,
json_tokener_error_parse_null,
json_tokener_error_parse_boolean,
json_tokener_error_parse_number,
json_tokener_error_parse_array,
json_tokener_error_parse_object_key_name,
json_tokener_error_parse_object_key_sep,
json_tokener_error_parse_object_value_sep,
json_tokener_error_parse_string,
json_tokener_error_parse_comment
};
enum json_tokener_state {
json_tokener_state_eatws,
json_tokener_state_start,
json_tokener_state_finish,
json_tokener_state_null,
json_tokener_state_comment_start,
json_tokener_state_comment,
json_tokener_state_comment_eol,
json_tokener_state_comment_end,
json_tokener_state_string,
json_tokener_state_string_escape,
json_tokener_state_escape_unicode,
json_tokener_state_boolean,
json_tokener_state_number,
json_tokener_state_array,
json_tokener_state_array_add,
json_tokener_state_array_sep,
json_tokener_state_object_field_start,
json_tokener_state_object_field,
json_tokener_state_object_field_end,
json_tokener_state_object_value,
json_tokener_state_object_value_add,
json_tokener_state_object_sep
json_tokener_state_eatws,
json_tokener_state_start,
json_tokener_state_finish,
json_tokener_state_null,
json_tokener_state_comment_start,
json_tokener_state_comment,
json_tokener_state_comment_eol,
json_tokener_state_comment_end,
json_tokener_state_string,
json_tokener_state_string_escape,
json_tokener_state_escape_unicode,
json_tokener_state_boolean,
json_tokener_state_number,
json_tokener_state_array,
json_tokener_state_array_add,
json_tokener_state_array_sep,
json_tokener_state_object_field_start,
json_tokener_state_object_field,
json_tokener_state_object_field_end,
json_tokener_state_object_value,
json_tokener_state_object_value_add,
json_tokener_state_object_sep
};
struct json_tokener_srec
{
enum json_tokener_state state, saved_state;
struct json_object *obj;
struct json_object *current;
char *obj_field_name;
struct json_tokener_srec {
enum json_tokener_state state, saved_state;
struct json_object *obj;
struct json_object *current;
char *obj_field_name;
};
#define JSON_TOKENER_MAX_DEPTH 32
struct json_tokener
{
char *str;
struct printbuf *pb;
int depth, is_double, st_pos, char_offset;
enum json_tokener_error err;
unsigned int ucs_char;
char quote_char;
struct json_tokener_srec stack[JSON_TOKENER_MAX_DEPTH];
struct json_tokener {
char *str;
struct printbuf *pb;
int depth, is_double, st_pos, char_offset;
enum json_tokener_error err;
unsigned int ucs_char;
char quote_char;
struct json_tokener_srec stack[JSON_TOKENER_MAX_DEPTH];
};
extern const char* json_tokener_errors[];
extern const char *json_tokener_errors[];
extern struct json_tokener* json_tokener_new();
extern struct json_tokener *json_tokener_new();
extern void json_tokener_free(struct json_tokener *tok);
extern void json_tokener_reset(struct json_tokener *tok);
extern struct json_object* json_tokener_parse(char *str);
extern struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
char *str, int len);
extern struct json_object *json_tokener_parse(char *str);
extern struct json_object *json_tokener_parse_ex(struct json_tokener *tok, char *str, int len);
#endif
......@@ -51,71 +51,69 @@
#include "json_tokener.h"
#include "json_util.h"
struct json_object* json_object_from_file(char *filename)
struct json_object *json_object_from_file(char *filename)
{
struct printbuf *pb;
struct json_object *obj;
char buf[JSON_FILE_BUF_SIZE];
int fd, ret;
if((fd = open(filename, O_RDONLY)) < 0) {
mc_error("json_object_from_file: error reading file %s: %s\n",
filename, strerror(errno));
return error_ptr(-1);
}
if(!(pb = printbuf_new())) {
mc_error("json_object_from_file: printbuf_new failed\n");
return error_ptr(-1);
}
while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
printbuf_memappend(pb, buf, ret);
}
close(fd);
if(ret < 0) {
mc_abort("json_object_from_file: error reading file %s: %s\n",
filename, strerror(errno));
printbuf_free(pb);
return error_ptr(-1);
}
obj = json_tokener_parse(pb->buf);
printbuf_free(pb);
return obj;
struct printbuf *pb;
struct json_object *obj;
char buf[JSON_FILE_BUF_SIZE];
int fd, ret;
if ((fd = open(filename, O_RDONLY)) < 0) {
mc_error("json_object_from_file: error reading file %s: %s\n", filename, strerror(errno));
return error_ptr(-1);
}
if (!(pb = printbuf_new())) {
mc_error("json_object_from_file: printbuf_new failed\n");
return error_ptr(-1);
}
while ((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
printbuf_memappend(pb, buf, ret);
}
close(fd);
if (ret < 0) {
mc_abort("json_object_from_file: error reading file %s: %s\n", filename, strerror(errno));
printbuf_free(pb);
return error_ptr(-1);
}
obj = json_tokener_parse(pb->buf);
printbuf_free(pb);
return obj;
}
int json_object_to_file(char *filename, struct json_object *obj)
{
char *json_str;
int fd, ret;
unsigned int wpos, wsize;
if(!obj) {
mc_error("json_object_to_file: object is null\n");
return -1;
}
if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
mc_error("json_object_to_file: error opening file %s: %s\n",
filename, strerror(errno));
return -1;
}
if(!(json_str = json_object_to_json_string(obj))) { return -1; }
wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
wpos = 0;
while(wpos < wsize) {
if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
close(fd);
mc_error("json_object_to_file: error writing file %s: %s\n",
filename, strerror(errno));
return -1;
}
/* because of the above check for ret < 0, we can safely cast and add */
wpos += (unsigned int)ret;
}
close(fd);
return 0;
char *json_str;
int fd, ret;
unsigned int wpos, wsize;
if (!obj) {
mc_error("json_object_to_file: object is null\n");
return -1;
}
if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
mc_error("json_object_to_file: error opening file %s: %s\n", filename, strerror(errno));
return -1;
}
if (!(json_str = json_object_to_json_string(obj))) {
return -1;
}
wsize = (unsigned int) (strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
wpos = 0;
while (wpos < wsize) {
if ((ret = write(fd, json_str + wpos, wsize - wpos)) < 0) {
close(fd);
mc_error("json_object_to_file: error writing file %s: %s\n", filename, strerror(errno));
return -1;
}
/* because of the above check for ret < 0, we can safely cast and add */
wpos += (unsigned int) ret;
}
close(fd);
return 0;
}
......@@ -17,7 +17,7 @@
#define JSON_FILE_BUF_SIZE 4096
/* utlitiy functions */
extern struct json_object* json_object_from_file(char *filename);
extern struct json_object *json_object_from_file(char *filename);
extern int json_object_to_file(char *filename, struct json_object *obj);
#endif
......@@ -31,7 +31,7 @@ void lh_abort(const char *msg, ...)
uint32_t lh_ptr_hash(void *k)
{
/* CAW: refactored to be 64bit nice */
return (uint32_t)((((ptrdiff_t)k * LH_PRIME) >> 4) & ULONG_MAX);
return (uint32_t) ((((ptrdiff_t) k * LH_PRIME) >> 4) & ULONG_MAX);
}
int lh_ptr_equal(void *k1, void *k2)
......@@ -42,48 +42,47 @@ int lh_ptr_equal(void *k1, void *k2)
uint32_t lh_char_hash(void *k)
{
unsigned int h = 0;
const char* data = k;
while( *data!=0 ) h = h*129 + (unsigned int)(*data++) + LH_PRIME;
const char *data = k;
while (*data != 0)
h = h * 129 + (unsigned int) (*data++) + LH_PRIME;
return h;
}
int lh_char_equal(void *k1, void *k2)
{
return (strcmp((char*)k1, (char*)k2) == 0);
return (strcmp((char *) k1, (char *) k2) == 0);
}
struct lh_table* lh_table_new(int size, char *name,
lh_entry_free_fn *free_fn,
lh_hash_fn *hash_fn,
lh_equal_fn *equal_fn)
struct lh_table *lh_table_new(int size, char *name, lh_entry_free_fn * free_fn, lh_hash_fn * hash_fn, lh_equal_fn * equal_fn)
{
int i;
struct lh_table *t;
t = calloc(1, sizeof(struct lh_table));
if(!t) lh_abort("lh_table_new: calloc failed\n");
if (!t)
lh_abort("lh_table_new: calloc failed\n");
t->count = 0;
t->size = size;
t->name = name;
t->table = calloc(size, sizeof(struct lh_entry));
if(!t->table) lh_abort("lh_table_new: calloc failed\n");
if (!t->table)
lh_abort("lh_table_new: calloc failed\n");
t->free_fn = free_fn;
t->hash_fn = hash_fn;
t->equal_fn = equal_fn;
for(i = 0; i < size; i++) t->table[i].k = LH_EMPTY;
for (i = 0; i < size; i++)
t->table[i].k = LH_EMPTY;
return t;
}
struct lh_table* lh_kchar_table_new(int size, char *name,
lh_entry_free_fn *free_fn)
struct lh_table *lh_kchar_table_new(int size, char *name, lh_entry_free_fn * free_fn)
{
return lh_table_new(size, name, free_fn, lh_char_hash, lh_char_equal);
}
struct lh_table* lh_kptr_table_new(int size, char *name,
lh_entry_free_fn *free_fn)
struct lh_table *lh_kptr_table_new(int size, char *name, lh_entry_free_fn * free_fn)
{
return lh_table_new(size, name, free_fn, lh_ptr_hash, lh_ptr_equal);
}
......@@ -95,7 +94,7 @@ void lh_table_resize(struct lh_table *t, int new_size)
new_t = lh_table_new(new_size, t->name, NULL, t->hash_fn, t->equal_fn);
ent = t->head;
while(ent) {
while (ent) {
lh_table_insert(new_t, ent->k, ent->v);
ent = ent->next;
}
......@@ -111,8 +110,8 @@ void lh_table_resize(struct lh_table *t, int new_size)
void lh_table_free(struct lh_table *t)
{
struct lh_entry *c;
for(c = t->head; c != NULL; c = c->next) {
if(t->free_fn) {
for (c = t->head; c != NULL; c = c->next) {
if (t->free_fn) {
t->free_fn(c);
}
}
......@@ -126,22 +125,25 @@ int lh_table_insert(struct lh_table *t, void *k, void *v)
uint32_t h, n;
t->inserts++;
if(t->count > t->size * 0.66) lh_table_resize(t, t->size * 2);
if (t->count > t->size * 0.66)
lh_table_resize(t, t->size * 2);
h = t->hash_fn(k);
n = h % t->size;
while( 1 ) {
if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) break;
while (1) {
if (t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED)
break;
t->collisions++;
if(++n == t->size) n = 0;
if (++n == t->size)
n = 0;
}
t->table[n].k = k;
t->table[n].v = v;
t->count++;
if(t->head == NULL) {
if (t->head == NULL) {
t->head = t->tail = &t->table[n];
t->table[n].next = t->table[n].prev = NULL;
} else {
......@@ -155,43 +157,50 @@ int lh_table_insert(struct lh_table *t, void *k, void *v)
}
struct lh_entry* lh_table_lookup_entry(struct lh_table *t, void *k)
struct lh_entry *lh_table_lookup_entry(struct lh_table *t, void *k)
{
uint32_t h = t->hash_fn(k);
uint32_t n = h % t->size;
t->lookups++;
while( 1 ) {
if(t->table[n].k == LH_EMPTY) return NULL;
if(t->table[n].k != LH_FREED &&
t->equal_fn(t->table[n].k, k)) return &t->table[n];
if(++n == t->size) n = 0;
while (1) {
if (t->table[n].k == LH_EMPTY)
return NULL;
if (t->table[n].k != LH_FREED && t->equal_fn(t->table[n].k, k))
return &t->table[n];
if (++n == t->size)
n = 0;
}
return NULL;
}
void* lh_table_lookup(struct lh_table *t, void *k)
void *lh_table_lookup(struct lh_table *t, void *k)
{
struct lh_entry *e = lh_table_lookup_entry(t, k);
if(e) return e->v;
if (e)
return e->v;
return NULL;
}
int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
{
ptrdiff_t n = (ptrdiff_t)(e - t->table); /* CAW: fixed to be 64bit nice, still need the crazy negative case... */
ptrdiff_t n = (ptrdiff_t) (e - t->table); /* CAW: fixed to be 64bit nice, still need the crazy negative case... */
/* CAW: this is bad, really bad, maybe stack goes other direction on this machine... */
if(n < 0) { return -2; }
if (n < 0) {
return -2;
}
if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) return -1;
if (t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED)
return -1;
t->count--;
if(t->free_fn) t->free_fn(e);
if (t->free_fn)
t->free_fn(e);
t->table[n].v = NULL;
t->table[n].k = LH_FREED;
if(t->tail == &t->table[n] && t->head == &t->table[n]) {
if (t->tail == &t->table[n] && t->head == &t->table[n]) {
t->head = t->tail = NULL;
} else if (t->head == &t->table[n]) {
t->head->next->prev = NULL;
......@@ -211,7 +220,7 @@ int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
int lh_table_delete(struct lh_table *t, void *k)
{
struct lh_entry *e = lh_table_lookup_entry(t, k);
if(!e) return -1;
if (!e)
return -1;
return lh_table_delete_entry(t, e);
}
......@@ -8,7 +8,7 @@
* it under the terms of the MIT license. See COPYING for details.
*
*/
#ifndef _linkhash_h_
#define _linkhash_h_
#include <inttypes.h>
......@@ -33,11 +33,11 @@ struct lh_entry;
/**
* callback function prototypes
*/
typedef void (lh_entry_free_fn) (struct lh_entry *e);
typedef void (lh_entry_free_fn) (struct lh_entry * e);
/**
* callback function prototypes
*/
typedef uint32_t (lh_hash_fn) (void *k);
typedef uint32_t(lh_hash_fn) (void *k);
/**
* callback function prototypes
*/
......@@ -171,10 +171,7 @@ for(entry = table->head; entry && ((tmp = entry->next) || 1); entry = tmp)
* and C strings respectively.
* @return a pointer onto the linkhash table.
*/
extern struct lh_table* lh_table_new(int size, char *name,
lh_entry_free_fn *free_fn,
lh_hash_fn *hash_fn,
lh_equal_fn *equal_fn);
extern struct lh_table *lh_table_new(int size, char *name, lh_entry_free_fn * free_fn, lh_hash_fn * hash_fn, lh_equal_fn * equal_fn);
/**
* Convenience function to create a new linkhash
......@@ -184,8 +181,7 @@ extern struct lh_table* lh_table_new(int size, char *name,
* @param free_fn callback function used to free memory for entries.
* @return a pointer onto the linkhash table.
*/
extern struct lh_table* lh_kchar_table_new(int size, char *name,
lh_entry_free_fn *free_fn);
extern struct lh_table *lh_kchar_table_new(int size, char *name, lh_entry_free_fn * free_fn);
/**
......@@ -196,8 +192,7 @@ extern struct lh_table* lh_kchar_table_new(int size, char *name,
* @param free_fn callback function used to free memory for entries.
* @return a pointer onto the linkhash table.
*/
extern struct lh_table* lh_kptr_table_new(int size, char *name,
lh_entry_free_fn *free_fn);
extern struct lh_table *lh_kptr_table_new(int size, char *name, lh_entry_free_fn * free_fn);
/**
......@@ -224,7 +219,7 @@ extern int lh_table_insert(struct lh_table *t, void *k, void *v);
* @param k a pointer to the key to lookup
* @return a pointer to the record structure of the value or NULL if it does not exist.
*/
extern struct lh_entry* lh_table_lookup_entry(struct lh_table *t, void *k);
extern struct lh_entry *lh_table_lookup_entry(struct lh_table *t, void *k);
/**
* Lookup a record into the table
......@@ -232,7 +227,7 @@ extern struct lh_entry* lh_table_lookup_entry(struct lh_table *t, void *k);
* @param k a pointer to the key to lookup
* @return a pointer to the found value or NULL if it does not exist.
*/
extern void* lh_table_lookup(struct lh_table *t, void *k);
extern void *lh_table_lookup(struct lh_table *t, void *k);
/**
......
......@@ -25,44 +25,44 @@
#include "debug.h"
#include "printbuf.h"
struct printbuf* printbuf_new()
struct printbuf *printbuf_new()
{
struct printbuf *p;
if(!(p = calloc(1, sizeof(struct printbuf)))) return NULL;
p->size = 32;
p->bpos = 0;
if(!(p->buf = malloc(p->size))) {
free(p);
return NULL;
}
return p;
struct printbuf *p;
if (!(p = calloc(1, sizeof(struct printbuf))))
return NULL;
p->size = 32;
p->bpos = 0;
if (!(p->buf = malloc(p->size))) {
free(p);
return NULL;
}
return p;
}
int printbuf_memappend(struct printbuf *p, char *buf, int size)
{
char *t;
if(p->size - p->bpos <= size) {
int new_size = max(p->size * 2, p->bpos + size + 8);
char *t;
if (p->size - p->bpos <= size) {
int new_size = max(p->size * 2, p->bpos + size + 8);
#ifdef PRINTBUF_DEBUG
mc_debug("printbuf_memappend: realloc "
"bpos=%d wrsize=%d old_size=%d new_size=%d\n",
p->bpos, size, p->size, new_size);
mc_debug("printbuf_memappend: realloc " "bpos=%d wrsize=%d old_size=%d new_size=%d\n", p->bpos, size, p->size, new_size);
#endif /* PRINTBUF_DEBUG */
if(!(t = realloc(p->buf, new_size))) return -1;
p->size = new_size;
p->buf = t;
}
memcpy(p->buf + p->bpos, buf, size);
p->bpos += size;
p->buf[p->bpos]= '\0';
return size;
if (!(t = realloc(p->buf, new_size)))
return -1;
p->size = new_size;
p->buf = t;
}
memcpy(p->buf + p->bpos, buf, size);
p->bpos += size;
p->buf[p->bpos] = '\0';
return size;
}
#if !HAVE_VSNPRINTF && defined(WIN32)
# define vsnprintf _vsnprintf
#elif !HAVE_VSNPRINTF /* !HAVE_VSNPRINTF */
#elif !HAVE_VSNPRINTF /* !HAVE_VSNPRINTF */
# error Need vsnprintf!
#endif /* !HAVE_VSNPRINTF && defined(WIN32) */
......@@ -76,22 +76,26 @@ static int vasprintf(char **buf, const char *fmt, va_list ap)
int chars;
char *b;
if(!buf) { return -1; }
if (!buf) {
return -1;
}
#ifdef WIN32
chars = _vscprintf(fmt, ap)+1;
chars = _vscprintf(fmt, ap) + 1;
#else /* !defined(WIN32) */
/* CAW: RAWR! We have to hope to god here that vsnprintf doesn't overwrite
our buffer like on some 64bit sun systems.... but hey, its time to move on */
chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap)+1;
if(chars < 0) { chars *= -1; } /* CAW: old glibc versions have this problem */
chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap) + 1;
if (chars < 0) {
chars *= -1;
} /* CAW: old glibc versions have this problem */
#endif /* defined(WIN32) */
b = (char*)malloc(sizeof(char)*chars);
if(!b) { return -1; }
b = (char *) malloc(sizeof(char) * chars);
if (!b) {
return -1;
}
if((chars = vsprintf(b, fmt, ap)) < 0)
{
if ((chars = vsprintf(b, fmt, ap)) < 0) {
free(b);
} else {
*buf = b;
......@@ -103,42 +107,43 @@ static int vasprintf(char **buf, const char *fmt, va_list ap)
int sprintbuf(struct printbuf *p, const char *msg, ...)
{
va_list ap;
char *t;
int size;
char buf[128];
/* user stack buffer first */
va_start(ap, msg);
size = vsnprintf(buf, 128, msg, ap);
va_end(ap);
/* if string is greater than stack buffer, then use dynamic string
with vasprintf. Note: some implementation of vsnprintf return -1
if output is truncated whereas some return the number of bytes that
would have been writen - this code handles both cases. */
if(size == -1 || size > 127) {
int ret;
va_start(ap, msg);
if((size = vasprintf(&t, msg, ap)) == -1) return -1;
va_end(ap);
ret = printbuf_memappend(p, t, size);
free(t);
return ret;
} else {
return printbuf_memappend(p, buf, size);
}
va_list ap;
char *t;
int size;
char buf[128];
/* user stack buffer first */
va_start(ap, msg);
size = vsnprintf(buf, 128, msg, ap);
va_end(ap);
/* if string is greater than stack buffer, then use dynamic string
with vasprintf. Note: some implementation of vsnprintf return -1
if output is truncated whereas some return the number of bytes that
would have been writen - this code handles both cases. */
if (size == -1 || size > 127) {
int ret;
va_start(ap, msg);
if ((size = vasprintf(&t, msg, ap)) == -1)
return -1;
va_end(ap);
ret = printbuf_memappend(p, t, size);
free(t);
return ret;
} else {
return printbuf_memappend(p, buf, size);
}
}
void printbuf_reset(struct printbuf *p)
{
p->buf[0] = '\0';
p->bpos = 0;
p->buf[0] = '\0';
p->bpos = 0;
}
void printbuf_free(struct printbuf *p)
{
if(p) {
free(p->buf);
free(p);
}
if (p) {
free(p->buf);
free(p);
}
}
......@@ -15,24 +15,19 @@
#undef PRINTBUF_DEBUG
struct printbuf {
char *buf;
int bpos;
int size;
char *buf;
int bpos;
int size;
};
extern struct printbuf*
printbuf_new();
extern struct printbuf *printbuf_new();
extern int
printbuf_memappend(struct printbuf *p, char *buf, int size);
extern int printbuf_memappend(struct printbuf *p, char *buf, int size);
extern int
sprintbuf(struct printbuf *p, const char *msg, ...);
extern int sprintbuf(struct printbuf *p, const char *msg, ...);
extern void
printbuf_reset(struct printbuf *p);
extern void printbuf_reset(struct printbuf *p);
extern void
printbuf_free(struct printbuf *p);
extern void printbuf_free(struct printbuf *p);
#endif
......@@ -39,4 +39,3 @@ char *url_encode(char *url, size_t l);
char *url_decode(char *url, size_t l);
#endif
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论