diff options
-rw-r--r-- | include/mupdf/fitz/output.h | 9 | ||||
-rw-r--r-- | source/fitz/tempfile.c | 36 |
2 files changed, 40 insertions, 5 deletions
diff --git a/include/mupdf/fitz/output.h b/include/mupdf/fitz/output.h index 36d194f8..94a3e45f 100644 --- a/include/mupdf/fitz/output.h +++ b/include/mupdf/fitz/output.h @@ -103,8 +103,13 @@ int fz_snprintf(char *buffer, int space, const char *fmt, ...); /* fz_tempfilename: Get a temporary filename based upon 'base'. - This must be freed. + + 'hint' is the path of a file (normally the existing document file) + supplied to give the function an idea of what directory to use. This + may or may not be used depending on the implementations whim. + + The returned path must be freed. */ -char *fz_tempfilename(fz_context *ctx, const char *base); +char *fz_tempfilename(fz_context *ctx, const char *base, const char *hint); #endif diff --git a/source/fitz/tempfile.c b/source/fitz/tempfile.c index 2115923e..77c7c04a 100644 --- a/source/fitz/tempfile.c +++ b/source/fitz/tempfile.c @@ -1,17 +1,47 @@ #include "mupdf/fitz.h" /* Produce a (hopefully) unique temporary filename based upon a given - * 'base' name. + * 'base' name, with a 'hint' for where to create it. * * Isolated into a file that can be modified on a per-platform basis * if required. */ -char *fz_tempfilename(fz_context *ctx, const char *base) +/* For now, put temporary files with the hint. */ +#define USE_HINT_FOR_DIR + +#if defined(_WIN32) || defined(_WIN64) +#define DIRSEP '\\' +#else +#define DIRSEP '/' +#endif + +char *fz_tempfilename(fz_context *ctx, const char *base, const char *hint) { - char *tmp = tempnam(".", base); + char *tmp; char *ret; +#ifdef USE_HINT_FOR_DIR + char *hintpath; + int hintlen; + + hintlen = strlen(hint); + hintpath = fz_malloc(ctx, 1 + hintlen); + if (hint == NULL) + fz_throw(ctx, FZ_ERROR_GENERIC, "Failed to construct temporary file name"); + + while (hintlen > 0 && hint[hintlen-1] != DIRSEP) + hintlen--; + + if (hintlen > 0) + memcpy(hintpath, hint, hintlen); + hintpath[hintlen] = 0; + tmp = tempnam(hintlen > 0 ? hintpath : ".", base); + fz_free(ctx, hintpath); +#else + tmp = tempnam(".", base); +#endif + if (tmp == NULL) fz_throw(ctx, FZ_ERROR_GENERIC, "Failed to construct temporary file name"); ret = fz_strdup(ctx, tmp); |