Skip to content

Commit

Permalink
Merge pull request #67 from cxong/drive_root
Browse files Browse the repository at this point in the history
Implement tinydir_file_open for drive root (fixes #66)
  • Loading branch information
lautis0503 authored Feb 15, 2020
2 parents 5d90797 + 97a6032 commit ec6bff2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
2 changes: 2 additions & 0 deletions samples/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ CMakeCache.txt
cmake_install.cmake

# Visual Studio
.vs/
Debug/
out/
Win32/
*.opensdf
*.sdf
Expand Down
62 changes: 39 additions & 23 deletions tinydir.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2013-2018, tinydir authors:
Copyright (c) 2013-2019, tinydir authors:
- Cong Xu
- Lautis Sun
- Baudouin Feildel
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
{
Expand Down

0 comments on commit ec6bff2

Please sign in to comment.