C: Json comments: added an obi_read_comment function to read one value

from comments
This commit is contained in:
Celine Mercier
2019-08-31 18:28:51 +02:00
parent 53dcbc8ea3
commit 7423bacac0
2 changed files with 60 additions and 0 deletions

View File

@@ -238,7 +238,45 @@ char* obi_add_comment(char* comments, const char* key, const char* value)
// Free structure
cJSON_Delete(comments_struct);
return new_comments;
}
char* obi_read_comment(char* comments, const char* key)
{
cJSON* comments_struct = NULL;
cJSON* value_json = NULL;
char* value = NULL;
comments_struct = cJSON_Parse(comments);
if (comments_struct == NULL)
{
obi_set_errno(OBI_JSON_ERROR);
obidebug(1, "\nError parsing the comments when reading comments, key: %s", key);
return NULL;
}
value_json = cJSON_GetObjectItem(comments_struct, key);
if (value_json == NULL)
{
obi_set_errno(OBI_JSON_ERROR);
obidebug(1, "\nError getting a value when reading a comment, key: %s", key);
return NULL;
}
value = cJSON_Print(value_json);
if (value == NULL)
{
obi_set_errno(OBI_JSON_ERROR);
obidebug(1, "\nError formatting a value when reading a comment, key: %s", key);
return NULL;
}
// Free structure
cJSON_Delete(comments_struct);
return value;
}