From 5030104f69657366d2e9136071162f62237093ec Mon Sep 17 00:00:00 2001 From: Lautis Sun Date: Wed, 7 Sep 2016 16:42:49 +0800 Subject: [PATCH 1/4] Fix unicode support #39 --- tinydir.h | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/tinydir.h b/tinydir.h index 0a17736..f7fbfa4 100644 --- a/tinydir.h +++ b/tinydir.h @@ -624,10 +624,10 @@ int tinydir_file_open(tinydir_file *file, const _tinydir_char_t *path) #if ((defined _MSC_VER) && (_MSC_VER >= 1400)) _tsplitpath_s( path, - drive_buf, sizeof drive_buf, - dir_name_buf, sizeof dir_name_buf, - file_name_buf, sizeof file_name_buf, - ext_buf, sizeof ext_buf); + drive_buf, _TINYDIR_PATH_MAX, + dir_name_buf, _TINYDIR_PATH_MAX, + file_name_buf, _TINYDIR_FILENAME_MAX, + ext_buf, sizeof _TINYDIR_FILENAME_MAX); #else _tsplitpath( path, @@ -636,6 +636,15 @@ int tinydir_file_open(tinydir_file *file, const _tinydir_char_t *path) file_name_buf, ext_buf); #endif + +/* _splitpath_s not work fine with only filename and widechar support */ +#ifdef _UNICODE + if (drive_buf[0] == L'\xFEFE') + drive_buf[0] = '\0'; + if (dir_name_buf[0] == L'\xFEFE') + dir_name_buf[0] = '\0'; +#endif + if (errno) { errno = EINVAL; @@ -643,11 +652,7 @@ int tinydir_file_open(tinydir_file *file, const _tinydir_char_t *path) } /* Emulate the behavior of dirname by returning "." for dir name if it's empty */ -#if ((defined _MSC_VER || defined __MINGW32__) && (defined UNICODE)) - if (drive_buf[0] == '\0' && drive_buf[1] == '\0' && dir_name_buf[0] == '\0' && dir_name_buf[1] == '\0') -#else if (drive_buf[0] == '\0' && dir_name_buf[0] == '\0') -#endif { _tinydir_strcpy(dir_name_buf, TINYDIR_STRING(".")); } From 2887d68125cbcc0ca0ce0602b13079254cadf06e Mon Sep 17 00:00:00 2001 From: Lautis Sun Date: Wed, 7 Sep 2016 17:17:53 +0800 Subject: [PATCH 2/4] Add declaration of tinydir_file_open --- tinydir.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tinydir.h b/tinydir.h index f7fbfa4..8f6d1b5 100644 --- a/tinydir.h +++ b/tinydir.h @@ -209,6 +209,8 @@ int tinydir_readfile_n(const tinydir_dir *dir, tinydir_file *file, size_t i); _TINYDIR_FUNC int tinydir_open_subdir_n(tinydir_dir *dir, size_t i); +_TINYDIR_FUNC +int tinydir_file_open(tinydir_file *file, const _tinydir_char_t *path); _TINYDIR_FUNC void _tinydir_get_ext(tinydir_file *file); _TINYDIR_FUNC From d4d5eb2d07762457096f602d8fa2ffaa81397b10 Mon Sep 17 00:00:00 2001 From: Lautis Sun Date: Thu, 8 Sep 2016 00:12:13 +0800 Subject: [PATCH 3/4] Define _TINYDIR_PATH_MAX as OS supports --- tinydir.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tinydir.h b/tinydir.h index 8f6d1b5..bc6c2bb 100644 --- a/tinydir.h +++ b/tinydir.h @@ -84,15 +84,27 @@ extern "C" { #define _tinydir_strncmp strncmp #endif -#define _TINYDIR_PATH_MAX 4096 +#if (defined _MSC_VER || defined __MINGW32__) +#include +#define PATH_MAX MAX_PATH +#elif defined __linux__ +#include +#endif +#define _TINYDIR_PATH_MAX PATH_MAX + #ifdef _MSC_VER /* extra chars for the "\\*" mask */ # define _TINYDIR_PATH_EXTRA 2 #else # define _TINYDIR_PATH_EXTRA 0 #endif + #define _TINYDIR_FILENAME_MAX 256 +#if (defined _MSC_VER || defined __MINGW32__) +#define _TINYDIR_DRIVE_MAX 3 +#endif + #ifdef _MSC_VER # define _TINYDIR_FUNC static __inline #elif !defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L @@ -626,7 +638,7 @@ int tinydir_file_open(tinydir_file *file, const _tinydir_char_t *path) #if ((defined _MSC_VER) && (_MSC_VER >= 1400)) _tsplitpath_s( path, - drive_buf, _TINYDIR_PATH_MAX, + drive_buf, _TINYDIR_DRIVE_MAX, dir_name_buf, _TINYDIR_PATH_MAX, file_name_buf, _TINYDIR_FILENAME_MAX, ext_buf, sizeof _TINYDIR_FILENAME_MAX); From 637c79edd341247cbf7f84b90da34b8920f3851f Mon Sep 17 00:00:00 2001 From: Lautis Sun Date: Thu, 8 Sep 2016 00:22:02 +0800 Subject: [PATCH 4/4] Make dir_name_buf no longer than _TINYDIR_FILENAME_MAX Like the previous commit, _splitpath_s may not work fine with too long buffer of parameters. Refer to https://msdn.microsoft.com/en-us/library/8e46eyt7.aspx#Anchor_3 --- tinydir.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tinydir.h b/tinydir.h index bc6c2bb..156c593 100644 --- a/tinydir.h +++ b/tinydir.h @@ -639,7 +639,7 @@ int tinydir_file_open(tinydir_file *file, const _tinydir_char_t *path) _tsplitpath_s( path, drive_buf, _TINYDIR_DRIVE_MAX, - dir_name_buf, _TINYDIR_PATH_MAX, + dir_name_buf, _TINYDIR_FILENAME_MAX, file_name_buf, _TINYDIR_FILENAME_MAX, ext_buf, sizeof _TINYDIR_FILENAME_MAX); #else