added qse_getpathcore()/qse_getmbspathcore()/qse_getwcspathcore()

added qse_fs_dupfspathforwcs()
improved qse_fs_mkdirXXX()
This commit is contained in:
hyung-hwan 2014-11-27 15:58:51 +00:00
parent 99e9160c53
commit 947f5a8d8d
7 changed files with 286 additions and 73 deletions

View File

@ -129,11 +129,9 @@ enum qse_fs_trait_t
};
typedef enum qse_fs_trait_t qse_fs_trait_t;
typedef int (*qse_fs_cbs_mk_t) (
qse_fs_t* fs,
const qse_char_t* path
);
/**
* \return -1 on failure, 0 to skip, 1 to delete
*/
typedef int (*qse_fs_cbs_del_t) (
qse_fs_t* fs,
const qse_char_t* path
@ -141,7 +139,6 @@ typedef int (*qse_fs_cbs_del_t) (
struct qse_fs_cbs_t
{
qse_fs_cbs_mk_t mk;
qse_fs_cbs_del_t del;
};
typedef struct qse_fs_cbs_t qse_fs_cbs_t;
@ -170,6 +167,15 @@ enum qse_fs_opt_t
typedef enum qse_fs_opt_t qse_fs_opt_t;
enum qse_fs_mkdir_flag_t
{
QSE_FS_MKDIR_PARENT = (1 << 0),
QSE_FS_MKDIRMBS_PARENT = QSE_FS_MKDIR_PARENT,
QSE_FS_MKDIRWCS_PARENT = QSE_FS_MKDIR_PARENT
};
typedef enum qse_fs_mkdir_flag_t qse_fs_mkdir_flag_t;
enum qse_fs_delfile_flag_t
{
QSE_FS_DELFILE_GLOB = (1 << 0),
@ -271,12 +277,14 @@ QSE_EXPORT int qse_fs_move (
QSE_EXPORT int qse_fs_mkdirmbs (
qse_fs_t* fs,
const qse_mchar_t* path
const qse_mchar_t* path,
int flags
);
QSE_EXPORT int qse_fs_mkdirwcs (
qse_fs_t* fs,
const qse_wchar_t* path
const qse_wchar_t* path,
int flags
);
QSE_EXPORT int qse_fs_delfilembs (
@ -304,11 +312,11 @@ QSE_EXPORT int qse_fs_deldirwcs (
);
#if defined(QSE_CHAR_IS_MCHAR)
# define qse_fs_mkdir(fs,path) qse_fs_mkdirmbs(fs,path)
# define qse_fs_mkdir(fs,path,flags) qse_fs_mkdirmbs(fs,path,flags)
# define qse_fs_delfile(fs,path,flags) qse_fs_delfilembs(fs,path,flags)
# define qse_fs_deldir(fs,path,flags) qse_fs_deldirmbs(fs,path,flags)
#else
# define qse_fs_mkdir(fs,path) qse_fs_mkdirwcs(fs,path)
# define qse_fs_mkdir(fs,path,flags) qse_fs_mkdirwcs(fs,path,flags)
# define qse_fs_delfile(fs,path,flags) qse_fs_delfilewcs(fs,path,flags)
# define qse_fs_deldir(fs,path,flags) qse_fs_deldirwcs(fs,path,flags)
#endif

View File

@ -27,7 +27,7 @@
#ifndef _QSE_CMN_PATH_H_
#define _QSE_CMN_PATH_H_
/** @file
/** \file
* This file provides functions for path name manipulation.
*/
@ -91,7 +91,7 @@ QSE_EXPORT const qse_wchar_t* qse_wcsbasename (
* A path name beginning with a segment separator is absolute.
* On Win32/OS2/DOS, it also returns 1 if a path name begins with a drive
* letter followed by a colon.
* @return 1 if absolute, 0 if not.
* \return 1 if absolute, 0 if not.
*/
QSE_EXPORT int qse_ismbsabspath (
const qse_mchar_t* path
@ -122,27 +122,35 @@ QSE_EXPORT int qse_ismbsdrivecurpath (
);
/**
* The qse_canonmbspath() function canonicalizes a path name @a path by deleting
* The qse_getmbspathroot() function returns the core part of \a path
* excluding a special prefix.
*/
QSE_EXPORT qse_mchar_t* qse_getmbspathcore (
const qse_mchar_t* path
);
/**
* The qse_canonmbspath() function canonicalizes a path name \a path by deleting
* unnecessary path segments from it and stores the result to a memory buffer
* pointed to by @a canon. Canonicalization is purely performed on the path
* pointed to by \a canon. Canonicalization is purely performed on the path
* name without refering to actual file systems. It null-terminates the
* canonical path in @a canon and returns the number of characters excluding
* canonical path in \a canon and returns the number of characters excluding
* the terminating null.
*
* @code
* \code
* qse_mchar_t buf[64];
* qse_canonmbspath (QSE_MT("/usr/local/../bin/sh"), buf);
* qse_printf (QSE_T("%hs\n")); // prints /usr/bin/sh
* @endcode
* \endcode
*
* If #QSE_CANONPATH_EMPTYSINGLEDOT is clear in the @a flags, a single dot
* is produced if the input @a path resolves to the current directory logically.
* If #QSE_CANONPATH_EMPTYSINGLEDOT is clear in the \a flags, a single dot
* is produced if the input \a path resolves to the current directory logically.
* For example, dir/.. is canonicalized to a single period; If it is set,
* an empty string is produced. Even a single period as an input produces
* an empty string if it is set.
*
* The output is empty returning 0 regardless of @a flags if the input
* @a path is empty.
* The output is empty returning 0 regardless of \a flags if the input
* \a path is empty.
*
* The caller must ensure that it is large enough to hold the resulting
* canonical path before calling because this function does not check the
@ -151,7 +159,7 @@ QSE_EXPORT int qse_ismbsdrivecurpath (
* buffer as long as the number of characters and a terminating null in
* the original path.
*
* @return number of characters in the resulting canonical path excluding
* \return number of characters in the resulting canonical path excluding
* the terminating null.
*/
QSE_EXPORT qse_size_t qse_canonmbspath (
@ -165,7 +173,7 @@ QSE_EXPORT qse_size_t qse_canonmbspath (
* A path name beginning with a segment separator is absolute.
* On Win32/OS2/DOS, it also returns 1 if a path name begins with a drive
* letter followed by a colon.
* @return 1 if absolute, 0 if not.
* \return 1 if absolute, 0 if not.
*/
QSE_EXPORT int qse_iswcsabspath (
const qse_wchar_t* path
@ -196,27 +204,35 @@ QSE_EXPORT int qse_iswcsdrivecurpath (
);
/**
* The qse_canonwcspath() function canonicalizes a path name @a path by deleting
* The qse_getwcspathroot() function returns the core part of \a path
* excluding a special prefix.
*/
QSE_EXPORT qse_wchar_t* qse_getwcspathcore (
const qse_wchar_t* path
);
/**
* The qse_canonwcspath() function canonicalizes a path name \a path by deleting
* unnecessary path segments from it and stores the result to a memory buffer
* pointed to by @a canon. Canonicalization is purely performed on the path
* pointed to by \a canon. Canonicalization is purely performed on the path
* name without refering to actual file systems. It null-terminates the
* canonical path in @a canon and returns the number of characters excluding
* canonical path in \a canon and returns the number of characters excluding
* the terminating null.
*
* @code
* \code
* qse_wchar_t buf[64];
* qse_canonwcspath (QSE_WT("/usr/local/../bin/sh"), buf);
* qse_printf (QSE_T("%ls\n")); // prints /usr/bin/sh
* @endcode
* \endcode
*
* If #QSE_CANONPATH_EMPTYSINGLEDOT is clear in the @a flags, a single dot
* is produced if the input @a path resolves to the current directory logically.
* If #QSE_CANONPATH_EMPTYSINGLEDOT is clear in the \a flags, a single dot
* is produced if the input \a path resolves to the current directory logically.
* For example, dir/.. is canonicalized to a single period; If it is set,
* an empty string is produced. Even a single period as an input produces
* an empty string if it is set.
*
* The output is empty returning 0 regardless of @a flags if the input
* @a path is empty.
* The output is empty returning 0 regardless of \a flags if the input
* \a path is empty.
*
* The caller must ensure that it is large enough to hold the resulting
* canonical path before calling because this function does not check the
@ -225,7 +241,7 @@ QSE_EXPORT int qse_iswcsdrivecurpath (
* buffer as long as the number of characters and a terminating null in
* the original path.
*
* @return number of characters in the resulting canonical path excluding
* \return number of characters in the resulting canonical path excluding
* the terminating null.
*/
QSE_EXPORT qse_size_t qse_canonwcspath (
@ -239,12 +255,14 @@ QSE_EXPORT qse_size_t qse_canonwcspath (
# define qse_isdrivepath(p) qse_ismbsdrivepath(p)
# define qse_isdriveabspath(p) qse_ismbsdriveabspath(p)
# define qse_isdrivecurpath(p) qse_ismbsdrivecurpath(p)
# define qse_getpathcore(p) qse_getmbspathcore(p)
# define qse_canonpath(p,c,f) qse_canonmbspath(p,c,f)
#else
# define qse_isabspath(p) qse_iswcsabspath(p)
# define qse_isdrivepath(p) qse_iswcsdrivepath(p)
# define qse_isdriveabspath(p) qse_iswcsdriveabspath(p)
# define qse_isdrivecurpath(p) qse_iswcsdrivecurpath(p)
# define qse_getpathcore(p) qse_getwcspathcore(p)
# define qse_canonpath(p,c,f) qse_canonwcspath(p,c,f)
#endif

View File

@ -71,63 +71,102 @@ static int make_directory_in_fs (qse_fs_t* fs, const qse_fs_char_t* fspath)
/* --------------------------------------------------------------------- */
static int make_directory_with_mbs (qse_fs_t* fs, const qse_mchar_t* path)
static int make_directory_chain (qse_fs_t* fs, qse_fs_char_t* fspath)
{
qse_fs_char_t* p, c;
int ret = 0;
p = get_fspath_core (fspath);
canon_fspath (p, p, 0);
for (; *p; p++)
{
if (IS_FSPATHSEP(*p))
{
c = *(p + 1);
*(p + 1) = QSE_FS_T('\0');
ret = make_directory_in_fs (fs, fspath);
if (ret <= -1 && fs->errnum != QSE_FS_EEXIST)
{
return -1;
goto done; /* abort */
}
*(p + 1) = c;
}
}
if (!IS_FSPATHSEP(*(p - 1))) ret = make_directory_in_fs (fs, fspath);
done:
return ret;
}
int qse_fs_mkdirmbs (qse_fs_t* fs, const qse_mchar_t* path, int flags)
{
qse_fs_char_t* fspath;
int ret;
#if 0
if (fs->cbs.mk)
if (*path == QSE_MT('\0'))
{
int x;
x = fs->cbs.mk (fs, path);
if (x <= -1) return -1;
if (x == 0) return 0; /* skipped */
fs->errnum = QSE_FS_EINVAL;
return -1;
}
#endif
fspath = (qse_fs_char_t*)qse_fs_makefspathformbs (fs, path);
if (!fspath) return -1;
if (flags & QSE_FS_MKDIRMBS_PARENT)
{
fspath = qse_fs_dupfspathformbs (fs, path);
if (!fspath) return -1;
ret = make_directory_chain (fs, fspath);
}
else
{
fspath = (qse_fs_char_t*)qse_fs_makefspathformbs (fs, path);
if (!fspath) return -1;
ret = make_directory_in_fs (fs, fspath);
}
ret = make_directory_from_fs (fs, fspath);
qse_fs_freefspathformbs (fs, path, fspath);
return ret;
}
static int make_directory_with_wcs (qse_fs_t* fs, const qse_wchar_t* path)
int qse_fs_mkdirwcs (qse_fs_t* fs, const qse_wchar_t* path, int flags)
{
qse_fs_char_t* fspath;
int ret;
#if 0
if (fs->cbs.mk)
if (*path == QSE_WT('\0'))
{
int x;
x = fs->cbs.del (fs, path);
if (x <= -1) return -1;
if (x == 0) return 0; /* skipped */
fs->errnum = QSE_FS_EINVAL;
return -1;
}
#endif
fspath = (qse_fs_char_t*)qse_fs_makefspathforwcs (fs, path);
if (!fspath) return -1;
if (flags & QSE_FS_MKDIRWCS_PARENT)
{
fspath = qse_fs_dupfspathforwcs (fs, path);
if (!fspath) return -1;
ret = make_directory_chain (fs, fspath);
}
else
{
fspath = (qse_fs_char_t*)qse_fs_makefspathforwcs (fs, path);
if (!fspath) return -1;
ret = make_directory_in_fs (fs, fspath);
}
ret = make_directory_from_fs (fs, fspath);
qse_fs_freefspathforwcs (fs, path, fspath);
return ret;
}
/* --------------------------------------------------------------------- */
int qse_fs_mkdirmbs (qse_fs_t* fs, const qse_mchar_t* path)
{
return 0;
}
int qse_fs_mkdirwcs (qse_fs_t* fs, const qse_wchar_t* path)
{
return 0;
}
/*
mknodmbs
mkfifombs
mknodwcs
mknodwcs
*/

View File

@ -778,6 +778,34 @@ qse_fs_char_t* qse_fs_makefspathforwcs (qse_fs_t* fs, const qse_wchar_t* path)
return fspath;
}
qse_fs_char_t* qse_fs_dupfspathformbs (qse_fs_t* fs, const qse_mchar_t* path)
{
qse_fs_char_t* fspath;
#if defined(QSE_FS_CHAR_IS_MCHAR)
fspath = qse_mbsdup (path, fs->mmgr);
#else
fspath = qse_mbstowcsdupwithcmgr (path, QSE_NULL, fs->mmgrm fs->cmgr);
if (!fspath) fs->errnum = QSE_FS_ENOMEM;
#endif
return fspath;
}
qse_fs_char_t* qse_fs_dupfspathforwcs (qse_fs_t* fs, const qse_wchar_t* path)
{
qse_fs_char_t* fspath;
#if defined(QSE_FS_CHAR_IS_MCHAR)
fspath = qse_wcstombsdupwithcmgr (path, QSE_NULL, fs->mmgr, fs->cmgr);
if (!fspath) fs->errnum = QSE_FS_ENOMEM;
#else
fspath = qse_wcsdup (path, fs->mmgr);
#endif
return fspath;
}
void qse_fs_freefspathformbs (qse_fs_t* fs, const qse_mchar_t* path, qse_fs_char_t* fspath)
{
if (path != fspath) QSE_MMGR_FREE (fs->mmgr, fspath);

View File

@ -30,6 +30,7 @@
#include <qse/cmn/dir.h>
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/path.h>
#if defined(_WIN32)
# include <windows.h>
@ -71,6 +72,18 @@
# define free_str_with_mbs(fs,mbs,str) QSE_MMGR_FREE((fs)->mmgr,str)
#endif
#if defined(QSE_FS_CHAR_IS_MCHAR)
# define canon_fspath(path,canon,flags) qse_canonmbspath(path,canon,flags)
# define get_fspath_core(fspath) qse_getmbspathcore(fspath)
# define IS_FSPATHSEP(x) QSE_ISPATHMBSEP(x)
# define QSE_FS_T(x) QSE_MT(x)
#else
# define canon_fspath(fspath,canon,flags) qse_canonwcspath(fspath,canon,flags)
# define get_fspath_core(fspath) qse_getwcspathcore(fspath)
# define IS_FSPATHSEP(x) QSE_ISPATHWCSEP(x)
# define QSE_FS_T(x) QSE_WT(x)
#endif
#if defined(__cplusplus)
extern "C" {
#endif
@ -95,6 +108,16 @@ qse_fs_char_t* qse_fs_makefspathforwcs (
const qse_wchar_t* path
);
qse_fs_char_t* qse_fs_dupfspathformbs (
qse_fs_t* fs,
const qse_mchar_t* path
);
qse_fs_char_t* qse_fs_dupfspathforwcs (
qse_fs_t* fs,
const qse_wchar_t* path
);
void qse_fs_freefspathformbs (
qse_fs_t* fs,
const qse_mchar_t* path,

View File

@ -26,6 +26,10 @@
#include <qse/cmn/path.h>
/* TODO: support the \\?\ prefix and the \\.\ prefix on windows
* support \\?\UNC\server\path which is equivalent to \\server\path.
* */
/* ------------------------------------------------------------------ */
/* MBS IMPLEMENTATION */
/* ------------------------------------------------------------------ */
@ -40,6 +44,7 @@
(s[0] >= QSE_MT('a') && s[0] <= QSE_MT('z'))) && \
s[1] == QSE_MT(':'))
int qse_ismbsabspath (const qse_mchar_t* path)
{
if (IS_MSEP(path[0])) return 1;
@ -48,7 +53,7 @@ int qse_ismbsabspath (const qse_mchar_t* path)
* but the path within the drive is kind of relative */
if (IS_MDRIVE(path)) return 1;
#endif
return 0;
return 0;
}
int qse_ismbsdrivepath (const qse_mchar_t* path)
@ -75,6 +80,25 @@ int qse_ismbsdrivecurpath (const qse_mchar_t* path)
return 0;
}
qse_mchar_t* qse_getmbspathcore (const qse_mchar_t* path)
{
#if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
if (IS_MDRIVE(path)) return path + 2;
#if defined(_WIN32)
else if (IS_MSEP(*ptr) && IS_MSEP(*(ptr + 1)) && !IS_MSEP_OR_MNIL(*(ptr + 2)))
{
/* UNC Path */
ptr += 2;
do { ptr++; } while (!IS_MSEP_OR_MNIL(*ptr));
if (IS_MSEP(*ptr)) return ptr;
}
#endif
/* TOOD: \\server\XXX \\.\XXX \\?\XXX \\?\UNC\server\XXX */
#endif
return path;
}
qse_size_t qse_canonmbspath (const qse_mchar_t* path, qse_mchar_t* canon, int flags)
{
const qse_mchar_t* ptr;
@ -390,6 +414,23 @@ int qse_iswcsdrivecurpath (const qse_wchar_t* path)
return 0;
}
qse_wchar_t* qse_getwcspathcore (const qse_wchar_t* path)
{
#if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
if (IS_WDRIVE(path)) return path + 2;
#if defined(_WIN32)
else if (IS_WSEP(*ptr) && IS_WSEP(*(ptr + 1)) && !IS_WSEP_OR_WNIL(*(ptr + 2)))
{
/* UNC Path */
ptr += 2;
do { ptr++; } while (!IS_WSEP_OR_WNIL(*ptr));
if (IS_WSEP(*ptr)) return ptr;
}
#endif
#endif
return path;
}
qse_size_t qse_canonwcspath (const qse_wchar_t* path, qse_wchar_t* canon, int flags)
{
const qse_wchar_t* ptr;

View File

@ -7,6 +7,7 @@
#include <qse/cmn/mbwc.h>
#include <locale.h>
static int fs_del (qse_fs_t* fs, const qse_char_t* path)
{
qse_printf (QSE_T("Deleting [%s]\n"), path);
@ -14,26 +15,81 @@ static int fs_del (qse_fs_t* fs, const qse_char_t* path)
return 1;
}
static void print_usage (const qse_char_t* argv0)
{
qse_fprintf (QSE_STDERR, QSE_T("Usage: %s command filename\n"), qse_basename(argv0));
qse_fprintf (QSE_STDERR, QSE_T("Command is one of delfile | delfile-r | deldir | deldir-r | mkdir | mkdir-p\n"));
qse_fprintf (QSE_STDERR, QSE_T("Filename is a pattern for delXXX\n"));
}
static int fs_main (int argc, qse_char_t* argv[])
{
qse_fs_t* fs;
qse_fs_cbs_t cbs;
int ret = 0;
if (argc != 2)
if (argc != 3)
{
qse_fprintf (QSE_STDERR, QSE_T("Usage: %s file-pattern-to-delete\n"), qse_basename(argv[0]));
print_usage (argv[0]);
return -1;
}
fs = qse_fs_open (QSE_MMGR_GETDFL(), 0);
fs = qse_fs_open (QSE_MMGR_GETDFL(), 0);
qse_memset (&cbs, 0, QSE_SIZEOF(cbs));
cbs.del = fs_del;
qse_fs_setopt (fs, QSE_FS_CBS, &cbs);
if (qse_fs_delfile (fs, argv[1], QSE_FS_DELDIRMBS_GLOB | QSE_FS_DELDIRMBS_RECURSIVE) <= -1)
if (qse_strcmp(argv[1], QSE_T("delfile")) == 0)
{
qse_fprintf (QSE_STDERR, QSE_T("cannot delete files - %d\n"), qse_fs_geterrnum(fs));
if (qse_fs_delfile (fs, argv[2], QSE_FS_DELFILEMBS_GLOB) <= -1)
{
qse_fprintf (QSE_STDERR, QSE_T("cannot delete files - %d\n"), qse_fs_geterrnum(fs));
ret = -1;
}
}
else if (qse_strcmp(argv[1], QSE_T("delfile-r")) == 0)
{
if (qse_fs_delfile (fs, argv[2], QSE_FS_DELFILE_GLOB | QSE_FS_DELFILE_RECURSIVE) <= -1)
{
qse_fprintf (QSE_STDERR, QSE_T("cannot delete files - %d\n"), qse_fs_geterrnum(fs));
ret = -1;
}
}
else if (qse_strcmp (argv[1], QSE_T("deldir")) == 0)
{
if (qse_fs_deldir (fs, argv[2], QSE_FS_DELDIR_GLOB) <= -1)
{
qse_fprintf (QSE_STDERR, QSE_T("cannot delete directories - %d\n"), qse_fs_geterrnum(fs));
ret = -1;
}
}
else if (qse_strcmp (argv[1], QSE_T("deldir-r")) == 0)
{
if (qse_fs_deldir (fs, argv[2], QSE_FS_DELDIR_GLOB | QSE_FS_DELDIR_RECURSIVE) <= -1)
{
qse_fprintf (QSE_STDERR, QSE_T("cannot delete directories - %d\n"), qse_fs_geterrnum(fs));
ret = -1;
}
}
else if (qse_strcmp (argv[1], QSE_T("mkdir")) == 0)
{
if (qse_fs_mkdir (fs, argv[2], 0) <= -1)
{
qse_fprintf (QSE_STDERR, QSE_T("cannot make directory - %d\n"), qse_fs_geterrnum(fs));
ret = -1;
}
}
else if (qse_strcmp (argv[1], QSE_T("mkdir-p")) == 0)
{
if (qse_fs_mkdir (fs, argv[2], QSE_FS_MKDIR_PARENT) <= -1)
{
qse_fprintf (QSE_STDERR, QSE_T("cannot make directory - %d\n"), qse_fs_geterrnum(fs));
ret = -1;
}
}
else
{
print_usage (argv[0]);
ret = -1;
}
@ -46,7 +102,7 @@ int main (int argc, qse_achar_t* argv[])
int x;
#if defined(_WIN32)
char locale[100];
UINT codepage = GetConsoleOutputCP();
UINT codepage = GetConsoleOutputCP();
if (codepage == CP_UTF8)
{
/*SetConsoleOUtputCP (CP_UTF8);*/