Skip to content

Commit

Permalink
Use readdir by default (fixes #41)
Browse files Browse the repository at this point in the history
Add windows unicode test
Update .gitignore for VS2015
  • Loading branch information
cxong committed Sep 25, 2016
1 parent 1c2e211 commit ae6501d
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 21 deletions.
1 change: 1 addition & 0 deletions samples/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Win32/
*.vcxproj.user
*.vcxproj
*.vcxproj.filters
*.VC.*
*.sln

# Linux
Expand Down
4 changes: 4 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Win32/
*.vcxproj.user
*.vcxproj
*.vcxproj.filters
*.VC.*
*.sln

# Linux
Expand All @@ -41,3 +42,6 @@ Makefile
# OS X
CMakeScripts/
*.xcodeproj

# Test files
*.tmp
10 changes: 9 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ endif()
# Add tests
enable_testing()

add_library(util util.c util.h)

add_executable(file_open_test file_open_test.c)
target_link_libraries(file_open_test cbehave)
target_link_libraries(file_open_test util cbehave)
add_test(NAME file_open_test COMMAND file_open_test)

if(MSVC)
add_executable(windows_unicode_test windows_unicode_test.c)
target_link_libraries(windows_unicode_test cbehave)
add_test(NAME windows_unicode_test COMMAND windows_unicode_test)
endif()
21 changes: 1 addition & 20 deletions tests/file_open_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,8 @@

#include <tinydir.h>
#include "cbehave.h"
#include "util.h"

static void make_temp_file(const char *prefix, char *out)
{
#ifdef _MSC_VER
if (GetTempFileName(".", prefix, 0, out) != 0)
{
// Strip the ".\\" prefix
if (strncmp(out, ".\\", 2) == 0)
{
memmove(out, out + 2, strlen(out));
}
// Create file
fclose(fopen(out, "w"));
}
#else
#include <stdlib.h>
#include <unistd.h>
sprintf(out, "%sXXXXXX", prefix);
close(mkstemp(out));
#endif
}

FEATURE(file_open, "File open")
SCENARIO("Open file in current directory")
Expand Down
28 changes: 28 additions & 0 deletions tests/util.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdio.h>

#ifdef _MSC_VER
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif


void make_temp_file(const char *prefix, char *out)
{
#ifdef _MSC_VER
if (GetTempFileName(".", prefix, 0, out) != 0)
{
// Strip the ".\\" prefix
if (strncmp(out, ".\\", 2) == 0)
{
memmove(out, out + 2, strlen(out));
}
// Create file
fclose(fopen(out, "w"));
}
#else
#include <stdlib.h>
#include <unistd.h>
sprintf(out, "%sXXXXXX", prefix);
close(mkstemp(out));
#endif
}
3 changes: 3 additions & 0 deletions tests/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void make_temp_file(const char *prefix, char *out);
55 changes: 55 additions & 0 deletions tests/windows_unicode_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <stdio.h>

#define UNICODE
#include <tinydir.h>
#include "cbehave.h"


#define TEST_PATH L"windows_unicode_test"
#define TEST_PATH_A "windows_unicode_test"

void make_temp_file_utf16(const wchar_t *prefix, wchar_t *out)
{
if (GetTempFileNameW(TEST_PATH, prefix, 0, out) != 0)
{
// Create file
fclose(_wfopen(out, L"w"));
// Strip the ".\\" prefix
if (wcsncmp(out, TEST_PATH L"\\", wcslen(TEST_PATH) + 1) == 0)
{
memmove(out, out + wcslen(TEST_PATH) + 1, wcslen(out));
}
}
}

FEATURE(windows_unicode, "Windows Unicode")
SCENARIO("Open directory with unicode file names")
GIVEN("a directory with unicode file names")
CreateDirectoryW(TEST_PATH, NULL);
wchar_t name[4096];
make_temp_file_utf16(L"t📁", name);
WHEN("we open the directory")
tinydir_dir dir;
int r = tinydir_open(&dir, TEST_PATH);
THEN("the result should be successful")
SHOULD_INT_EQUAL(r, 0);
AND("iterating it, we should find the file")
bool found = false;
while (dir.has_next)
{
tinydir_file file;
tinydir_readfile(&dir, &file);
if (wcscmp((wchar_t *)file.name, name) == 0)
{
found = true;
}
tinydir_next(&dir);
}
SHOULD_BE_TRUE(found);
tinydir_close(&dir);
_wremove(name);
RemoveDirectoryW(TEST_PATH);
SCENARIO_END
FEATURE_END

CBEHAVE_RUN("Windows unicode:", TEST_FEATURE(windows_unicode))
5 changes: 5 additions & 0 deletions tinydir.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ extern "C" {
# define _TINYDIR_FUNC static inline
#endif

/* readdir_r usage; define TINYDIR_USE_READDIR_R to use it (if supported) */
#ifdef TINYDIR_USE_READDIR_R

/* readdir_r is a POSIX-only function, and may not be available under various
* environments/settings, e.g. MinGW. Use readdir fallback */
#if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE ||\
Expand All @@ -139,6 +142,8 @@ extern "C" {
# define _TINYDIR_USE_READDIR
#endif

#endif

/* MINGW32 has two versions of dirent, ASCII and UNICODE*/
#ifndef _MSC_VER
#if (defined __MINGW32__) && (defined _UNICODE)
Expand Down

0 comments on commit ae6501d

Please sign in to comment.