diff options
author | Tor Andersson <tor.andersson@artifex.com> | 2012-08-28 17:34:23 +0200 |
---|---|---|
committer | Tor Andersson <tor.andersson@artifex.com> | 2012-08-28 17:34:23 +0200 |
commit | 2597722d7aa1be523058bc8449120d32768f44b3 (patch) | |
tree | 63090b06d0479a237e32d28795d279c511eee25f /fitz/doc_document.c | |
parent | 25552e9f2ec3f17a0ce1cbd8d7818170d132959a (diff) | |
download | mupdf-2597722d7aa1be523058bc8449120d32768f44b3.tar.xz |
Add fz_open_document_with_stream function.
Use a "magic" string for filetype detection: filename or mime-type.
Diffstat (limited to 'fitz/doc_document.c')
-rw-r--r-- | fitz/doc_document.c | 58 |
1 files changed, 43 insertions, 15 deletions
diff --git a/fitz/doc_document.c b/fitz/doc_document.c index 1233aeb9..eb50285b 100644 --- a/fitz/doc_document.c +++ b/fitz/doc_document.c @@ -5,6 +5,11 @@ extern struct pdf_document *pdf_open_document(fz_context *ctx, char *filename); extern struct xps_document *xps_open_document(fz_context *ctx, char *filename); extern struct cbz_document *cbz_open_document(fz_context *ctx, char *filename); +extern struct pdf_document *pdf_open_document_with_stream(fz_context *ctx, fz_stream *file); +extern struct xps_document *xps_open_document_with_stream(fz_context *ctx, fz_stream *file); +extern struct cbz_document *cbz_open_document_with_stream(fz_context *ctx, fz_stream *file); + + static inline int fz_tolower(int c) { if (c >= 'A' && c <= 'Z') @@ -24,25 +29,48 @@ static inline int fz_strcasecmp(char *a, char *b) } fz_document * +fz_open_document_with_stream(fz_context *ctx, char *magic, fz_stream *stream) +{ + char *ext = strrchr(magic, '.'); + + if (ext) + { + if (!fz_strcasecmp(ext, ".xps") || !fz_strcasecmp(ext, ".rels")) + return (fz_document*) xps_open_document_with_stream(ctx, stream); + if (!fz_strcasecmp(ext, ".cbz") || !fz_strcasecmp(ext, ".zip")) + return (fz_document*) cbz_open_document_with_stream(ctx, stream); + if (!fz_strcasecmp(ext, ".pdf")) + return (fz_document*) pdf_open_document_with_stream(ctx, stream); + } + + if (!strcmp(magic, "cbz") || !strcmp(magic, "application/x-cbz")) + return (fz_document*) cbz_open_document_with_stream(ctx, stream); + if (!strcmp(magic, "xps") || !strcmp(magic, "application/vnd.ms-xpsdocument")) + return (fz_document*) xps_open_document_with_stream(ctx, stream); + if (!strcmp(magic, "pdf") || !strcmp(magic, "application/pdf")) + return (fz_document*) pdf_open_document_with_stream(ctx, stream); + + /* last guess: pdf */ + return (fz_document*) pdf_open_document_with_stream(ctx, stream); +} + +fz_document * fz_open_document(fz_context *ctx, char *filename) { char *ext = strrchr(filename, '.'); - if (ext && (!fz_strcasecmp(ext, ".xps") || !fz_strcasecmp(ext, ".rels"))) - return (fz_document*) xps_open_document(ctx, filename); - if (ext && !fz_strcasecmp(ext, ".cbz")) - return (fz_document*) cbz_open_document(ctx, filename); -#if 0 - /* We used to only open pdf files if they ended in .pdf. For now, - * until we move to detecting filetypes by their content, we disable - * this code, and assume that any file that hasn't matched an - * extension already, is a PDF. */ - if (ext && !fz_strcasecmp(ext, ".pdf")) - return (fz_document*) pdf_open_document(ctx, filename); - fz_throw(ctx, "unknown document type: '%s'", filename); - return NULL; -#else + + if (ext) + { + if (!fz_strcasecmp(ext, ".xps") || !fz_strcasecmp(ext, ".rels")) + return (fz_document*) xps_open_document(ctx, filename); + if (!fz_strcasecmp(ext, ".cbz") || !fz_strcasecmp(ext, ".zip")) + return (fz_document*) cbz_open_document(ctx, filename); + if (!fz_strcasecmp(ext, ".pdf")) + return (fz_document*) pdf_open_document(ctx, filename); + } + + /* last guess: pdf */ return (fz_document*) pdf_open_document(ctx, filename); -#endif } void |