uLib  User mode C/C++ extended API library for Win32 programmers.
FileDir.h
Go to the documentation of this file.
1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 // Project: uLib - User mode utility library.
3 // Module: Hierarchial file directory listings using DLinkList.
4 // Author: Copyright (c) Love Nystrom.
5 // License: NNOSL (BSD descendant, see NNOSL.txt in the base directory).
6 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 
8 #ifndef __FileDir_h_incl__
9 #define __FileDir_h_incl__
10 
11 #include <uLib/Common.h>
12 #include <uLib/ListFunc.h>
13 #include <uLib/MemFunc.h>
14 
15 //==---------------------------------------------------------------------------
25 //==---------------------------------------------------------------------------
27 
35 
36 CSTR LongestCommonPath( CSTR PathName1, CSTR PathName2 );
37 
38 #ifdef __cplusplus // The rest of this module is C++
39 
40 class FileEntry; // Forward declarations needed here because..
41 class DirEntry; // ..doxygen miscompiled the doc with the class/struct..
42 struct ScanDirCtx; // ..specifier directly in the pointer declarations.
43 
44 typedef FileEntry* PFileEntry;
45 typedef DirEntry* PDirEntry;
47 
54 
55 typedef bool (__stdcall *PFnDirAddAction)( PDirEntry pDir, LPWIN32_FIND_DATA pFD, PScanDirCtx pCtx );
56 typedef bool (__stdcall *PFnFileAddAction)( PFileEntry pFile, LPWIN32_FIND_DATA pFD, PScanDirCtx pCtx );
58 
63 
64 typedef bool (__stdcall *PFnDirAction)( PDirEntry pDir, PScanDirCtx pCtx );
65 typedef bool (__stdcall *PFnFileAction)( PFileEntry pFile, PScanDirCtx pCtx );
67 
69 
70 typedef bool (__stdcall *PFnAbortScan)( PDirEntry pDir, PScanDirCtx pCtx );
71 
89 
90 #if !defined(__DOXYGEN__)
91 
92 extern PFileEntry (__stdcall *NewFileEntry)( LPWIN32_FIND_DATA pfd, PDirEntry pDir, PScanDirCtx pCtx );
93 extern PDirEntry (__stdcall *NewDirEntry)( LPWIN32_FIND_DATA pfd, PDirEntry pDir, PScanDirCtx pCtx );
94 
95 #else // doxygen can't seem to parse the plain function pointers, so subst with mock-ups...
96  extern PFileEntry __stdcall NewFileEntry( LPWIN32_FIND_DATA pfd, PDirEntry pDir, PScanDirCtx pCtx );
99  extern PDirEntry __stdcall NewDirEntry( LPWIN32_FIND_DATA pfd, PDirEntry pDir, PScanDirCtx pCtx );
100 #endif
101 
105 
106 PFileEntry __stdcall __new_FileEntry( LPWIN32_FIND_DATA pfd, PDirEntry pDir, PScanDirCtx pCtx );
107 PDirEntry __stdcall __new_DirEntry( LPWIN32_FIND_DATA pfd, PDirEntry pDir, PScanDirCtx pCtx );
109 
110 //==---------------------------------------------------------------------------
111 
113 #ifndef USE_FILE_ENTRY_SIZEATTR
114 #define USE_FILE_ENTRY_SIZEATTR 1 // Define as 0 if not needed (to save RAM).
115 #endif
116 #define FILEDIR_NEWALLOC 0 // Use custom new/delete for File/DirEntry
117 
119 //==---------------------------------------------------------------------------
123 //==---------------------------------------------------------------------------
124 
125 #pragma pack( push, 4 )
126 class FileEntry : public LIST_ENTRY { // x86: 24(44)B + vtbl*, x64: 48(68)B + vtbl*
127 public:
130  #if USE_FILE_ENTRY_SIZEATTR // Optional std data (20B)...
131  UINT64 Size;
132  DWORD Attr;
133  FILETIME Time;
134  #endif
135 
137  PVOID pData;
138 
139  // Action callbacks.. Default is NULL.
140 
142 
144 
147 
149 
150  // Constructor/Destructor..
151 
152  FileEntry( LPWIN32_FIND_DATA pFd, PDirEntry Dir, PScanDirCtx pCtx = NULL );
153  FileEntry( CSTR Name, PDirEntry Dir, PScanDirCtx pCtx = NULL );
154 
157  //+ N.B: The destructor must be virtual to allow polymorphism via NewFileEntry().
158  //+ This also means you can't equate (this) and (&Flink), and this is not POD.
159 
160  virtual ~FileEntry();
161 
165 
166  CSTR Rename( CSTR newName );
167 
170 
171  UINT GetPathName( TSTR Buffer, UINT BufLen );
172 
175 
176  bool GetFindData( LPWIN32_FIND_DATA pData );
177 
180 
181  bool RemoveFromParent();
182 
186 
187  static bool __stdcall _delete( PLIST_ENTRY Entry, void* pData );
188 
189  #if _DEBUG
190  static bool __stdcall _dump( PLIST_ENTRY Entry, void* pData );
191  #endif
192 
193  #if FILEDIR_NEWALLOC
194  void* operator new( size_t nBytes ) { return mem_Alloc( nBytes ); }
195  void operator delete( void* pBlk ) { mem_Free( pBlk ); }
196  #endif
197 protected:
199  void _init( CSTR name, PDirEntry dir ); // [internal]
201 };
202 #pragma pack( pop )
203 
204 //==---------------------------------------------------------------------------
219 //==---------------------------------------------------------------------------
220 
221 #pragma pack( push, 2 )
222 struct ScanDirCtx // Treat as opaque (unless you're an expert).
223 {
224  // Control data. Treat as R/O.
225 
228  WORD _rsv; //-- DWord align CurrentDir.
229  TCHAR CurrentDir[ MAX_PATH ];
231 
232  // Application data
233 
234  PVOID UserData;
235 
240 
241  PDLinkList ErrorDirs; // Disposed by the d'tor.
242 
243  ScanDirCtx( UINT Depth = 0 );
244  ~ScanDirCtx();
245 };
246 #pragma pack( pop )
247 #ifdef __DOXYGEN__
248 typedef struct ScanDirCtx ScanDirCtx; // Agh.. Recursive re-definition :(
249 #endif
250 
251 extern ScanDirCtx _defScanCtx;
252 
253 #define SCF_FILES 0x0001
254 #define SCF_DIRS 0x0002
255 #ifndef __DOXYGEN__ // NYI (postponed, complicated ramifications)
256 #define SCF_SPLIT 0x0004 // TODO: [uLib:FileDir.h] Split composite basedir entries.
257 #endif
258 
259 #define MAX_DIRECTORY_DEPTH 256
260 
261 //==---------------------------------------------------------------------------
265 //==---------------------------------------------------------------------------
266 
267 #pragma pack( push, 4 )
268 class DirEntry : public LIST_ENTRY { // x86: 28(32)B + vtbl*, x64: 56(60)B + vtbl*
269 public:
270 
272  #if USE_FILE_ENTRY_SIZEATTR
273  DWORD Attr;
274  #endif
277 
279  PVOID pData;
280 
281  // Action callbacks., Default is NULL.
282 
284 
286 
289 
291 
293 
295 
301 
302  static PDirEntry ScanDir(
303  CSTR BaseDir,
304  UINT Depth = 0,
305  PDirEntry Parent = NULL,
306  PUINT pCount = NULL,
307  PScanDirCtx pCtx = NULL
308  );
309 
310  // Constructor/Destructor..
311 
312  DirEntry( LPWIN32_FIND_DATA pFd, PDirEntry pParent = NULL, PScanDirCtx pCtx = NULL );
313  DirEntry( CSTR name, PDirEntry pParent = NULL, PScanDirCtx pCtx = NULL );
314 
317 
318  virtual ~DirEntry();
319 
323 
325 
329 
330  PFileEntry AddFile( LPWIN32_FIND_DATA pFd, PScanDirCtx pCtx );
331 
334 
335  bool RemoveFromParent();
336 
339 
340  UINT GetFullPath( TSTR Buff, UINT BufLen );
341 
344 
345  CSTR GetName();
346 
349 
350  UINT GetBranchCount();
351 
354 
355  UINT GetFileCount();
356 
359 
361 
364 
366 
384 
385  UINT Scan( PScanDirCtx pCtx ); // The actual scanner.
386 
390 
391  static bool __stdcall _delete( PLIST_ENTRY Entry, void* pData );
392 
393  #if _DEBUG
394  static bool __stdcall _dump( PLIST_ENTRY Entry, void* pData );
395  #endif
396 
397  #if FILEDIR_NEWALLOC
398  void* operator new( size_t nBytes ) { return mem_Alloc( nBytes ); }
399  void operator delete( void* pBlk ) { mem_Free( pBlk ); }
400  #endif
401 
402 protected:
404  void _init( CSTR name, PDirEntry parent );
405  static PDirEntry _splitPath( CSTR BaseDir, PDirEntry Parent, PScanDirCtx pCtx );
407 };
408 #pragma pack( pop )
409 
410 #endif//def __cplusplus
411 #endif//ndef __FileDir_h_incl__
413 // EOF
unsigned long DWORD
Definition: Common.h:414
UINT GetFileCount()
Definition: FileDir.cpp:664
PDirEntry __stdcall __new_DirEntry(LPWIN32_FIND_DATA pfd, PDirEntry pDir, PScanDirCtx pCtx)
Definition: FileDir.cpp:44
bool(__stdcall * PFnDirAddAction)(PDirEntry pDir, LPWIN32_FIND_DATA pFD, PScanDirCtx pCtx)
Definition: FileDir.h:55
TCHAR CurrentDir[MAX_PATH]
Definition: FileDir.h:229
#define CSTR
Definition: Common.h:329
virtual ~FileEntry()
Definition: FileDir.cpp:157
unsigned short WORD
Definition: Common.h:413
PDirEntry pDir
Definition: FileDir.cpp:50
UINT Scan(PScanDirCtx pCtx)
Definition: FileDir.cpp:448
UINT GetFullPath(TSTR Buff, UINT BufLen)
Definition: FileDir.cpp:585
void * mem_Alloc(size_t Bytes)
Definition: MemFunc.cpp:33
PDirEntry pDir
Definition: FileDir.h:136
CSTR Rename(CSTR newName)
Definition: FileDir.cpp:171
CSTR Name
Definition: FileDir.h:128
DirEntry(LPWIN32_FIND_DATA pFd, PDirEntry pParent=NULL, PScanDirCtx pCtx=NULL)
Definition: FileDir.cpp:244
#define TSTR
Definition: Common.h:328
ScanDirCtx(UINT Depth=0)
Definition: FileDir.cpp:19
WORD _rsv
Definition: FileDir.h:228
struct _LIST_ENTRY * PLIST_ENTRY
PVOID pData
Definition: FileDir.h:279
bool(__stdcall * PFnFileAddAction)(PFileEntry pFile, LPWIN32_FIND_DATA pFD, PScanDirCtx pCtx)
Definition: FileDir.h:56
static PFnFileAction DelAction
Definition: FileDir.h:148
static bool __stdcall _delete(PLIST_ENTRY Entry, void *pData)
Definition: FileDir.cpp:164
PDLinkList ErrorDirs
Definition: FileDir.h:241
PDirEntry AddSubDir(CSTR pzDir, PScanDirCtx pCtx)
Definition: FileDir.cpp:699
virtual ~DirEntry()
Definition: FileDir.cpp:253
TSTR PathEnd
Definition: FileDir.h:230
bool(__stdcall * PFnDirAction)(PDirEntry pDir, PScanDirCtx pCtx)
Definition: FileDir.h:64
static PDirEntry ScanDir(CSTR BaseDir, UINT Depth=0, PDirEntry Parent=NULL, PUINT pCount=NULL, PScanDirCtx pCtx=NULL)
Definition: FileDir.cpp:329
bool(__stdcall * PFnAbortScan)(PDirEntry pDir, PScanDirCtx pCtx)
Definition: FileDir.h:70
UINT64 GetSizeOfBranch()
Definition: FileDir.cpp:692
void * mem_Free(void *pBlk)
Definition: MemFunc.cpp:124
PVOID pData
Definition: FileDir.h:137
DirEntry * PDirEntry
Definition: FileDir.h:45
~ScanDirCtx()
Definition: FileDir.cpp:30
static bool __stdcall _delete(PLIST_ENTRY Entry, void *pData)
Definition: FileDir.cpp:273
CSTR GetName()
Definition: FileDir.cpp:537
PFileEntry __stdcall NewFileEntry(LPWIN32_FIND_DATA pfd, PDirEntry pDir, PScanDirCtx pCtx)
PDLinkList Files
Definition: FileDir.h:276
UINT NestDepth
Definition: FileDir.h:226
unsigned __int64 UINT64
Definition: Common.h:400
bool(__stdcall * PFnFileAction)(PFileEntry pFile, PScanDirCtx pCtx)
Definition: FileDir.h:65
PDLinkList SubDir
Definition: FileDir.h:275
PVOID UserData
Definition: FileDir.h:234
ScanDirCtx _defScanCtx
Definition: FileDir.cpp:17
CSTR Name
Definition: FileDir.h:271
UINT64 GetSizeOfFiles()
Definition: FileDir.cpp:677
static PFnAbortScan Abort
Definition: FileDir.h:294
UINT MaxNest
Definition: FileDir.h:226
class DLinkList * PDLinkList
Definition: ListFunc.h:143
CSTR LongestCommonPath(CSTR PathName1, CSTR PathName2)
Definition: FileDir.cpp:756
PDirEntry PScanDirCtx pCtx
Definition: FileDir.cpp:51
UINT GetBranchCount()
Definition: FileDir.cpp:642
FileEntry(LPWIN32_FIND_DATA pFd, PDirEntry Dir, PScanDirCtx pCtx=NULL)
Definition: FileDir.cpp:144
WORD Flags
Definition: FileDir.h:227
Common include; Added types, small "ubiquitous" utilities, et c.
FileEntry * PFileEntry
Definition: FileDir.h:42
UINT GetPathName(TSTR Buffer, UINT BufLen)
Definition: FileDir.cpp:193
PDirEntry __stdcall NewDirEntry(LPWIN32_FIND_DATA pfd, PDirEntry pDir, PScanDirCtx pCtx)
PFileEntry AddFile(LPWIN32_FIND_DATA pFd, PScanDirCtx pCtx)
Definition: FileDir.cpp:717
ScanDirCtx * PScanDirCtx
Definition: FileDir.h:46
PDirEntry pParent
Definition: FileDir.h:278
static PFnDirAction DelAction
Definition: FileDir.h:290
bool RemoveFromParent()
Definition: FileDir.cpp:184
bool GetFindData(LPWIN32_FIND_DATA pData)
Definition: FileDir.cpp:210
CSTR Ext
Definition: FileDir.h:129
PFileEntry __stdcall __new_FileEntry(LPWIN32_FIND_DATA pfd, PDirEntry pDir, PScanDirCtx pCtx)
Definition: FileDir.cpp:41
static PFnFileAddAction AddAction
Definition: FileDir.h:143
static PFnDirAddAction AddAction
Definition: FileDir.h:285
bool RemoveFromParent()
Definition: FileDir.cpp:728