qse/samples/si/dir01.c

132 lines
2.8 KiB
C
Raw Normal View History

2016-04-29 03:55:42 +00:00
#include <qse/si/dir.h>
2013-04-07 01:53:54 +00:00
#include <qse/cmn/main.h>
#include <qse/cmn/mbwc.h>
2016-04-29 03:55:42 +00:00
#include <qse/si/sio.h>
2013-04-07 01:53:54 +00:00
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/fmt.h>
2013-04-07 01:53:54 +00:00
#include <locale.h>
2013-04-07 01:53:54 +00:00
#if defined(_WIN32)
# include <windows.h>
#endif
static int list_dir (int argc, qse_char_t* argv[], int flags)
2013-04-07 01:53:54 +00:00
{
qse_dir_t* dir = QSE_NULL;
2013-04-07 01:53:54 +00:00
qse_dir_ent_t dirent;
void* xpath = QSE_NULL;
void* xdirpath;
2013-04-07 01:53:54 +00:00
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;
}
}
2013-04-07 01:53:54 +00:00
while (qse_dir_read (dir, &dirent) > 0)
{
if (flags & QSE_DIR_MBSPATH)
qse_printf (QSE_T("%hs\n"), dirent.name);
else
qse_printf (QSE_T("%ls\n"), dirent.name);
2013-04-07 01:53:54 +00:00
}
qse_printf (QSE_T("----------------------------------------\n"));
if (qse_dir_reset (dir, xdirpath) <= -1)
{
qse_printf (QSE_T("Cannot reset\n"));
goto oops;
}
2013-04-07 01:53:54 +00:00
while (qse_dir_read (dir, &dirent) > 0)
{
if (flags & QSE_DIR_MBSPATH)
qse_printf (QSE_T("%hs\n"), dirent.name);
else
qse_printf (QSE_T("%ls\n"), dirent.name);
2013-04-07 01:53:54 +00:00
}
QSE_MMGR_FREE(QSE_MMGR_GETDFL(), xpath);
2013-04-07 01:53:54 +00:00
qse_dir_close (dir);
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;
2013-04-07 01:53:54 +00:00
}
int qse_main (int argc, qse_achar_t* argv[])
{
int x;
2013-04-07 01:53:54 +00:00
#if defined(_WIN32)
char locale[100];
UINT codepage = GetConsoleOutputCP();
2013-04-07 01:53:54 +00:00
if (codepage == CP_UTF8)
{
/*SetConsoleOUtputCP (CP_UTF8);*/
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
}
else
{
/* .codepage */
qse_fmtuintmaxtombs (locale, QSE_COUNTOF(locale),
codepage, 10, -1, QSE_MT('\0'), QSE_MT("."));
2013-04-07 01:53:54 +00:00
setlocale (LC_ALL, locale);
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
2013-04-07 01:53:54 +00:00
}
#else
setlocale (LC_ALL, "");
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
2013-04-07 01:53:54 +00:00
#endif
2017-09-16 08:54:25 +00:00
qse_open_stdsios ();
x = qse_run_main (argc, argv, dir_main);
2017-09-16 08:54:25 +00:00
qse_close_stdsios ();
return x;
2013-04-07 01:53:54 +00:00
}