aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-03-03 12:52:21 +0100
committerdec05eba <dec05eba@protonmail.com>2021-03-03 12:52:21 +0100
commitef49fb417f582ec837cde6b551a0bf0cfafe3d4d (patch)
tree0f1230ff9286496a30bb25291e81c6e0157297b2 /src/main.c
parent3d5be6f0623e61ecfb1c8086263a615f6b13f1d7 (diff)
Use cJSON instead of the existing json library
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c67
1 files changed, 29 insertions, 38 deletions
diff --git a/src/main.c b/src/main.c
index 359347d..85a331b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -7,7 +7,7 @@
#include "rss.h"
#include "rss_html_common.h"
#include "html.h"
-#include "json.h"
+#include "../depends/cJSON.h"
#include "alloc.h"
#include <stdio.h>
@@ -70,74 +70,65 @@ static void data_file_get_downloaded(const char *dir_name, const char *data_file
return;
}
- struct json_value_s *json_root = json_parse(file_data, file_size);
+ cJSON *json_root = cJSON_ParseWithLength(file_data, file_size);
if(!json_root) {
fprintf(stderr, "Failed to parse file %s as json\n", data_filepath);
goto cleanup;
}
+ free(file_data);
+ file_data = NULL;
- struct json_object_s *json_root_obj = json_value_as_object(json_root);
- if(!json_root_obj) {
+ if(!cJSON_IsObject(json_root)) {
fprintf(stderr, "File %s contains malformed json. Expected json root element to be an object\n", data_filepath);
goto cleanup;
}
- struct json_value_s *downloaded_json = json_object_get_field_by_name(json_root_obj, "downloaded");
+ const cJSON *downloaded_json = cJSON_GetObjectItemCaseSensitive(json_root, "downloaded");
if(!downloaded_json) {
fprintf(stderr, "File %s contains malformed json. Expected json to contain \"downloaded\"\n", data_filepath);
goto cleanup;
}
- struct json_array_s *downloaded_json_arr = json_value_as_array(downloaded_json);
- if(!downloaded_json_arr) {
+ if(!cJSON_IsArray(downloaded_json)) {
fprintf(stderr, "File %s contains malformed json. Expected \"downloaded\" to be an array\n", data_filepath);
goto cleanup;
}
size_t dir_name_len = strlen(dir_name);
- struct json_array_element_s *downloaded_item = downloaded_json_arr->start;
- for(; downloaded_item; downloaded_item = downloaded_item->next) {
- struct json_object_s *downloaded_item_obj = json_value_as_object(downloaded_item->value);
- if(!downloaded_item_obj)
+ const cJSON *downloaded_item_json = NULL;
+ cJSON_ArrayForEach(downloaded_item_json, downloaded_json) {
+ if(!cJSON_IsObject(downloaded_item_json))
continue;
- struct json_value_s *time_json = json_object_get_field_by_name(downloaded_item_obj, "time");
- if(!time_json || time_json->type != json_type_string)
+ const cJSON *time_json = cJSON_GetObjectItemCaseSensitive(downloaded_item_json, "time");
+ if(!time_json || !cJSON_IsString(time_json))
continue;
- struct json_string_s *time_json_str = json_value_as_string(time_json);
- struct json_value_s *title_json = json_object_get_field_by_name(downloaded_item_obj, "title");
- struct json_string_s *title_str = NULL;
- if(title_json && title_json->type == json_type_string)
- title_str = json_value_as_string(title_json);
-
- struct json_value_s *filename_json = json_object_get_field_by_name(downloaded_item_obj, "filename");
- struct json_string_s *filename_str = NULL;
- if(filename_json && filename_json->type == json_type_string)
- filename_str = json_value_as_string(filename_json);
+ const cJSON *title_json = cJSON_GetObjectItemCaseSensitive(downloaded_item_json, "title");
+ const cJSON *filename_json = cJSON_GetObjectItemCaseSensitive(downloaded_item_json, "filename");
/* Filename limit is 256, so this should be safe... */
char title[256];
- if(filename_str) {
- strcpy(title, filename_str->string);
- } else if(title_str) {
+ if(filename_json && cJSON_IsString(filename_json)) {
+ strcpy(title, filename_json->valuestring);
+ } else if(title_json && cJSON_IsString(title_json)) {
if(is_html) {
strcpy(title, dir_name);
title[dir_name_len] = '/';
- strcpy(title + dir_name_len + 1, title_str->string);
+ strcpy(title + dir_name_len + 1, title_json->valuestring);
} else {
- strcpy(title, title_str->string);
+ strcpy(title, title_json->valuestring);
}
} else {
continue;
}
- callback(title, atof(time_json_str->string), userdata);
+ callback(title, atof(time_json->valuestring), userdata);
}
cleanup:
- free(json_root);
+ cJSON_Delete(json_root);
free(file_data);
}
@@ -273,7 +264,7 @@ int is_program_running() {
}
/* plugin is NULL for rss */
-typedef int (*IterateTrackedItemCallback)(char *title, char *link, char *plugin, char *config_dir, struct json_object_s *json_data, void *userdata);
+typedef int (*IterateTrackedItemCallback)(char *title, char *link, char *plugin, char *config_dir, cJSON *json_data, void *userdata);
static void iterate_tracked_items(char *config_dir, IterateTrackedItemCallback iterate_callback, void *userdata) {
/*
TODO: Only iterate updated items. To make that work, sync_rss and sync_html need to be updated to update
@@ -312,7 +303,7 @@ static void iterate_tracked_items(char *config_dir, IterateTrackedItemCallback i
char *link_file_content = NULL;
char *data_file_content = NULL;
char *plugin_file_content = NULL;
- struct json_value_s *json_data = NULL;
+ cJSON *json_data = NULL;
strcpy(item_filepath + item_filepath_len + title_len, "/link");
long link_file_size = 0;
@@ -331,19 +322,19 @@ static void iterate_tracked_items(char *config_dir, IterateTrackedItemCallback i
goto cleanup_item;
}
- json_data = json_parse(data_file_content, data_file_size);
- if(!json_data || json_data->type != json_type_object) {
+ json_data = cJSON_ParseWithLength(data_file_content, data_file_size);
+ if(!json_data || !cJSON_IsObject(json_data)) {
fprintf(stderr, "Rss corrupt for %s\n", dir->d_name);
goto cleanup_item;
}
free(data_file_content);
data_file_content = NULL;
- if(iterate_callback(dir->d_name, link_file_content, plugin_file_content, config_dir, json_value_as_object(json_data), userdata) != 0)
+ if(iterate_callback(dir->d_name, link_file_content, plugin_file_content, config_dir, json_data, userdata) != 0)
fprintf(stderr, "Failed to sync %s\n", dir->d_name);
cleanup_item:
- free(json_data);
+ cJSON_Delete(json_data);
free(plugin_file_content);
free(data_file_content);
free(link_file_content);
@@ -352,7 +343,7 @@ static void iterate_tracked_items(char *config_dir, IterateTrackedItemCallback i
closedir(d);
}
-static int iterate_tracked_item_rss_callback(char *title, char *link, char *plugin, char *config_dir, struct json_object_s *json_data, void *userdata) {
+static int iterate_tracked_item_rss_callback(char *title, char *link, char *plugin, char *config_dir, cJSON *json_data, void *userdata) {
(void)plugin;
TransmissionSession *transmission_session = userdata;
TrackedRss tracked_rss;
@@ -367,7 +358,7 @@ typedef struct {
const char *download_dir;
} IterateHtmlItemUserdata;
-static int iterate_tracked_item_html_callback(char *title, char *link, char *plugin, char *config_dir, struct json_object_s *json_data, void *userdata) {
+static int iterate_tracked_item_html_callback(char *title, char *link, char *plugin, char *config_dir, cJSON *json_data, void *userdata) {
if(!plugin) {
fprintf(stderr, "Missing plugin name for html item: %s\n", title);
return -1;