Merge pull request #113 from tesselode/fix-multi-targets
[wdl/wdl-ol.git] / WDL / dirscan.h
blob56bd3e983827fa6cfe46c13eb1b82a8bae539f34
1 /*
2 WDL - dirscan.h
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
28 well.
34 #ifndef _WDL_DIRSCAN_H_
35 #define _WDL_DIRSCAN_H_
37 #include "wdlstring.h"
39 #ifndef _WIN32
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <dirent.h>
43 #endif
45 class WDL_DirScan
47 public:
48 WDL_DirScan() :
49 #ifdef _WIN32
50 m_h(INVALID_HANDLE_VALUE)
51 #ifndef WDL_NO_SUPPORT_UTF8
52 , m_wcmode(false)
53 #endif
54 #else
55 m_h(NULL), m_ent(NULL)
56 #endif
60 ~WDL_DirScan()
62 Close();
65 int First(const char *dirname
66 #ifdef _WIN32
67 , int isExactSpec=0
68 #endif
69 ) // returns 0 if success
71 WDL_FastString scanstr(dirname);
72 const int l = scanstr.GetLength();
73 if (l < 1) return -1;
75 #ifdef _WIN32
76 if (!isExactSpec)
78 if (dirname[l-1] == '\\' || dirname[l-1] == '/') scanstr.SetLen(l-1);
79 m_leading_path = scanstr;
80 scanstr.Append("\\*");
82 else
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);
92 #else
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 /
96 #endif
98 Close();
99 #ifdef _WIN32
100 #ifndef WDL_NO_SUPPORT_UTF8
101 m_h=INVALID_HANDLE_VALUE;
102 #ifdef WDL_SUPPORT_WIN9X
103 m_wcmode = GetVersion()< 0x80000000;
104 #else
105 m_wcmode = true;
106 #endif
108 if (m_wcmode)
110 int reqbuf = MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,scanstr.Get(),-1,NULL,0);
111 if (reqbuf > 1000)
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);
118 else
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)
129 #endif
130 m_h=FindFirstFile(scanstr.Get(),(WIN32_FIND_DATA*)&m_fd);
131 return (m_h == INVALID_HANDLE_VALUE);
132 #else
133 m_ent=0;
134 m_h=opendir(scanstr.Get());
135 return !m_h || Next();
136 #endif
138 int Next() // returns 0 on success
140 #ifdef _WIN32
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);
144 #endif
145 return !FindNextFile(m_h,(WIN32_FIND_DATA*)&m_fd);
146 #else
147 if (!m_h) return -1;
148 return !(m_ent=readdir(m_h));
149 #endif
151 void Close()
153 #ifdef _WIN32
154 if (m_h != INVALID_HANDLE_VALUE) FindClose(m_h);
155 m_h=INVALID_HANDLE_VALUE;
156 #else
157 if (m_h) closedir(m_h);
158 m_h=0; m_ent=0;
159 #endif
162 #ifdef _WIN32
163 const char *GetCurrentFN()
165 #ifndef WDL_NO_SUPPORT_UTF8
166 if (m_wcmode)
168 if (!WideCharToMultiByte(CP_UTF8,0,m_fd.cFileName,-1,m_tmpbuf,sizeof(m_tmpbuf),NULL,NULL))
169 m_tmpbuf[0]=0;
170 return m_tmpbuf;
172 #endif
173 return ((WIN32_FIND_DATA *)&m_fd)->cFileName;
175 #else
176 const char *GetCurrentFN() const { return m_ent?m_ent->d_name : ""; }
177 #endif
178 template<class T> void GetCurrentFullFN(T *str)
180 str->Set(m_leading_path.Get());
181 #ifdef _WIN32
182 str->Append("\\");
183 #else
184 str->Append("/");
185 #endif
186 str->Append(GetCurrentFN());
188 int GetCurrentIsDirectory() const
190 #ifdef _WIN32
191 return !!(m_fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
192 #else
193 #ifndef __APPLE__
194 // we could enable this on OSX, need to check to make sure realpath(x,NULL) is supported on 10.5+
195 char tmp[2048];
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);
200 if (rp)
202 DIR *d = opendir(rp);
203 free(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; }
214 #endif
215 return m_ent && (m_ent->d_type == DT_DIR);
216 #endif
219 // these are somewhat windows specific calls, eh
220 #ifdef _WIN32
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)
230 char tmp[2048];
231 snprintf(tmp,sizeof(tmp),"%s/%s",m_leading_path.Get(),GetCurrentFN());
232 struct stat64 st={0,};
233 stat64(tmp,&st);
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)
243 char tmp[2048];
244 snprintf(tmp,sizeof(tmp),"%s/%s",m_leading_path.Get(),GetCurrentFN());
245 struct stat64 st={0,};
246 stat64(tmp,&st);
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)
255 char tmp[2048];
256 snprintf(tmp,sizeof(tmp),"%s/%s",m_leading_path.Get(),GetCurrentFN());
257 struct stat64 st={0,};
258 stat64(tmp,&st);
260 if (HighWord) *HighWord = (DWORD)(st.st_size>>32);
261 return (DWORD)(st.st_size&0xffffffff);
264 #endif
266 private:
267 #ifdef _WIN32
269 #ifndef WDL_NO_SUPPORT_UTF8
270 bool m_wcmode;
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 ;)
273 #else
274 WIN32_FIND_DATA m_fd;
275 #endif
276 HANDLE m_h;
277 #else
278 DIR *m_h;
279 struct dirent *m_ent;
280 #endif
281 WDL_FastString m_leading_path;
282 } WDL_FIXALIGN;
284 #endif