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>2016-06-20 22:50:14 +0300
committerLautis Sun <lautis0503@gmail.com>2016-06-20 22:50:14 +0300
commit4092c3d5e5f28e769f7035c511fc21c7717152f4 (patch)
treea5de550af1a694ae72d60f494c34431c0b523735
parent7c467d17c0445705d9c639d0b483d104e1ce4dee (diff)
Add UNICODE support in windows based on #19 (Fixes #17)
ATTENTION: NOT TESTED YET.
-rw-r--r--[-rwxr-xr-x]tinydir.h114
1 files changed, 70 insertions, 44 deletions
diff --git a/tinydir.h b/tinydir.h
index 6040ad1..6353957 100755..100644
--- a/tinydir.h
+++ b/tinydir.h
@@ -38,6 +38,7 @@ extern "C" {
#ifdef _MSC_VER
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
+# include <tchar.h>
# pragma warning(push)
# pragma warning (disable : 4996)
#else
@@ -50,6 +51,27 @@ extern "C" {
/* types */
+/* Windows UNICODE wide character support */
+#if ((defined _MSC_VER) && (defined _UNICODE))
+#define _tinydir_char_t TCHAR
+#define TINYDIR_STRING(s) TEXT(s)
+#define _tinydir_strlen _tcslen
+#define _tinydir_strcpy _tcscpy
+#define _tinydir_strcat _tcscat
+#define _tinydir_strcmp _tcscmp
+#define _tinydir_strrchr _tcsrchr
+#define _tinydir_strncmp _tcsncmp
+#else
+#define _tinydir_char_t char
+#define TINYDIR_STRING(s) s
+#define _tinydir_strlen strlen
+#define _tinydir_strcpy strcpy
+#define _tinydir_strcat strcat
+#define _tinydir_strcmp strcmp
+#define _tinydir_strrchr strrchr
+#define _tinydir_strncmp strncmp
+#endif
+
#define _TINYDIR_PATH_MAX 4096
#ifdef _MSC_VER
/* extra chars for the "\\*" mask */
@@ -105,9 +127,9 @@ extern "C" {
typedef struct tinydir_file
{
- char path[_TINYDIR_PATH_MAX];
- char name[_TINYDIR_FILENAME_MAX];
- char *extension;
+ _tinydir_char_t path[_TINYDIR_PATH_MAX];
+ _tinydir_char_t name[_TINYDIR_FILENAME_MAX];
+ _tinydir_char_t *extension;
int is_dir;
int is_reg;
@@ -118,7 +140,7 @@ typedef struct tinydir_file
typedef struct tinydir_dir
{
- char path[_TINYDIR_PATH_MAX];
+ _tinydir_char_t path[_TINYDIR_PATH_MAX];
int has_next;
size_t n_files;
@@ -139,9 +161,9 @@ typedef struct tinydir_dir
/* declarations */
_TINYDIR_FUNC
-int tinydir_open(tinydir_dir *dir, const char *path);
+int tinydir_open(tinydir_dir *dir, const _tinydir_char_t *path);
_TINYDIR_FUNC
-int tinydir_open_sorted(tinydir_dir *dir, const char *path);
+int tinydir_open_sorted(tinydir_dir *dir, const _tinydir_char_t *path);
_TINYDIR_FUNC
void tinydir_close(tinydir_dir *dir);
@@ -169,7 +191,7 @@ size_t _tinydir_dirent_buf_size(DIR *dirp);
/* definitions*/
_TINYDIR_FUNC
-int tinydir_open(tinydir_dir *dir, const char *path)
+int tinydir_open(tinydir_dir *dir, const _tinydir_char_t *path)
{
#ifndef _MSC_VER
#ifndef _TINYDIR_USE_READDIR
@@ -177,16 +199,16 @@ int tinydir_open(tinydir_dir *dir, const char *path)
int size; /* using int size */
#endif
#else
- char path_buf[_TINYDIR_PATH_MAX];
+ _tinydir_char_t path_buf[_TINYDIR_PATH_MAX];
#endif
- char *pathp;
+ _tinydir_char_t *pathp;
- if (dir == NULL || path == NULL || strlen(path) == 0)
+ if (dir == NULL || path == NULL || _tinydir_strlen(path) == 0)
{
errno = EINVAL;
return -1;
}
- if (strlen(path) + _TINYDIR_PATH_EXTRA >= _TINYDIR_PATH_MAX)
+ if (_tinydir_strlen(path) + _TINYDIR_PATH_EXTRA >= _TINYDIR_PATH_MAX)
{
errno = ENAMETOOLONG;
return -1;
@@ -204,17 +226,17 @@ int tinydir_open(tinydir_dir *dir, const char *path)
#endif
tinydir_close(dir);
- strcpy(dir->path, path);
+ _tinydir_strcpy(dir->path, path);
/* Remove trailing slashes */
- pathp = &dir->path[strlen(dir->path) - 1];
- while (pathp != dir->path && (*pathp == '\\' || *pathp == '/'))
+ pathp = &dir->path[_tinydir_strlen(dir->path) - 1];
+ while (pathp != dir->path && (*pathp == TINYDIR_STRING('\\') || *pathp == TINYDIR_STRING('/')))
{
- *pathp = '\0';
+ *pathp = TINYDIR_STRING('\0');
pathp++;
}
#ifdef _MSC_VER
- strcpy(path_buf, dir->path);
- strcat(path_buf, "\\*");
+ _tinydir_strcpy(path_buf, dir->path);
+ _tinydir_strcat(path_buf, TINYDIR_STRING("\\*"));
dir->_h = FindFirstFileA(path_buf, &dir->_f);
if (dir->_h == INVALID_HANDLE_VALUE)
{
@@ -256,7 +278,7 @@ bail:
}
_TINYDIR_FUNC
-int tinydir_open_sorted(tinydir_dir *dir, const char *path)
+int tinydir_open_sorted(tinydir_dir *dir, const _tinydir_char_t *path)
{
/* Count the number of files first, to pre-allocate the files array */
size_t n_files = 0;
@@ -415,8 +437,8 @@ int tinydir_readfile(const tinydir_dir *dir, tinydir_file *file)
errno = ENOENT;
return -1;
}
- if (strlen(dir->path) +
- strlen(
+ if (_tinydir_strlen(dir->path) +
+ _tinydir_strlen(
#ifdef _MSC_VER
dir->_f.cFileName
#else
@@ -429,7 +451,7 @@ int tinydir_readfile(const tinydir_dir *dir, tinydir_file *file)
errno = ENAMETOOLONG;
return -1;
}
- if (strlen(
+ if (_tinydir_strlen(
#ifdef _MSC_VER
dir->_f.cFileName
#else
@@ -441,16 +463,16 @@ int tinydir_readfile(const tinydir_dir *dir, tinydir_file *file)
return -1;
}
- strcpy(file->path, dir->path);
- strcat(file->path, "/");
- strcpy(file->name,
+ _tinydir_strcpy(file->path, dir->path);
+ _tinydir_strcat(file->path, TINYDIR_STRING("/"));
+ _tinydir_strcpy(file->name,
#ifdef _MSC_VER
dir->_f.cFileName
#else
dir->_e->d_name
#endif
);
- strcat(file->path, file->name);
+ _tinydir_strcat(file->path, file->name);
#ifndef _MSC_VER
if (stat(file->path, &file->_s) == -1)
{
@@ -510,7 +532,7 @@ 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)
{
- char path[_TINYDIR_PATH_MAX];
+ _tinydir_char_t path[_TINYDIR_PATH_MAX];
if (dir == NULL)
{
errno = EINVAL;
@@ -522,7 +544,7 @@ int tinydir_open_subdir_n(tinydir_dir *dir, size_t i)
return -1;
}
- strcpy(path, dir->_files[i].path);
+ _tinydir_strcpy(path, dir->_files[i].path);
tinydir_close(dir);
if (tinydir_open_sorted(dir, path) == -1)
{
@@ -534,26 +556,26 @@ int tinydir_open_subdir_n(tinydir_dir *dir, size_t i)
/* Open a single file given its path */
_TINYDIR_FUNC
-int tinydir_file_open(tinydir_file *file, const char *path)
+int tinydir_file_open(tinydir_file *file, const _tinydir_char_t *path)
{
tinydir_dir dir;
int result = 0;
int found = 0;
- char dir_name_buf[_TINYDIR_PATH_MAX];
- char file_name_buf[_TINYDIR_FILENAME_MAX];
- char *dir_name;
- char *base_name;
+ _tinydir_char_t dir_name_buf[_TINYDIR_PATH_MAX];
+ _tinydir_char_t file_name_buf[_TINYDIR_FILENAME_MAX];
+ _tinydir_char_t *dir_name;
+ _tinydir_char_t *base_name;
#ifdef _MSC_VER
- char drive_buf[_TINYDIR_PATH_MAX];
- char ext_buf[_TINYDIR_FILENAME_MAX];
+ _tinydir_char_t drive_buf[_TINYDIR_PATH_MAX];
+ _tinydir_char_t ext_buf[_TINYDIR_FILENAME_MAX];
#endif
- if (file == NULL || path == NULL || strlen(path) == 0)
+ if (file == NULL || path == NULL || _tinydir_strlen(path) == 0)
{
errno = EINVAL;
return -1;
}
- if (strlen(path) + _TINYDIR_PATH_EXTRA >= _TINYDIR_PATH_MAX)
+ if (_tinydir_strlen(path) + _TINYDIR_PATH_EXTRA >= _TINYDIR_PATH_MAX)
{
errno = ENAMETOOLONG;
return -1;
@@ -561,7 +583,11 @@ int tinydir_file_open(tinydir_file *file, const char *path)
/* Get the parent path */
#ifdef _MSC_VER
+#ifdef _UNICODE
+ if (_wsplitpath_s(
+#else
if (_splitpath_s(
+#endif
path,
drive_buf, sizeof drive_buf,
dir_name_buf, sizeof dir_name_buf,
@@ -572,15 +598,15 @@ int tinydir_file_open(tinydir_file *file, const char *path)
return -1;
}
/* Concatenate the drive letter and dir name to form full dir name */
- strcat(drive_buf, dir_name_buf);
+ _tinydir_strcat(drive_buf, dir_name_buf);
dir_name = drive_buf;
/* Concatenate the file name and extension to form base name */
- strcat(file_name_buf, ext_buf);
+ _tinydir_strcat(file_name_buf, ext_buf);
base_name = file_name_buf;
#else
- strcpy(dir_name_buf, path);
+ _tinydir_strcpy(dir_name_buf, path);
dir_name = dirname(dir_name_buf);
- strcpy(file_name_buf, path);
+ _tinydir_strcpy(file_name_buf, path);
base_name = basename(file_name_buf);
#endif
@@ -598,7 +624,7 @@ int tinydir_file_open(tinydir_file *file, const char *path)
result = -1;
goto bail;
}
- if (strcmp(file->name, base_name) == 0)
+ if (_tinydir_strcmp(file->name, base_name) == 0)
{
/* File found */
found = 1;
@@ -620,10 +646,10 @@ bail:
_TINYDIR_FUNC
void _tinydir_get_ext(tinydir_file *file)
{
- char *period = strrchr(file->name, '.');
+ _tinydir_char_t *period = _tinydir_strrchr(file->name, TINYDIR_STRING('.'));
if (period == NULL)
{
- file->extension = &(file->name[strlen(file->name)]);
+ file->extension = &(file->name[_tinydir_strlen(file->name)]);
}
else
{
@@ -640,7 +666,7 @@ int _tinydir_file_cmp(const void *a, const void *b)
{
return -(fa->is_dir - fb->is_dir);
}
- return strncmp(fa->name, fb->name, _TINYDIR_FILENAME_MAX);
+ return _tinydir_strncmp(fa->name, fb->name, _TINYDIR_FILENAME_MAX);
}
#ifndef _MSC_VER