mirror of
https://codeberg.org/vyn/mirai.git
synced 2025-07-03 01:33:19 +00:00
Add helper lib for strings and vectors
This commit is contained in:
parent
6ef9740db9
commit
2bb7fcfcc6
10 changed files with 168 additions and 123 deletions
|
@ -7,89 +7,89 @@
|
|||
#ifndef MIRAI_TODOTXT_H
|
||||
#define MIRAI_TODOTXT_H
|
||||
|
||||
#include "TaskItem.h"
|
||||
#include "utils.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <regex>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
/*#include "TaskItem.h"*/
|
||||
/*#include "utils.h"*/
|
||||
/*#include <fstream>*/
|
||||
/*#include <iostream>*/
|
||||
/*#include <iterator>*/
|
||||
/*#include <memory>*/
|
||||
/*#include <regex>*/
|
||||
/*#include <stdexcept>*/
|
||||
/*#include <string>*/
|
||||
/*#include <vector>*/
|
||||
|
||||
namespace mirai {
|
||||
/*namespace mirai {*/
|
||||
|
||||
class TodoTxtFormat {
|
||||
public:
|
||||
/*class TodoTxtFormat {*/
|
||||
/*public:*/
|
||||
|
||||
static TaskList readFile() {
|
||||
std::ifstream file("../newqml/todo.txt");
|
||||
if (!file.is_open()) {
|
||||
throw std::runtime_error("todo.txt file not found");
|
||||
}
|
||||
std::vector<TaskItem> taskItems;
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
auto taskItem = parseLine(line);
|
||||
taskItems.push_back(taskItem);
|
||||
}
|
||||
file.close();
|
||||
TaskList tasks({
|
||||
.tasks = taskItems
|
||||
});
|
||||
return tasks;
|
||||
}
|
||||
/*static TaskList readFile() {*/
|
||||
/*std::ifstream file("../newqml/todo.txt");*/
|
||||
/*if (!file.is_open()) {*/
|
||||
/*throw std::runtime_error("todo.txt file not found");*/
|
||||
/*}*/
|
||||
/*std::vector<TaskItem> taskItems;*/
|
||||
/*std::string line;*/
|
||||
/*while (std::getline(file, line)) {*/
|
||||
/*auto taskItem = parseLine(line);*/
|
||||
/*taskItems.push_back(taskItem);*/
|
||||
/*}*/
|
||||
/*file.close();*/
|
||||
/*TaskList tasks({*/
|
||||
/*.tasks = taskItems*/
|
||||
/*});*/
|
||||
/*return tasks;*/
|
||||
/*}*/
|
||||
|
||||
static void writeFile(TaskList& tasks) {
|
||||
std::ofstream file("../newqml/todo.txt");
|
||||
if (!file.is_open()) {
|
||||
throw std::runtime_error("can't create todo.txt");
|
||||
}
|
||||
for (const auto& task : tasks.getTasks()) {
|
||||
file << fieldWithSpace(task->state == DONE ? "x" : task->priority);
|
||||
file << fieldWithSpace(task->state == DONE ? task->getDate() : "");
|
||||
file << fieldWithSpace(task->getCreationDate());
|
||||
file << task->getText() << '\n';
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
/*static void writeFile(TaskList& tasks) {*/
|
||||
/*std::ofstream file("../newqml/todo.txt");*/
|
||||
/*if (!file.is_open()) {*/
|
||||
/*throw std::runtime_error("can't create todo.txt");*/
|
||||
/*}*/
|
||||
/*for (const auto& task : tasks.getTasks()) {*/
|
||||
/*file << fieldWithSpace(task->state == DONE ? "x" : task->priority);*/
|
||||
/*file << fieldWithSpace(task->state == DONE ? task->getDate() : "");*/
|
||||
/*file << fieldWithSpace(task->getCreationDate());*/
|
||||
/*file << task->getText() << '\n';*/
|
||||
/*}*/
|
||||
/*file.close();*/
|
||||
/*}*/
|
||||
|
||||
private:
|
||||
/*private:*/
|
||||
|
||||
static std::string fieldWithSpace(const std::string& field) {
|
||||
if (field.length() == 0)
|
||||
return "";
|
||||
return (field + " ");
|
||||
}
|
||||
/*static std::string fieldWithSpace(const std::string& field) {*/
|
||||
/*if (field.length() == 0)*/
|
||||
/*return "";*/
|
||||
/*return (field + " ");*/
|
||||
/*}*/
|
||||
|
||||
static TaskItem parseLine(const std::string& line) {
|
||||
std::smatch matches;
|
||||
std::regex regex("(^x )?(\\([A-Z]\\) )?([0-9]{4}-[0-9]{2}-[0-9]{2} )?([0-9]{4}-[0-9]{2}-[0-9]{2} )?(.*)");
|
||||
std::regex_match(line, matches, regex);
|
||||
/*static TaskItem parseLine(const std::string& line) {*/
|
||||
/*std::smatch matches;*/
|
||||
/*std::regex regex("(^x )?(\\([A-Z]\\) )?([0-9]{4}-[0-9]{2}-[0-9]{2} )?([0-9]{4}-[0-9]{2}-[0-9]{2} )?(.*)");*/
|
||||
/*std::regex_match(line, matches, regex);*/
|
||||
|
||||
for (size_t i = 0; i < matches.size(); ++i) {
|
||||
std::ssub_match sub_match = matches[i];
|
||||
std::string piece = sub_match.str();
|
||||
}
|
||||
/*for (size_t i = 0; i < matches.size(); ++i) {*/
|
||||
/*std::ssub_match sub_match = matches[i];*/
|
||||
/*std::string piece = sub_match.str();*/
|
||||
/*}*/
|
||||
|
||||
const TaskItemState taskState = trim_copy(matches[1].str()) == "x" ? DONE : TODO;
|
||||
const std::string priority = trim_copy(matches[2].str());
|
||||
const std::string firstDate = trim_copy(matches[3].str());
|
||||
const std::string secondDate = trim_copy(matches[4].str());
|
||||
const std::string text = trim_copy(matches[5].str());
|
||||
/*const TaskItemState taskState = trim_copy(matches[1].str()) == "x" ? DONE : TODO;*/
|
||||
/*const std::string priority = trim_copy(matches[2].str());*/
|
||||
/*const std::string firstDate = trim_copy(matches[3].str());*/
|
||||
/*const std::string secondDate = trim_copy(matches[4].str());*/
|
||||
/*const std::string text = trim_copy(matches[5].str());*/
|
||||
|
||||
const std::string date = taskState == DONE ? firstDate : "";
|
||||
/*const std::string date = taskState == DONE ? firstDate : "";*/
|
||||
|
||||
|
||||
|
||||
return {
|
||||
.text = text,
|
||||
.state = taskState,
|
||||
.date = date,
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
/*return {*/
|
||||
/*.text = text,*/
|
||||
/*.state = taskState,*/
|
||||
/*.date = date,*/
|
||||
/*};*/
|
||||
/*}*/
|
||||
/*};*/
|
||||
/*}*/
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue