All Classes Files Functions Variables Enumerations Enumerator Macros
findfiles.cpp
Go to the documentation of this file.
1 
16 #include "findfiles.hpp"
17 
20 {};
21 
24 {};
25 
30 // Check if file should be filtered
31 bool FindFiles::isFiltered( string fileName, ConfigOpts *confOpt )
32 {
33  // Find the location of the file extension
34  unsigned int pos = fileName.find_last_of(".");
35  // Store the files extension
36  string itemExt = fileName.substr(pos+1);
37  // Reset the position var
38  pos = -1;
39  // Find the first extension to filter for
40  pos = confOpt->getOptions( confOpt->FileTypeFilter_opt ).find( confOpt->getOptions( confOpt->DelimiterFilter_opt )[0] );
41  // Initialise old position (oPos) and set to 0
42  int oPos = 0;
43  // While the position is less than file filter length
44  while( pos < confOpt->getOptions( confOpt->FileTypeFilter_opt ).length() )
45  {
46  // Set the new position to the last found position
47  int nPos = pos;
48  // Save the item for comparison
49  string cmpFilter = confOpt->getOptions( confOpt->FileTypeFilter_opt ).substr(oPos, nPos-oPos);
50  // Check if the files extension matches the comparison item
51  if ( itemExt.find( cmpFilter ) < itemExt.length() )
52  {
53  // Return with an error
54  return 1;
55  }
56  // Set the old position to the new position +1
57  oPos = nPos+1;
58  // Set the position to the next found delimeter
59  pos = confOpt->getOptions( confOpt->FileTypeFilter_opt ).find( confOpt->getOptions( confOpt->DelimiterFilter_opt )[0], pos+1 );
60  }
61 
62  // Return with success
63  return 0;
64 };
65 
69 // Detect if input is a file or a directory
71 {
72  // Create an object of type struct stat
73  struct stat readBuffer;
74  // Pass the temporary input to stat and check if it exists
75  if( stat( confOpt->getOptions( confOpt->InputSourceArgument_opt ).c_str(), &readBuffer ) != 0 )
76  return 1;
77  // Check if the temporary input is a file
78  if( S_ISREG( readBuffer.st_mode ) )
79  {
80  // Set file flag for input
81  confOpt->setFlags( confOpt->ReadFromFile_flag );
82  }
83  // Check if the temporary input is a directory
84  else if( S_ISDIR( readBuffer.st_mode ) )
85  {
86  // Set directory flag for input
87  confOpt->setFlags( confOpt->ReadFromDirectory_flag );
88  }
89  // If the temporary input not a directory or a file
90  else
91  {
92  // Exit with an error
93  return 1;
94  }
95 
96  // Exit with success
97  return 0;
98 };
99 
104 // Counts/records files from an input directory 'rDir'
105 bool FindFiles::readDir( string rDir, ConfigOpts *confOpt )
106 {
107  // A pointer to objects of type DIR
108  DIR *p_dir;
109  // Pointer to an object of type struct dirent
110  // Representing the directory entry at the current position in
111  // the directory stream which includes the file name and number
112  struct dirent *p_file;
113  // Create an object of type struct stat
114  struct stat readBuffer;
115  // Check if the directory does not exist
116  if( ( p_dir = opendir( rDir.c_str( ) ) ) == NULL )
117  {
118  cout << "Error: Unable to open " << rDir << endl;
119  // Exit with an error
120  return 1;
121  }
122  // Check if the file does exist
123  while ( ( p_file = readdir( p_dir ) ) != NULL )
124  {
125  // Check if the file name is not . or ..
126  if ( string( p_file->d_name ) != "." && string( p_file->d_name ) != ".." )
127  {
128  // Create a temporary input and store the root dir and file/dir name
129  string tmpInput = rDir + string( SLASH ) + string( p_file->d_name );
130  // Pass the temporary input to stat
131  stat( tmpInput.c_str(), &readBuffer );
132  // Check if the temporary input is a file
133  if( S_ISREG( readBuffer.st_mode ) )
134  {
135  // Check if the file is smaller than maximum episode size
136  if( readBuffer.st_size < atof( confOpt->getOptions( confOpt->MaxEpisodeSize_opt ).c_str() ) )
137  {
138  // Check if the file should be filtered
139  if( !isFiltered( string( p_file->d_name ), confOpt ) )
140  {
141  // Store the file in the list array
142  confOpt->v_fileList.push_back ( tmpInput );
143  }
144  }
145  }
146  // Check if the temporary input is a directory
147  else if( S_ISDIR( readBuffer.st_mode ) )
148  {
149  // Check if the recurse flag is set
150  if( confOpt->getFlags( confOpt->Recurse_flag ) )
151  {
152  // Open the new directory
153  readDir( tmpInput, confOpt );
154  }
155  }
156  }
157  }
158  // Go back to the begining of the directory
159  closedir( p_dir );
160 
161  // Return with success
162  return 0;
163 };
164 
168 // Counts/records files from an input file
170 {
171  // Create a input file stream object
172  ifstream infile;
173  // Open the file
174  infile.open( confOpt->getOptions( confOpt->InputSourceArgument_opt ).c_str() );
175  //Check if stream is good for i/o operations
176  if( !infile.good() )
177  {
178  cout << "Error: Unable to open " << confOpt->getOptions( confOpt->InputSourceArgument_opt ) << endl;
179  // Return with an error
180  return 1;
181  }
182  // Read until EOF
183  while ( !infile.eof() )
184  {
185  // Create a temporary string to hold each line
186  string tmpLine;
187  // Read a line into temp
188  getline( infile , tmpLine );
189  // Check if the line contains more than 2 characters
190  if ( tmpLine.length() > 2 )
191  {
192  // Count only lines with info
193  confOpt->v_fileList.push_back ( tmpLine );
194  }
195  }
196  // Clear the EOF flag
197  infile.clear();
198  // Close the file
199  infile.close();
200 
201  // Return with success
202  return 0;
203 };
static bool getFlags(flagValue)
Retrieves the user configuration flags from an array.
Definition: mainPublic.cpp:280
max size of a episode, in bytes
Definition: options.hpp:69
static bool readDir(string, ConfigOpts *)
Counts and records files from an input directory.
Definition: findfiles.cpp:105
file types that should be filtered
Definition: options.hpp:73
read input from a directory
Definition: options.hpp:97
FindFiles()
Default constructor.
Definition: findfiles.cpp:19
#define SLASH
Definition: options.hpp:32
static string getOptions(optionValue)
Retrieves the user configuration options from an array.
Definition: mainPublic.cpp:129
static bool readFile(ConfigOpts *)
Counts and records files from an input file.
Definition: findfiles.cpp:169
read input from a file
Definition: options.hpp:96
static bool readInput(ConfigOpts *)
Detect if input is a file or a directory.
Definition: findfiles.cpp:70
the delimters used to parse the file filter
Definition: options.hpp:74
recurse directories
Definition: options.hpp:98
static vector< string > v_fileList
A vector that contains the list of input files to be parsed.
Definition: options.hpp:142
The FindFiles class auto-detects whether input is a file or directory.
input directory/file from args
Definition: options.hpp:79
Stores all config options, settings, and flags for clerk.
Definition: options.hpp:60
static void setFlags(flagValue)
Sets the user configuration flags into an array.
Definition: mainPublic.cpp:137
~FindFiles()
Default destructor.
Definition: findfiles.cpp:23
static bool isFiltered(string, ConfigOpts *)
Check if file should be filtered.
Definition: findfiles.cpp:31