json_object_generic_delete(jso);
}
-struct json_object* json_object_new_string(const char *s)
+static int set_string_len(struct json_object *jso, const char *s, int len)
{
- struct json_object *jso = json_object_new(json_type_string);
- if (!jso)
- return NULL;
- jso->_to_json_string = &json_object_string_to_json_string;
- jso->o.c_string.len = strlen(s);
- if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) {
- memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len + 1);
- } else {
- jso->o.c_string.str.ptr = strdup(s);
- if (!jso->o.c_string.str.ptr)
+ char *dstbuf = NULL;
+ if (len < LEN_DIRECT_STRING_DATA)
+ {
+ dstbuf = jso->o.c_string.str.data;
+ }
+ else
+ {
+ dstbuf = (char *) malloc(len + 1);
+ if (dstbuf == NULL)
{
- json_object_generic_delete(jso);
errno = ENOMEM;
- return NULL;
+ return 0;
}
+ jso->o.c_string.str.ptr = dstbuf;
}
- return jso;
+ memcpy(dstbuf, (const void *)s, len);
+ dstbuf[len] = '\0';
+ jso->o.c_string.len = len;
+ return 1;
+}
+
+struct json_object *json_object_new_string(const char *s)
+{
+ return json_object_new_string_len(s, (int)strlen(s));
}
-struct json_object* json_object_new_string_len(const char *s, const int len)
+struct json_object *json_object_new_string_len(const char *s, const int len)
{
- char *dstbuf;
struct json_object *jso = json_object_new(json_type_string);
if (!jso)
return NULL;
jso->_to_json_string = &json_object_string_to_json_string;
- if(len < LEN_DIRECT_STRING_DATA) {
- dstbuf = jso->o.c_string.str.data;
- } else {
- jso->o.c_string.str.ptr = (char*)malloc(len + 1);
- if (!jso->o.c_string.str.ptr)
- {
- json_object_generic_delete(jso);
- errno = ENOMEM;
- return NULL;
- }
- dstbuf = jso->o.c_string.str.ptr;
+ if (set_string_len(jso, s, len) == 0)
+ {
+ json_object_generic_delete(jso);
+ return NULL;
}
- memcpy(dstbuf, (const void *)s, len);
- dstbuf[len] = '\0';
- jso->o.c_string.len = len;
return jso;
}
}
}
-int json_object_set_string(json_object* jso, const char* s) {
+int json_object_set_string(json_object *jso, const char *s)
+{
return json_object_set_string_len(jso, s, (int)(strlen(s)));
}
-int json_object_set_string_len(json_object* jso, const char* s, int len){
- char *dstbuf;
- if (jso==NULL || jso->o_type!=json_type_string) return 0;
- if (len<LEN_DIRECT_STRING_DATA) {
- dstbuf=jso->o.c_string.str.data;
- if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
- } else {
- dstbuf=(char *)malloc(len+1);
- if (dstbuf==NULL) return 0;
- if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
- jso->o.c_string.str.ptr=dstbuf;
- }
- jso->o.c_string.len=len;
- memcpy(dstbuf, (const void *)s, len);
- dstbuf[len] = '\0';
- return 1;
+int json_object_set_string_len(json_object *jso, const char *s, int len)
+{
+ char *old_ptr = NULL;
+ int ret;
+ if (jso == NULL || jso->o_type != json_type_string)
+ return 0;
+ if (jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
+ old_ptr = jso->o.c_string.str.ptr;
+ ret = set_string_len(jso, s, len);
+ if (ret != 0 && old_ptr != NULL)
+ free(old_ptr);
+ return ret;
}
/* json_object_array */