diff options
| author | Albrecht Schlosser <albrechts.fltk@online.de> | 2017-11-10 12:56:00 +0000 |
|---|---|---|
| committer | Albrecht Schlosser <albrechts.fltk@online.de> | 2017-11-10 12:56:00 +0000 |
| commit | 4a088d28f5607ee2713069de71b497eef335e9fd (patch) | |
| tree | afb49c2d5c08e5b1ed1c4046be237e0b031f1cdb /src/drivers/WinAPI | |
| parent | ff1e508e5d3462c2da910fedfa442b0f2b9b3617 (diff) | |
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
Diffstat (limited to 'src/drivers/WinAPI')
| -rw-r--r-- | src/drivers/WinAPI/Fl_WinAPI_System_Driver.H | 1 | ||||
| -rw-r--r-- | src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx | 48 |
2 files changed, 45 insertions, 4 deletions
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 @@ -65,6 +65,42 @@ extern "C" { } /* + 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. This function must be implemented once for every platform. @@ -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); } |
