3 Copyright (C) 2005 and later Cockos Incorporated
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
25 This file provides the interface and implementation for WDL_DirScan, a simple
26 (and somewhat portable) directory reading class. On non-Win32 systems it wraps
27 opendir()/readdir()/etc. On Win32, it uses FindFirst*, and supports wildcards as
34 #ifndef _WDL_DIRSCAN_H_
35 #define _WDL_DIRSCAN_H_
37 #include "wdlstring.h"
40 #include <sys/types.h>
50 m_h(INVALID_HANDLE_VALUE
)
51 #ifndef WDL_NO_SUPPORT_UTF8
55 m_h(NULL
), m_ent(NULL
)
65 int First(const char *dirname
69 ) // returns 0 if success
71 WDL_FastString
scanstr(dirname
);
72 const int l
= scanstr
.GetLength();
78 if (dirname
[l
-1] == '\\' || dirname
[l
-1] == '/') scanstr
.SetLen(l
-1);
79 m_leading_path
= scanstr
;
80 scanstr
.Append("\\*");
84 m_leading_path
= scanstr
;
86 // remove trailing wildcards and directory separator from m_leading_path
87 const char *sp
= m_leading_path
.Get();
88 int idx
= m_leading_path
.GetLength() - 1;
89 while (idx
> 0 && sp
[idx
] != '/' && sp
[idx
] != '\\') idx
--;
90 if (idx
> 0) m_leading_path
.SetLen(idx
);
93 if (dirname
[l
-1] == '\\' || dirname
[l
-1] == '/') scanstr
.SetLen(l
-1);
94 m_leading_path
= scanstr
;
95 if (!scanstr
.GetLength()) scanstr
.Set("/"); // fix for scanning /
100 #ifndef WDL_NO_SUPPORT_UTF8
101 m_h
=INVALID_HANDLE_VALUE
;
102 #ifdef WDL_SUPPORT_WIN9X
103 m_wcmode
= GetVersion()< 0x80000000;
110 int reqbuf
= MultiByteToWideChar(CP_UTF8
,MB_ERR_INVALID_CHARS
,scanstr
.Get(),-1,NULL
,0);
113 WDL_TypedBuf
<WCHAR
> tmp
;
114 tmp
.Resize(reqbuf
+10);
115 if (MultiByteToWideChar(CP_UTF8
,MB_ERR_INVALID_CHARS
,scanstr
.Get(),-1,tmp
.Get(),tmp
.GetSize()))
116 m_h
=FindFirstFileW(tmp
.Get(),&m_fd
);
120 WCHAR wfilename
[1024];
121 if (MultiByteToWideChar(CP_UTF8
,MB_ERR_INVALID_CHARS
,scanstr
.Get(),-1,wfilename
,1024))
122 m_h
=FindFirstFileW(wfilename
,&m_fd
);
126 if (m_h
==INVALID_HANDLE_VALUE
) m_wcmode
=false;
128 if (m_h
==INVALID_HANDLE_VALUE
)
130 m_h
=FindFirstFile(scanstr
.Get(),(WIN32_FIND_DATA
*)&m_fd
);
131 return (m_h
== INVALID_HANDLE_VALUE
);
134 m_h
=opendir(scanstr
.Get());
135 return !m_h
|| Next();
138 int Next() // returns 0 on success
141 if (m_h
== INVALID_HANDLE_VALUE
) return -1;
142 #ifndef WDL_NO_SUPPORT_UTF8
143 if (m_wcmode
) return !FindNextFileW(m_h
,&m_fd
);
145 return !FindNextFile(m_h
,(WIN32_FIND_DATA
*)&m_fd
);
148 return !(m_ent
=readdir(m_h
));
154 if (m_h
!= INVALID_HANDLE_VALUE
) FindClose(m_h
);
155 m_h
=INVALID_HANDLE_VALUE
;
157 if (m_h
) closedir(m_h
);
163 const char *GetCurrentFN()
165 #ifndef WDL_NO_SUPPORT_UTF8
168 if (!WideCharToMultiByte(CP_UTF8
,0,m_fd
.cFileName
,-1,m_tmpbuf
,sizeof(m_tmpbuf
),NULL
,NULL
))
173 return ((WIN32_FIND_DATA
*)&m_fd
)->cFileName
;
176 const char *GetCurrentFN() const { return m_ent
?m_ent
->d_name
: ""; }
178 template<class T
> void GetCurrentFullFN(T
*str
)
180 str
->Set(m_leading_path
.Get());
186 str
->Append(GetCurrentFN());
188 int GetCurrentIsDirectory() const
191 return !!(m_fd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
194 // we could enable this on OSX, need to check to make sure realpath(x,NULL) is supported on 10.5+
196 if (m_ent
&& m_ent
->d_type
== DT_LNK
)
198 snprintf(tmp
,sizeof(tmp
),"%s/%s",m_leading_path
.Get(),m_ent
->d_name
);
199 char *rp
= realpath(tmp
,NULL
);
202 DIR *d
= opendir(rp
);
205 if (d
) { closedir(d
); return 1; }
208 else if (m_ent
&& m_ent
->d_type
== DT_UNKNOWN
)
210 snprintf(tmp
,sizeof(tmp
),"%s/%s",m_leading_path
.Get(),m_ent
->d_name
);
211 DIR *d
= opendir(tmp
);
212 if (d
) { closedir(d
); return 1; }
215 return m_ent
&& (m_ent
->d_type
== DT_DIR
);
219 // these are somewhat windows specific calls, eh
221 DWORD
GetCurrentFileSize(DWORD
*HighWord
=NULL
) const { if (HighWord
) *HighWord
= m_fd
.nFileSizeHigh
; return m_fd
.nFileSizeLow
; }
222 void GetCurrentLastWriteTime(FILETIME
*ft
) const { *ft
= m_fd
.ftLastWriteTime
; }
223 void GetCurrentLastAccessTime(FILETIME
*ft
) const { *ft
= m_fd
.ftLastAccessTime
; }
224 void GetCurrentCreationTime(FILETIME
*ft
) const { *ft
= m_fd
.ftCreationTime
; }
225 DWORD
GetFileAttributes() const { return m_fd
.dwFileAttributes
; }
226 #elif defined(_WDL_SWELL_H_)
228 void GetCurrentCreationTime(FILETIME
*ft
)
231 snprintf(tmp
,sizeof(tmp
),"%s/%s",m_leading_path
.Get(),GetCurrentFN());
232 struct stat64 st
={0,};
234 unsigned long long a
=(unsigned long long)st
.st_ctime
; // seconds since january 1st, 1970
235 a
+=11644473600ull; // 1601->1970
236 a
*=10000000; // seconds to 1/10th microseconds (100 nanoseconds)
237 ft
->dwLowDateTime
=a
& 0xffffffff;
238 ft
->dwHighDateTime
=a
>>32;
241 void GetCurrentLastWriteTime(FILETIME
*ft
)
244 snprintf(tmp
,sizeof(tmp
),"%s/%s",m_leading_path
.Get(),GetCurrentFN());
245 struct stat64 st
={0,};
247 unsigned long long a
=(unsigned long long)st
.st_mtime
; // seconds since january 1st, 1970
248 a
+=11644473600ull; // 1601->1970
249 a
*=10000000; // seconds to 1/10th microseconds (100 nanoseconds)
250 ft
->dwLowDateTime
=a
& 0xffffffff;
251 ft
->dwHighDateTime
=a
>>32;
253 DWORD
GetCurrentFileSize(DWORD
*HighWord
=NULL
)
256 snprintf(tmp
,sizeof(tmp
),"%s/%s",m_leading_path
.Get(),GetCurrentFN());
257 struct stat64 st
={0,};
260 if (HighWord
) *HighWord
= (DWORD
)(st
.st_size
>>32);
261 return (DWORD
)(st
.st_size
&0xffffffff);
269 #ifndef WDL_NO_SUPPORT_UTF8
271 WIN32_FIND_DATAW m_fd
;
272 char m_tmpbuf
[MAX_PATH
*5]; // even if each byte gets encoded as 4 utf-8 bytes this should be plenty ;)
274 WIN32_FIND_DATA m_fd
;
279 struct dirent
*m_ent
;
281 WDL_FastString m_leading_path
;