mirror of
https://github.com/json-c/json-c.git
synced 2026-03-21 22:19: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);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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) {
|
||||
char *dstbuf = NULL;
|
||||
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)
|
||||
}
|
||||
else
|
||||
{
|
||||
dstbuf = (char *) malloc(len + 1);
|
||||
if (dstbuf == NULL)
|
||||
{
|
||||
json_object_generic_delete(jso);
|
||||
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);
|
||||
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 *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;
|
||||
}
|
||||
|
||||
@@ -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)));
|
||||
}
|
||||
|
||||
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 */
|
||||
|
||||
Reference in New Issue
Block a user