summaryrefslogtreecommitdiff
path: root/src/process_url.c
diff options
context:
space:
mode:
authorMole Shang <[email protected]>2023-08-06 22:18:31 +0800
committerMole Shang <[email protected]>2023-08-06 22:18:31 +0800
commit189fba5a4b63706575f9287cd0b1760a331d636e (patch)
tree40ed29ac9a5ac711dfeb9586e82e620da11fe1ef /src/process_url.c
parent99eded34e119d5b88d5210a617ecc35ba7347306 (diff)
downloadhinata-189fba5a4b63706575f9287cd0b1760a331d636e.tar.gz
hinata-189fba5a4b63706575f9287cd0b1760a331d636e.tar.bz2
hinata-189fba5a4b63706575f9287cd0b1760a331d636e.zip
process_url: initial callback support
Diffstat (limited to 'src/process_url.c')
-rw-r--r--src/process_url.c35
1 files changed, 27 insertions, 8 deletions
diff --git a/src/process_url.c b/src/process_url.c
index e5ac12e..1b391f2 100644
--- a/src/process_url.c
+++ b/src/process_url.c
@@ -36,6 +36,8 @@ mtx_t mtx;
cnd_t cnd;
bool corrupted;
static const char *outdir_g, *referer_g;
+static callback_t callback_g;
+static callback_struct_t *p_callback_struct_g;
static CURLU *h;
/*NOTE: Use logger(X) (defined as a generic macro) to log errors. */
@@ -412,6 +414,14 @@ static int download(curl_conf_t *curl_c) {
return 0;
}
+static void replace_illegal_char(char *str) {
+ for (unsigned char i = 0; illegal_char[i] != '\0'; i++) {
+ if (repchr(str, illegal_char[i], ' '))
+ DEBUG_PRINT("Found illegal character '%c', replacing ...\n",
+ illegal_char[i]);
+ }
+}
+
void curl_init(curl_conf_t *curl) {
curl_global_init(CURL_GLOBAL_ALL);
h = curl_url();
@@ -473,6 +483,10 @@ void poll_status(status_t *stat) {
thrd_join(tid[i], &r);
}
merge_and_cleanup(curl_conf);
+ // Perform the callback
+ if (is_empty_queue(&dl_queue) && callback_g) {
+ callback_g(p_callback_struct_g);
+ }
append_log("Download %s finished.\n", curl_conf->outfn);
curl_conf = NULL;
}
@@ -496,13 +510,17 @@ int get(const char *URL, char **pdstr) {
return res;
}
-/* Add an URL to dl_queue.
+/* Add an URL to dl_queue and register all the stuff.
* - If outdir is NULL or a empty string, reuse the cached outdir_g
* - If fn is NULL or a empty string, infer the filename from URL (otherwise
* fail and quit')
- * - If referer is NULL or a empty string, uses NULL */
+ * - If referer is NULL or a empty string, uses NULL
+ * - If callback || callback_struct is valid, execute the callback function
+ * after download
+ */
void add_url(const char *URL, const char *outdir, const char *fn,
- const char *referer) {
+ const char *referer, callback_t callback,
+ callback_struct_t *p_callback_struct) {
if (outdir && outdir[0] != '\0') {
outdir_g = outdir;
}
@@ -511,6 +529,11 @@ void add_url(const char *URL, const char *outdir, const char *fn,
referer_g = NULL;
}
DEBUG_PRINT("referer_g: %s\n", referer_g);
+ callback_g = callback;
+ if (p_callback_struct) {
+ p_callback_struct_g = p_callback_struct;
+ replace_illegal_char(p_callback_struct_g->title);
+ }
char *filename;
if (fn == NULL || fn[0] == '\0') {
@@ -518,11 +541,7 @@ void add_url(const char *URL, const char *outdir, const char *fn,
} else {
filename = malloc(strlen(fn) + 1);
strcpy(filename, fn);
- for (unsigned char i = 0; illegal_char[i] != '\0'; i++) {
- if (repchr(filename, illegal_char[i], ' '))
- DEBUG_PRINT("Found illegal character '%c' in filename, replacing ...\n",
- illegal_char[i]);
- }
+ replace_illegal_char(filename);
}
// Pass our cache (outdir_g) to parse_url()