2012-11-09 17:31:33 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2019-06-06 05:28:23 +00:00
|
|
|
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
|
2012-11-09 17:31:33 +00:00
|
|
|
|
2014-11-19 14:42:24 +00:00
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
2012-11-09 17:31:33 +00:00
|
|
|
|
2014-11-19 14:42:24 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2012-11-09 17:31:33 +00:00
|
|
|
*/
|
|
|
|
|
2016-04-29 03:55:42 +00:00
|
|
|
#include <qse/si/dir.h>
|
2012-11-09 17:31:33 +00:00
|
|
|
#include <qse/cmn/str.h>
|
|
|
|
#include <qse/cmn/mbwc.h>
|
|
|
|
#include <qse/cmn/path.h>
|
2016-09-23 14:53:51 +00:00
|
|
|
#include <qse/cmn/arr.h>
|
2016-04-29 03:55:42 +00:00
|
|
|
#include "../cmn/mem-prv.h"
|
2012-11-09 17:31:33 +00:00
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
# include <windows.h>
|
|
|
|
#elif defined(__OS2__)
|
|
|
|
# define INCL_DOSFILEMGR
|
|
|
|
# define INCL_ERRORS
|
|
|
|
# include <os2.h>
|
|
|
|
#elif defined(__DOS__)
|
|
|
|
# include <dos.h>
|
|
|
|
# include <errno.h>
|
|
|
|
#else
|
2016-04-29 03:55:42 +00:00
|
|
|
# include "../cmn/syscall.h"
|
2012-11-09 17:31:33 +00:00
|
|
|
#endif
|
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
|
|
|
|
#define STATUS_OPENED (1 << 0)
|
|
|
|
#define STATUS_DONE (1 << 1)
|
|
|
|
#define STATUS_DONE_ERR (1 << 2)
|
|
|
|
#define STATUS_POPHEAP (1 << 3)
|
|
|
|
#define STATUS_SORT_ERR (1 << 4)
|
|
|
|
|
2014-11-25 03:25:46 +00:00
|
|
|
#define IS_CURDIR_M(x) ((x)[0] == QSE_MT('.') && (x)[1] == QSE_MT('\0'))
|
|
|
|
#define IS_PREVDIR_M(x) ((x)[0] == QSE_MT('.') && (x)[1] == QSE_MT('.') && (x)[2] == QSE_MT('\0'))
|
|
|
|
|
|
|
|
#define IS_CURDIR_W(x) ((x)[0] == QSE_WT('.') && (x)[1] == QSE_WT('\0'))
|
|
|
|
#define IS_PREVDIR_W(x) ((x)[0] == QSE_WT('.') && (x)[1] == QSE_WT('.') && (x)[2] == QSE_WT('\0'))
|
|
|
|
|
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
|
|
|
# define IS_CURDIR(x) IS_CURDIR_M(x)
|
|
|
|
# define IS_PREVDIR(x) IS_PREVDIR_M(x)
|
|
|
|
#else
|
|
|
|
# define IS_CURDIR(x) IS_CURDIR_W(x)
|
|
|
|
# define IS_PREVDIR(x) IS_PREVDIR_W(x)
|
|
|
|
#endif
|
|
|
|
|
2012-11-09 17:31:33 +00:00
|
|
|
struct qse_dir_t
|
|
|
|
{
|
|
|
|
qse_mmgr_t* mmgr;
|
2012-12-25 14:10:02 +00:00
|
|
|
qse_dir_errnum_t errnum;
|
2012-11-09 17:31:33 +00:00
|
|
|
int flags;
|
|
|
|
|
2014-12-11 16:03:58 +00:00
|
|
|
qse_wcs_t wbuf;
|
2012-11-09 17:31:33 +00:00
|
|
|
qse_mbs_t mbuf;
|
|
|
|
|
2016-09-23 14:53:51 +00:00
|
|
|
qse_arr_t* stab;
|
2013-04-04 15:04:45 +00:00
|
|
|
int status;
|
|
|
|
|
2012-11-09 17:31:33 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
HANDLE h;
|
2014-11-24 17:01:04 +00:00
|
|
|
WIN32_FIND_DATA wfd;
|
2012-11-09 17:31:33 +00:00
|
|
|
#elif defined(__OS2__)
|
|
|
|
HDIR h;
|
2012-12-06 13:02:46 +00:00
|
|
|
#if defined(FIL_STANDARDL)
|
2014-11-24 17:01:04 +00:00
|
|
|
FILEFINDBUF3L ffb;
|
2012-12-06 13:02:46 +00:00
|
|
|
#else
|
2014-11-24 17:01:04 +00:00
|
|
|
FILEFINDBUF3 ffb;
|
2012-12-06 13:02:46 +00:00
|
|
|
#endif
|
2012-11-09 17:31:33 +00:00
|
|
|
ULONG count;
|
|
|
|
#elif defined(__DOS__)
|
|
|
|
struct find_t f;
|
|
|
|
#else
|
2013-09-11 09:14:29 +00:00
|
|
|
QSE_DIR* dp;
|
2012-11-09 17:31:33 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
int qse_dir_init (qse_dir_t* dir, qse_mmgr_t* mmgr, const qse_char_t* path, int flags);
|
|
|
|
void qse_dir_fini (qse_dir_t* dir);
|
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
static void close_dir_safely (qse_dir_t* dir);
|
2012-11-09 17:31:33 +00:00
|
|
|
static int reset_to_path (qse_dir_t* dir, const qse_char_t* path);
|
2013-04-04 15:04:45 +00:00
|
|
|
static int read_ahead_and_sort (qse_dir_t* dir, const qse_char_t* path);
|
2012-11-09 17:31:33 +00:00
|
|
|
|
2016-04-29 03:55:42 +00:00
|
|
|
#include "../cmn/syserr.h"
|
2012-12-25 14:10:02 +00:00
|
|
|
IMPLEMENT_SYSERR_TO_ERRNUM (dir, DIR)
|
|
|
|
|
|
|
|
qse_dir_t* qse_dir_open (
|
|
|
|
qse_mmgr_t* mmgr, qse_size_t xtnsize,
|
|
|
|
const qse_char_t* path, int flags, qse_dir_errnum_t* errnum)
|
2012-11-09 17:31:33 +00:00
|
|
|
{
|
|
|
|
qse_dir_t* dir;
|
|
|
|
|
|
|
|
dir = QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(*dir) + xtnsize);
|
|
|
|
if (dir)
|
|
|
|
{
|
|
|
|
if (qse_dir_init (dir, mmgr, path, flags) <= -1)
|
|
|
|
{
|
2012-12-25 14:10:02 +00:00
|
|
|
if (errnum) *errnum = qse_dir_geterrnum (dir);
|
2012-11-09 17:31:33 +00:00
|
|
|
QSE_MMGR_FREE (mmgr, dir);
|
|
|
|
dir = QSE_NULL;
|
|
|
|
}
|
|
|
|
else QSE_MEMSET (dir + 1, 0, xtnsize);
|
|
|
|
}
|
2014-07-11 14:17:00 +00:00
|
|
|
else if (errnum) *errnum = QSE_DIR_ENOMEM;
|
2012-11-09 17:31:33 +00:00
|
|
|
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qse_dir_close (qse_dir_t* dir)
|
|
|
|
{
|
|
|
|
qse_dir_fini (dir);
|
|
|
|
QSE_MMGR_FREE (dir->mmgr, dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
qse_mmgr_t* qse_dir_getmmgr (qse_dir_t* dir)
|
|
|
|
{
|
|
|
|
return dir->mmgr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* qse_dir_getxtn (qse_dir_t* dir)
|
|
|
|
{
|
|
|
|
return QSE_XTN (dir);
|
|
|
|
}
|
|
|
|
|
2012-12-25 14:10:02 +00:00
|
|
|
qse_dir_errnum_t qse_dir_geterrnum (qse_dir_t* dir)
|
|
|
|
{
|
|
|
|
return dir->errnum;
|
|
|
|
}
|
|
|
|
|
2016-09-23 14:53:51 +00:00
|
|
|
static int compare_dirent (qse_arr_t* arr, const void* dptr1, qse_size_t dlen1, const void* dptr2, qse_size_t dlen2)
|
2013-04-04 15:04:45 +00:00
|
|
|
{
|
|
|
|
int n = QSE_MEMCMP (dptr1, dptr2, ((dlen1 < dlen2)? dlen1: dlen2));
|
|
|
|
if (n == 0 && dlen1 != dlen2) n = (dlen1 > dlen2)? 1: -1;
|
|
|
|
return -n;
|
|
|
|
}
|
|
|
|
|
2012-11-09 17:31:33 +00:00
|
|
|
int qse_dir_init (qse_dir_t* dir, qse_mmgr_t* mmgr, const qse_char_t* path, int flags)
|
|
|
|
{
|
2012-11-16 17:11:18 +00:00
|
|
|
int n;
|
2014-12-11 16:03:58 +00:00
|
|
|
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
|
|
|
|
}
|
2012-11-16 17:11:18 +00:00
|
|
|
|
2012-11-09 17:31:33 +00:00
|
|
|
QSE_MEMSET (dir, 0, QSE_SIZEOF(*dir));
|
|
|
|
|
|
|
|
dir->mmgr = mmgr;
|
|
|
|
dir->flags = flags;
|
|
|
|
|
2014-12-11 16:03:58 +00:00
|
|
|
if (qse_wcs_init (&dir->wbuf, mmgr, 256) <= -1) goto oops_0;
|
2013-04-04 15:04:45 +00:00
|
|
|
if (qse_mbs_init (&dir->mbuf, mmgr, 256) <= -1) goto oops_1;
|
2012-11-09 17:31:33 +00:00
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
dir->h = INVALID_HANDLE_VALUE;
|
|
|
|
#endif
|
|
|
|
|
2012-11-16 17:11:18 +00:00
|
|
|
n = reset_to_path (dir, path);
|
2013-04-04 15:04:45 +00:00
|
|
|
if (n <= -1) goto oops_2;
|
|
|
|
|
|
|
|
if (dir->flags & QSE_DIR_SORT)
|
2012-11-16 17:11:18 +00:00
|
|
|
{
|
2016-09-23 14:53:51 +00:00
|
|
|
dir->stab = qse_arr_open (dir->mmgr, 0, 128);
|
2013-04-04 15:04:45 +00:00
|
|
|
if (dir->stab == QSE_NULL) goto oops_3;
|
|
|
|
|
2016-09-23 14:53:51 +00:00
|
|
|
/*qse_arr_setscale (dir->stab, 1);*/
|
|
|
|
qse_arr_setcopier (dir->stab, QSE_ARR_COPIER_INLINE);
|
|
|
|
qse_arr_setcomper (dir->stab, compare_dirent);
|
2013-04-04 15:04:45 +00:00
|
|
|
if (read_ahead_and_sort (dir, path) <= -1) goto oops_4;
|
2012-11-16 17:11:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
2013-04-04 15:04:45 +00:00
|
|
|
|
|
|
|
oops_4:
|
2016-09-23 14:53:51 +00:00
|
|
|
qse_arr_close (dir->stab);
|
2013-04-04 15:04:45 +00:00
|
|
|
oops_3:
|
|
|
|
close_dir_safely (dir);
|
|
|
|
oops_2:
|
|
|
|
qse_mbs_fini (&dir->mbuf);
|
|
|
|
oops_1:
|
2014-12-11 16:03:58 +00:00
|
|
|
qse_wcs_fini (&dir->wbuf);
|
2013-04-04 15:04:45 +00:00
|
|
|
oops_0:
|
|
|
|
return -1;
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void close_dir_safely (qse_dir_t* dir)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32)
|
|
|
|
if (dir->h != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
FindClose (dir->h);
|
|
|
|
dir->h = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
#elif defined(__OS2__)
|
2013-04-04 15:04:45 +00:00
|
|
|
if (dir->status & STATUS_OPENED)
|
2012-11-09 17:31:33 +00:00
|
|
|
{
|
|
|
|
DosFindClose (dir->h);
|
2013-04-04 15:04:45 +00:00
|
|
|
dir->status &= ~STATUS_OPENED;
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
#elif defined(__DOS__)
|
2013-04-04 15:04:45 +00:00
|
|
|
if (dir->status & STATUS_OPENED)
|
2012-11-09 17:31:33 +00:00
|
|
|
{
|
|
|
|
_dos_findclose (&dir->f);
|
2013-04-04 15:04:45 +00:00
|
|
|
dir->status &= ~STATUS_OPENED;
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (dir->dp)
|
|
|
|
{
|
|
|
|
QSE_CLOSEDIR (dir->dp);
|
|
|
|
dir->dp = QSE_NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void qse_dir_fini (qse_dir_t* dir)
|
|
|
|
{
|
|
|
|
close_dir_safely (dir);
|
|
|
|
|
|
|
|
qse_mbs_fini (&dir->mbuf);
|
2014-12-11 16:03:58 +00:00
|
|
|
qse_wcs_fini (&dir->wbuf);
|
2013-04-04 15:04:45 +00:00
|
|
|
|
2016-09-23 14:53:51 +00:00
|
|
|
if (dir->stab) qse_arr_close (dir->stab);
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
|
2014-12-11 16:03:58 +00:00
|
|
|
static qse_mchar_t* wcs_to_mbuf (qse_dir_t* dir, const qse_wchar_t* wcs, qse_mbs_t* mbuf)
|
2012-11-09 17:31:33 +00:00
|
|
|
{
|
|
|
|
qse_size_t ml, wl;
|
|
|
|
|
2012-12-25 14:10:02 +00:00
|
|
|
if (qse_wcstombs (wcs, &wl, QSE_NULL, &ml) <= -1)
|
|
|
|
{
|
|
|
|
dir->errnum = QSE_DIR_EINVAL;
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-11 16:03:58 +00:00
|
|
|
if (qse_mbs_setlen (mbuf, ml) == (qse_size_t)-1)
|
2012-12-25 14:10:02 +00:00
|
|
|
{
|
|
|
|
dir->errnum = QSE_DIR_ENOMEM;
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
2012-11-09 17:31:33 +00:00
|
|
|
|
2014-12-11 16:03:58 +00:00
|
|
|
qse_wcstombs (wcs, &wl, QSE_MBS_PTR(mbuf), &ml);
|
|
|
|
return QSE_MBS_PTR(mbuf);
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
|
2014-12-11 16:03:58 +00:00
|
|
|
static qse_wchar_t* mbs_to_wbuf (qse_dir_t* dir, const qse_mchar_t* mbs, qse_wcs_t* wbuf)
|
2012-11-09 17:31:33 +00:00
|
|
|
{
|
|
|
|
qse_size_t ml, wl;
|
|
|
|
|
2012-12-25 14:10:02 +00:00
|
|
|
if (qse_mbstowcs (mbs, &ml, QSE_NULL, &wl) <= -1)
|
|
|
|
{
|
|
|
|
dir->errnum = QSE_DIR_EINVAL;
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
2014-12-11 16:03:58 +00:00
|
|
|
if (qse_wcs_setlen (wbuf, wl) == (qse_size_t)-1)
|
2012-12-25 14:10:02 +00:00
|
|
|
{
|
|
|
|
dir->errnum = QSE_DIR_ENOMEM;
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
2012-11-09 17:31:33 +00:00
|
|
|
|
2014-12-11 16:03:58 +00:00
|
|
|
qse_mbstowcs (mbs, &ml, QSE_WCS_PTR(wbuf), &wl);
|
|
|
|
return QSE_WCS_PTR(wbuf);
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
|
2014-12-11 16:03:58 +00:00
|
|
|
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__)
|
2012-11-09 17:31:33 +00:00
|
|
|
static qse_mchar_t* make_mbsdos_path (qse_dir_t* dir, const qse_mchar_t* mpath)
|
|
|
|
{
|
|
|
|
if (mpath[0] == QSE_MT('\0'))
|
|
|
|
{
|
2012-12-25 14:10:02 +00:00
|
|
|
if (qse_mbs_cpy (&dir->mbuf, QSE_MT("*.*")) == (qse_size_t)-1)
|
|
|
|
{
|
|
|
|
dir->errnum = QSE_DIR_ENOMEM;
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qse_size_t len;
|
|
|
|
if ((len = qse_mbs_cpy (&dir->mbuf, mpath)) == (qse_size_t)-1 ||
|
|
|
|
(!QSE_ISPATHMBSEP(mpath[len - 1]) &&
|
|
|
|
!qse_ismbsdrivecurpath(mpath) &&
|
|
|
|
qse_mbs_ccat (&dir->mbuf, QSE_MT('\\')) == (qse_size_t)-1) ||
|
|
|
|
qse_mbs_cat (&dir->mbuf, QSE_MT("*.*")) == (qse_size_t)-1)
|
|
|
|
{
|
2012-12-25 14:10:02 +00:00
|
|
|
dir->errnum = QSE_DIR_ENOMEM;
|
2012-11-09 17:31:33 +00:00
|
|
|
return QSE_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QSE_MBS_PTR(&dir->mbuf);
|
|
|
|
}
|
|
|
|
|
2014-12-11 16:03:58 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
/*
|
2012-11-09 17:31:33 +00:00
|
|
|
static qse_char_t* make_dos_path (qse_dir_t* dir, const qse_char_t* path)
|
|
|
|
{
|
|
|
|
if (path[0] == QSE_T('\0'))
|
|
|
|
{
|
2012-12-25 14:10:02 +00:00
|
|
|
if (qse_str_cpy (&dir->tbuf, QSE_T("*.*")) == (qse_size_t)-1)
|
|
|
|
{
|
|
|
|
dir->errnum = QSE_DIR_ENOMEM;
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qse_size_t len;
|
|
|
|
if ((len = qse_str_cpy (&dir->tbuf, path)) == (qse_size_t)-1 ||
|
|
|
|
(!QSE_ISPATHSEP(path[len - 1]) &&
|
|
|
|
!qse_isdrivecurpath(path) &&
|
|
|
|
qse_str_ccat (&dir->tbuf, QSE_T('\\')) == (qse_size_t)-1) ||
|
|
|
|
qse_str_cat (&dir->tbuf, QSE_T("*.*")) == (qse_size_t)-1)
|
|
|
|
{
|
2012-12-25 14:10:02 +00:00
|
|
|
dir->errnum = QSE_DIR_ENOMEM;
|
2012-11-09 17:31:33 +00:00
|
|
|
return QSE_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QSE_STR_PTR(&dir->tbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static qse_mchar_t* mkdospath (qse_dir_t* dir, const qse_char_t* path)
|
|
|
|
{
|
|
|
|
|
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
|
|
|
return make_dos_path (dir, path);
|
|
|
|
#else
|
|
|
|
if (dir->flags & QSE_DIR_MBSPATH)
|
|
|
|
{
|
|
|
|
return make_mbsdos_path (dir, (const qse_mchar_t*) path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qse_char_t* tptr;
|
|
|
|
qse_mchar_t* mptr;
|
|
|
|
|
|
|
|
tptr = make_dos_path (dir, path);
|
|
|
|
if (tptr == QSE_NULL) return QSE_NULL;
|
|
|
|
|
|
|
|
mptr = wcs_to_mbuf (dir, QSE_STR_PTR(&dir->tbuf), &dir->mbuf);
|
|
|
|
if (mptr == QSE_NULL) return QSE_NULL;
|
|
|
|
|
|
|
|
return mptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
2014-12-11 16:03:58 +00:00
|
|
|
*/
|
2012-11-09 17:31:33 +00:00
|
|
|
|
|
|
|
static int reset_to_path (qse_dir_t* dir, const qse_char_t* path)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32)
|
|
|
|
/* ------------------------------------------------------------------- */
|
2014-12-11 16:03:58 +00:00
|
|
|
const qse_char_t* tptr;
|
2020-01-06 07:21:17 +00:00
|
|
|
HANDLE dh;
|
|
|
|
WIN32_FIND_DATA wfd;
|
2012-11-09 17:31:33 +00:00
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
dir->status &= ~STATUS_DONE;
|
|
|
|
dir->status &= ~STATUS_DONE_ERR;
|
2012-11-09 17:31:33 +00:00
|
|
|
|
|
|
|
if (dir->flags & QSE_DIR_MBSPATH)
|
|
|
|
{
|
2014-12-11 16:03:58 +00:00
|
|
|
qse_mchar_t* mptr;
|
|
|
|
|
2020-01-06 07:21:17 +00:00
|
|
|
mptr = make_mbsdos_path(dir, (const qse_mchar_t*)path);
|
2012-11-09 17:31:33 +00:00
|
|
|
if (mptr == QSE_NULL) return -1;
|
2014-12-11 16:03:58 +00:00
|
|
|
|
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
|
|
|
tptr = mptr;
|
|
|
|
#else
|
|
|
|
tptr = mbs_to_wbuf (dir, mptr, &dir->wbuf);
|
|
|
|
#endif
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-11 16:03:58 +00:00
|
|
|
qse_wchar_t* wptr;
|
|
|
|
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;
|
2012-11-09 17:31:33 +00:00
|
|
|
#endif
|
2014-12-11 16:03:58 +00:00
|
|
|
}
|
2012-11-09 17:31:33 +00:00
|
|
|
if (tptr == QSE_NULL) return -1;
|
|
|
|
|
2020-01-06 07:21:17 +00:00
|
|
|
dh = FindFirstFile(tptr, &wfd);
|
|
|
|
if (dh == INVALID_HANDLE_VALUE)
|
2012-12-25 14:10:02 +00:00
|
|
|
{
|
|
|
|
dir->errnum = syserr_to_errnum (GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-09 17:31:33 +00:00
|
|
|
|
2020-01-06 07:21:17 +00:00
|
|
|
close_dir_safely (dir);
|
|
|
|
|
|
|
|
dir->h = dh;
|
|
|
|
dir->wfd = wfd;
|
2012-11-09 17:31:33 +00:00
|
|
|
return 0;
|
|
|
|
/* ------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#elif defined(__OS2__)
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------- */
|
|
|
|
APIRET rc;
|
2014-12-11 16:03:58 +00:00
|
|
|
const qse_mchar_t* mptr;
|
2020-01-06 07:21:17 +00:00
|
|
|
HDIR h = HDIR_CREATE;
|
|
|
|
#if defined(FIL_STANDARDL)
|
|
|
|
FILEFINDBUF3L ffb = { 0 };
|
|
|
|
#else
|
|
|
|
FILEFINDBUF3 ffb = { 0 };
|
|
|
|
#endif
|
|
|
|
ULONG count = 1;
|
2012-11-09 17:31:33 +00:00
|
|
|
|
|
|
|
if (dir->flags & QSE_DIR_MBSPATH)
|
|
|
|
{
|
2014-12-11 16:03:58 +00:00
|
|
|
mptr = make_mbsdos_path (dir, (const qse_mchar_t*)path);
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-11 16:03:58 +00:00
|
|
|
qse_wchar_t* wptr;
|
|
|
|
QSE_ASSERT (dir->flags & QSE_DIR_WCSPATH);
|
|
|
|
|
|
|
|
wptr = make_wcsdos_path (dir, (const qse_wchar_t*)path);
|
|
|
|
if (wptr == QSE_NULL) return -1;
|
|
|
|
mptr = wcs_to_mbuf (dir, wptr, &dir->mbuf);
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
if (mptr == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
rc = DosFindFirst (
|
|
|
|
mptr,
|
2020-01-06 07:21:17 +00:00
|
|
|
&h,
|
2012-11-09 17:31:33 +00:00
|
|
|
FILE_DIRECTORY | FILE_READONLY,
|
2020-01-06 07:21:17 +00:00
|
|
|
&ffb,
|
2012-11-09 17:31:33 +00:00
|
|
|
QSE_SIZEOF(dir->ffb),
|
2020-01-06 07:21:17 +00:00
|
|
|
&count,
|
2014-12-11 16:03:58 +00:00
|
|
|
#if defined(FIL_STANDARDL)
|
2012-12-06 13:02:46 +00:00
|
|
|
FIL_STANDARDL
|
2014-12-11 16:03:58 +00:00
|
|
|
#else
|
2012-12-06 13:02:46 +00:00
|
|
|
FIL_STANDARD
|
2014-12-11 16:03:58 +00:00
|
|
|
#endif
|
2012-12-06 13:02:46 +00:00
|
|
|
);
|
2012-11-09 17:31:33 +00:00
|
|
|
|
2012-12-25 14:10:02 +00:00
|
|
|
if (rc != NO_ERROR)
|
|
|
|
{
|
|
|
|
dir->errnum = syserr_to_errnum (rc);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-09 17:31:33 +00:00
|
|
|
|
2020-01-06 07:21:17 +00:00
|
|
|
close_dir_safely (dir);
|
|
|
|
|
|
|
|
dir->h = h;
|
|
|
|
dir->ffb = ffb;
|
|
|
|
dir->count = count;
|
2013-04-04 15:04:45 +00:00
|
|
|
dir->status |= STATUS_OPENED;
|
2012-11-09 17:31:33 +00:00
|
|
|
return 0;
|
|
|
|
/* ------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#elif defined(__DOS__)
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------- */
|
|
|
|
unsigned int rc;
|
2014-12-11 16:03:58 +00:00
|
|
|
const qse_mchar_t* mptr;
|
2020-01-06 07:21:17 +00:00
|
|
|
struct find_t f;
|
2012-11-09 17:31:33 +00:00
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
dir->status &= ~STATUS_DONE;
|
|
|
|
dir->status &= ~STATUS_DONE_ERR;
|
2012-11-09 17:31:33 +00:00
|
|
|
|
|
|
|
if (dir->flags & QSE_DIR_MBSPATH)
|
|
|
|
{
|
2019-06-12 07:07:18 +00:00
|
|
|
mptr = make_mbsdos_path(dir, (const qse_mchar_t*)path);
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-11 16:03:58 +00:00
|
|
|
qse_wchar_t* wptr;
|
|
|
|
|
2016-04-28 15:29:28 +00:00
|
|
|
QSE_ASSERT (dir->flags & QSE_DIR_WCSPATH);
|
2014-12-11 16:03:58 +00:00
|
|
|
|
2019-06-12 07:07:18 +00:00
|
|
|
wptr = make_wcsdos_path(dir, (const qse_wchar_t*)path);
|
2014-12-11 16:03:58 +00:00
|
|
|
if (wptr == QSE_NULL) return -1;
|
2019-06-12 07:07:18 +00:00
|
|
|
mptr = wcs_to_mbuf(dir, wptr, &dir->mbuf);
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
if (mptr == QSE_NULL) return -1;
|
|
|
|
|
2020-01-06 07:21:17 +00:00
|
|
|
rc = _dos_findfirst(mptr, _A_NORMAL | _A_SUBDIR, &f);
|
2012-12-25 14:10:02 +00:00
|
|
|
if (rc != 0)
|
|
|
|
{
|
2019-06-12 07:07:18 +00:00
|
|
|
dir->errnum = syserr_to_errnum(errno);
|
2012-12-25 14:10:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-11-09 17:31:33 +00:00
|
|
|
|
2020-01-06 07:21:17 +00:00
|
|
|
close_dir_safely (dir);
|
|
|
|
|
|
|
|
dir->f = f;
|
2013-04-04 15:04:45 +00:00
|
|
|
dir->status |= STATUS_OPENED;
|
2012-11-09 17:31:33 +00:00
|
|
|
return 0;
|
|
|
|
/* ------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#else
|
|
|
|
DIR* dp;
|
|
|
|
|
|
|
|
if (dir->flags & QSE_DIR_MBSPATH)
|
|
|
|
{
|
2019-06-12 07:07:18 +00:00
|
|
|
const qse_mchar_t* mpath = (const qse_mchar_t*)path;
|
|
|
|
dp = QSE_OPENDIR(mpath[0] == QSE_MT('\0')? QSE_MT("."): mpath);
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-11 16:03:58 +00:00
|
|
|
const qse_wchar_t* wpath;
|
|
|
|
QSE_ASSERT (dir->flags & QSE_DIR_WCSPATH);
|
|
|
|
|
|
|
|
wpath = (const qse_wchar_t*)path;
|
|
|
|
if (wpath[0] == QSE_WT('\0'))
|
2012-11-09 17:31:33 +00:00
|
|
|
{
|
2019-06-12 07:07:18 +00:00
|
|
|
dp = QSE_OPENDIR(QSE_MT("."));
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qse_mchar_t* mptr;
|
|
|
|
|
2019-06-12 07:07:18 +00:00
|
|
|
mptr = wcs_to_mbuf(dir, wpath, &dir->mbuf);
|
2012-11-09 17:31:33 +00:00
|
|
|
if (mptr == QSE_NULL) return -1;
|
|
|
|
|
2019-06-12 07:07:18 +00:00
|
|
|
dp = QSE_OPENDIR(mptr);
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-11 16:03:58 +00:00
|
|
|
|
2012-12-25 14:10:02 +00:00
|
|
|
if (dp == QSE_NULL)
|
|
|
|
{
|
2019-06-12 07:07:18 +00:00
|
|
|
dir->errnum = syserr_to_errnum(errno);
|
2012-12-25 14:10:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-11-09 17:31:33 +00:00
|
|
|
|
2020-01-06 07:21:17 +00:00
|
|
|
close_dir_safely (dir);
|
|
|
|
|
2012-11-09 17:31:33 +00:00
|
|
|
dir->dp = dp;
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int qse_dir_reset (qse_dir_t* dir, const qse_char_t* path)
|
|
|
|
{
|
2020-01-06 07:21:17 +00:00
|
|
|
if (reset_to_path(dir, path) <= -1) return -1;
|
2013-04-04 15:04:45 +00:00
|
|
|
|
|
|
|
if (dir->flags & QSE_DIR_SORT)
|
|
|
|
{
|
2016-09-23 14:53:51 +00:00
|
|
|
qse_arr_clear (dir->stab);
|
2013-04-04 15:04:45 +00:00
|
|
|
if (read_ahead_and_sort (dir, path) <= -1)
|
|
|
|
{
|
|
|
|
dir->status |= STATUS_SORT_ERR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dir->status &= ~STATUS_SORT_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
|
2014-12-11 16:03:58 +00:00
|
|
|
static int read_dir_to_buf (qse_dir_t* dir, void** name)
|
2012-11-09 17:31:33 +00:00
|
|
|
{
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------- */
|
2013-04-04 15:04:45 +00:00
|
|
|
if (dir->status & STATUS_DONE) return (dir->status & STATUS_DONE_ERR)? -1: 0;
|
2012-11-09 17:31:33 +00:00
|
|
|
|
2014-12-09 13:41:58 +00:00
|
|
|
if (dir->flags & QSE_DIR_SKIPSPCDIR)
|
2014-11-25 03:25:46 +00:00
|
|
|
{
|
|
|
|
/* skip . and .. */
|
|
|
|
while (IS_CURDIR(dir->wfd.cFileName) || IS_PREVDIR(dir->wfd.cFileName))
|
|
|
|
{
|
2020-01-06 07:21:17 +00:00
|
|
|
if (FindNextFile(dir->h, &dir->wfd) == FALSE)
|
2014-11-25 03:25:46 +00:00
|
|
|
{
|
|
|
|
DWORD x = GetLastError();
|
|
|
|
if (x == ERROR_NO_MORE_FILES)
|
|
|
|
{
|
|
|
|
dir->status |= STATUS_DONE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dir->errnum = syserr_to_errnum (x);
|
|
|
|
dir->status |= STATUS_DONE;
|
|
|
|
dir->status |= STATUS_DONE_ERR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-09 17:31:33 +00:00
|
|
|
if (dir->flags & QSE_DIR_MBSPATH)
|
|
|
|
{
|
2014-12-11 16:03:58 +00:00
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
2020-01-06 07:21:17 +00:00
|
|
|
if (mbs_to_mbuf(dir, dir->wfd.cFileName, &dir->mbuf) == QSE_NULL) return -1;
|
2014-12-11 16:03:58 +00:00
|
|
|
#else
|
2020-01-06 07:21:17 +00:00
|
|
|
if (wcs_to_mbuf(dir, dir->wfd.cFileName, &dir->mbuf) == QSE_NULL) return -1;
|
2014-12-11 16:03:58 +00:00
|
|
|
#endif
|
|
|
|
*name = QSE_MBS_PTR(&dir->mbuf);
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-11 16:03:58 +00:00
|
|
|
QSE_ASSERT (dir->flags & QSE_DIR_WCSPATH);
|
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
|
|
|
if (mbs_to_wbuf (dir, dir->wfd.cFileName, &dir->wbuf) == QSE_NULL) return -1;
|
|
|
|
#else
|
|
|
|
if (wcs_to_wbuf (dir, dir->wfd.cFileName, &dir->wbuf) == QSE_NULL) return -1;
|
2012-11-09 17:31:33 +00:00
|
|
|
#endif
|
2014-12-11 16:03:58 +00:00
|
|
|
*name = QSE_WCS_PTR(&dir->wbuf);
|
|
|
|
}
|
2012-11-09 17:31:33 +00:00
|
|
|
|
|
|
|
if (FindNextFile (dir->h, &dir->wfd) == FALSE)
|
2012-12-25 14:10:02 +00:00
|
|
|
{
|
|
|
|
DWORD x = GetLastError();
|
2013-04-04 15:04:45 +00:00
|
|
|
if (x == ERROR_NO_MORE_FILES) dir->status |= STATUS_DONE;
|
2012-12-25 14:10:02 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
dir->errnum = syserr_to_errnum (x);
|
2013-04-04 15:04:45 +00:00
|
|
|
dir->status |= STATUS_DONE;
|
|
|
|
dir->status |= STATUS_DONE_ERR;
|
2012-12-25 14:10:02 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-09 17:31:33 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
/* ------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#elif defined(__OS2__)
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------- */
|
|
|
|
APIRET rc;
|
|
|
|
|
|
|
|
if (dir->count <= 0) return 0;
|
|
|
|
|
2014-12-09 13:41:58 +00:00
|
|
|
if (dir->flags & QSE_DIR_SKIPSPCDIR)
|
2014-11-25 03:25:46 +00:00
|
|
|
{
|
|
|
|
/* skip . and .. */
|
|
|
|
while (IS_CURDIR_M(dir->ffb.achName) || IS_PREVDIR_M(dir->ffb.achName))
|
|
|
|
{
|
|
|
|
rc = DosFindNext (dir->h, &dir->ffb, QSE_SIZEOF(dir->ffb), &dir->count);
|
|
|
|
if (rc == ERROR_NO_MORE_FILES)
|
|
|
|
{
|
|
|
|
dir->count = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (rc != NO_ERROR)
|
|
|
|
{
|
|
|
|
dir->errnum = syserr_to_errnum (rc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-09 17:31:33 +00:00
|
|
|
if (dir->flags & QSE_DIR_MBSPATH)
|
|
|
|
{
|
2014-12-11 16:03:58 +00:00
|
|
|
if (mbs_to_mbuf (dir, dir->ffb.achName, &dir->mbuf) == QSE_NULL) return -1;
|
2012-11-09 17:31:33 +00:00
|
|
|
*name = QSE_MBS_PTR(&dir->mbuf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-11 16:03:58 +00:00
|
|
|
QSE_ASSERT (dir->flags & QSE_DIR_WCSPATH);
|
|
|
|
if (mbs_to_wbuf (dir, dir->ffb.achName, &dir->wbuf) == QSE_NULL) return -1;
|
|
|
|
*name = QSE_WCS_PTR(&dir->wbuf);
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
2014-12-11 16:03:58 +00:00
|
|
|
|
2012-11-09 17:31:33 +00:00
|
|
|
|
|
|
|
rc = DosFindNext (dir->h, &dir->ffb, QSE_SIZEOF(dir->ffb), &dir->count);
|
|
|
|
if (rc == ERROR_NO_MORE_FILES) dir->count = 0;
|
2012-12-25 14:10:02 +00:00
|
|
|
else if (rc != NO_ERROR)
|
|
|
|
{
|
|
|
|
dir->errnum = syserr_to_errnum (rc);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-09 17:31:33 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
/* ------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#elif defined(__DOS__)
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------- */
|
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
if (dir->status & STATUS_DONE) return (dir->status & STATUS_DONE_ERR)? -1: 0;
|
2012-11-09 17:31:33 +00:00
|
|
|
|
2014-12-09 13:41:58 +00:00
|
|
|
if (dir->flags & QSE_DIR_SKIPSPCDIR)
|
2014-11-25 03:25:46 +00:00
|
|
|
{
|
|
|
|
/* skip . and .. */
|
|
|
|
while (IS_CURDIR_M(dir->f.name) || IS_PREVDIR_M(dir->f.name))
|
|
|
|
{
|
|
|
|
if (_dos_findnext (&dir->f) != 0)
|
|
|
|
{
|
|
|
|
if (errno == ENOENT)
|
|
|
|
{
|
|
|
|
dir->status |= STATUS_DONE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dir->errnum = syserr_to_errnum (errno);
|
|
|
|
dir->status |= STATUS_DONE;
|
|
|
|
dir->status |= STATUS_DONE_ERR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-09 17:31:33 +00:00
|
|
|
if (dir->flags & QSE_DIR_MBSPATH)
|
|
|
|
{
|
2014-12-11 16:03:58 +00:00
|
|
|
if (mbs_to_mbuf (dir, dir->f.name, &dir->mbuf) == QSE_NULL) return -1;
|
2012-11-09 17:31:33 +00:00
|
|
|
*name = QSE_MBS_PTR(&dir->mbuf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-11 16:03:58 +00:00
|
|
|
QSE_ASSERT (dir->flags & QSE_DIR_WCSPATH);
|
|
|
|
|
|
|
|
if (mbs_to_wbuf (dir, dir->f.name, &dir->wbuf) == QSE_NULL) return -1;
|
|
|
|
*name = QSE_WCS_PTR(&dir->wbuf);
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
|
2012-12-25 14:10:02 +00:00
|
|
|
if (_dos_findnext (&dir->f) != 0)
|
|
|
|
{
|
2013-04-04 15:04:45 +00:00
|
|
|
if (errno == ENOENT) dir->status |= STATUS_DONE;
|
2012-12-25 14:10:02 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
dir->errnum = syserr_to_errnum (errno);
|
2013-04-04 15:04:45 +00:00
|
|
|
dir->status |= STATUS_DONE;
|
|
|
|
dir->status |= STATUS_DONE_ERR;
|
2012-12-25 14:10:02 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-09 17:31:33 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
/* ------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------- */
|
|
|
|
qse_dirent_t* de;
|
|
|
|
|
2014-11-25 03:25:46 +00:00
|
|
|
read:
|
2012-11-09 17:31:33 +00:00
|
|
|
errno = 0;
|
|
|
|
de = QSE_READDIR (dir->dp);
|
2012-12-25 14:10:02 +00:00
|
|
|
if (de == NULL)
|
|
|
|
{
|
|
|
|
if (errno == 0) return 0;
|
|
|
|
dir->errnum = syserr_to_errnum (errno);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-09 17:31:33 +00:00
|
|
|
|
2014-12-09 13:41:58 +00:00
|
|
|
if (dir->flags & QSE_DIR_SKIPSPCDIR)
|
2014-11-25 03:25:46 +00:00
|
|
|
{
|
|
|
|
/* skip . and .. */
|
|
|
|
if (IS_CURDIR_M(de->d_name) ||
|
|
|
|
IS_PREVDIR_M(de->d_name)) goto read;
|
|
|
|
}
|
|
|
|
|
2012-11-09 17:31:33 +00:00
|
|
|
if (dir->flags & QSE_DIR_MBSPATH)
|
|
|
|
{
|
2014-12-11 16:03:58 +00:00
|
|
|
if (mbs_to_mbuf (dir, de->d_name, &dir->mbuf) == QSE_NULL) return -1;
|
2012-11-09 17:31:33 +00:00
|
|
|
*name = QSE_MBS_PTR(&dir->mbuf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-11 16:03:58 +00:00
|
|
|
QSE_ASSERT (dir->flags & QSE_DIR_WCSPATH);
|
|
|
|
|
|
|
|
if (mbs_to_wbuf (dir, de->d_name, &dir->wbuf) == QSE_NULL) return -1;
|
|
|
|
*name = QSE_WCS_PTR(&dir->wbuf);
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
|
|
|
|
2014-04-22 05:16:30 +00:00
|
|
|
return 1;
|
2012-11-09 17:31:33 +00:00
|
|
|
/* ------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
static int read_ahead_and_sort (qse_dir_t* dir, const qse_char_t* path)
|
2012-11-09 17:31:33 +00:00
|
|
|
{
|
|
|
|
int x;
|
|
|
|
void* name;
|
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
while (1)
|
2012-11-09 17:31:33 +00:00
|
|
|
{
|
2014-12-11 16:03:58 +00:00
|
|
|
x = read_dir_to_buf (dir, &name);
|
2013-04-04 15:04:45 +00:00
|
|
|
if (x >= 1)
|
2012-11-09 17:31:33 +00:00
|
|
|
{
|
2013-04-04 15:04:45 +00:00
|
|
|
qse_size_t size;
|
|
|
|
|
|
|
|
if (dir->flags & QSE_DIR_MBSPATH)
|
|
|
|
size = (qse_mbslen(name) + 1) * QSE_SIZEOF(qse_mchar_t);
|
|
|
|
else
|
|
|
|
size = (qse_wcslen(name) + 1) * QSE_SIZEOF(qse_wchar_t);
|
|
|
|
|
2016-09-23 14:53:51 +00:00
|
|
|
if (qse_arr_pushheap (dir->stab, name, size) == (qse_size_t)-1)
|
2013-04-04 15:04:45 +00:00
|
|
|
{
|
|
|
|
dir->errnum = QSE_DIR_ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
2013-04-04 15:04:45 +00:00
|
|
|
else if (x == 0) break;
|
|
|
|
else return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dir->status &= ~STATUS_POPHEAP;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int qse_dir_read (qse_dir_t* dir, qse_dir_ent_t* ent)
|
|
|
|
{
|
|
|
|
if (dir->flags & QSE_DIR_SORT)
|
|
|
|
{
|
|
|
|
if (dir->status & STATUS_SORT_ERR) return -1;
|
|
|
|
|
2016-09-23 14:53:51 +00:00
|
|
|
if (dir->status & STATUS_POPHEAP) qse_arr_popheap (dir->stab);
|
2013-04-04 15:04:45 +00:00
|
|
|
else dir->status |= STATUS_POPHEAP;
|
|
|
|
|
2016-09-23 14:53:51 +00:00
|
|
|
if (QSE_ARR_SIZE(dir->stab) <= 0) return 0; /* no more entry */
|
2013-04-04 15:04:45 +00:00
|
|
|
|
2016-09-23 14:53:51 +00:00
|
|
|
ent->name = QSE_ARR_DPTR(dir->stab, 0);
|
2013-04-04 15:04:45 +00:00
|
|
|
return 1;
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|
2013-04-04 15:04:45 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
void* name;
|
|
|
|
|
2019-12-23 04:25:34 +00:00
|
|
|
x = read_dir_to_buf(dir, &name);
|
2013-04-04 15:04:45 +00:00
|
|
|
if (x >= 1)
|
|
|
|
{
|
2019-12-23 04:25:34 +00:00
|
|
|
QSE_MEMSET (ent, 0, QSE_SIZEOF(*ent));
|
2013-04-04 15:04:45 +00:00
|
|
|
ent->name = name;
|
|
|
|
}
|
2012-11-09 17:31:33 +00:00
|
|
|
|
2013-04-04 15:04:45 +00:00
|
|
|
return x;
|
|
|
|
}
|
2012-11-09 17:31:33 +00:00
|
|
|
}
|