Change 'File' concept to a 'Resource' abstract concept

This commit is contained in:
Vyn 2024-05-09 14:39:10 +02:00
parent 7eb54cddce
commit ca34562a1c
23 changed files with 447 additions and 351 deletions

View file

@ -6,7 +6,6 @@
#include "TodoMd.h"
#include "TaskItem.h"
#include "core/TasksFile.h"
#include "cpp-utils/debug.h"
#include "utils.h"
#include <fstream>
@ -17,50 +16,6 @@
namespace mirai
{
std::unique_ptr<TasksFile> TodoMdFormat::readFile(const std::string &path)
{
cpputils::debug::Timer readMdFormatDuration;
std::ifstream file(path);
if (!file.is_open()) {
throw std::runtime_error("todo.txt file not found");
}
std::vector<TaskItem> taskItems;
std::string line;
if (!std::getline(file, line) || line.substr(0, 2) != "# ") {
std::cerr << "Couldn't find the task list name" << std::endl;
}
std::string name = line.substr(2);
std::string currentDate = "";
auto tasksFile = std::make_unique<TasksFile>(
TasksFileConstructor{.name = name, .path = path, .tasks = taskItems}
);
cpputils::debug::Timer stringToTaskDuration;
stringToTaskDuration.reset();
cpputils::debug::Timer gelinesDuration;
while (std::getline(file, line)) {
if (std::string_view{line.data(), 3} == "## ") {
currentDate = line.substr(3);
} else if (std::string_view{line.data(), 5} == "- [ ]" || std::string_view{line.data(), 5} == "- [X]") {
stringToTaskDuration.start();
TaskItemData taskItemData = stringToTask(line, currentDate);
stringToTaskDuration.stop();
taskItemData.date = currentDate;
tasksFile->addTask(taskItemData);
}
}
gelinesDuration.printTimeElapsed("getlinesDuration");
stringToTaskDuration.printTimeElapsed("stringToTaskDuration");
file.close();
tasksFile->setDirty(false);
readMdFormatDuration.printTimeElapsed("Reading MD File duration of " + path);
return tasksFile;
}
Tags TodoMdFormat::extractTagsFromMetadata(std::string metadata)
{
Tags tags;
@ -77,25 +32,6 @@ Tags TodoMdFormat::extractTagsFromMetadata(std::string metadata)
return tags;
}
void TodoMdFormat::writeFile(TasksFile &tasks)
{
std::ofstream file(tasks.getPath());
if (!file.is_open()) {
throw std::runtime_error("can't create todo.txt");
}
std::string currentDate = "";
file << "# " << tasks.getName() << "\n";
for (const auto &task : tasks.getTasks()) {
if (currentDate != task->getDate()) {
currentDate = task->getDate();
file << "\n## " << (task->getDate() != "" ? task->getDate() : "No date") << "\n\n";
}
file << taskToString(*task) << '\n';
}
file.close();
}
std::string TodoMdFormat::fieldWithSpace(const std::string &field)
{
if (field.length() == 0) {