mirror of
https://github.com/json-c/json-c.git
synced 2026-04-09 15:29:07 +08:00
json_object: cleanup of *set_string* functions
This commit also has the side-effect that errno is set on failed calls to json_object_set_string(_len).
This commit is contained in:
@@ -1029,49 +1029,45 @@ static void json_object_string_delete(struct json_object* jso)
|
|||||||
json_object_generic_delete(jso);
|
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);
|
char *dstbuf = NULL;
|
||||||
if (!jso)
|
if (len < LEN_DIRECT_STRING_DATA)
|
||||||
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)
|
|
||||||
{
|
|
||||||
json_object_generic_delete(jso);
|
|
||||||
errno = ENOMEM;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return jso;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
dstbuf = jso->o.c_string.str.data;
|
||||||
} else {
|
}
|
||||||
jso->o.c_string.str.ptr = (char*)malloc(len + 1);
|
else
|
||||||
if (!jso->o.c_string.str.ptr)
|
{
|
||||||
|
dstbuf = (char *) malloc(len + 1);
|
||||||
|
if (dstbuf == NULL)
|
||||||
{
|
{
|
||||||
json_object_generic_delete(jso);
|
|
||||||
errno = ENOMEM;
|
errno = ENOMEM;
|
||||||
return NULL;
|
return 0;
|
||||||
}
|
}
|
||||||
dstbuf = jso->o.c_string.str.ptr;
|
jso->o.c_string.str.ptr = dstbuf;
|
||||||
}
|
}
|
||||||
memcpy(dstbuf, (const void *)s, len);
|
memcpy(dstbuf, (const void *)s, len);
|
||||||
dstbuf[len] = '\0';
|
dstbuf[len] = '\0';
|
||||||
jso->o.c_string.len = len;
|
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 *jso = json_object_new(json_type_string);
|
||||||
|
if (!jso)
|
||||||
|
return NULL;
|
||||||
|
jso->_to_json_string = &json_object_string_to_json_string;
|
||||||
|
if (set_string_len(jso, s, len) == 0)
|
||||||
|
{
|
||||||
|
json_object_generic_delete(jso);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return jso;
|
return jso;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1101,26 +1097,23 @@ int json_object_get_string_len(const struct json_object *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)));
|
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){
|
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;
|
char *old_ptr = NULL;
|
||||||
if (len<LEN_DIRECT_STRING_DATA) {
|
int ret;
|
||||||
dstbuf=jso->o.c_string.str.data;
|
if (jso == NULL || jso->o_type != json_type_string)
|
||||||
if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
|
return 0;
|
||||||
} else {
|
if (jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
|
||||||
dstbuf=(char *)malloc(len+1);
|
old_ptr = jso->o.c_string.str.ptr;
|
||||||
if (dstbuf==NULL) return 0;
|
ret = set_string_len(jso, s, len);
|
||||||
if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
|
if (ret != 0 && old_ptr != NULL)
|
||||||
jso->o.c_string.str.ptr=dstbuf;
|
free(old_ptr);
|
||||||
}
|
return ret;
|
||||||
jso->o.c_string.len=len;
|
|
||||||
memcpy(dstbuf, (const void *)s, len);
|
|
||||||
dstbuf[len] = '\0';
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* json_object_array */
|
/* json_object_array */
|
||||||
|
|||||||
Reference in New Issue
Block a user