added sys::resetdir() to awk

fixed bugs in qse_dir_reset()
This commit is contained in:
hyung-hwan 2020-01-06 07:21:17 +00:00
parent ec930a92ab
commit 351736b43c
2 changed files with 82 additions and 15 deletions

View File

@ -1001,6 +1001,52 @@ static int fnc_readdir (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
return 0; return 0;
} }
static int fnc_resetdir (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
{
sys_list_t* sys_list;
sys_node_t* sys_node;
qse_awk_int_t rx = RC_ERROR;
sys_list = rtx_to_sys_list(rtx, fi);
sys_node = get_sys_list_node_with_arg(rtx, sys_list, qse_awk_rtx_getarg(rtx, 0));
if (sys_node && sys_node->ctx.type == SYS_NODE_DATA_DIR)
{
qse_char_t* path;
qse_awk_val_t* a1;
a1 = qse_awk_rtx_getarg(rtx, 1);
path = qse_awk_rtx_getvalstr(rtx, a1, QSE_NULL);
if (path)
{
if (qse_dir_reset(sys_node->ctx.u.dir, path) <= -1)
{
rx = direrr_to_rc(qse_dir_geterrnum(sys_node->ctx.u.dir));
set_errmsg_on_sys_list (rtx, sys_list, rc_to_errstr(rx));
}
else
{
rx = 0; /* success */
qse_awk_rtx_freevalstr (rtx, a1, path);
}
}
else
{
rx = awkerr_to_rc(qse_awk_rtx_geterrnum(rtx));
set_errmsg_on_sys_list (rtx, sys_list, rc_to_errstr(rx));
}
}
else
{
rx = RC_EINVAL;
set_errmsg_on_sys_list (rtx, sys_list, rc_to_errstr(rx));
}
/* no error check for qse_awk_rtx_makeintval() here since ret
* is 0 or -1. it will never fail for those numbers */
qse_awk_rtx_setretval (rtx, qse_awk_rtx_makeintval(rtx, rx));
return 0;
}
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
static int fnc_fork (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) static int fnc_fork (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
@ -2483,6 +2529,7 @@ static fnctab_t fnctab[] =
{ QSE_T("pipe"), { { 2, 3, QSE_T("rrv") }, fnc_pipe, 0 } }, { QSE_T("pipe"), { { 2, 3, QSE_T("rrv") }, fnc_pipe, 0 } },
{ QSE_T("read"), { { 2, 3, QSE_T("vrv") }, fnc_read, 0 } }, { QSE_T("read"), { { 2, 3, QSE_T("vrv") }, fnc_read, 0 } },
{ QSE_T("readdir"), { { 2, 2, QSE_T("vr") }, fnc_readdir, 0 } }, { QSE_T("readdir"), { { 2, 2, QSE_T("vr") }, fnc_readdir, 0 } },
{ QSE_T("resetdir"), { { 2, 2, QSE_NULL }, fnc_resetdir, 0 } },
{ QSE_T("settime"), { { 1, 1, QSE_NULL }, fnc_settime, 0 } }, { QSE_T("settime"), { { 1, 1, QSE_NULL }, fnc_settime, 0 } },
{ QSE_T("sleep"), { { 1, 1, QSE_NULL }, fnc_sleep, 0 } }, { QSE_T("sleep"), { { 1, 1, QSE_NULL }, fnc_sleep, 0 } },
{ QSE_T("strftime"), { { 2, 3, QSE_NULL }, fnc_strftime, 0 } }, { QSE_T("strftime"), { { 2, 3, QSE_NULL }, fnc_strftime, 0 } },

View File

@ -430,6 +430,8 @@ static int reset_to_path (qse_dir_t* dir, const qse_char_t* path)
#if defined(_WIN32) #if defined(_WIN32)
/* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */
const qse_char_t* tptr; const qse_char_t* tptr;
HANDLE dh;
WIN32_FIND_DATA wfd;
dir->status &= ~STATUS_DONE; dir->status &= ~STATUS_DONE;
dir->status &= ~STATUS_DONE_ERR; dir->status &= ~STATUS_DONE_ERR;
@ -438,7 +440,7 @@ static int reset_to_path (qse_dir_t* dir, const qse_char_t* path)
{ {
qse_mchar_t* mptr; qse_mchar_t* mptr;
mptr = make_mbsdos_path (dir, (const qse_mchar_t*)path); mptr = make_mbsdos_path(dir, (const qse_mchar_t*)path);
if (mptr == QSE_NULL) return -1; if (mptr == QSE_NULL) return -1;
#if defined(QSE_CHAR_IS_MCHAR) #if defined(QSE_CHAR_IS_MCHAR)
@ -463,13 +465,17 @@ static int reset_to_path (qse_dir_t* dir, const qse_char_t* path)
} }
if (tptr == QSE_NULL) return -1; if (tptr == QSE_NULL) return -1;
dir->h = FindFirstFile (tptr, &dir->wfd); dh = FindFirstFile(tptr, &wfd);
if (dir->h == INVALID_HANDLE_VALUE) if (dh == INVALID_HANDLE_VALUE)
{ {
dir->errnum = syserr_to_errnum (GetLastError()); dir->errnum = syserr_to_errnum (GetLastError());
return -1; return -1;
} }
close_dir_safely (dir);
dir->h = dh;
dir->wfd = wfd;
return 0; return 0;
/* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */
@ -478,9 +484,13 @@ static int reset_to_path (qse_dir_t* dir, const qse_char_t* path)
/* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */
APIRET rc; APIRET rc;
const qse_mchar_t* mptr; const qse_mchar_t* mptr;
HDIR h = HDIR_CREATE;
dir->h = HDIR_CREATE; #if defined(FIL_STANDARDL)
dir->count = 1; FILEFINDBUF3L ffb = { 0 };
#else
FILEFINDBUF3 ffb = { 0 };
#endif
ULONG count = 1;
if (dir->flags & QSE_DIR_MBSPATH) if (dir->flags & QSE_DIR_MBSPATH)
{ {
@ -499,11 +509,11 @@ static int reset_to_path (qse_dir_t* dir, const qse_char_t* path)
rc = DosFindFirst ( rc = DosFindFirst (
mptr, mptr,
&dir->h, &h,
FILE_DIRECTORY | FILE_READONLY, FILE_DIRECTORY | FILE_READONLY,
&dir->ffb, &ffb,
QSE_SIZEOF(dir->ffb), QSE_SIZEOF(dir->ffb),
&dir->count, &count,
#if defined(FIL_STANDARDL) #if defined(FIL_STANDARDL)
FIL_STANDARDL FIL_STANDARDL
#else #else
@ -517,6 +527,11 @@ static int reset_to_path (qse_dir_t* dir, const qse_char_t* path)
return -1; return -1;
} }
close_dir_safely (dir);
dir->h = h;
dir->ffb = ffb;
dir->count = count;
dir->status |= STATUS_OPENED; dir->status |= STATUS_OPENED;
return 0; return 0;
/* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */
@ -526,6 +541,7 @@ static int reset_to_path (qse_dir_t* dir, const qse_char_t* path)
/* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */
unsigned int rc; unsigned int rc;
const qse_mchar_t* mptr; const qse_mchar_t* mptr;
struct find_t f;
dir->status &= ~STATUS_DONE; dir->status &= ~STATUS_DONE;
dir->status &= ~STATUS_DONE_ERR; dir->status &= ~STATUS_DONE_ERR;
@ -546,13 +562,16 @@ static int reset_to_path (qse_dir_t* dir, const qse_char_t* path)
} }
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, &f);
if (rc != 0) if (rc != 0)
{ {
dir->errnum = syserr_to_errnum(errno); dir->errnum = syserr_to_errnum(errno);
return -1; return -1;
} }
close_dir_safely (dir);
dir->f = f;
dir->status |= STATUS_OPENED; dir->status |= STATUS_OPENED;
return 0; return 0;
/* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */
@ -592,6 +611,8 @@ static int reset_to_path (qse_dir_t* dir, const qse_char_t* path)
return -1; return -1;
} }
close_dir_safely (dir);
dir->dp = dp; dir->dp = dp;
return 0; return 0;
#endif #endif
@ -599,8 +620,7 @@ static int reset_to_path (qse_dir_t* dir, const qse_char_t* path)
int qse_dir_reset (qse_dir_t* dir, const qse_char_t* path) int qse_dir_reset (qse_dir_t* dir, const qse_char_t* path)
{ {
close_dir_safely (dir); if (reset_to_path(dir, path) <= -1) return -1;
if (reset_to_path (dir, path) <= -1) return -1;
if (dir->flags & QSE_DIR_SORT) if (dir->flags & QSE_DIR_SORT)
{ {
@ -631,7 +651,7 @@ static int read_dir_to_buf (qse_dir_t* dir, void** name)
/* skip . and .. */ /* skip . and .. */
while (IS_CURDIR(dir->wfd.cFileName) || IS_PREVDIR(dir->wfd.cFileName)) while (IS_CURDIR(dir->wfd.cFileName) || IS_PREVDIR(dir->wfd.cFileName))
{ {
if (FindNextFile (dir->h, &dir->wfd) == FALSE) if (FindNextFile(dir->h, &dir->wfd) == FALSE)
{ {
DWORD x = GetLastError(); DWORD x = GetLastError();
if (x == ERROR_NO_MORE_FILES) if (x == ERROR_NO_MORE_FILES)
@ -653,9 +673,9 @@ static int read_dir_to_buf (qse_dir_t* dir, void** name)
if (dir->flags & QSE_DIR_MBSPATH) if (dir->flags & QSE_DIR_MBSPATH)
{ {
#if defined(QSE_CHAR_IS_MCHAR) #if defined(QSE_CHAR_IS_MCHAR)
if (mbs_to_mbuf (dir, dir->wfd.cFileName, &dir->mbuf) == QSE_NULL) return -1; if (mbs_to_mbuf(dir, dir->wfd.cFileName, &dir->mbuf) == QSE_NULL) return -1;
#else #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;
#endif #endif
*name = QSE_MBS_PTR(&dir->mbuf); *name = QSE_MBS_PTR(&dir->mbuf);
} }