Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 1721f75c rédigé par Kubat's avatar Kubat
Parcourir les fichiers

Use defined function to not reinvent the wheel

parent d06a35a8
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!76Database update from FS
...@@ -66,8 +66,8 @@ repo_new(struct lkt_repo *const repo_, const char *name_, const char *url_, vola ...@@ -66,8 +66,8 @@ repo_new(struct lkt_repo *const repo_, const char *name_, const char *url_, vola
.base_url = url_, .base_url = url_,
.get_id_json = GET_ID_JSON, .get_id_json = GET_ID_JSON,
.get_id_file = GET_ID_FILE, .get_id_file = GET_ID_FILE,
.get_all_json = DEFAULT_URL "/api_karas.php", // TODO .get_all_json = DEFAULT_URL "/api_karas.php", // TODO FIXME
.kara_dir = "/home/kara/", // TODO .kara_dir = "/home/kara/", // TODO FIXME
.version = 1, .version = 1,
/* The db */ /* The db */
...@@ -137,11 +137,9 @@ int ...@@ -137,11 +137,9 @@ int
__json_sync(struct lkt_repo *const repo, struct json_object **json) __json_sync(struct lkt_repo *const repo, struct json_object **json)
{ {
RETURN_UNLESS(json, "Invalid argument", 1); RETURN_UNLESS(json, "Invalid argument", 1);
CURL *curl_handle; CURL *curl_handle;
CURLcode res; CURLcode res;
int ret = 1; int ret = 1;
struct memory file = { struct memory file = {
.mem = NULL, .mem = NULL,
.size = 0. .size = 0.
...@@ -170,14 +168,12 @@ err: ...@@ -170,14 +168,12 @@ err:
int int
repo_get_id(struct lkt_repo *const repo, const uint64_t id, struct kara_metadata *mdt) repo_get_id(struct lkt_repo *const repo, const uint64_t id, struct kara_metadata *mdt)
{ {
RETURN_UNLESS(mdt, "Invalid argument", 1);
CURL *curl_handle; CURL *curl_handle;
CURLcode res; CURLcode res;
struct json_object *jobj, *field; struct json_object *jobj, *field;
int ret = 1, err = 0; int ret = 1, err = 0;
char url[URL_MAX_LEN]; char url[URL_MAX_LEN];
RETURN_UNLESS(mdt, "Invalid argument", 1);
struct memory file = { struct memory file = {
.mem = NULL, .mem = NULL,
.size = 0, .size = 0,
...@@ -203,41 +199,19 @@ repo_get_id(struct lkt_repo *const repo, const uint64_t id, struct kara_metadata ...@@ -203,41 +199,19 @@ repo_get_id(struct lkt_repo *const repo, const uint64_t id, struct kara_metadata
/* Read the json. */ /* Read the json. */
jobj = json_tokener_parse(file.mem); jobj = json_tokener_parse(file.mem);
if (json_object_object_get_ex(jobj, "message", &field)) { if (json_object_object_get_ex(jobj, "message", &field)) {
LOG_ERROR("Kara with id %lu not found, message is: %s", id, json_object_get_string(field)); LOG_ERROR("Kara with id %lu not found, message is: %s", id, json_object_get_string(field));
goto err; goto err;
} }
LOG_INFO("Got kara with id %lu", id); LOG_INFO("Got kara with id %lu", id);
err |= safe_json_get_string(jobj, "song_name", mdt->song_name, LEKTOR_TAG_MAX); err |= safe_json_get_string(jobj, "song_name", mdt->song_name, LEKTOR_TAG_MAX);
err |= safe_json_get_string(jobj, "source_name", mdt->source_name, LEKTOR_TAG_MAX); err |= safe_json_get_string(jobj, "source_name", mdt->source_name, LEKTOR_TAG_MAX);
err |= safe_json_get_string(jobj, "category", mdt->category, LEKTOR_TAG_MAX); err |= safe_json_get_string(jobj, "category", mdt->category, LEKTOR_TAG_MAX);
err |= safe_json_get_string(jobj, "language", mdt->language, LEKTOR_TAG_MAX); err |= safe_json_get_string(jobj, "language", mdt->language, LEKTOR_TAG_MAX);
err |= safe_json_get_string(jobj, "author_name", mdt->author_name, LEKTOR_TAG_MAX); err |= safe_json_get_string(jobj, "author_name", mdt->author_name, LEKTOR_TAG_MAX);
err |= safe_json_get_string(jobj, "song_type", mdt->song_type, LEKTOR_TAG_MAX); err |= safe_json_get_string(jobj, "song_type", mdt->song_type, LEKTOR_TAG_MAX);
GOTO_IF(err || safe_json_get_int32(jobj, "song_number", &mdt->song_number), "Invalid json", err);
errno = 0;
if (!json_object_object_get_ex(jobj, "song_number", &field)) {
LOG_ERROR("%s", "No object with key 'song_number' in json, error");
goto err;
}
mdt->song_number = json_object_get_int(field);
if (errno == EINVAL) {
LOG_ERROR("%s", "Invalid integer for field with key 'song_number'");
goto err;
}
if (mdt->song_number == INT32_MAX || mdt->song_number == INT32_MIN) {
LOG_ERROR("Out of bound int32_t '%d' for field with key 'song_number'", mdt->song_number);
goto err;
}
ret = 0; ret = 0;
err: err:
json_object_put(jobj); /* Delete object. */ json_object_put(jobj); /* Delete object. */
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter