Skip to content

Instantly share code, notes, and snippets.

@dylanliuh2o
Last active April 11, 2024 09:55
Show Gist options
  • Save dylanliuh2o/3b321e1b07f074bab1ec0228df25acce to your computer and use it in GitHub Desktop.
Save dylanliuh2o/3b321e1b07f074bab1ec0228df25acce to your computer and use it in GitHub Desktop.
Central Directory Manager
#include <string>
#include <filesystem>
#include <map>
namespace fs = std::filesystem;
const std::map<std::string, fs::path> dirs {
{ "WorkDir", fs::path("work_dir") },
{ "SMT-FileDir", fs::path("work_dir/smt") },
{ "Logs", fs::path("work_dir/log") },
{ "ErrorLog", fs::path("work_dir/log/error") },
{ "CoverageResult", fs::path("work_dir/coverage") },
{ "AssertionResult", fs::path("work_dir/assertion") },
} ;
class DirManager
{
public:
DirManager(const std::map<std::string, fs::path>& init_dirs = {}, const fs::path& cwd = fs::current_path())
: _dirs(init_dirs), _cwd(cwd) { }
~DirManager() = default ;
private:
DirManager(const DirManager&) = delete ;
DirManager(const DirManager&&) = delete ;
public:
bool SetCurrentDir(const fs::path& cwd) ;
fs::path GetPath(const std::string& name) ;
bool AddPath(const std::string& name, const fs::path& path, bool force_add = false) ;
fs::path RemovePath(const std::string& name) ;
private:
std::map<std::string, fs::path> _dirs ; // key: path description value: path
fs::path _cwd ;
} ;
bool
DirManager::SetCurrentDir(const fs::path& cwd)
{
if (!fs::exists(cwd)) { return false ; }
_cwd = cwd ;
return true ;
}
fs::path
DirManager::GetPath(const std::string& name)
{
if (_dirs.count(name)) {
// join path
fs::path path = _cwd / _dirs[name] ;
if (fs::exists(path) || fs::create_directories(path)) {
return path ;
}
}
return fs::path() ;
}
bool
DirManager::AddPath(const std::string& name, const fs::path& path, bool force_add /* = false */)
{
if (!_dirs.count(name) || force_add) {
_dirs[name] = path ;
return true ;
}
return false ;
}
fs::path
DirManager::RemovePath(const std::string& name)
{
if (_dirs.count(name)) {
fs::path path = _dirs[name] ;
_dirs.erase(name) ;
return path ;
}
return fs::path() ;
}
DirManager global_dir_manager(dirs) ;
void generate_logs()
{
fs::path log_path = global_dir_manager.GetPath("Logs") ;
fs::path error_log_path = global_dir_manager.GetPath("ErrorLog") ;
if (!log_path.empty()) {
// do sth.
}
if (!error_log_path.empty()) {
// do sth.
}
}
void generate_assertion()
{
fs::path assert_res_path = global_dir_manager.GetPath("AssertionResult") ;
if (!assert_res_path.empty()) {
// do sth.
}
}
void generate_coverage()
{
fs::path cov_res_path = global_dir_manager.GetPath("CoverageResult") ;
if (!cov_res_path.empty()) {
// do sth.
}
}
void generate_smtfile()
{
fs::path smt_file_path = global_dir_manager.GetPath("SMT-FileDir") ;
if (!smt_file_path.empty()) {
// do sth.
}
}
int main()
{
generate_logs() ;
generate_assertion() ;
generate_coverage() ;
generate_smtfile() ;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment