This is a wrap that queries the number of files and folder from a given directory, also list the folders and files into a vector. I have posted another wrap in this discussion that do the same things and following some really good suggestions I created another this time using filesystem
.
The filesystem I used is this implementation I found on GitHub because the filesystem I tried to use in codeblocks kept throwing errors even the filesystem from GCC 9.2.
I didn't implement a cache mechanism because I don't know how to yet.
wrap.h
#ifndef WRAP_H_INCLUDED
#define WRAP_H_INCLUDED
#include <string>
#include <vector>
#include <ghc/filesystem.hpp>
namespace dir
{
class Wrap
{
private:
Wrap() {}
enum mode {FOLDER = 0, FILE_};
int number_of_entities_in_directory(std::string file_path, mode type_);
std::vector<std::string> entities_in_directory(std::string file_path, mode type_);
int number_of_entities_in_directory_recursively(std::string file_path, mode type_);
std::vector<std::string> entities_in_directory_recursively(std::string file_path, mode type_);
public:
static int number_of_files_in_directory(std::string file_path);
static int number_of_folders_in_directory(std::string file_path);
static void files_in_directory(std::string file_path, std::vector<std::string>& files);
static void folders_in_directory(std::string file_path, std::vector<std::string>& folders);
static int number_of_files_in_directory_recursively(std::string file_path);
static int number_of_folders_in_directory_recursively(std::string file_path);
static void files_in_directory_recursively(std::string file_path, std::vector<std::string>& files);
static void folders_in_directory_recursively(std::string file_path, std::vector<std::string>& folders);
};
}
#endif // WRAP_H_INCLUDED
wrap.cpp
#include "wrap.h"
#include <ghc/filesystem.hpp>
using namespace ghc::filesystem;
using namespace std;
using namespace dir;
int Wrap::number_of_entities_in_directory(string file_path, mode type_)
{
int counter = 0;
string secure_file_path = file_path + "/";
if ( ! exists(secure_file_path))
{
throw runtime_error("directory does not exist");
}
if(ghc::filesystem::is_empty(secure_file_path))
return 0;
directory_iterator it (secure_file_path);
directory_iterator endit;
while(it != endit)
{
if(type_ == FILE_ && is_regular_file(*it))
++counter;
else if(type_ == FOLDER && is_directory(*it))
++counter;
++it;
}
return counter;
}
vector<string> Wrap::entities_in_directory(string file_path, mode type_)
{
vector<string> entities;
string secure_file_path = file_path + "/";
if ( ! exists(secure_file_path))
{
throw runtime_error("directory does not exist");
}
directory_iterator it(secure_file_path);
directory_iterator endit;
if( ! ghc::filesystem::is_empty(secure_file_path))
while (it != endit)
{
if(type_ == FILE_ && is_regular_file(*it))
entities.push_back(it->path().filename());
else if(type_ == FOLDER && is_directory(*it))
entities.push_back(it->path().filename());
++it;
}
return entities;
}
int Wrap::number_of_entities_in_directory_recursively(string file_path,
mode type_)
{
int counter = 0;
string secure_file_path = file_path + "/";
if ( ! exists(secure_file_path))
{
throw runtime_error("directory does not exist");
}
if(ghc::filesystem::is_empty(secure_file_path))
return 0;
recursive_directory_iterator it (secure_file_path);
recursive_directory_iterator endit;
while(it != endit)
{
if(type_ == FILE_ && is_regular_file(*it))
++counter;
else if(type_ == FOLDER && is_directory(*it))
++counter;
++it;
}
return counter;
}
vector<string> Wrap::entities_in_directory_recursively(string file_path,
mode type_)
{
vector<string> entities;
string secure_file_path = file_path + "/";
if ( ! exists(secure_file_path))
{
throw runtime_error("directory does not exist");
}
recursive_directory_iterator it(secure_file_path);
recursive_directory_iterator endit;
if( ! ghc::filesystem::is_empty(secure_file_path))
while (it != endit)
{
if(type_ == FILE_ && is_regular_file(*it))
entities.push_back(it->path().filename());
else if(type_ == FOLDER && is_directory(*it))
entities.push_back(it->path().filename());
++it;
}
return entities;
}
int Wrap::number_of_files_in_directory(string file_path)
{
if(file_path.empty())
return 0;
Wrap dr;
return dr.number_of_entities_in_directory(file_path, FILE_);
}
int Wrap::number_of_folders_in_directory(string file_path)
{
if(file_path.empty())
return 0;
Wrap dr;
return dr.number_of_entities_in_directory(file_path, FOLDER);;
}
void Wrap::files_in_directory(string file_path, vector<std::string>& files)
{
if(file_path.empty())
return;
Wrap dr;
files = dr.entities_in_directory(file_path, FILE_);
}
void Wrap::folders_in_directory(string file_path, vector<std::string>& folders)
{
if(file_path.empty())
return;
Wrap dr;
folders = dr.entities_in_directory(file_path, FOLDER);
}
int Wrap::number_of_files_in_directory_recursively(string file_path)
{
if(file_path.empty())
return 0;
Wrap dr;
return dr.number_of_entities_in_directory_recursively(file_path, FILE_);
}
int Wrap::number_of_folders_in_directory_recursively(string file_path)
{
if(file_path.empty())
return 0;
Wrap dr;
return dr.number_of_entities_in_directory_recursively(file_path, FOLDER);;
}
void Wrap::files_in_directory_recursively(string file_path,
vector<std::string>& files)
{
if(file_path.empty())
return;
Wrap dr;
files = dr.entities_in_directory_recursively(file_path, FILE_);
}
void Wrap::folders_in_directory_recursively(string file_path,
vector<std::string>& folders)
{
if(file_path.empty())
return;
Wrap dr;
folders = dr.entities_in_directory_recursively(file_path, FOLDER);
}
main.cpp
#include <iostream>
#include <vector>
#include <wrap.h>
using namespace dir;
using namespace std;
int main()
{
string path = "D:/Music/";
vector<string> vec;
Wrap::folders_in_directory(path, vec);
for(auto i : vec)
cout << i << endl;
return 0;
}
I created the wrapper as a static library project and added the header file to the include
folder, hence the line:
#include <wrap.h>