diff options
| author | Manolo Gouy <Manolo> | 2017-10-12 14:42:12 +0000 |
|---|---|---|
| committer | Manolo Gouy <Manolo> | 2017-10-12 14:42:12 +0000 |
| commit | 789ed089c903922d0d66fcc2849dd24fbcaf2495 (patch) | |
| tree | 8b36dfccefd881d7b85d81f2818933d447316096 | |
| parent | a526818778f1e378dbcfe300dc5ab42df023adfe (diff) | |
Add fl_open_ext() to control whether the file is opened in binary/text mode.
This new function allows to write Fl_SVG_Image::fl_gzopen() in a completely platform-independent way.
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.4@12489 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
| -rw-r--r-- | FL/Fl_System_Driver.H | 1 | ||||
| -rw-r--r-- | FL/fl_utf8.h | 2 | ||||
| -rw-r--r-- | src/Fl_SVG_Image.cxx | 9 | ||||
| -rw-r--r-- | src/drivers/WinAPI/Fl_WinAPI_System_Driver.H | 1 | ||||
| -rw-r--r-- | src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx | 7 | ||||
| -rw-r--r-- | src/fl_utf8.cxx | 24 |
6 files changed, 35 insertions, 9 deletions
diff --git a/FL/Fl_System_Driver.H b/FL/Fl_System_Driver.H index bd73a96f9..b1fb209c6 100644 --- a/FL/Fl_System_Driver.H +++ b/FL/Fl_System_Driver.H @@ -87,6 +87,7 @@ public: virtual char *getenv(const char* v) {return NULL;} virtual int putenv(char* v) {return -1;} virtual int open(const char* f, int oflags, int pmode) {return -1;} + virtual int open_ext(const char* f, int translation, int oflags, int pmode) {return this->open(f, oflags, pmode);} virtual FILE *fopen(const char* f, const char *mode); virtual int system(const char* cmd) {return -1;} virtual int execvp(const char *file, char *const *argv) {return -1;} diff --git a/FL/fl_utf8.h b/FL/fl_utf8.h index ca4ac27e9..54b192b1f 100644 --- a/FL/fl_utf8.h +++ b/FL/fl_utf8.h @@ -177,6 +177,8 @@ FL_EXPORT int fl_execvp(const char *file, char *const *argv); /* OD: Portable UTF-8 aware open wrapper */ FL_EXPORT int fl_open(const char* f, int o, ...); +FL_EXPORT int fl_open_ext(const char* fname, int translation, int oflags, ...); + /* OD: Portable UTF-8 aware unlink wrapper */ FL_EXPORT int fl_unlink(const char *f); diff --git a/src/Fl_SVG_Image.cxx b/src/Fl_SVG_Image.cxx index 0b29b8c48..3c8957afd 100644 --- a/src/Fl_SVG_Image.cxx +++ b/src/Fl_SVG_Image.cxx @@ -28,9 +28,6 @@ #include <stdlib.h> #if defined(HAVE_LIBZ) #include <zlib.h> -# ifdef _WIN32 -# include <fcntl.h> -# endif #endif #if !defined(HAVE_LONG_LONG) @@ -82,11 +79,7 @@ float Fl_SVG_Image::svg_scaling_(int W, int H) { /** Opens for reading a potentially gzip'ed file identified by a UTF-8 encoded filename. */ void* Fl_SVG_Image::fl_gzopen(const char *fname) { #if defined(HAVE_LIBZ) - int flags = 0; -# ifdef _WIN32 - flags = _O_BINARY; -# endif - int fd = fl_open(fname, flags); + int fd = fl_open_ext(fname, 0, 0); if (fd < 0) return NULL; return gzdopen(fd, "r"); #else diff --git a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H index 43e0f54e2..fdc686fed 100644 --- a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H +++ b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H @@ -50,6 +50,7 @@ public: virtual char *getenv(const char* v); virtual int putenv(char* v) {return _putenv(v);} virtual int open(const char* f, int oflags, int pmode); + virtual int open_ext(const char* f, int translation, int oflags, int pmode); virtual FILE *fopen(const char* f, const char *mode); virtual int system(const char* cmd); virtual int execvp(const char *file, char *const *argv); diff --git a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx index efbe65d01..aa1ff7e50 100644 --- a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx +++ b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx @@ -42,6 +42,7 @@ typedef RPC_STATUS (WINAPI* uuid_func)(UUID __RPC_FAR *Uuid); #include <time.h> #include <direct.h> #include <io.h> +#include <fcntl.h> // Apparently Borland C++ defines DIRECTORY in <direct.h>, which // interfers with the Fl_File_Icon enumeration of the same name. # ifdef DIRECTORY @@ -136,6 +137,12 @@ int Fl_WinAPI_System_Driver::open(const char* f, int oflags, int pmode) { else return _wopen(wbuf, oflags, pmode); } +int Fl_WinAPI_System_Driver::open_ext(const char* f, int translation, int oflags, int pmode) { + if (oflags == 0) oflags = _O_RDONLY; + oflags |= (translation ? _O_TEXT : _O_BINARY); + return this->open(f, oflags, pmode); +} + FILE *Fl_WinAPI_System_Driver::fopen(const char* f, const char *mode) { size_t l = strlen(f); unsigned wn = fl_utf8toUtf16(f, (unsigned) l, NULL, 0) + 1; // Query length diff --git a/src/fl_utf8.cxx b/src/fl_utf8.cxx index b7d58eecd..afd5c6005 100644 --- a/src/fl_utf8.cxx +++ b/src/fl_utf8.cxx @@ -309,7 +309,7 @@ char *fl_getenv(const char* v) { \param f the UTF-8 encoded filename \param oflags other arguments are as in the standard open() function \return a file descriptor upon successful completion, or -1 in case of error. - \sa fl_fopen(). + \sa fl_fopen(), fl_open_ext(). */ int fl_open(const char* f, int oflags, ...) { @@ -321,6 +321,28 @@ int fl_open(const char* f, int oflags, ...) return Fl::system_driver()->open(f, oflags, pmode); } +/** Cross-platform function to open files with a UTF-8 encoded name. + In comparison with fl_open(), this function allows to control whether + the file is opened in binary (a.k.a. untranslated) mode. This is especially + useful under the MSWindows platform where files are by default opened in + text (translated) mode. + \param fname the UTF-8 encoded filename + \param translation if zero, the file is to be accessed in untranslated (a.k.a. binary) + mode. + \param oflags,... these arguments are as in the standard open() function. + Setting \p oflags to zero opens the file for reading. + \return a file descriptor upon successful completion, or -1 in case of error. + */ +int fl_open_ext(const char* fname, int translation, int oflags, ...) +{ + int pmode; + va_list ap; + va_start(ap, oflags); + pmode = va_arg (ap, int); + va_end(ap); + return Fl::system_driver()->open_ext(fname, translation, oflags, pmode); +} + /** Cross-platform function to open files with a UTF-8 encoded name. |
