Artifact 16c2c7129051abe489be828e7179368640c67aa7
1 //--- K.I.LIB ---
2 // kl_find.h : FindFirstFile wrapper
3
4 #include "stdafx.h"
5 #include "kilib.h"
6
7 #define isDots(p) (*p=='.' && (p[1]=='\0' || (p[1]=='.' && p[2]=='\0')))
8
9 bool kiFindFile::findfirst( const char* wild, WIN32_FIND_DATA* pfd )
10 {
11 HANDLE xh = ::FindFirstFile( wild, pfd );
12 if( xh==INVALID_HANDLE_VALUE )
13 return false;
14 while( isDots(pfd->cFileName) )
15 if( !::FindNextFile( xh, pfd ) )
16 {
17 ::FindClose( xh );
18 return false;
19 }
20 ::FindClose( xh );
21 return true;
22 }
23
24 void kiFindFile::close()
25 {
26 first=true;
27 if( h!=INVALID_HANDLE_VALUE )
28 {
29 ::FindClose( h ), h=INVALID_HANDLE_VALUE;
30 }
31 }
32
33 bool kiFindFile::begin( const char* wild )
34 {
35 close();
36
37 h = ::FindFirstFile( wild, &fd );
38 if( h==INVALID_HANDLE_VALUE )
39 return false;
40 while( isDots(fd.cFileName) )
41 if( !::FindNextFile( h, &fd ) )
42 {
43 close();
44 return false;
45 }
46 return true;
47 }
48
49 bool kiFindFile::next( WIN32_FIND_DATA* pfd )
50 {
51 if( h==INVALID_HANDLE_VALUE )
52 return false;
53 if( first )
54 {
55 first = false;
56 ki_memcpy( pfd, &fd, sizeof(fd) );
57 return true;
58 }
59 if( !::FindNextFile( h, pfd ) )
60 return false;
61 while( isDots(fd.cFileName) )
62 if( !::FindNextFile( h, pfd ) )
63 return false;
64 return true;
65 }
66
67 #undef isDots