EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DirWalker.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Zhur
24 */
25 
26 #include "eve-core.h"
27 
28 #include "utils/DirWalker.h"
29 
31 #ifdef HAVE_WINDOWS_H
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 }
43 
45 {
46  CloseDir();
47 }
48 
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 }
57 
58 bool DirWalker::OpenDir( const char* dir, const char* suffix )
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 }
83 
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 }
100 
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 }
bool OpenDir(const char *dir, const char *suffix="")
Opens directory for listing.
Definition: DirWalker.cpp:58
const char * currentFileName()
Definition: DirWalker.cpp:49
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
bool NextFile()
Iterates over to next file in directory.
Definition: DirWalker.cpp:101