diff --git a/libscan/arc/arc.c b/libscan/arc/arc.c index cf0db58..81d5836 100644 --- a/libscan/arc/arc.c +++ b/libscan/arc/arc.c @@ -152,8 +152,7 @@ scan_code_t parse_archive(scan_arc_ctx_t *ctx, vfile_t *f, document_t *doc) { sub_job->ext = (int) strlen(sub_job->filepath); } - //TODO: -// parse(sub_job); + ctx->parse(sub_job); } } diff --git a/libscan/arc/arc.h b/libscan/arc/arc.h index 43c347f..fd6fb35 100644 --- a/libscan/arc/arc.h +++ b/libscan/arc/arc.h @@ -14,6 +14,7 @@ typedef int archive_mode_t; typedef struct { archive_mode_t mode; + parse_callback_t parse; log_callback_t log; logf_callback_t logf; store_callback_t store; diff --git a/libscan/cbr/cbr.c b/libscan/cbr/cbr.c index 970b0a5..a281560 100644 --- a/libscan/cbr/cbr.c +++ b/libscan/cbr/cbr.c @@ -7,18 +7,6 @@ #include #include -unsigned int cbr_mime; -unsigned int cbz_mime; - -void cbr_init() { - //TODO: get mime str -// cbr_mime = mime_get_mime_by_string(ScanCtx.mime_table, "application/x-cbr"); -// cbz_mime = mime_get_mime_by_string(ScanCtx.mime_table, "application/x-cbz"); -} - -int is_cbr(unsigned int mime) { - return mime == cbr_mime; -} void parse_cbr(scan_cbr_ctx_t *ctx, vfile_t *f, document_t *doc) { @@ -56,10 +44,7 @@ void parse_cbr(scan_cbr_ctx_t *ctx, vfile_t *f, document_t *doc) { archive_read_close(rar_in); archive_read_free(rar_in); - doc->mime = cbz_mime; - - //TODO: get mime string -// parse_ebook(out_buf, out_buf_used, doc); - doc->mime = cbr_mime; + parse_ebook_mem(&ctx->ebook_ctx, out_buf, out_buf_used, "application/x-cbz", doc); + doc->mime = ctx->cbr_mime; free(out_buf); } diff --git a/libscan/cbr/cbr.h b/libscan/cbr/cbr.h index 46c3ae2..e791993 100644 --- a/libscan/cbr/cbr.h +++ b/libscan/cbr/cbr.h @@ -5,14 +5,17 @@ #include "../scan.h" typedef struct { + scan_ebook_ctx_t ebook_ctx; + unsigned int cbr_mime; log_callback_t log; logf_callback_t logf; store_callback_t store; } scan_cbr_ctx_t; -void cbr_init(); - -int is_cbr(unsigned int mime); +__always_inline +static int is_cbr(scan_cbr_ctx_t *ctx, unsigned int mime) { + return mime == ctx->cbr_mime; +} void parse_cbr(scan_cbr_ctx_t *ctx, vfile_t *f, document_t *doc); diff --git a/libscan/ebook/ebook.c b/libscan/ebook/ebook.c index c020863..b59eb27 100644 --- a/libscan/ebook/ebook.c +++ b/libscan/ebook/ebook.c @@ -165,10 +165,7 @@ void fill_image(fz_context *fzctx, UNUSED(fz_device *dev), } } -void parse_ebook(scan_ebook_ctx_t *ctx, vfile_t *f, const char* mime_str, document_t *doc) { - - size_t buf_len; - void * buf = read_all(f, &buf_len); +void parse_ebook_mem(scan_ebook_ctx_t *ctx, void* buf, size_t buf_len, const char* mime_str, document_t *doc) { static int mu_is_initialized = 0; if (!mu_is_initialized) { @@ -188,10 +185,10 @@ void parse_ebook(scan_ebook_ctx_t *ctx, vfile_t *f, const char* mime_str, docum fz_var(err); fz_try(fzctx) - { - stream = fz_open_memory(fzctx, buf, buf_len); - fzdoc = fz_open_document_with_stream(fzctx, mime_str, stream); - } + { + stream = fz_open_memory(fzctx, buf, buf_len); + fzdoc = fz_open_document_with_stream(fzctx, mime_str, stream); + } fz_catch(fzctx) err = fzctx->error.errcode; @@ -204,7 +201,7 @@ void parse_ebook(scan_ebook_ctx_t *ctx, vfile_t *f, const char* mime_str, docum char title[4096] = {'\0',}; fz_try(fzctx) - fz_lookup_metadata(fzctx, fzdoc, FZ_META_INFO_TITLE, title, sizeof(title)); + fz_lookup_metadata(fzctx, fzdoc, FZ_META_INFO_TITLE, title, sizeof(title)); fz_catch(fzctx) ; @@ -218,7 +215,7 @@ void parse_ebook(scan_ebook_ctx_t *ctx, vfile_t *f, const char* mime_str, docum int page_count = -1; fz_var(err); fz_try(fzctx) - page_count = fz_count_pages(fzctx, fzdoc); + page_count = fz_count_pages(fzctx, fzdoc); fz_catch(fzctx) err = fzctx->error.errcode; @@ -249,7 +246,7 @@ void parse_ebook(scan_ebook_ctx_t *ctx, vfile_t *f, const char* mime_str, docum fz_page *page = NULL; fz_var(err); fz_try(fzctx) - page = fz_load_page(fzctx, fzdoc, current_page); + page = fz_load_page(fzctx, fzdoc, current_page); fz_catch(fzctx) err = fzctx->error.errcode; if (err != 0) { @@ -276,12 +273,12 @@ void parse_ebook(scan_ebook_ctx_t *ctx, vfile_t *f, const char* mime_str, docum fz_var(err); fz_try(fzctx) - fz_run_page(fzctx, page, dev, fz_identity, NULL); + fz_run_page(fzctx, page, dev, fz_identity, NULL); fz_always(fzctx) - { - fz_close_device(fzctx, dev); - fz_drop_device(fzctx, dev); - } + { + fz_close_device(fzctx, dev); + fz_drop_device(fzctx, dev); + } fz_catch(fzctx) err = fzctx->error.errcode; @@ -325,3 +322,9 @@ void parse_ebook(scan_ebook_ctx_t *ctx, vfile_t *f, const char* mime_str, docum fz_drop_document(fzctx, fzdoc); fz_drop_context(fzctx); } + +void parse_ebook(scan_ebook_ctx_t *ctx, vfile_t *f, const char* mime_str, document_t *doc) { + size_t buf_len; + void * buf = read_all(f, &buf_len); + parse_ebook_mem(ctx, buf, buf_len, mime_str, doc); +} diff --git a/libscan/ebook/ebook.h b/libscan/ebook/ebook.h index 236ecd2..b72e1be 100644 --- a/libscan/ebook/ebook.h +++ b/libscan/ebook/ebook.h @@ -16,5 +16,6 @@ typedef struct { } scan_ebook_ctx_t; void parse_ebook(scan_ebook_ctx_t *ctx, vfile_t *f, const char* mime_str, document_t *doc); +void parse_ebook_mem(scan_ebook_ctx_t *ctx, void* buf, size_t buf_len, const char* mime_str, document_t *doc); #endif diff --git a/libscan/scan.h b/libscan/scan.h index 9778c58..74f9eaf 100644 --- a/libscan/scan.h +++ b/libscan/scan.h @@ -53,10 +53,6 @@ typedef int scan_code_t; #define CTX_LOG_FATALF(filepath, fmt, ...) ctx->logf(filepath, LEVEL_FATAL, fmt, __VA_ARGS__); exit(-1); #define CTX_LOG_FATAL(filepath, str) ctx->log(filepath, LEVEL_FATAL, str); exit(-1); -typedef void (*store_callback_t)(char *key, size_t key_len, char *buf, size_t buf_len); -typedef void (*logf_callback_t)(char *filepath, int level, char *format, ...); -typedef void (*log_callback_t)(char *filepath, int level, char *str); - // This is written to file as a 16-bit int! enum metakey { MetaContent = META_STR(1), @@ -157,3 +153,8 @@ typedef struct parse_job_t { #include "media/media.h" #include "ooxml/ooxml.h" #include "text/text.h" + +typedef void (*store_callback_t)(char *key, size_t key_len, char *buf, size_t buf_len); +typedef void (*logf_callback_t)(char *filepath, int level, char *format, ...); +typedef void (*log_callback_t)(char *filepath, int level, char *str); +typedef void (*parse_callback_t)(parse_job_t *job); diff --git a/libscan/text/text.c b/libscan/text/text.c index aec00cd..1662c7e 100644 --- a/libscan/text/text.c +++ b/libscan/text/text.c @@ -7,7 +7,7 @@ scan_code_t parse_text(scan_text_ctx_t *ctx, struct vfile *f, document_t *doc) { char *buf = malloc(to_read); int ret = f->read(f, buf, to_read); if (ret < 0) { - //TODO: log + CTX_LOG_ERRORF(doc->filepath, "read() returned error code: [%d]", ret) return SCAN_ERR_READ; }