masters-thesis/op-finder-lib/src/OperationStorage.cpp
Erki df8e5c5d09
All checks were successful
continuous-integration/drone/push Build is passing
Add unit testing for expected output
2021-03-10 01:12:47 +02:00

60 lines
1.2 KiB
C++

//
// Created by erki on 28.02.21.
//
#include "OperationStorage.hpp"
#include <fstream>
#include <filesystem>
#include <llvm/Support/CommandLine.h>
OperationStorage::~OperationStorage()
{ }
void OperationStorage::enablePrettyPrint()
{
_pretty_print = true;
}
void OperationStorage::toStream(std::ostream& stream)
{
nlohmann::json json = _operations;
if (_pretty_print)
stream << std::setw(4) << json;
else
stream << json;
}
void OperationStorage::toFile(const std::string& output_filename)
{
std::ofstream file(output_filename);
toStream(file);
}
void OperationStorage::pushOperation(const std::string& original_filename, OperationLog&& op)
{
const std::string filename = _convertFilepath(original_filename);
auto it = _operations.find(filename);
if (it == _operations.end())
it = _operations.emplace(filename, std::vector<OperationLog>()).first;
it->second.emplace_back(std::move(op));
}
const std::unordered_map<std::string, std::vector<OperationLog>>& OperationStorage::getOperations() const
{
return _operations;
}
std::string OperationStorage::_convertFilepath(const std::string& original)
{
const std::filesystem::path path = original;
return path.filename().string();
}