EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DirWalker Class Reference

Simple class for directory listing. More...

#include "DirWalker.h"

Collaboration diagram for DirWalker:

Public Member Functions

 DirWalker ()
 
 ~DirWalker ()
 
const char * currentFileName ()
 
bool OpenDir (const char *dir, const char *suffix="")
 Opens directory for listing. More...
 
void CloseDir ()
 Closes opened directory. More...
 
bool NextFile ()
 Iterates over to next file in directory. More...
 

Protected Attributes

DIR * mDir
 
struct dirent * mFile
 
std::string mSuffix
 

Detailed Description

Simple class for directory listing.

Note
Capt: Even that I don't always like boost, it has a very useful class called FileSystem which does this stuff very crossplatform.
Author
Zhur, Bloody.Rabbit

Definition at line 38 of file DirWalker.h.

Constructor & Destructor Documentation

DirWalker::DirWalker ( )

Definition at line 30 of file DirWalker.cpp.

32 : mFind( INVALID_HANDLE_VALUE ),
33  mFindData(),
34  mValid( false ),
35  mFirst( false )
36 #else /* !HAVE_WINDOWS_H */
37 : mDir( NULL ),
38  mFile( NULL ),
39  mSuffix()
40 #endif /* !HAVE_WINDOWS_H */
41 {
42 }
std::string mSuffix
Definition: DirWalker.h:82
DIR * mDir
Definition: DirWalker.h:80
struct dirent * mFile
Definition: DirWalker.h:81
DirWalker::~DirWalker ( )

Definition at line 44 of file DirWalker.cpp.

References CloseDir().

45 {
46  CloseDir();
47 }
void CloseDir()
Closes opened directory.
Definition: DirWalker.cpp:84

Here is the call graph for this function:

Member Function Documentation

void DirWalker::CloseDir ( )

Closes opened directory.

Definition at line 84 of file DirWalker.cpp.

References mDir, mFile, and mSuffix.

Referenced by OpenDir(), and ~DirWalker().

85 {
86 #ifdef HAVE_WINDOWS_H
87  if( INVALID_HANDLE_VALUE != mFind )
88  ::FindClose( mFind );
89 
90  mValid = false;
91  mFirst = false;
92 #else /* !HAVE_WINDOWS_H */
93  if( NULL != mDir )
94  ::closedir( mDir );
95 
96  mFile = NULL;
97  mSuffix.clear();
98 #endif /* !HAVE_WINDOWS_H */
99 }
std::string mSuffix
Definition: DirWalker.h:82
DIR * mDir
Definition: DirWalker.h:80
struct dirent * mFile
Definition: DirWalker.h:81

Here is the caller graph for this function:

const char * DirWalker::currentFileName ( )
Returns
Current file name.

Definition at line 49 of file DirWalker.cpp.

References mFile.

Referenced by NextFile().

50 {
51 #ifdef HAVE_WINDOWS_H
52  return mValid ? mFindData.cFileName : NULL;
53 #else /* !HAVE_WINDOWS_H */
54  return NULL != mFile ? mFile->d_name : NULL;
55 #endif /* !HAVE_WINDOWS_H */
56 }
struct dirent * mFile
Definition: DirWalker.h:81

Here is the caller graph for this function:

bool DirWalker::NextFile ( )

Iterates over to next file in directory.

Note
This function must be called after OpenDir() before calling currentFileName().
Return values
trueIteration successful.
falseIteration failed; most likely there are no more files.

Definition at line 101 of file DirWalker.cpp.

References currentFileName(), mDir, mFile, and mSuffix.

102 {
103 #ifdef HAVE_WINDOWS_H
104  if( INVALID_HANDLE_VALUE == mFind )
105  return false;
106 
107  if( mFirst )
108  mFirst = false;
109  else
110  mValid = ( TRUE == ::FindNextFile( mFind, &mFindData ) );
111 
112  return mValid;
113 #else /* !HAVE_WINDOWS_H */
114  if( NULL == mDir )
115  return false;
116 
117  // We need to do the suffix matching here
118  while( NULL != ( mFile = ::readdir( mDir ) ) )
119  {
120  const std::string filename( currentFileName() );
121  const size_t pos = filename.rfind( mSuffix );
122 
123  if( std::string::npos == pos )
124  continue;
125  else if( filename.length() != ( pos + mSuffix.length() ) )
126  continue;
127  else
128  return true;
129  }
130 
131  return false;
132 #endif /* !HAVE_WINDOWS_H */
133 }
const char * currentFileName()
Definition: DirWalker.cpp:49
std::string mSuffix
Definition: DirWalker.h:82
DIR * mDir
Definition: DirWalker.h:80
struct dirent * mFile
Definition: DirWalker.h:81

Here is the call graph for this function:

bool DirWalker::OpenDir ( const char *  dir,
const char *  suffix = "" 
)

Opens directory for listing.

Parameters
[in]dirPath to directory; must NOT end with backslash.
[in]suffixOnly files with this suffix are matched.
Return values
trueDirectory opened successfully.
Returns
false Failed to open directory.

Definition at line 58 of file DirWalker.cpp.

References CloseDir(), mDir, mFile, and mSuffix.

59 {
60  CloseDir();
61 
62 #ifdef HAVE_WINDOWS_H
63  // Let Windows do the suffix matching
64  std::string d( dir );
65  d += "/*";
66  d += suffix;
67 
68  mFind = ::FindFirstFile( d.c_str(), &mFindData );
69  mValid = ( INVALID_HANDLE_VALUE != mFind );
70  mFirst = true;
71 
72  // If no file was found, see if at least
73  // the directory was found ...
74  return ( mValid ? true : ( ERROR_PATH_NOT_FOUND != ::GetLastError() ) );
75 #else /* !HAVE_WINDOWS_H */
76  mDir = ::opendir( dir );
77  mFile = NULL;
78  mSuffix = suffix;
79 
80  return ( NULL != mDir );
81 #endif /* !HAVE_WINDOWS_H */
82 }
std::string mSuffix
Definition: DirWalker.h:82
DIR * mDir
Definition: DirWalker.h:80
void CloseDir()
Closes opened directory.
Definition: DirWalker.cpp:84
struct dirent * mFile
Definition: DirWalker.h:81

Here is the call graph for this function:

Member Data Documentation

DIR* DirWalker::mDir
protected

Definition at line 80 of file DirWalker.h.

Referenced by CloseDir(), NextFile(), and OpenDir().

struct dirent* DirWalker::mFile
protected

Definition at line 81 of file DirWalker.h.

Referenced by CloseDir(), currentFileName(), NextFile(), and OpenDir().

std::string DirWalker::mSuffix
protected

Definition at line 82 of file DirWalker.h.

Referenced by CloseDir(), NextFile(), and OpenDir().


The documentation for this class was generated from the following files: