summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2012-04-20 16:00:13 +0200
committerRobin Watts <robin.watts@artifex.com>2012-04-20 16:10:02 +0100
commitc6e27a74283a1fa31a930f1909dbc46e131f0447 (patch)
treeb0104b5dd19f07fec8d2c03720c3efc8a8f60166
parent9fbd2de7411f6c87c6f1ad0d9adfba9fe5164e0f (diff)
downloadmupdf-c6e27a74283a1fa31a930f1909dbc46e131f0447.tar.xz
Pass UTF8 filename to fz_open_document / fz_open_file to remove kludges.
Use _wopen on a UTF8 -> wchar_t decoded filename to support UTF8 filenames for win32.
-rw-r--r--apps/pdfapp.c16
-rw-r--r--apps/pdfapp.h2
-rw-r--r--apps/win_main.c16
-rw-r--r--apps/x11_main.c16
-rw-r--r--fitz/doc_document.c2
-rw-r--r--fitz/fitz.h17
-rw-r--r--fitz/stm_open.c14
7 files changed, 33 insertions, 50 deletions
diff --git a/apps/pdfapp.c b/apps/pdfapp.c
index 66513570..52bb4c48 100644
--- a/apps/pdfapp.c
+++ b/apps/pdfapp.c
@@ -89,26 +89,14 @@ void pdfapp_invert(pdfapp_t *app, fz_bbox rect)
fz_invert_pixmap_rect(app->image, rect);
}
-void pdfapp_open(pdfapp_t *app, char *filename, int fd, int reload)
+void pdfapp_open(pdfapp_t *app, char *filename, int reload)
{
fz_context *ctx = app->ctx;
- fz_stream *file;
char *password = "";
fz_try(ctx)
{
- file = fz_open_fd(ctx, fd);
-
- if (strstr(filename, ".rels"))
- app->doc = (fz_document*) xps_open_document(ctx, filename);
- else if (strstr(filename, ".xps") || strstr(filename, ".XPS") || strstr(filename, ".rels"))
- app->doc = (fz_document*) xps_open_document_with_stream(file);
- else if (strstr(filename, ".cbz") || strstr(filename, ".CBZ") || strstr(filename, ".zip") || strstr(filename, ".ZIP"))
- app->doc = (fz_document*) cbz_open_document_with_stream(file);
- else
- app->doc = (fz_document*) pdf_open_document_with_stream(file);
-
- fz_close(file);
+ app->doc = fz_open_document(ctx, filename);
if (fz_needs_password(app->doc))
{
diff --git a/apps/pdfapp.h b/apps/pdfapp.h
index 6812323b..f93b816a 100644
--- a/apps/pdfapp.h
+++ b/apps/pdfapp.h
@@ -100,7 +100,7 @@ struct pdfapp_s
};
void pdfapp_init(fz_context *ctx, pdfapp_t *app);
-void pdfapp_open(pdfapp_t *app, char *filename, int fd, int reload);
+void pdfapp_open(pdfapp_t *app, char *filename, int reload);
void pdfapp_close(pdfapp_t *app);
char *pdfapp_version(pdfapp_t *app);
diff --git a/apps/win_main.c b/apps/win_main.c
index cc71a120..358b21bb 100644
--- a/apps/win_main.c
+++ b/apps/win_main.c
@@ -594,15 +594,8 @@ void windocopy(pdfapp_t *app)
void winreloadfile(pdfapp_t *app)
{
- int fd;
-
pdfapp_close(app);
-
- fd = _wopen(wbuf, O_BINARY | O_RDONLY, 0666);
- if (fd < 0)
- winerror(&gapp, "cannot reload file");
-
- pdfapp_open(app, filename, fd, 1);
+ pdfapp_open(app, filename, 1);
}
void winopenuri(pdfapp_t *app, char *buf)
@@ -834,7 +827,6 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShow
LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(), &argc);
char argv0[256];
MSG msg;
- int fd;
int code;
fz_context *ctx;
@@ -861,15 +853,11 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShow
exit(0);
}
- fd = _wopen(wbuf, O_BINARY | O_RDONLY, 0666);
- if (fd < 0)
- winerror(&gapp, "cannot open file");
-
code = WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, filename, sizeof filename, NULL, NULL);
if (code == 0)
winerror(&gapp, "cannot convert filename to utf-8");
- pdfapp_open(&gapp, filename, fd, 0);
+ pdfapp_open(&gapp, filename, 0);
while (GetMessage(&msg, NULL, 0, 0))
{
diff --git a/apps/x11_main.c b/apps/x11_main.c
index b62f97db..6ab5b43a 100644
--- a/apps/x11_main.c
+++ b/apps/x11_main.c
@@ -532,15 +532,8 @@ void onselreq(Window requestor, Atom selection, Atom target, Atom property, Time
void winreloadfile(pdfapp_t *app)
{
- int fd;
-
pdfapp_close(app);
-
- fd = open(filename, O_RDONLY, 0666);
- if (fd < 0)
- fz_throw(gapp.ctx, "cannot reload file '%s'", filename);
-
- pdfapp_open(app, filename, fd, 1);
+ pdfapp_open(app, filename, 1);
}
void winopenuri(pdfapp_t *app, char *buf)
@@ -612,7 +605,6 @@ int main(int argc, char **argv)
int oldy = 0;
int resolution = -1;
int pageno = 1;
- int fd;
fd_set fds;
int width = -1;
int height = -1;
@@ -663,11 +655,7 @@ int main(int argc, char **argv)
gapp.resolution = resolution;
gapp.pageno = pageno;
- fd = open(filename, O_RDONLY, 0666);
- if (fd < 0)
- fz_throw(gapp.ctx, "cannot open file '%s'", filename);
-
- pdfapp_open(&gapp, filename, fd, 0);
+ pdfapp_open(&gapp, filename, 0);
FD_ZERO(&fds);
diff --git a/fitz/doc_document.c b/fitz/doc_document.c
index 15ad4b62..22229892 100644
--- a/fitz/doc_document.c
+++ b/fitz/doc_document.c
@@ -27,7 +27,7 @@ fz_document *
fz_open_document(fz_context *ctx, char *filename)
{
char *ext = strrchr(filename, '.');
- if (ext && !fz_strcasecmp(ext, ".xps"))
+ 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);
diff --git a/fitz/fitz.h b/fitz/fitz.h
index 48b67ff8..9e0c0311 100644
--- a/fitz/fitz.h
+++ b/fitz/fitz.h
@@ -534,20 +534,20 @@ int fz_strlcpy(char *dst, const char *src, int n);
int fz_strlcat(char *dst, const char *src, int n);
/*
- fz_chartorune: UTF8 decode a string of chars to a rune.
+ fz_chartorune: UTF8 decode a single rune from a sequence of chars.
rune: Pointer to an int to assign the decoded 'rune' to.
- str: Pointer to a UTF8 encoded string
+ str: Pointer to a UTF8 encoded string.
Returns the number of bytes consumed. Does not throw exceptions.
*/
int fz_chartorune(int *rune, char *str);
/*
- runetochar: UTF8 encode a run to a string of chars.
+ fz_runetochar: UTF8 encode a rune to a sequence of chars.
- str: Pointer to a place to put the UTF8 encoded string.
+ str: Pointer to a place to put the UTF8 encoded character.
rune: Pointer to a 'rune'.
@@ -557,7 +557,7 @@ int fz_chartorune(int *rune, char *str);
int fz_runetochar(char *str, int rune);
/*
- fz_runelen: Count many chars are required to represent a rune.
+ fz_runelen: Count how many chars are required to represent a rune.
rune: The rune to encode.
@@ -986,7 +986,12 @@ typedef struct fz_stream_s fz_stream;
/*
fz_open_file: Open the named file and wrap it in a stream.
- filename: Path to a file as it would be given to open(2).
+ filename: Path to a file. On non-Windows machines the filename should
+ be exactly as it would be passed to open(2). On Windows machines, the
+ path should be UTF-8 encoded so that non-ASCII characters can be
+ represented. Other platforms do the encoding as standard anyway (and
+ in most cases, particularly for MacOS and Linux, the encoding they
+ use is UTF-8 anyway).
*/
fz_stream *fz_open_file(fz_context *ctx, const char *filename);
diff --git a/fitz/stm_open.c b/fitz/stm_open.c
index 1e303825..ced32d80 100644
--- a/fitz/stm_open.c
+++ b/fitz/stm_open.c
@@ -129,7 +129,21 @@ fz_open_fd(fz_context *ctx, int fd)
fz_stream *
fz_open_file(fz_context *ctx, const char *name)
{
+#ifdef _WIN32
+ char *s = (char*)name;
+ wchar_t *wname, *d;
+ int c, fd;
+ d = wname = fz_malloc(ctx, (strlen(name)+1) * sizeof(wchar_t));
+ while (*s) {
+ s += fz_chartorune(&c, s);
+ *d++ = c;
+ }
+ *d = 0;
+ fd = _wopen(wname, O_BINARY | O_RDONLY, 0);
+ fz_free(ctx, wname);
+#else
int fd = open(name, O_BINARY | O_RDONLY, 0);
+#endif
if (fd == -1)
fz_throw(ctx, "cannot open %s", name);
return fz_open_fd(ctx, fd);