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
This commit is contained in:
Manolo Gouy 2017-10-12 14:42:12 +00:00
parent a526818778
commit 789ed089c9
6 changed files with 35 additions and 9 deletions

View File

@ -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;}

View File

@ -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);

View File

@ -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

View File

@ -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);

View File

@ -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

View File

@ -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.