enhanced dir.c to handle both mbs and wcs by implementing the new QSE_DIR_WCSPATH option

This commit is contained in:
hyung-hwan 2014-12-11 16:03:58 +00:00
parent 0a9a80c567
commit 77939a5940
12 changed files with 326 additions and 155 deletions

View File

@ -60,8 +60,9 @@ typedef enum qse_dir_errnum_t qse_dir_errnum_t;
enum qse_dir_flag_t enum qse_dir_flag_t
{ {
QSE_DIR_MBSPATH = (1 << 0), QSE_DIR_MBSPATH = (1 << 0),
QSE_DIR_SORT = (1 << 1), QSE_DIR_WCSPATH = (1 << 1),
QSE_DIR_SKIPSPCDIR = (1 << 2) /**< limited to normal entries excluding . and .. */ QSE_DIR_SORT = (1 << 2),
QSE_DIR_SKIPSPCDIR = (1 << 3) /**< limited to normal entries excluding . and .. */
}; };
typedef enum qse_dir_flag_t qse_dir_flag_t; typedef enum qse_dir_flag_t qse_dir_flag_t;

View File

@ -33,6 +33,7 @@
*/ */
#include <qse/types.h> #include <qse/types.h>
#include <qse/macros.h> #include <qse/macros.h>
#include <qse/cmn/str.h>
typedef qse_cmgr_t* (*qse_cmgr_finder_t) (const qse_char_t* name); typedef qse_cmgr_t* (*qse_cmgr_finder_t) (const qse_char_t* name);
@ -470,6 +471,20 @@ QSE_EXPORT qse_mchar_t* qse_wcsnatombsdup (
qse_mmgr_t* mmgr qse_mmgr_t* mmgr
); );
#if defined(QSE_CHAR_IS_MCHAR)
# define qse_strtombsdup(sa,mmgr) qse_strdup(sa,mmgr)
# define qse_strtowcsdup(sa,mmgr) qse_mbstowcsdup(sa,QSE_NULL,mmgr)
# define qse_strtombsdupwithcmgr(sa,mmgr,cmgr) qse_strdup(sa,mmgr)
# define qse_strtowcsdupwithcmgr(sa,mmgr,cmgr) qse_mbstowcsdupwithcmgr(sa,QSE_NULL,mmgr,cmgr)
#else
# define qse_strtombsdup(sa,mmgr) qse_wcstombsdup(sa,QSE_NULL,mmgr)
# define qse_strtowcsdup(sa,mmgr) qse_wcsdup(sa,mmgr)
# define qse_strtombsdupwithcmgr(sa,mmgr,cmgr) qse_wcstombsdupwithcmgr(sa,QSE_NULL,mmgr,cmgr)
# define qse_strtowcsdupwithcmgr(sa,mmgr,cmgr) qse_wcsdup(sa,mmgr)
#endif
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif

View File

@ -1392,6 +1392,7 @@ QSE_EXPORT qse_wchar_t* qse_wcstradup (
# define qse_cstradup(sa,len,mmgr) qse_wcstradup(sa,len,mmgr) # define qse_cstradup(sa,len,mmgr) qse_wcstradup(sa,len,mmgr)
#endif #endif
/** /**
* The qse_mbsstr() function searchs a string \a str for the first occurrence * The qse_mbsstr() function searchs a string \a str for the first occurrence
* of a substring \a sub. * of a substring \a sub.

View File

@ -71,7 +71,7 @@ struct qse_dir_t
qse_dir_errnum_t errnum; qse_dir_errnum_t errnum;
int flags; int flags;
qse_str_t tbuf; qse_wcs_t wbuf;
qse_mbs_t mbuf; qse_mbs_t mbuf;
qse_lda_t* stab; qse_lda_t* stab;
@ -158,13 +158,27 @@ static int compare_dirent (qse_lda_t* lda, const void* dptr1, qse_size_t dlen1,
int qse_dir_init (qse_dir_t* dir, qse_mmgr_t* mmgr, const qse_char_t* path, int flags) int qse_dir_init (qse_dir_t* dir, qse_mmgr_t* mmgr, const qse_char_t* path, int flags)
{ {
int n; int n;
int path_flags;
path_flags = flags & (QSE_DIR_MBSPATH | QSE_DIR_WCSPATH);
if (path_flags == (QSE_DIR_MBSPATH | QSE_DIR_WCSPATH) || path_flags == 0)
{
/* if both are set or none are set, force it to the default */
#if defined(QSE_CHAR_IS_MCHAR)
flags |= QSE_DIR_MBSPATH;
flags &= ~QSE_DIR_WCSPATH;
#else
flags |= QSE_DIR_WCSPATH;
flags &= ~QSE_DIR_MBSPATH;
#endif
}
QSE_MEMSET (dir, 0, QSE_SIZEOF(*dir)); QSE_MEMSET (dir, 0, QSE_SIZEOF(*dir));
dir->mmgr = mmgr; dir->mmgr = mmgr;
dir->flags = flags; dir->flags = flags;
if (qse_str_init (&dir->tbuf, mmgr, 256) <= -1) goto oops_0; if (qse_wcs_init (&dir->wbuf, mmgr, 256) <= -1) goto oops_0;
if (qse_mbs_init (&dir->mbuf, mmgr, 256) <= -1) goto oops_1; if (qse_mbs_init (&dir->mbuf, mmgr, 256) <= -1) goto oops_1;
#if defined(_WIN32) #if defined(_WIN32)
@ -194,7 +208,7 @@ oops_3:
oops_2: oops_2:
qse_mbs_fini (&dir->mbuf); qse_mbs_fini (&dir->mbuf);
oops_1: oops_1:
qse_str_fini (&dir->tbuf); qse_wcs_fini (&dir->wbuf);
oops_0: oops_0:
return -1; return -1;
} }
@ -233,12 +247,12 @@ void qse_dir_fini (qse_dir_t* dir)
close_dir_safely (dir); close_dir_safely (dir);
qse_mbs_fini (&dir->mbuf); qse_mbs_fini (&dir->mbuf);
qse_str_fini (&dir->tbuf); qse_wcs_fini (&dir->wbuf);
if (dir->stab) qse_lda_close (dir->stab); if (dir->stab) qse_lda_close (dir->stab);
} }
static qse_mchar_t* wcs_to_mbuf (qse_dir_t* dir, const qse_wchar_t* wcs, qse_mbs_t* mbs) static qse_mchar_t* wcs_to_mbuf (qse_dir_t* dir, const qse_wchar_t* wcs, qse_mbs_t* mbuf)
{ {
qse_size_t ml, wl; qse_size_t ml, wl;
@ -248,17 +262,17 @@ static qse_mchar_t* wcs_to_mbuf (qse_dir_t* dir, const qse_wchar_t* wcs, qse_mbs
return QSE_NULL; return QSE_NULL;
} }
if (qse_mbs_setlen (mbs, ml) == (qse_size_t)-1) if (qse_mbs_setlen (mbuf, ml) == (qse_size_t)-1)
{ {
dir->errnum = QSE_DIR_ENOMEM; dir->errnum = QSE_DIR_ENOMEM;
return QSE_NULL; return QSE_NULL;
} }
qse_wcstombs (wcs, &wl, QSE_MBS_PTR(mbs), &ml); qse_wcstombs (wcs, &wl, QSE_MBS_PTR(mbuf), &ml);
return QSE_MBS_PTR(mbs); return QSE_MBS_PTR(mbuf);
} }
static qse_wchar_t* mbs_to_wbuf (qse_dir_t* dir, const qse_mchar_t* mbs, qse_wcs_t* wcs) static qse_wchar_t* mbs_to_wbuf (qse_dir_t* dir, const qse_mchar_t* mbs, qse_wcs_t* wbuf)
{ {
qse_size_t ml, wl; qse_size_t ml, wl;
@ -267,16 +281,39 @@ static qse_wchar_t* mbs_to_wbuf (qse_dir_t* dir, const qse_mchar_t* mbs, qse_wcs
dir->errnum = QSE_DIR_EINVAL; dir->errnum = QSE_DIR_EINVAL;
return QSE_NULL; return QSE_NULL;
} }
if (qse_wcs_setlen (wcs, wl) == (qse_size_t)-1) if (qse_wcs_setlen (wbuf, wl) == (qse_size_t)-1)
{ {
dir->errnum = QSE_DIR_ENOMEM; dir->errnum = QSE_DIR_ENOMEM;
return QSE_NULL; return QSE_NULL;
} }
qse_mbstowcs (mbs, &ml, QSE_WCS_PTR(wcs), &wl); qse_mbstowcs (mbs, &ml, QSE_WCS_PTR(wbuf), &wl);
return QSE_WCS_PTR(wcs); return QSE_WCS_PTR(wbuf);
} }
static qse_wchar_t* wcs_to_wbuf (qse_dir_t* dir, const qse_wchar_t* wcs, qse_wcs_t* wbuf)
{
if (qse_wcs_cpy (&dir->wbuf, wcs) == (qse_size_t)-1)
{
dir->errnum = QSE_DIR_ENOMEM;
return QSE_NULL;
}
return QSE_WCS_PTR(wbuf);
}
static qse_mchar_t* mbs_to_mbuf (qse_dir_t* dir, const qse_mchar_t* mbs, qse_mbs_t* mbuf)
{
if (qse_mbs_cpy (&dir->mbuf, mbs) == (qse_size_t)-1)
{
dir->errnum = QSE_DIR_ENOMEM;
return QSE_NULL;
}
return QSE_MBS_PTR(mbuf);
}
#if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
static qse_mchar_t* make_mbsdos_path (qse_dir_t* dir, const qse_mchar_t* mpath) static qse_mchar_t* make_mbsdos_path (qse_dir_t* dir, const qse_mchar_t* mpath)
{ {
if (mpath[0] == QSE_MT('\0')) if (mpath[0] == QSE_MT('\0'))
@ -304,6 +341,35 @@ static qse_mchar_t* make_mbsdos_path (qse_dir_t* dir, const qse_mchar_t* mpath)
return QSE_MBS_PTR(&dir->mbuf); return QSE_MBS_PTR(&dir->mbuf);
} }
static qse_wchar_t* make_wcsdos_path (qse_dir_t* dir, const qse_wchar_t* wpath)
{
if (wpath[0] == QSE_WT('\0'))
{
if (qse_wcs_cpy (&dir->wbuf, QSE_WT("*.*")) == (qse_size_t)-1)
{
dir->errnum = QSE_DIR_ENOMEM;
return QSE_NULL;
}
}
else
{
qse_size_t len;
if ((len = qse_wcs_cpy (&dir->wbuf, wpath)) == (qse_size_t)-1 ||
(!QSE_ISPATHWCSEP(wpath[len - 1]) &&
!qse_iswcsdrivecurpath(wpath) &&
qse_wcs_ccat (&dir->wbuf, QSE_WT('\\')) == (qse_size_t)-1) ||
qse_wcs_cat (&dir->wbuf, QSE_WT("*.*")) == (qse_size_t)-1)
{
dir->errnum = QSE_DIR_ENOMEM;
return QSE_NULL;
}
}
return QSE_WCS_PTR(&dir->wbuf);
}
#endif
/*
static qse_char_t* make_dos_path (qse_dir_t* dir, const qse_char_t* path) static qse_char_t* make_dos_path (qse_dir_t* dir, const qse_char_t* path)
{ {
if (path[0] == QSE_T('\0')) if (path[0] == QSE_T('\0'))
@ -357,31 +423,44 @@ static qse_mchar_t* mkdospath (qse_dir_t* dir, const qse_char_t* path)
#endif #endif
} }
*/
static int reset_to_path (qse_dir_t* dir, const qse_char_t* path) static int reset_to_path (qse_dir_t* dir, const qse_char_t* path)
{ {
#if defined(_WIN32) #if defined(_WIN32)
/* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */
qse_char_t* tptr; const qse_char_t* tptr;
dir->status &= ~STATUS_DONE; dir->status &= ~STATUS_DONE;
dir->status &= ~STATUS_DONE_ERR; dir->status &= ~STATUS_DONE_ERR;
#if defined(QSE_CHAR_IS_MCHAR)
tptr = make_dos_path (dir, path);
#else
if (dir->flags & QSE_DIR_MBSPATH) if (dir->flags & QSE_DIR_MBSPATH)
{ {
qse_mchar_t* mptr = make_mbsdos_path (dir, (const qse_mchar_t*) path); qse_mchar_t* mptr;
mptr = make_mbsdos_path (dir, (const qse_mchar_t*)path);
if (mptr == QSE_NULL) return -1; if (mptr == QSE_NULL) return -1;
tptr = mbs_to_wbuf (dir, mptr, &dir->tbuf);
#if defined(QSE_CHAR_IS_MCHAR)
tptr = mptr;
#else
tptr = mbs_to_wbuf (dir, mptr, &dir->wbuf);
#endif
} }
else else
{ {
tptr = make_dos_path (dir, path); qse_wchar_t* wptr;
if (tptr == QSE_NULL) return -1; QSE_ASSERT (dir->flags & QSE_DIR_WCSPATH);
}
wptr = make_wcsdos_path (dir, (const qse_wchar_t*)path);
if (wptr == QSE_NULL) return -1;
#if defined(QSE_CHAR_IS_MCHAR)
tptr = wcs_to_mbuf (dir, wptr, &dir->mbuf);
#else
tptr = wptr;
#endif #endif
}
if (tptr == QSE_NULL) return -1; if (tptr == QSE_NULL) return -1;
dir->h = FindFirstFile (tptr, &dir->wfd); dir->h = FindFirstFile (tptr, &dir->wfd);
@ -398,25 +477,24 @@ static int reset_to_path (qse_dir_t* dir, const qse_char_t* path)
/* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */
APIRET rc; APIRET rc;
qse_mchar_t* mptr; const qse_mchar_t* mptr;
dir->h = HDIR_CREATE; dir->h = HDIR_CREATE;
dir->count = 1; dir->count = 1;
#if defined(QSE_CHAR_IS_MCHAR)
mptr = make_dos_path (dir, path);
#else
if (dir->flags & QSE_DIR_MBSPATH) if (dir->flags & QSE_DIR_MBSPATH)
{ {
mptr = make_mbsdos_path (dir, (const qse_mchar_t*)path); mptr = make_mbsdos_path (dir, (const qse_mchar_t*)path);
} }
else else
{ {
qse_char_t* tptr = make_dos_path (dir, path); qse_wchar_t* wptr;
if (tptr == QSE_NULL) return -1; QSE_ASSERT (dir->flags & QSE_DIR_WCSPATH);
mptr = wcs_to_mbuf (dir, tptr, &dir->mbuf);
wptr = make_wcsdos_path (dir, (const qse_wchar_t*)path);
if (wptr == QSE_NULL) return -1;
mptr = wcs_to_mbuf (dir, wptr, &dir->mbuf);
} }
#endif
if (mptr == QSE_NULL) return -1; if (mptr == QSE_NULL) return -1;
rc = DosFindFirst ( rc = DosFindFirst (
@ -447,25 +525,25 @@ static int reset_to_path (qse_dir_t* dir, const qse_char_t* path)
/* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */
unsigned int rc; unsigned int rc;
qse_mchar_t* mptr; const qse_mchar_t* mptr;
dir->status &= ~STATUS_DONE; dir->status &= ~STATUS_DONE;
dir->status &= ~STATUS_DONE_ERR; dir->status &= ~STATUS_DONE_ERR;
#if defined(QSE_CHAR_IS_MCHAR)
mptr = make_dos_path (dir, path);
#else
if (dir->flags & QSE_DIR_MBSPATH) if (dir->flags & QSE_DIR_MBSPATH)
{ {
mptr = make_mbsdos_path (dir, (const qse_mchar_t*)path); mptr = make_mbsdos_path (dir, (const qse_mchar_t*)path);
} }
else else
{ {
qse_char_t* tptr = make_dos_path (dir, path); qse_wchar_t* wptr;
if (tptr == QSE_NULL) return -1;
mptr = wcs_to_mbuf (dir, tptr, &dir->mbuf); QSE_ASSERT (dir->flags & QSE_DIR_WCSPTH);
wptr = make_wcsdos_path (dir, (const qse_wchar_t*)path);
if (wptr == QSE_NULL) return -1;
mptr = wcs_to_mbuf (dir, wptr, &dir->mbuf);
} }
#endif
if (mptr == QSE_NULL) return -1; if (mptr == QSE_NULL) return -1;
rc = _dos_findfirst (mptr, _A_NORMAL | _A_SUBDIR, &dir->f); rc = _dos_findfirst (mptr, _A_NORMAL | _A_SUBDIR, &dir->f);
@ -482,17 +560,21 @@ static int reset_to_path (qse_dir_t* dir, const qse_char_t* path)
#else #else
DIR* dp; DIR* dp;
#if defined(QSE_CHAR_IS_MCHAR)
dp = QSE_OPENDIR (path[0] == QSE_MT('\0')? QSE_T("."): path);
#else
if (dir->flags & QSE_DIR_MBSPATH) if (dir->flags & QSE_DIR_MBSPATH)
{ {
const qse_mchar_t* mpath = (const qse_mchar_t*)path; const qse_mchar_t* mpath;
mpath = (const qse_mchar_t*)path;
dp = QSE_OPENDIR (mpath == QSE_MT('\0')? QSE_MT("."): mpath); dp = QSE_OPENDIR (mpath == QSE_MT('\0')? QSE_MT("."): mpath);
} }
else else
{ {
if (path[0] == QSE_T('\0'))
const qse_wchar_t* wpath;
QSE_ASSERT (dir->flags & QSE_DIR_WCSPATH);
wpath = (const qse_wchar_t*)path;
if (wpath[0] == QSE_WT('\0'))
{ {
dp = QSE_OPENDIR (QSE_MT(".")); dp = QSE_OPENDIR (QSE_MT("."));
} }
@ -500,13 +582,13 @@ static int reset_to_path (qse_dir_t* dir, const qse_char_t* path)
{ {
qse_mchar_t* mptr; qse_mchar_t* mptr;
mptr = wcs_to_mbuf (dir, path, &dir->mbuf); mptr = wcs_to_mbuf (dir, wpath, &dir->mbuf);
if (mptr == QSE_NULL) return -1; if (mptr == QSE_NULL) return -1;
dp = QSE_OPENDIR (mptr); dp = QSE_OPENDIR (mptr);
} }
} }
#endif
if (dp == QSE_NULL) if (dp == QSE_NULL)
{ {
dir->errnum = syserr_to_errnum (errno); dir->errnum = syserr_to_errnum (errno);
@ -540,7 +622,7 @@ int qse_dir_reset (qse_dir_t* dir, const qse_char_t* path)
return 0; return 0;
} }
static int read_dir_to_tbuf (qse_dir_t* dir, void** name) static int read_dir_to_buf (qse_dir_t* dir, void** name)
{ {
#if defined(_WIN32) #if defined(_WIN32)
@ -571,29 +653,25 @@ static int read_dir_to_tbuf (qse_dir_t* dir, void** name)
} }
} }
#if defined(QSE_CHAR_IS_MCHAR)
if (qse_str_cpy (&dir->tbuf, dir->wfd.cFileName) == (qse_size_t)-1)
{
dir->errnum = QSE_DIR_ENOMEM;
return -1;
}
*name = QSE_STR_PTR(&dir->tbuf);
#else
if (dir->flags & QSE_DIR_MBSPATH) if (dir->flags & QSE_DIR_MBSPATH)
{ {
#if defined(QSE_CHAR_IS_MCHAR)
if (mbs_to_mbuf (dir, dir->wfd.cFileName, &dir->mbuf) == QSE_NULL) return -1;
#else
if (wcs_to_mbuf (dir, dir->wfd.cFileName, &dir->mbuf) == QSE_NULL) return -1; if (wcs_to_mbuf (dir, dir->wfd.cFileName, &dir->mbuf) == QSE_NULL) return -1;
*name = QSE_STR_PTR(&dir->mbuf); #endif
*name = QSE_MBS_PTR(&dir->mbuf);
} }
else else
{ {
if (qse_str_cpy (&dir->tbuf, dir->wfd.cFileName) == (qse_size_t)-1) QSE_ASSERT (dir->flags & QSE_DIR_WCSPATH);
{ #if defined(QSE_CHAR_IS_MCHAR)
dir->errnum = QSE_DIR_ENOMEM; if (mbs_to_wbuf (dir, dir->wfd.cFileName, &dir->wbuf) == QSE_NULL) return -1;
return -1; #else
} if (wcs_to_wbuf (dir, dir->wfd.cFileName, &dir->wbuf) == QSE_NULL) return -1;
*name = QSE_STR_PTR(&dir->tbuf);
}
#endif #endif
*name = QSE_WCS_PTR(&dir->wbuf);
}
if (FindNextFile (dir->h, &dir->wfd) == FALSE) if (FindNextFile (dir->h, &dir->wfd) == FALSE)
{ {
@ -636,29 +714,18 @@ static int read_dir_to_tbuf (qse_dir_t* dir, void** name)
} }
} }
#if defined(QSE_CHAR_IS_MCHAR)
if (qse_str_cpy (&dir->tbuf, dir->ffb.achName) == (qse_size_t)-1)
{
dir->errnum = QSE_DIR_ENOMEM;
return -1;
}
*name = QSE_STR_PTR(&dir->tbuf);
#else
if (dir->flags & QSE_DIR_MBSPATH) if (dir->flags & QSE_DIR_MBSPATH)
{ {
if (qse_mbs_cpy (&dir->mbuf, dir->ffb.achName) == (qse_size_t)-1) if (mbs_to_mbuf (dir, dir->ffb.achName, &dir->mbuf) == QSE_NULL) return -1;
{
dir->errnum = QSE_DIR_ENOMEM;
return -1;
}
*name = QSE_MBS_PTR(&dir->mbuf); *name = QSE_MBS_PTR(&dir->mbuf);
} }
else else
{ {
if (mbs_to_wbuf (dir, dir->ffb.achName, &dir->tbuf) == QSE_NULL) return -1; QSE_ASSERT (dir->flags & QSE_DIR_WCSPATH);
*name = QSE_STR_PTR(&dir->tbuf); if (mbs_to_wbuf (dir, dir->ffb.achName, &dir->wbuf) == QSE_NULL) return -1;
*name = QSE_WCS_PTR(&dir->wbuf);
} }
#endif
rc = DosFindNext (dir->h, &dir->ffb, QSE_SIZEOF(dir->ffb), &dir->count); rc = DosFindNext (dir->h, &dir->ffb, QSE_SIZEOF(dir->ffb), &dir->count);
if (rc == ERROR_NO_MORE_FILES) dir->count = 0; if (rc == ERROR_NO_MORE_FILES) dir->count = 0;
@ -700,29 +767,18 @@ static int read_dir_to_tbuf (qse_dir_t* dir, void** name)
} }
} }
#if defined(QSE_CHAR_IS_MCHAR)
if (qse_str_cpy (&dir->tbuf, dir->f.name) == (qse_size_t)-1)
{
dir->errnum = QSE_DIR_ENOMEM;
return -1;
}
*name = QSE_STR_PTR(&dir->tbuf);
#else
if (dir->flags & QSE_DIR_MBSPATH) if (dir->flags & QSE_DIR_MBSPATH)
{ {
if (qse_mbs_cpy (&dir->mbuf, dir->f.name) == (qse_size_t)-1) if (mbs_to_mbuf (dir, dir->f.name, &dir->mbuf) == QSE_NULL) return -1;
{
dir->errnum = QSE_DIR_ENOMEM;
return -1;
}
*name = QSE_MBS_PTR(&dir->mbuf); *name = QSE_MBS_PTR(&dir->mbuf);
} }
else else
{ {
if (mbs_to_wbuf (dir, dir->f.name, &dir->tbuf) == QSE_NULL) return -1; QSE_ASSERT (dir->flags & QSE_DIR_WCSPATH);
*name = QSE_STR_PTR(&dir->tbuf);
if (mbs_to_wbuf (dir, dir->f.name, &dir->wbuf) == QSE_NULL) return -1;
*name = QSE_WCS_PTR(&dir->wbuf);
} }
#endif
if (_dos_findnext (&dir->f) != 0) if (_dos_findnext (&dir->f) != 0)
{ {
@ -760,30 +816,18 @@ read:
IS_PREVDIR_M(de->d_name)) goto read; IS_PREVDIR_M(de->d_name)) goto read;
} }
#if defined(QSE_CHAR_IS_MCHAR)
if (qse_str_cpy (&dir->tbuf, de->d_name) == (qse_size_t)-1)
{
dir->errnum = QSE_DIR_ENOMEM;
return -1;
}
*name = QSE_STR_PTR(&dir->tbuf);
#else
if (dir->flags & QSE_DIR_MBSPATH) if (dir->flags & QSE_DIR_MBSPATH)
{ {
if (qse_mbs_cpy (&dir->mbuf, de->d_name) == (qse_size_t)-1) if (mbs_to_mbuf (dir, de->d_name, &dir->mbuf) == QSE_NULL) return -1;
{
dir->errnum = QSE_DIR_ENOMEM;
return -1;
}
*name = QSE_MBS_PTR(&dir->mbuf); *name = QSE_MBS_PTR(&dir->mbuf);
} }
else else
{ {
if (mbs_to_wbuf (dir, de->d_name, &dir->tbuf) == QSE_NULL) return -1; QSE_ASSERT (dir->flags & QSE_DIR_WCSPATH);
*name = QSE_STR_PTR(&dir->tbuf);
if (mbs_to_wbuf (dir, de->d_name, &dir->wbuf) == QSE_NULL) return -1;
*name = QSE_WCS_PTR(&dir->wbuf);
} }
#endif
return 1; return 1;
/* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */
@ -798,19 +842,15 @@ static int read_ahead_and_sort (qse_dir_t* dir, const qse_char_t* path)
while (1) while (1)
{ {
x = read_dir_to_tbuf (dir, &name); x = read_dir_to_buf (dir, &name);
if (x >= 1) if (x >= 1)
{ {
qse_size_t size; qse_size_t size;
#if defined(QSE_CHAR_IS_MCHAR)
size = (qse_mbslen(name) + 1) * QSE_SIZEOF(qse_mchar_t);
#else
if (dir->flags & QSE_DIR_MBSPATH) if (dir->flags & QSE_DIR_MBSPATH)
size = (qse_mbslen(name) + 1) * QSE_SIZEOF(qse_mchar_t); size = (qse_mbslen(name) + 1) * QSE_SIZEOF(qse_mchar_t);
else else
size = (qse_wcslen(name) + 1) * QSE_SIZEOF(qse_wchar_t); size = (qse_wcslen(name) + 1) * QSE_SIZEOF(qse_wchar_t);
#endif
if (qse_lda_pushheap (dir->stab, name, size) == (qse_size_t)-1) if (qse_lda_pushheap (dir->stab, name, size) == (qse_size_t)-1)
{ {
@ -846,7 +886,7 @@ int qse_dir_read (qse_dir_t* dir, qse_dir_ent_t* ent)
int x; int x;
void* name; void* name;
x = read_dir_to_tbuf (dir, &name); x = read_dir_to_buf (dir, &name);
if (x >= 1) if (x >= 1)
{ {
QSE_MEMSET (ent, 0, QSE_SIZEOF(ent)); QSE_MEMSET (ent, 0, QSE_SIZEOF(ent));

View File

@ -31,7 +31,7 @@
* qse_dir_xxx() and qse_glob() don't support mbs and wcs separately. * qse_dir_xxx() and qse_glob() don't support mbs and wcs separately.
* while the functions here support them. */ * while the functions here support them. */
static int delete_file_from_fs (qse_fs_t* fs, const qse_fs_char_t* fspath) int qse_fs_sysrmfile (qse_fs_t* fs, const qse_fs_char_t* fspath)
{ {
#if defined(_WIN32) #if defined(_WIN32)
@ -74,7 +74,7 @@ static int delete_file_from_fs (qse_fs_t* fs, const qse_fs_char_t* fspath)
return 0; return 0;
} }
static int delete_directory_from_fs (qse_fs_t* fs, const qse_fs_char_t* fspath) int qse_fs_sysrmdir (qse_fs_t* fs, const qse_fs_char_t* fspath)
{ {
#if defined(_WIN32) #if defined(_WIN32)
@ -138,7 +138,7 @@ static int delete_file (qse_fs_t* fs, const qse_char_t* path, int purge)
fspath = qse_fs_makefspath(fs, path); fspath = qse_fs_makefspath(fs, path);
if (!fspath) return -1; if (!fspath) return -1;
ret = delete_file_from_fs (fs, fspath); ret = qse_fs_sysrmfile (fs, fspath);
qse_fs_freefspath (fs, path, fspath); qse_fs_freefspath (fs, path, fspath);
if (ret <= -1 && purge) if (ret <= -1 && purge)
@ -176,7 +176,7 @@ static int delete_directory_nocbs (qse_fs_t* fs, const qse_char_t* path)
fspath = qse_fs_makefspath(fs, path); fspath = qse_fs_makefspath(fs, path);
if (!fspath) return -1; if (!fspath) return -1;
ret = delete_directory_from_fs (fs, fspath); ret = qse_fs_sysrmdir (fs, fspath);
qse_fs_freefspath (fs, path, fspath); qse_fs_freefspath (fs, path, fspath);
return ret; return ret;
@ -301,8 +301,8 @@ static int delete_from_fs_with_mbs (qse_fs_t* fs, const qse_mchar_t* path, int d
if (x == 0) return 0; /* skipped */ if (x == 0) return 0; /* skipped */
} }
ret = dir? delete_directory_from_fs (fs, fspath): ret = dir? qse_fs_sysrmdir (fs, fspath):
delete_file_from_fs (fs, fspath); qse_fs_sysrmfile (fs, fspath);
qse_fs_freefspathformbs (fs, path, fspath); qse_fs_freefspathformbs (fs, path, fspath);
@ -337,8 +337,8 @@ static int delete_from_fs_with_wcs (qse_fs_t* fs, const qse_wchar_t* path, int d
fspath = qse_fs_makefspathforwcs (fs, path); fspath = qse_fs_makefspathforwcs (fs, path);
if (!fspath) return -1; if (!fspath) return -1;
ret = dir? delete_directory_from_fs (fs, fspath): ret = dir? qse_fs_sysrmdir (fs, fspath):
delete_file_from_fs (fs, fspath); qse_fs_sysrmfile (fs, fspath);
qse_fs_freefspathforwcs (fs, path, fspath); qse_fs_freefspathforwcs (fs, path, fspath);

View File

@ -26,7 +26,7 @@
#include "fs.h" #include "fs.h"
static int make_directory_in_fs (qse_fs_t* fs, const qse_fs_char_t* fspath) int qse_fs_sysmkdir (qse_fs_t* fs, const qse_fs_char_t* fspath)
{ {
#if defined(_WIN32) #if defined(_WIN32)
@ -78,7 +78,7 @@ static int make_directory_in_fs (qse_fs_t* fs, const qse_fs_char_t* fspath)
#else #else
if (QSE_MKDIR (fspath, 0755) <= -1) if (QSE_MKDIR (fspath, 0777) <= -1) /* TODO: proper mode?? */
{ {
fs->errnum = qse_fs_syserrtoerrnum (fs, errno); fs->errnum = qse_fs_syserrtoerrnum (fs, errno);
return -1; return -1;
@ -120,7 +120,7 @@ static int make_directory_chain (qse_fs_t* fs, qse_fs_char_t* fspath)
c = *(p + 1); c = *(p + 1);
*(p + 1) = QSE_FS_T('\0'); *(p + 1) = QSE_FS_T('\0');
#endif #endif
ret = make_directory_in_fs (fs, fspath); ret = qse_fs_sysmkdir (fs, fspath);
if (ret <= -1 && fs->errnum != QSE_FS_EEXIST) if (ret <= -1 && fs->errnum != QSE_FS_EEXIST)
{ {
return -1; return -1;
@ -134,7 +134,7 @@ static int make_directory_chain (qse_fs_t* fs, qse_fs_char_t* fspath)
} }
} }
if (!IS_FSPATHSEP(*(p - 1))) ret = make_directory_in_fs (fs, fspath); if (!IS_FSPATHSEP(*(p - 1))) ret = qse_fs_sysmkdir (fs, fspath);
done: done:
return ret; return ret;
@ -165,7 +165,7 @@ int qse_fs_mkdirmbs (qse_fs_t* fs, const qse_mchar_t* path, int flags)
fspath = (qse_fs_char_t*)qse_fs_makefspathformbs (fs, path); fspath = (qse_fs_char_t*)qse_fs_makefspathformbs (fs, path);
if (!fspath) return -1; if (!fspath) return -1;
ret = make_directory_in_fs (fs, fspath); ret = qse_fs_sysmkdir (fs, fspath);
} }
qse_fs_freefspathformbs (fs, path, fspath); qse_fs_freefspathformbs (fs, path, fspath);
@ -198,7 +198,7 @@ int qse_fs_mkdirwcs (qse_fs_t* fs, const qse_wchar_t* path, int flags)
fspath = (qse_fs_char_t*)qse_fs_makefspathforwcs (fs, path); fspath = (qse_fs_char_t*)qse_fs_makefspathforwcs (fs, path);
if (!fspath) return -1; if (!fspath) return -1;
ret = make_directory_in_fs (fs, fspath); ret = qse_fs_sysmkdir (fs, fspath);
} }
qse_fs_freefspathforwcs (fs, path, fspath); qse_fs_freefspathforwcs (fs, path, fspath);

View File

@ -367,6 +367,7 @@ oops:
#endif #endif
} }
#if 0
static int move_file_in_fs (qse_fs_t* fs, const qse_fs_char_t* oldpath, const qse_fs_char_t* newpath, int flags) static int move_file_in_fs (qse_fs_t* fs, const qse_fs_char_t* oldpath, const qse_fs_char_t* newpath, int flags)
{ {
#if defined(_WIN32) #if defined(_WIN32)
@ -458,3 +459,5 @@ static int move_file_in_fs (qse_fs_t* fs, const qse_fs_char_t* oldpath, const qs
#endif #endif
} }
#endif

View File

@ -818,11 +818,21 @@ void qse_fs_freefspathforwcs (qse_fs_t* fs, const qse_wchar_t* path, qse_fs_char
int qse_fs_getattr (qse_fs_t* fs, const qse_fs_char_t* fspath, qse_fs_attr_t* attr) int qse_fs_getattr (qse_fs_t* fs, const qse_fs_char_t* fspath, qse_fs_attr_t* attr)
{ {
#if defined(_WIN32) #if defined(_WIN32)
#error TODO
fs->errnum = QSE_FS_ENOIMPL;
return -1;
#elif defined(__OS2__) #elif defined(__OS2__)
#error TODO
/* TODO */
fs->errnum = QSE_FS_ENOIMPL;
return -1;
#elif defined(__DOS__) #elif defined(__DOS__)
#error TODO
fs->errnum = QSE_FS_ENOIMPL;
return -1;
#else #else
#if defined(HAVE_LSTAT) #if defined(HAVE_LSTAT)
qse_lstat_t st; qse_lstat_t st;

View File

@ -150,6 +150,21 @@ int qse_fs_getattr (
qse_fs_attr_t* attr qse_fs_attr_t* attr
); );
int qse_fs_sysmkdir (
qse_fs_t* fs,
const qse_fs_char_t* fspath
);
int qse_fs_sysrmfile (
qse_fs_t* fs,
const qse_fs_char_t* fspath
);
int qse_fs_sysrmdir (
qse_fs_t* fs,
const qse_fs_char_t* fspath
);
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif

View File

@ -1,39 +1,104 @@
#include <qse/cmn/dir.h> #include <qse/cmn/dir.h>
#include <qse/cmn/main.h> #include <qse/cmn/main.h>
#include <qse/cmn/mbwc.h> #include <qse/cmn/mbwc.h>
#include <qse/cmn/stdio.h> #include <qse/cmn/sio.h>
#include <qse/cmn/mem.h> #include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/fmt.h>
#include <locale.h> #include <locale.h>
#if defined(_WIN32) #if defined(_WIN32)
# include <windows.h> # include <windows.h>
#endif #endif
static int test_main (int argc, qse_char_t* argv[]) static int list_dir (int argc, qse_char_t* argv[], int flags)
{ {
qse_dir_t* dir; qse_dir_t* dir = QSE_NULL;
qse_dir_ent_t dirent; qse_dir_ent_t dirent;
void* xpath = QSE_NULL;
void* xdirpath;
if (argc < 2)
{
if (flags & QSE_DIR_MBSPATH)
xdirpath = QSE_MT(".");
else
xdirpath = QSE_WT(".");
dir = qse_dir_open (QSE_MMGR_GETDFL(), 0, xdirpath, flags, QSE_NULL);
if (!dir)
{
qse_printf (QSE_T("Cannot open .\n"));
goto oops;
}
}
else
{
if (flags & QSE_DIR_MBSPATH)
xpath = qse_strtombsdup (argv[1], QSE_MMGR_GETDFL());
else
xpath = qse_strtowcsdup (argv[1], QSE_MMGR_GETDFL());
xdirpath = xpath;
dir = qse_dir_open (QSE_MMGR_GETDFL(), 0, xdirpath, flags, QSE_NULL);
if (!dir)
{
qse_printf (QSE_T("Cannot open %s\n"), argv[1]);
goto oops;
}
}
dir = qse_dir_open (QSE_MMGR_GETDFL(), 0, (argc < 2? QSE_T("."): argv[1]), QSE_DIR_SORT, QSE_NULL);
while (qse_dir_read (dir, &dirent) > 0) while (qse_dir_read (dir, &dirent) > 0)
{ {
qse_printf (QSE_T("%s\n"), dirent.name); if (flags & QSE_DIR_MBSPATH)
qse_printf (QSE_T("%hs\n"), dirent.name);
else
qse_printf (QSE_T("%ls\n"), dirent.name);
} }
qse_printf (QSE_T("----------------------------------------\n")); qse_printf (QSE_T("----------------------------------------\n"));
qse_dir_reset (dir, (argc < 2? QSE_T("."): argv[1])); if (qse_dir_reset (dir, xdirpath) <= -1)
{
qse_printf (QSE_T("Cannot reset\n"));
goto oops;
}
while (qse_dir_read (dir, &dirent) > 0) while (qse_dir_read (dir, &dirent) > 0)
{ {
qse_printf (QSE_T("%s\n"), dirent.name); if (flags & QSE_DIR_MBSPATH)
qse_printf (QSE_T("%hs\n"), dirent.name);
else
qse_printf (QSE_T("%ls\n"), dirent.name);
} }
QSE_MMGR_FREE(QSE_MMGR_GETDFL(), xpath);
qse_dir_close (dir); qse_dir_close (dir);
return 0; return 0;
oops:
if (xpath) QSE_MMGR_FREE(QSE_MMGR_GETDFL(), xpath);
if (dir) qse_dir_close(dir);
return -1;
}
static int dir_main (int argc, qse_char_t* argv[])
{
if (list_dir (argc, argv, QSE_DIR_MBSPATH) <= -1) return -1;
qse_printf (QSE_T("===============================\n"));
if (list_dir (argc, argv, QSE_DIR_MBSPATH | QSE_DIR_SORT) <= -1) return -1;
qse_printf (QSE_T("===============================\n"));
if (list_dir (argc, argv, QSE_DIR_WCSPATH) <= -1) return -1;
qse_printf (QSE_T("===============================\n"));
if (list_dir (argc, argv, QSE_DIR_WCSPATH | QSE_DIR_SORT) <= -1) return -1;
return 0;
} }
int qse_main (int argc, qse_achar_t* argv[]) int qse_main (int argc, qse_achar_t* argv[])
{ {
int x;
#if defined(_WIN32) #if defined(_WIN32)
char locale[100]; char locale[100];
UINT codepage = GetConsoleOutputCP(); UINT codepage = GetConsoleOutputCP();
@ -44,7 +109,9 @@ int qse_main (int argc, qse_achar_t* argv[])
} }
else else
{ {
sprintf (locale, ".%u", (unsigned int)codepage); /* .codepage */
qse_fmtuintmaxtombs (locale, QSE_COUNTOF(locale),
codepage, 10, -1, QSE_MT('\0'), QSE_MT("."));
setlocale (LC_ALL, locale); setlocale (LC_ALL, locale);
qse_setdflcmgrbyid (QSE_CMGR_SLMB); qse_setdflcmgrbyid (QSE_CMGR_SLMB);
} }
@ -52,6 +119,13 @@ int qse_main (int argc, qse_achar_t* argv[])
setlocale (LC_ALL, ""); setlocale (LC_ALL, "");
qse_setdflcmgrbyid (QSE_CMGR_SLMB); qse_setdflcmgrbyid (QSE_CMGR_SLMB);
#endif #endif
return qse_runmain (argc, argv, test_main);
qse_openstdsios ();
x = qse_runmain (argc, argv, dir_main);
qse_closestdsios ();
return x;
} }

View File

@ -5,8 +5,12 @@
#include <qse/cmn/path.h> #include <qse/cmn/path.h>
#include <qse/cmn/main.h> #include <qse/cmn/main.h>
#include <qse/cmn/mbwc.h> #include <qse/cmn/mbwc.h>
#include <qse/cmn/fmt.h>
#include <locale.h> #include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
static int fs_del (qse_fs_t* fs, const qse_char_t* path) static int fs_del (qse_fs_t* fs, const qse_char_t* path)
{ {
@ -110,7 +114,9 @@ int main (int argc, qse_achar_t* argv[])
} }
else else
{ {
sprintf (locale, ".%u", (unsigned int)codepage); /* .codepage */
qse_fmtuintmaxtombs (locale, QSE_COUNTOF(locale),
codepage, 10, -1, QSE_MT('\0'), QSE_MT("."));
setlocale (LC_ALL, locale); setlocale (LC_ALL, locale);
qse_setdflcmgrbyid (QSE_CMGR_SLMB); qse_setdflcmgrbyid (QSE_CMGR_SLMB);
} }

View File

@ -6,8 +6,12 @@
#include <qse/cmn/main.h> #include <qse/cmn/main.h>
#include <qse/cmn/mbwc.h> #include <qse/cmn/mbwc.h>
#include <qse/cmn/opt.h> #include <qse/cmn/opt.h>
#include <qse/cmn/fmt.h>
#include <locale.h> #include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
static void print_usage (const qse_char_t* argv0) static void print_usage (const qse_char_t* argv0)
{ {
@ -109,7 +113,9 @@ int main (int argc, qse_achar_t* argv[])
} }
else else
{ {
sprintf (locale, ".%u", (unsigned int)codepage); /* .codepage */
qse_fmtuintmaxtombs (locale, QSE_COUNTOF(locale),
codepage, 10, -1, QSE_MT('\0'), QSE_MT("."));
setlocale (LC_ALL, locale); setlocale (LC_ALL, locale);
qse_setdflcmgrbyid (QSE_CMGR_SLMB); qse_setdflcmgrbyid (QSE_CMGR_SLMB);
} }