From: Michael Clark Date: Wed, 25 Feb 2009 02:31:32 +0000 (+0000) Subject: * Don't use this as a variable, so we can compile with a C++ compiler X-Git-Tag: json-c-0.10-20120530~84 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=aaec1ef3c542accb118af48c09edc7e07675671a;p=json-c * 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, git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@33 327403b1-1117-474d-bef2-5cb71233fd97 --- diff --git a/ChangeLog b/ChangeLog index d0018ac..4cdd9a0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 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, * Null pointer dereference fix. Fix json_object_get_boolean strlen test to not return TRUE for zero length string. Remove redundant includes. diff --git a/arraylist.c b/arraylist.c index 7726de6..1d93f44 100644 --- a/arraylist.c +++ b/arraylist.c @@ -28,11 +28,12 @@ array_list_new(array_list_free_fn *free_fn) { 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; } @@ -64,7 +65,7 @@ static int array_list_expand_internal(struct array_list *arr, int max) 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; diff --git a/arraylist.h b/arraylist.h index 2948e04..bc85c80 100644 --- a/arraylist.h +++ b/arraylist.h @@ -12,6 +12,10 @@ #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); @@ -42,4 +46,8 @@ array_list_add(struct array_list *al, void *data); extern int array_list_length(struct array_list *al); +#ifdef __cplusplus +} +#endif + #endif diff --git a/debug.h b/debug.h index 951e994..59a4dfc 100644 --- a/debug.h +++ b/debug.h @@ -12,6 +12,10 @@ #ifndef _DEBUG_H_ #define _DEBUG_H_ +#ifdef __cplusplus +extern "C" { +#endif + extern void mc_set_debug(int debug); extern int mc_get_debug(void); @@ -39,4 +43,8 @@ extern void mc_info(const char *msg, ...); #define MC_INFO(x, ...) if (0) mc_info(x, ##__VA_ARGS__) #endif +#ifdef __cplusplus +} +#endif + #endif diff --git a/json_object.c b/json_object.c index 28ca102..6c20579 100644 --- a/json_object.c +++ b/json_object.c @@ -159,7 +159,9 @@ static void json_object_generic_delete(struct json_object* jso) 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; diff --git a/json_object.h b/json_object.h index 4c8f6db..80d2313 100644 --- a/json_object.h +++ b/json_object.h @@ -12,6 +12,10 @@ #ifndef _json_object_h_ #define _json_object_h_ +#ifdef __cplusplus +extern "C" { +#endif + #define JSON_OBJECT_DEF_HASH_ENTRIES 16 #undef FALSE @@ -26,15 +30,16 @@ extern const char *json_hex_chars; /* 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, @@ -42,7 +47,7 @@ enum json_type { json_type_object, json_type_array, json_type_string -}; +} json_type; /* reference counting functions */ @@ -307,4 +312,8 @@ extern struct json_object* json_object_new_string_len(const char *s, int len); */ extern const char* json_object_get_string(struct json_object *obj); +#ifdef __cplusplus +} +#endif + #endif diff --git a/json_object_private.h b/json_object_private.h index 35a44f3..9fb4011 100644 --- a/json_object_private.h +++ b/json_object_private.h @@ -12,6 +12,10 @@ #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); @@ -41,4 +45,8 @@ struct json_object_iter struct lh_entry *entry; }; +#ifdef __cplusplus +} +#endif + #endif diff --git a/json_tokener.c b/json_tokener.c index 9c05728..beaa956 100644 --- a/json_tokener.c +++ b/json_tokener.c @@ -57,7 +57,9 @@ const char* json_tokener_errors[] = { 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; @@ -100,7 +102,7 @@ struct json_object* json_tokener_parse(char *str) 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; } diff --git a/json_tokener.h b/json_tokener.h index 117d6ef..59035bb 100644 --- a/json_tokener.h +++ b/json_tokener.h @@ -15,6 +15,10 @@ #include #include "json_object.h" +#ifdef __cplusplus +extern "C" { +#endif + enum json_tokener_error { json_tokener_success, json_tokener_continue, @@ -87,4 +91,8 @@ 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); +#ifdef __cplusplus +} +#endif + #endif diff --git a/json_util.c b/json_util.c index 0262ca2..af6f3d6 100644 --- a/json_util.c +++ b/json_util.c @@ -62,11 +62,11 @@ struct json_object* json_object_from_file(char *filename) 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); @@ -76,7 +76,7 @@ struct json_object* json_object_from_file(char *filename) 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); diff --git a/json_util.h b/json_util.h index b3f53b6..6ab0287 100644 --- a/json_util.h +++ b/json_util.h @@ -14,10 +14,18 @@ #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 diff --git a/linkhash.c b/linkhash.c index 7385401..998cf7d 100644 --- a/linkhash.c +++ b/linkhash.c @@ -41,7 +41,7 @@ int lh_ptr_equal(const void *k1, const void *k2) 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; @@ -61,12 +61,12 @@ struct lh_table* lh_table_new(int size, const char *name, 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; diff --git a/linkhash.h b/linkhash.h index 9b5ef9d..90f219d 100644 --- a/linkhash.h +++ b/linkhash.h @@ -12,6 +12,10 @@ #ifndef _linkhash_h_ #define _linkhash_h_ +#ifdef __cplusplus +extern "C" { +#endif + /** * golden prime used in hash functions */ @@ -261,4 +265,8 @@ extern int lh_table_delete(struct lh_table *t, const void *k); void lh_abort(const char *msg, ...); void lh_table_resize(struct lh_table *t, int new_size); +#ifdef __cplusplus +} +#endif + #endif diff --git a/printbuf.c b/printbuf.c index bbb8657..a2182f4 100644 --- a/printbuf.c +++ b/printbuf.c @@ -29,10 +29,11 @@ struct printbuf* printbuf_new(void) { 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; } @@ -50,7 +51,7 @@ int printbuf_memappend(struct printbuf *p, const char *buf, int size) "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; } diff --git a/printbuf.h b/printbuf.h index 95f7a24..20c81cb 100644 --- a/printbuf.h +++ b/printbuf.h @@ -12,6 +12,10 @@ #ifndef _printbuf_h_ #define _printbuf_h_ +#ifdef __cplusplus +extern "C" { +#endif + #undef PRINTBUF_DEBUG struct printbuf { @@ -35,4 +39,8 @@ printbuf_reset(struct printbuf *p); extern void printbuf_free(struct printbuf *p); +#ifdef __cplusplus +} +#endif + #endif diff --git a/test1.c b/test1.c index a64a255..d2e6d0b 100644 --- a/test1.c +++ b/test1.c @@ -7,9 +7,9 @@ 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); @@ -39,7 +39,7 @@ int main(int argc, char **argv) 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)); diff --git a/test2.c b/test2.c index 39c4884..5f95565 100644 --- a/test2.c +++ b/test2.c @@ -8,7 +8,7 @@ int main(int argc, char **argv) { - struct json_object *new_obj; + json_object *new_obj; MC_SET_DEBUG(1);