0.9
* Don't use this as a variable, so we can compile with a C++ compiler
+ * Add casts from void* to type of assignment when using malloc
+ * Add #ifdef __cplusplus guards to all of the headers
+ * Add typedefs for json_object, json_tokener, array_list, printbuf, lh_table
Michael Clark, <michael@metaparadigm.com>
* Null pointer dereference fix. Fix json_object_get_boolean strlen test
to not return TRUE for zero length string. Remove redundant includes.
{
struct array_list *arr;
- if(!(arr = calloc(1, sizeof(struct array_list)))) return NULL;
+ arr = (struct array_list*)calloc(1, sizeof(struct array_list));
+ if(!arr) return NULL;
arr->size = ARRAY_LIST_DEFAULT_SIZE;
arr->length = 0;
arr->free_fn = free_fn;
- if(!(arr->array = calloc(sizeof(void*), arr->size))) {
+ if(!(arr->array = (void**)calloc(sizeof(void*), arr->size))) {
free(arr);
return NULL;
}
if(max < arr->size) return 0;
new_size = max(arr->size << 1, max);
if(!(t = realloc(arr->array, new_size*sizeof(void*)))) return -1;
- arr->array = t;
+ arr->array = (void**)t;
(void)memset(arr->array + arr->size, 0, (new_size-arr->size)*sizeof(void*));
arr->size = new_size;
return 0;
#ifndef _arraylist_h_
#define _arraylist_h_
+#ifdef __cplusplus
+extern "C" {
+#endif
+
#define ARRAY_LIST_DEFAULT_SIZE 32
typedef void (array_list_free_fn) (void *data);
extern int
array_list_length(struct array_list *al);
+#ifdef __cplusplus
+}
+#endif
+
#endif
#ifndef _DEBUG_H_
#define _DEBUG_H_
+#ifdef __cplusplus
+extern "C" {
+#endif
+
extern void mc_set_debug(int debug);
extern int mc_get_debug(void);
#define MC_INFO(x, ...) if (0) mc_info(x, ##__VA_ARGS__)
#endif
+#ifdef __cplusplus
+}
+#endif
+
#endif
static struct json_object* json_object_new(enum json_type o_type)
{
- struct json_object *jso = calloc(sizeof(struct json_object), 1);
+ struct json_object *jso;
+
+ jso = (struct json_object*)calloc(sizeof(struct json_object), 1);
if(!jso) return NULL;
jso->o_type = o_type;
jso->_ref_count = 1;
#ifndef _json_object_h_
#define _json_object_h_
+#ifdef __cplusplus
+extern "C" {
+#endif
+
#define JSON_OBJECT_DEF_HASH_ENTRIES 16
#undef FALSE
/* forward structure definitions */
typedef int boolean;
-struct printbuf;
-struct lh_table;
-struct array_list;
-struct json_object;
-struct json_object_iter;
+typedef struct printbuf printbuf;
+typedef struct lh_table lh_table;
+typedef struct array_list array_list;
+typedef struct json_object json_object;
+typedef struct json_object_iter json_object_iter;
+typedef struct json_tokener json_tokener;
/* supported object types */
-enum json_type {
+typedef enum json_type {
json_type_null,
json_type_boolean,
json_type_double,
json_type_object,
json_type_array,
json_type_string
-};
+} json_type;
/* reference counting functions */
*/
extern const char* json_object_get_string(struct json_object *obj);
+#ifdef __cplusplus
+}
+#endif
+
#endif
#ifndef _json_object_private_h_
#define _json_object_private_h_
+#ifdef __cplusplus
+extern "C" {
+#endif
+
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 lh_entry *entry;
};
+#ifdef __cplusplus
+}
+#endif
+
#endif
struct json_tokener* json_tokener_new(void)
{
- struct json_tokener *tok = calloc(1, sizeof(struct json_tokener));
+ struct json_tokener *tok;
+
+ tok = (struct json_tokener*)calloc(1, sizeof(struct json_tokener));
tok->pb = printbuf_new();
json_tokener_reset(tok);
return tok;
tok = json_tokener_new();
obj = json_tokener_parse_ex(tok, str, -1);
if(tok->err != json_tokener_success)
- obj = error_ptr(-tok->err);
+ obj = (struct json_object*)error_ptr(-tok->err);
json_tokener_free(tok);
return obj;
}
#include <stddef.h>
#include "json_object.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
enum json_tokener_error {
json_tokener_success,
json_tokener_continue,
extern struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
char *str, int len);
+#ifdef __cplusplus
+}
+#endif
+
#endif
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);
+ return (struct json_object*)error_ptr(-1);
}
if(!(pb = printbuf_new())) {
MC_ERROR("json_object_from_file: printbuf_new failed\n");
- return error_ptr(-1);
+ return (struct json_object*)error_ptr(-1);
}
while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
printbuf_memappend(pb, buf, ret);
MC_ABORT("json_object_from_file: error reading file %s: %s\n",
filename, strerror(errno));
printbuf_free(pb);
- return error_ptr(-1);
+ return (struct json_object*)error_ptr(-1);
}
obj = json_tokener_parse(pb->buf);
printbuf_free(pb);
#include "json_object.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
#define JSON_FILE_BUF_SIZE 4096
/* utility functions */
extern struct json_object* json_object_from_file(char *filename);
extern int json_object_to_file(char *filename, struct json_object *obj);
+#ifdef __cplusplus
+}
+#endif
+
#endif
unsigned long lh_char_hash(const void *k)
{
unsigned int h = 0;
- const char* data = k;
+ const char* data = (const char*)k;
while( *data!=0 ) h = h*129 + (unsigned int)(*data++) + LH_PRIME;
int i;
struct lh_table *t;
- t = calloc(1, sizeof(struct lh_table));
+ t = (struct lh_table*)calloc(1, sizeof(struct lh_table));
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));
+ t->table = (struct lh_entry*)calloc(size, sizeof(struct lh_entry));
if(!t->table) lh_abort("lh_table_new: calloc failed\n");
t->free_fn = free_fn;
t->hash_fn = hash_fn;
#ifndef _linkhash_h_
#define _linkhash_h_
+#ifdef __cplusplus
+extern "C" {
+#endif
+
/**
* golden prime used in hash functions
*/
void lh_abort(const char *msg, ...);
void lh_table_resize(struct lh_table *t, int new_size);
+#ifdef __cplusplus
+}
+#endif
+
#endif
{
struct printbuf *p;
- if(!(p = calloc(1, sizeof(struct printbuf)))) return NULL;
+ p = (struct printbuf*)calloc(1, sizeof(struct printbuf));
+ if(!p) return NULL;
p->size = 32;
p->bpos = 0;
- if(!(p->buf = malloc(p->size))) {
+ if(!(p->buf = (char*)malloc(p->size))) {
free(p);
return NULL;
}
"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;
+ if(!(t = (char*)realloc(p->buf, new_size))) return -1;
p->size = new_size;
p->buf = t;
}
#ifndef _printbuf_h_
#define _printbuf_h_
+#ifdef __cplusplus
+extern "C" {
+#endif
+
#undef PRINTBUF_DEBUG
struct printbuf {
extern void
printbuf_free(struct printbuf *p);
+#ifdef __cplusplus
+}
+#endif
+
#endif
int main(int argc, char **argv)
{
- struct json_tokener *tok;
- struct json_object *my_string, *my_int, *my_object, *my_array;
- struct json_object *new_obj;
+ json_tokener *tok;
+ json_object *my_string, *my_int, *my_object, *my_array;
+ json_object *new_obj;
int i;
MC_SET_DEBUG(1);
json_object_array_put_idx(my_array, 4, json_object_new_int(5));
printf("my_array=\n");
for(i=0; i < json_object_array_length(my_array); i++) {
- struct json_object *obj = json_object_array_get_idx(my_array, i);
+ json_object *obj = json_object_array_get_idx(my_array, i);
printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
}
printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
int main(int argc, char **argv)
{
- struct json_object *new_obj;
+ json_object *new_obj;
MC_SET_DEBUG(1);