Bar raiser round
/*
Implement Linux find command as an API. The API will support finding files that:
* Files that have a given size requirement.
* Files with a certain naming pattern.
Focus on 2 uses cases at first:
* Find all files over 5 MB somewhere under a directory.
* Find all XML files somewhere under a directory.
* Create a library that lets me do this easily.
Keep in mind that these are just 2 uses cases and that the library should be flexible.
*/
#include<bits/stdc++.h>
namespace fs=std::filesystem;
using namespace std;
//main api that accepts the directory and the filter condition
vector<fs::path> findfiles(const fs::path& root, std::function<bool(const fs::directory_entry&)> filter ){
vector<fs::path> result;
for(const auto& entry: fs::recursive_directory_iterator(root)){ // iterate recursively in the root to check for the condition
if(entry.is_regular_file() && filter(entry)){
result.push_back(entry.path());
}
}
return result;
}
int main(){
fs::path search_root=".";
// find all files that are over 5 mb
const unitmax_t Five=5*1024*1024;
auto big_files= find_files(search_root,[](const fs::directory_entry& entry){
return entry.file_size()>Five;
});
//find all the xml files
auto xml_files = find_files(search_root,[](const fs::directory_entry& entry){
return entry.path().extension() == ".xml";
});
// find all the files that follow both the above conditions
}
// this was my solution in the bar raiser round, the questions that he asked was that
/*
this approach clearly violates Open-closed principle and what if we want to make
new conditions tomorrow,
the solution i gave was that we have a parent class named conditions and then inherit
the methods into the child class each with its own condition, I mentioned that
I will use strategy design pattern to write the functionality of all the different
conditions and he looked happy.
*/