TNB Library
TnbFileFinder.h
[詳解]
1#pragma once
11#include "TnbStr.h"
12#include "TnbException.h"
13
14
15
16//TNB Library
17namespace TNB
18{
19
20
21
33{
34public:
37 {
41 };
42
43 #ifndef _TnbDOXYGEN //Document作成用シンボル
45 class iterator
46 {
48 public:
49 iterator(CAbstractFileFinder* pff) : m_pff(pff)
50 {
51 if ( m_pff != NULL && ! m_pff->IsFinding() )
52 {
53 m_pff = NULL;
54 }
55 }
56 const WIN32_FIND_DATA* operator->(void)
57 {
58 if ( m_pff == NULL ) { return NULL; }
59 return &(m_pff->Get());
60 }
61 const WIN32_FIND_DATA& operator*(void)
62 {
63 if ( m_pff == NULL ) { throw CEmptyException(); }
64 return m_pff->Get();
65 }
66 iterator& operator++(void)
67 {
68 if ( m_pff != NULL && ! m_pff->Next() ) { m_pff = NULL; }
69 return *this;
70 }
71 iterator& operator++(int)
72 {
73 return operator++();
74 }
75 bool operator==(const iterator& i) const
76 {
77 return m_pff == i.m_pff;
78 }
79 bool operator!=(const iterator& i) const
80 {
81 return ! operator==(i);
82 }
83 };
84 #endif //_TnbDOXYGEN
85
93 {
94 return iterator(this);
95 }
96
103 {
104 return iterator(NULL);
105 }
106
107
108 //-------------------------
109
110
112 CAbstractFileFinder(void) : m_isFinding(false)
113 {
114 }
115
117 virtual ~CAbstractFileFinder(void) {}
118
123 void Finish(void)
124 {
125 m_isFinding = false;
126 m_strPath.Empty();
127 m_strMask.Empty();
128 OnFinish();
129 }
130
140 bool Start(LPCTSTR lpszPath, LPCTSTR lpszMask = _T("*.*"), EFindType type = ALL)
141 {
142 Finish();
143 CStr s = lpszPath;
144 if ( lpszMask != NULL )
145 {
146 if ( lpszMask[0] != '\\' && lpszMask[0] != '/' )
147 {
148 s += _T("\\");
149 }
150 s += lpszMask;
151 s.Replace(_T("\\\\"), _T("\\"));
152 }
153 if ( OnStart(m_data, s) )
154 {
155 m_isFinding = true;
156 m_strPath = lpszPath;
157 m_strMask = lpszMask;
158 m_type = type;
159 if ( m_IsTarget(m_data, m_type) )
160 {
161 return true;
162 }
163 return Next();
164 }
165 Finish();
166 return false;
167 }
168
176 bool Next(void)
177 {
178 bool r = false;
179 if ( IsFinding() )
180 {
181 while ( true )
182 {
183 r = OnNext(m_data);
184 if ( ! r )
185 {
186 Finish();
187 break;
188 }
189 else if ( m_IsTarget(m_data, m_type) )
190 {
191 break;
192 }
193 }
194 }
195 return r;
196 }
197
203 bool IsFinding(void) const
204 {
205 return m_isFinding;
206 }
207
213 const WIN32_FIND_DATA& Get(void) const
214 {
215 if ( ! IsFinding() ) { throw CEmptyException(); }
216 return m_data;
217 }
218
224 const WIN32_FIND_DATA* operator->(void) const
225 {
226 return &Get();
227 }
228
233 CStr GetFoundName(void) const
234 {
235 if ( ! IsFinding() ) { return CStr(); }
236 CStr s = m_strPath;
237 s += _T("\\");
238 s += Get().cFileName;
239 s.Replace(_T("\\\\"), _T("\\"));
240 s.TrimLeft('\\');
241 return s;
242 }
243
250 bool IsDirectory(void) const
251 {
252 if ( IsFinding() && (Get().dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 )
253 {
254 return true;
255 }
256 return false;
257 }
258#if 0
265 virtual HFINDPUSH Push(void) { return NULL; };
266
272 virtual void Pop(HFINDPUSH h) {};
273#endif
274
275protected:
276
284 virtual bool OnStart(WIN32_FIND_DATA& _data, LPCTSTR lpszName) = 0;
285
294 virtual bool OnNext(WIN32_FIND_DATA& _data) = 0;
295
300 virtual void OnFinish(void) = 0;
301
302private:
303
304 bool m_isFinding;
305 CStr m_strPath;
306 CStr m_strMask;
307 EFindType m_type;
308 WIN32_FIND_DATA m_data;
309
318 bool m_IsTarget(const WIN32_FIND_DATA& data, EFindType type) const
319 {
320 if ( STRLIB::Compare(data.cFileName, _T("..")) == 0 )
321 {
322 return false;
323 }
324 if ( STRLIB::Compare(data.cFileName, _T(".")) == 0 )
325 {
326 return false;
327 }
328 switch ( type )
329 {
330 case FILE_ONLY:
331 if ( (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 )
332 {
333 return false;
334 }
335 break;
336 case DIR_ONLY:
337 if ( (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0 )
338 {
339 return false;
340 }
341 break;
342 default:
343 break;
344 }
345 return true;
346 }
347};
348
349
350
390{
391 DEFSUPER(CAbstractFileFinder);
392public:
393
395 CFileFinder(void) : _super(), m_hFind(INVALID_HANDLE_VALUE)
396 {
397 }
398
400 virtual ~CFileFinder(void)
401 {
402 Finish();
403 }
404
405protected:
406
414 virtual bool OnStart(WIN32_FIND_DATA& _data, LPCTSTR lpszName)
415 {
416 Finish();
417 m_hFind = ::FindFirstFile(lpszName, &_data);
418 return (m_hFind != INVALID_HANDLE_VALUE);
419 }
420
429 virtual bool OnNext(WIN32_FIND_DATA& _data)
430 {
431 if ( m_hFind != INVALID_HANDLE_VALUE )
432 {
433 return !! ::FindNextFile(m_hFind, &_data);
434 }
435 return false;
436 }
437
442 virtual void OnFinish(void)
443 {
444 if ( m_hFind != INVALID_HANDLE_VALUE )
445 {
446 ::FindClose(m_hFind);
447 m_hFind = INVALID_HANDLE_VALUE;
448 }
449 }
450#if 0
457 virtual HFINDPUSH Push(void)
458 {
459 if ( IsDirectory() )
460 {
461 TPushParam* P = new TPushParam;
462 P->hFind = m_hFind;
463 P->strPath = m_strPath;
464 P->data = m_data;
465 //
466 m_strPath += _T("\\");
467 m_strPath += m_data.cFileName;
468 m_hFind = ::FindFirstFile(m_strPath, &m_data);
469 if ( m_hFind != INVALID_HANDLE_VALUE )
470 {
471 if ( ! IsTarget(m_data, m_type) )
472 {
473 if ( Next() )
474 {
475 return P;
476 }
477 }
478 }
479 m_hFind = P->hFind;
480 m_strPath = P->strPath;
481 m_data = P->data;
482 delete P;
483 }
484 return NULL;
485 }
486
492 virtual void Pop(HFINDPUSH h)
493 {
494 if ( h != NULL )
495 {
496 TPushParam* P = reinterpret_cast<TPushParam*>(h);
497 m_hFind = P->hFind;
498 m_strPath = P->strPath;
499 m_data = P->data;
500 delete P;
501 }
502 };
503#endif
504
505private:
506 HANDLE m_hFind;
507 friend class CFileFinderTest;
508};
509
510
511
512}; //TNB
513
514#if 0
515struct WIN32_FIND_DATA
516{
517 DWORD dwFileAttributes;
518 FILETIME ftCreationTime;
519 FILETIME ftLastAccessTime;
520 FILETIME ftLastWriteTime;
521 DWORD nFileSizeHigh;
522 DWORD nFileSizeLow;
523 DWORD dwReserved0;
524 DWORD dwReserved1;
525 TCHAR cFileName[ MAX_PATH ];
526 TCHAR cAlternateFileName[ 14 ];
527} WIN32_FIND_DATA;
528
529enum
530{
531 FILE_ATTRIBUTE_ARCHIVE = 0x00000020,
532 FILE_ATTRIBUTE_COMPRESSED = 0x00000800,
533 FILE_ATTRIBUTE_DEVICE = 0x00000040,
534 FILE_ATTRIBUTE_DIRECTORY = 0x00000010,
535 FILE_ATTRIBUTE_ENCRYPTED = 0x00004000,
536 FILE_ATTRIBUTE_HIDDEN = 0x00000002,
537 FILE_ATTRIBUTE_NORMAL = 0x00000080,
538 FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000,
539 FILE_ATTRIBUTE_OFFLINE = 0x00001000,
540 FILE_ATTRIBUTE_READONLY = 0x00000001,
541 FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400,
542 FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200,
543 FILE_ATTRIBUTE_SYSTEM = 0x00000004,
544 FILE_ATTRIBUTE_TEMPORARY = 0x00000100,
545};
546#endif
例外状態管理関係のヘッダ
文字列管理関係のヘッダ
ファイル検索抽象クラス
Definition: TnbFileFinder.h:33
bool IsDirectory(void) const
[確認] ディレクトリか?
bool Next(void)
[検索] 次検索
iterator begin(void)
[反復] 先頭iterator.
Definition: TnbFileFinder.h:92
const WIN32_FIND_DATA & Get(void) const
[取得] 検索情報取得
virtual void OnFinish(void)=0
[通知] 検索終了通知
CStr GetFoundName(void) const
[取得] 検索ファイル名取得
void Finish(void)
[設定] 検索終了.
virtual bool OnStart(WIN32_FIND_DATA &_data, LPCTSTR lpszName)=0
[通知] 検索開始通知
virtual ~CAbstractFileFinder(void)
デストラクタ
bool Start(LPCTSTR lpszPath, LPCTSTR lpszMask=_T("*.*"), EFindType type=ALL)
[検索] 検索開始
bool IsFinding(void) const
[確認] 検索中確認
CAbstractFileFinder(void)
コンストラクタ
const WIN32_FIND_DATA * operator->(void) const
[取得] 検索情報取得
virtual bool OnNext(WIN32_FIND_DATA &_data)=0
[通知] 次検索通知
iterator end(void)
[反復] 最後iterator.
EFindType
検索タイプ
Definition: TnbFileFinder.h:37
@ DIR_ONLY
ディレクトリのみ
Definition: TnbFileFinder.h:40
@ FILE_ONLY
ファイルのみ
Definition: TnbFileFinder.h:39
[ETC] コピー不可能スーパークラス.
Definition: TnbDef.h:599
取得要素(空き)無し例外
Definition: TnbException.h:107
ファイル検索クラス
virtual ~CFileFinder(void)
デストラクタ
CFileFinder(void)
コンストラクタ
virtual void OnFinish(void)
[通知] 検索終了通知
virtual bool OnStart(WIN32_FIND_DATA &_data, LPCTSTR lpszName)
[通知] 検索開始通知
virtual bool OnNext(WIN32_FIND_DATA &_data)
[通知] 次検索通知
CStrT & TrimLeft(TYP t=' ')
[処理] 先頭から文字をトリム.
Definition: TnbStr.h:968
void Empty(void)
[削除] 空化
Definition: TnbStr.h:197
int Replace(TYP tOld, TYP tNew)
[処理] 文字置換.
Definition: TnbStr.h:1038
ランダムアクセスイテレータ.
int Compare(LPCSTR P1, LPCSTR P2, INT_PTR len=-1, DWORD dwCmpFlags=0)
[比較] 文字列比較(ASCII/SJIS用)
Definition: TnbStrLib.h:135
TNB::CStrT< TCHAR > CStr
文字列クラス
Definition: TnbStr.h:1785
CValueAcceleration operator*(const CValueSpeed &s, const CValueFrequency &f)
[計算] 掛算 (加速度 = 速度 × 周波数) .
TNB Library
Definition: TnbDoxyTitle.txt:2
ファイルタイム型.
Definition: TnbTime.h:893