From 4a088d28f5607ee2713069de71b497eef335e9fd Mon Sep 17 00:00:00 2001 From: Albrecht Schlosser Date: Fri, 10 Nov 2017 12:56:00 +0000 Subject: Add missing platform wrapper fl_chdir() for chdir(). Tested under Windows and Linux, but not yet used in library code. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.4@12549 ea41ed52-d2ee-0310-a9c1-e6b18d33e121 --- src/drivers/Posix/Fl_Posix_System_Driver.H | 1 + src/drivers/WinAPI/Fl_WinAPI_System_Driver.H | 1 + src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx | 48 +++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 4 deletions(-) (limited to 'src/drivers') diff --git a/src/drivers/Posix/Fl_Posix_System_Driver.H b/src/drivers/Posix/Fl_Posix_System_Driver.H index 5b2060283..6b6d25f61 100644 --- a/src/drivers/Posix/Fl_Posix_System_Driver.H +++ b/src/drivers/Posix/Fl_Posix_System_Driver.H @@ -60,6 +60,7 @@ public: virtual int access(const char* f, int mode) { return ::access(f, mode);} virtual int stat(const char* f, struct stat *b) { return ::stat(f, b);} virtual char *getcwd(char* b, int l) {return ::getcwd(b, l);} + virtual int chdir(const char* path) {return ::chdir(path);} virtual int unlink(const char* f) {return ::unlink(f);} virtual int rmdir(const char* f) {return ::rmdir(f);} virtual int rename(const char* f, const char *n) {return ::rename(f, n);} diff --git a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H index 6b3a1e85e..36a7bf471 100644 --- a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H +++ b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H @@ -58,6 +58,7 @@ public: virtual int access(const char* f, int mode); virtual int stat(const char* f, struct stat *b); virtual char *getcwd(char* b, int l); + virtual int chdir(const char* path); virtual int unlink(const char* f); virtual int mkdir(const char* f, int mode); virtual int rmdir(const char* f); diff --git a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx index 806a76b22..b1fb5a7e1 100644 --- a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx +++ b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx @@ -64,6 +64,42 @@ extern "C" { int (*compar)(struct dirent **, struct dirent **)); } +/* + Convert a filename or path from UTF-8 to Windows wide character encoding (UTF-16). + + This helper function is used througout this file to convert UTF-8 strings + to Windows specific UTF-8 encoding for filenames and paths. + + The argument 'wbuf' must have been initialized with NULL or a previous call + to malloc() or realloc(). The global static variables above, particularly + wbuf, may be used to share one static pointer for many calls. Ideally + every call to this function would have its own static pointer though. + + If the converted string doesn't fit into the allocated size of 'wbuf' or + 'wbuf' is NULL a new buffer is allocated with realloc(). Hence, the pointer + 'wbuf' can be shared among multiple calls of this function if it has been + initialized with NULL (or malloc or realloc) before the first call. + + The return value is either the old value of 'wbuf' (if the string fits) + or a pointer at the (re)allocated buffer. + + Pseudo doxygen docs (static function intentionally not documented): + + param[in] path path to new working directory + param[in,out] wbuf pointer to string buffer (in) + new string (out, pointer may be changed) + + returns pointer to (new) string (buffer); may be changed +*/ +static wchar_t *path_to_wchar(const char *path, wchar_t *&wbuf) { + unsigned len = (unsigned)strlen(path); + unsigned wn = fl_utf8toUtf16(path, len, NULL, 0) + 1; // Query length + wbuf = (wchar_t *)realloc(wbuf, sizeof(wchar_t) * wn); + wn = fl_utf8toUtf16(path, len, (unsigned short *)wbuf, wn); // Convert string + wbuf[wn] = 0; + return wbuf; +} + /* Creates a driver that manages all system related calls. @@ -250,11 +286,15 @@ char *Fl_WinAPI_System_Driver::getcwd(char* b, int l) { } } -int Fl_WinAPI_System_Driver::unlink(const char* f) { - size_t l = strlen(f); - unsigned wn = fl_utf8toUtf16(f, (unsigned) l, NULL, 0) + 1; // Query length +int Fl_WinAPI_System_Driver::chdir(const char *path) { + return _wchdir(path_to_wchar(path, wbuf)); +} + +int Fl_WinAPI_System_Driver::unlink(const char *fname) { + size_t len = strlen(fname); + unsigned wn = fl_utf8toUtf16(fname, (unsigned)len, NULL, 0) + 1; // Query length wbuf = (wchar_t*)realloc(wbuf, sizeof(wchar_t)*wn); - wn = fl_utf8toUtf16(f, (unsigned) l, (unsigned short *)wbuf, wn); // Convert string + wn = fl_utf8toUtf16(fname, (unsigned)len, (unsigned short *)wbuf, wn); // Convert string wbuf[wn] = 0; return _wunlink(wbuf); } -- cgit v1.2.3