11 #include <drift/dslcore.h>
12 #include <drift/Directory.h>
13 #include <drift/GenLib.h>
15 Directory::Directory() {
18 #if defined(__DSL_USE_READDIR_R__)
23 Directory::Directory(
const char * dir) {
26 #if defined(__DSL_USE_READDIR_R__)
32 Directory::Directory(
const wchar_t * dir) {
35 #if defined(__DSL_USE_READDIR_R__)
41 Directory::~Directory() {
47 if (dir == NULL || dir[0] == 0) {
return false; }
48 this->mDir =
new char[strlen(dir)+16];
51 int bufSize = int(strlen(dir)) + 16;
52 wchar_t * wbuf =
new wchar_t[bufSize];
53 int n = MultiByteToWideChar(CP_UTF8, 0, dir, -1, wbuf, bufSize);
55 n = MultiByteToWideChar(CP_THREAD_ACP, 0, dir, -1, wbuf, bufSize);
60 bool ret =
Open(wbuf);
64 if (mDir[strlen(mDir)-1] == PATH_SEP) { mDir[strlen(mDir)-1]=0; }
65 #if defined(__DSL_USE_READDIR_R__)
68 hFind = opendir(mDir);
69 strcat(mDir, PATH_SEPS);
79 if (dir == NULL || wcslen(dir) == 0) {
return false; }
80 this->wDir =
new wchar_t[wcslen(dir)+16];
83 if (wDir[wcslen(wDir)-1] != PATH_SEP) { wcscat(wDir, WPATH_SEPS); }
88 wcstombs(dir2, dir,
sizeof(dir2));
94 bool Directory::Read(
char * buf,
unsigned long bufSize,
bool * is_dir, int64 * size) {
95 memset(buf, 0, bufSize);
97 wchar_t * wbuf =
new wchar_t[bufSize+1];
98 bool ret =
Read(wbuf, bufSize, is_dir, size);
99 WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, buf, bufSize, NULL, NULL);
142 #if !defined(__DSL_USE_READDIR_R__)
143 dirent * de = readdir(hFind);
148 if (readdir_r(hFind, &de, &last_res) == 0 && last_res != NULL) {
151 if (is_dir != NULL && size == NULL && (de->d_type == DT_DIR || de->d_type == DT_REG)) {
152 *is_dir = (de->d_type == DT_DIR) ?
true:
false;
153 }
else if (is_dir != NULL || size != NULL) {
154 snprintf(buf, bufSize,
"%s%s", mDir, de->d_name);
156 if (stat(buf, &st) == 0) {
158 *is_dir = S_ISDIR(st.st_mode) ?
true:
false;
165 strlcpy(buf, de->d_name, bufSize);
173 bool Directory::Read(
wchar_t * buf,
unsigned long bufSize,
bool * is_dir, int64 * size) {
174 memset(buf, 0, bufSize);
177 hFind = FindFirstFileW(wDir, &wfdW);
178 wcsrchr(wDir, PATH_SEP)[1] = 0;
179 if (hFind != INVALID_HANDLE_VALUE) {
181 *is_dir = (wfdW.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ?
true :
false;
185 li.LowPart = wfdW.nFileSizeLow;
186 li.HighPart = wfdW.nFileSizeHigh;
189 wcsncpy(buf, wfdW.cFileName, bufSize);
195 if (FindNextFileW(this->hFind, &wfdW)) {
197 *is_dir = (wfdW.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ?
true :
false;
201 li.LowPart = wfdW.nFileSizeLow;
202 li.HighPart = wfdW.nFileSizeHigh;
205 wcsncpy(buf, wfdW.cFileName, bufSize);
210 char * tmp = (
char *)dsl_malloc(bufSize+1);
212 bool ret =
Read(tmp, bufSize, is_dir, size);
214 mbstowcs(buf, tmp, bufSize);
bool Read(char *buf, unsigned long bufSize, bool *is_dir=NULL, int64 *size=NULL)
bool Open(const char *dir)
Open directory (ANSI/UTF-8)
void Close()
Close the directory. Will be called automatically during deconstruction.
DSL_API void DSL_CC dsl_free(void *ptr)
DSL_API size_t DSL_CC strlcpy(char *dst, const char *src, size_t siz)