aboutsummaryrefslogtreecommitdiff
path: root/src/html.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/html.c
parent3d5be6f0623e61ecfb1c8086263a615f6b13f1d7 (diff)
Use cJSON instead of the existing json library
Diffstat (limited to 'src/html.c')
-rw-r--r--src/html.c66
1 files changed, 26 insertions, 40 deletions
diff --git a/src/html.c b/src/html.c
index b7abbdf..fc45843 100644
--- a/src/html.c
+++ b/src/html.c
@@ -5,10 +5,11 @@
#include "stringutils.h"
#include "rss_html_common.h"
#include "main.h"
-#include "json.h"
+#include "../depends/cJSON.h"
#include <limits.h>
#include <string.h>
#include <stdio.h>
+#include <stdlib.h>
#include <libgen.h>
#include <signal.h>
#include <time.h>
@@ -82,7 +83,7 @@ static int url_extract_domain(const char *url, char *domain, int domain_len) {
TODO: Rename input "title" to "url", to make input and output match (easier to test with).
*/
typedef int (*PluginListCallback)(const char *name, const char *url, void *userdata);
-static struct json_value_s* plugin_list(char *plugin_filepath, const char *url, struct json_array_s *downloaded_items, PluginListCallback callback, void *userdata) {
+static cJSON* plugin_list(char *plugin_filepath, const char *url, cJSON *downloaded_items, PluginListCallback callback, void *userdata) {
int result;
Buffer buffer;
buffer_init(&buffer);
@@ -97,12 +98,8 @@ static struct json_value_s* plugin_list(char *plugin_filepath, const char *url,
goto err_cleanup;
}
- if(downloaded_items) {
- struct json_value_s downloaded_items_value;
- downloaded_items_value.payload = downloaded_items;
- downloaded_items_value.type = json_type_array;
- size_t json_output_len = 0;
- void *json_body_str = json_write_minified(&downloaded_items_value, &json_output_len);
+ if(cJSON_IsArray(downloaded_items)) {
+ char *json_body_str = cJSON_PrintUnformatted(downloaded_items);
if(!json_body_str) {
fprintf(stderr, "Failed to convert downloaded items to json\n");
if(process_id != -1)
@@ -111,10 +108,10 @@ static struct json_value_s* plugin_list(char *plugin_filepath, const char *url,
close(stdout_file);
goto err_cleanup;
}
+ size_t json_output_len = strlen(json_body_str);
- /* This is a bug in the json library */
- json_output_len = strlen(json_body_str);
if(write(stdin_file, json_body_str, json_output_len) != (ssize_t)json_output_len) {
+ free(json_body_str);
fprintf(stderr, "Failed to write all bytes to plugin list\n");
if(process_id != -1)
kill(process_id, SIGKILL);
@@ -131,41 +128,34 @@ static struct json_value_s* plugin_list(char *plugin_filepath, const char *url,
goto err_cleanup;
}
- struct json_value_s *json_root = json_parse(buffer.data, buffer.size);
+ cJSON *json_root = cJSON_ParseWithLength(buffer.data, buffer.size);
if(!json_root) {
fprintf(stderr, "Failed to load plugin %s list output as json\n", basename(plugin_filepath));
goto err_cleanup;
}
buffer_deinit(&buffer);
- struct json_array_s *json_root_array = json_value_as_array(json_root);
- if(!json_root_array) {
+ if(!cJSON_IsArray(json_root)) {
fprintf(stderr, "Failed to load plugin %s list output as json\n", basename(plugin_filepath));
- free(json_root);
+ cJSON_Delete(json_root);
goto err_cleanup;
}
- struct json_array_element_s *json_array_element = json_root_array->start;
- for(; json_array_element; json_array_element = json_array_element->next) {
- struct json_object_s *array_element_value = json_value_as_object(json_array_element->value);
- if(!array_element_value)
- continue;
-
- struct json_value_s *name_json = json_object_get_field_by_name(array_element_value, "name");
- struct json_value_s *url_json = json_object_get_field_by_name(array_element_value, "url");
- if(!name_json || !url_json)
+ cJSON *array_item = NULL;
+ cJSON_ArrayForEach(array_item, json_root) {
+ if(!cJSON_IsObject(array_item))
continue;
- struct json_string_s *name_json_str = json_value_as_string(name_json);
- struct json_string_s *url_json_str = json_value_as_string(url_json);
- if(!name_json_str || !url_json_str)
+ cJSON *name_json = cJSON_GetObjectItemCaseSensitive(array_item, "name");
+ cJSON *url_json = cJSON_GetObjectItemCaseSensitive(array_item, "url");
+ if(!cJSON_IsString(name_json) || !cJSON_IsString(url_json))
continue;
/* Cast from const to mutable variable because I can */
- char *name = (char*)name_json_str->string;
+ char *name = name_json->valuestring;
string_replace(name, '/', '_');
name = strip(name);
- if(callback(name, url_json_str->string, userdata) != 0)
+ if(callback(name, url_json->valuestring, userdata) != 0)
break;
}
@@ -245,7 +235,7 @@ int add_html(const char *name, const char *url, char *html_config_dir, char *pro
plugin_list_userdata.found_start_after = 0;
plugin_list_userdata.start_after_url = NULL;
- struct json_value_s *json_root = plugin_list(domain_plugin_path, url, NULL, plugin_list_callback, &plugin_list_userdata);
+ cJSON *json_root = plugin_list(domain_plugin_path, url, NULL, plugin_list_callback, &plugin_list_userdata);
if(!json_root)
return -1;
@@ -322,7 +312,7 @@ int add_html(const char *name, const char *url, char *html_config_dir, char *pro
remove(in_progress_filepath);
cleanup:
- free(json_root);
+ cJSON_Delete(json_root);
return result;
}
@@ -407,14 +397,10 @@ int sync_html(TrackedHtml *tracked_html, char *program_dir, const char *download
if(get_plugin_filepath(program_dir, tracked_html->plugin, plugin_filepath) != 0)
return -1;
- struct json_value_s *downloaded_items = json_object_get_field_by_name(tracked_html->json_data, "downloaded");
- struct json_array_s *downloaded_items_array = NULL;
- if(downloaded_items) {
- downloaded_items_array = json_value_as_array(downloaded_items);
- if(!downloaded_items_array) {
- fprintf(stderr, "Corrupt json for html item: %s\n", tracked_html->title);
- return -1;
- }
+ cJSON *downloaded_items = cJSON_GetObjectItemCaseSensitive(tracked_html->json_data, "downloaded");
+ if(!cJSON_IsArray(downloaded_items)) {
+ fprintf(stderr, "Corrupt json for html item: %s\n", tracked_html->title);
+ return -1;
}
Buffer download_items_buffer;
@@ -422,7 +408,7 @@ int sync_html(TrackedHtml *tracked_html, char *program_dir, const char *download
int result = 0;
- struct json_value_s *json_root = plugin_list(plugin_filepath, tracked_html->link, downloaded_items_array, plugin_list_sync_callback, &download_items_buffer);
+ cJSON *json_root = plugin_list(plugin_filepath, tracked_html->link, downloaded_items, plugin_list_sync_callback, &download_items_buffer);
if(!json_root) {
result = -1;
goto cleanup;
@@ -447,7 +433,7 @@ int sync_html(TrackedHtml *tracked_html, char *program_dir, const char *download
}
cleanup:
- free(json_root);
+ cJSON_Delete(json_root);
buffer_deinit(&download_items_buffer);
html_config_dir[html_config_dir_len] = '\0';
return result;