Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/cxong/tinydir.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLautis Sun <lautis0503@gmail.com>2020-02-15 14:05:56 +0300
committerGitHub <noreply@github.com>2020-02-15 14:05:56 +0300
commitec6bff2043eaac3ad25423705e63a781762a0dfd (patch)
tree847e766a16efec5d9137eec9370af310cedb446b
parent5d907978e1e6f2a3b0ad8d51fa186d6fda863ea6 (diff)
parent97a6032ddb2a8f8033e73be5c4a25b0e3be648f1 (diff)
Merge pull request #67 from cxong/drive_root
Implement tinydir_file_open for drive root (fixes #66)
-rw-r--r--samples/.gitignore2
-rw-r--r--tinydir.h62
2 files changed, 41 insertions, 23 deletions
diff --git a/samples/.gitignore b/samples/.gitignore
index bc406f8..da0c473 100644
--- a/samples/.gitignore
+++ b/samples/.gitignore
@@ -24,7 +24,9 @@ CMakeCache.txt
cmake_install.cmake
# Visual Studio
+.vs/
Debug/
+out/
Win32/
*.opensdf
*.sdf
diff --git a/tinydir.h b/tinydir.h
index d819700..e08eb84 100644
--- a/tinydir.h
+++ b/tinydir.h
@@ -1,5 +1,5 @@
/*
-Copyright (c) 2013-2018, tinydir authors:
+Copyright (c) 2013-2019, tinydir authors:
- Cong Xu
- Lautis Sun
- Baudouin Feildel
@@ -534,7 +534,8 @@ int tinydir_readfile(const tinydir_dir *dir, tinydir_file *file)
}
_tinydir_strcpy(file->path, dir->path);
- _tinydir_strcat(file->path, TINYDIR_STRING("/"));
+ if (_tinydir_strcmp(dir->path, TINYDIR_STRING("/")) != 0)
+ _tinydir_strcat(file->path, TINYDIR_STRING("/"));
_tinydir_strcpy(file->name, filename);
_tinydir_strcat(file->path, filename);
#ifndef _MSC_VER
@@ -657,32 +658,32 @@ int tinydir_file_open(tinydir_file *file, const _tinydir_char_t *path)
/* Get the parent path */
#if (defined _MSC_VER || defined __MINGW32__)
#if ((defined _MSC_VER) && (_MSC_VER >= 1400))
- errno = _tsplitpath_s(
- path,
- drive_buf, _TINYDIR_DRIVE_MAX,
- dir_name_buf, _TINYDIR_FILENAME_MAX,
- file_name_buf, _TINYDIR_FILENAME_MAX,
- ext_buf, _TINYDIR_FILENAME_MAX);
+ errno = _tsplitpath_s(
+ path,
+ drive_buf, _TINYDIR_DRIVE_MAX,
+ dir_name_buf, _TINYDIR_FILENAME_MAX,
+ file_name_buf, _TINYDIR_FILENAME_MAX,
+ ext_buf, _TINYDIR_FILENAME_MAX);
#else
- _tsplitpath(
- path,
- drive_buf,
- dir_name_buf,
- file_name_buf,
- ext_buf);
+ _tsplitpath(
+ path,
+ drive_buf,
+ dir_name_buf,
+ file_name_buf,
+ ext_buf);
#endif
-if (errno)
-{
- return -1;
-}
+ if (errno)
+ {
+ return -1;
+ }
/* _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';
+ if (drive_buf[0] == L'\xFEFE')
+ drive_buf[0] = '\0';
+ if (dir_name_buf[0] == L'\xFEFE')
+ dir_name_buf[0] = '\0';
#endif
/* Emulate the behavior of dirname by returning "." for dir name if it's
@@ -701,9 +702,24 @@ if (errno)
_tinydir_strcpy(dir_name_buf, path);
dir_name = dirname(dir_name_buf);
_tinydir_strcpy(file_name_buf, path);
- base_name =basename(file_name_buf);
+ base_name = basename(file_name_buf);
#endif
+ /* Special case: if the path is a root dir, open the parent dir as the file */
+#if (defined _MSC_VER || defined __MINGW32__)
+ if (_tinydir_strlen(base_name) == 0)
+#else
+ if ((_tinydir_strcmp(base_name, TINYDIR_STRING("/"))) == 0)
+#endif
+ {
+ memset(file, 0, sizeof * file);
+ file->is_dir = 1;
+ file->is_reg = 0;
+ _tinydir_strcpy(file->path, dir_name);
+ file->extension = file->path + _tinydir_strlen(file->path);
+ return 0;
+ }
+
/* Open the parent directory */
if (tinydir_open(&dir, dir_name) == -1)
{