Skip to content
Snippets Groups Projects
Commit 4f62d07c authored by Andrea Censi's avatar Andrea Censi
Browse files

anche oggi un po meglio

parent 8b09da2f
No related branches found
No related tags found
No related merge requests found
......@@ -16,10 +16,10 @@ SET(json-c_sources
ADD_LIBRARY(json-c STATIC ${json-c_sources})
ADD_EXECUTABLE(test1 test1.c)
#ADD_EXECUTABLE(test1 test1.c)
ADD_EXECUTABLE(test2 test2.c)
TARGET_LINK_LIBRARIES(test1 json-c)
#TARGET_LINK_LIBRARIES(test1 json-c)
TARGET_LINK_LIBRARIES(test2 json-c)
......
......@@ -115,7 +115,7 @@ struct json_object* json_tokener_parse_len(char *str, int len) {
int jo_read_double_array(JO s, const char*name, double*p, int n, double when_null) {
JO jo = json_object_object_get(s, (char*)name);
JO jo = json_object_object_get(s, name);
if(!jo) {
/* mc_error("Field '%s' not found.\n", name); */
return 0;
......@@ -155,7 +155,7 @@ int jo_read_double_array(JO s, const char*name, double*p, int n, double when_nul
int jo_read_int_array(JO s, const char*name, int*p, int n, int when_null) {
int size, i;
JO jo = json_object_object_get(s, (char*)name);
JO jo = json_object_object_get(s, name);
if(!jo) {
/* mc_error("Field '%s' not found.\n", name); */
return 0;
......@@ -234,7 +234,7 @@ int json_to_double(JO jo, double*ptr) {
}
int jo_read_int(JO jo, const char*name, int*p) {
JO v = json_object_object_get(jo, (char*)name);
JO v = json_object_object_get(jo, name);
if(!v) {
return 0;
}
......@@ -249,7 +249,7 @@ double convert_to_double(JO jo) {
}
int jo_read_double(JO jo, const char*name, double*p) {
JO v = json_object_object_get(jo, (char*)name);
JO v = json_object_object_get(jo, name);
if(!v) {
/* mc_error("Field '%s' not found.\n", name); */
......@@ -260,6 +260,23 @@ int jo_read_double(JO jo, const char*name, double*p) {
return 1;
}
int jo_read_string(JO jo, const char*name, char*dest_string, size_t max_len) {
JO v = json_object_object_get(jo, name);
if(!v) return 0;
if(json_object_is_type(v, json_type_string)) {
strncpy(dest_string, json_object_get_string(v), max_len);
return 1;
} else {
strncpy(dest_string, "<string not found>", max_len);
return 0;
}
}
void jo_add_string(JO root, const char*name, const char*v) {
jo_add(root, name, jo_new_string(v));
}
void jo_add_double_array(JO root, const char*name, const double*v, int n) {
jo_add(root, name, jo_new_double_array(v, n));
}
......
......@@ -33,11 +33,14 @@ void jo_add_int (JO, const char*name, int v);
void jo_add_double_array (JO, const char*name, const double *v, int n);
void jo_add_int_array (JO, const char*name, const int *v, int n);
void jo_add_string (JO, const char*name, const char*v);
/** Return 0 if there isn't a field called 'name' */
int jo_read_int (JO, const char*name, int*p) ;
int jo_read_double(JO jo, const char*name, double*p);
int jo_read_double_array (JO, const char*name, double *p, int n, double when_null);
int jo_read_int_array (JO, const char*name, int *p, int n, int when_null);
int jo_read_string (JO, const char*name, char*v, size_t max_len);
int json_to_int(JO jo, int*ptr);
......
......@@ -37,11 +37,11 @@
/* #define REFCOUNT_DEBUG 1 */
char *json_number_chars = "0123456789.+-e";
char *json_hex_chars = "0123456789abcdef";
const char *json_number_chars = "0123456789.+-e";
const char *json_hex_chars = "0123456789abcdef";
#ifdef REFCOUNT_DEBUG
static char* json_type_name[] = {
static const char* json_type_name[] = {
"null",
"boolean",
"double",
......@@ -267,18 +267,18 @@ struct lh_table* json_object_get_object(struct json_object *this)
void json_object_object_add(struct json_object* this, const char *key,
struct json_object *val)
{
lh_table_delete(this->o.c_object, (char*)key);
lh_table_delete(this->o.c_object, key);
lh_table_insert(this->o.c_object, strdup(key), val);
}
struct json_object* json_object_object_get(struct json_object* this, const char *key)
{
return (struct json_object*) lh_table_lookup(this->o.c_object, (char*)key);
return (struct json_object*) lh_table_lookup(this->o.c_object, key);
}
void json_object_object_del(struct json_object* this, const char *key)
{
lh_table_delete(this->o.c_object, (char*)key);
lh_table_delete(this->o.c_object, key);
}
......
......@@ -20,8 +20,8 @@
#undef TRUE
#define TRUE ((boolean)1)
extern char *json_number_chars;
extern char *json_hex_chars;
extern const char *json_number_chars;
extern const char *json_hex_chars;
/* forward structure definitions */
......
......@@ -40,7 +40,7 @@ struct printbuf* printbuf_new()
}
int printbuf_memappend(struct printbuf *p, char *buf, int size)
int printbuf_memappend(struct printbuf *p, const char *buf, int size)
{
char *t;
if(p->size - p->bpos <= size) {
......
......@@ -24,7 +24,7 @@ extern struct printbuf*
printbuf_new(void);
extern int
printbuf_memappend(struct printbuf *p, char *buf, int size);
printbuf_memappend(struct printbuf *p, const char *buf, int size);
extern int
sprintbuf(struct printbuf *p, const char *msg, ...);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment