diff options
| author | Denis Evsyukov <[email protected]> | 2020-02-23 10:31:12 +0300 |
|---|---|---|
| committer | Denis Evsyukov <[email protected]> | 2020-02-23 10:31:12 +0300 |
| commit | 2a3704738a8267ecf947ab73dfc865a835b1ccc7 (patch) | |
| tree | a3350031fa21cde091c63d761e3a9e49eb33584c /src/main.cpp | |
| parent | [+] prefix function (diff) | |
| download | t-2a3704738a8267ecf947ab73dfc865a835b1ccc7.tar.xz t-2a3704738a8267ecf947ab73dfc865a835b1ccc7.zip | |
[+] file functions
Diffstat (limited to 'src/main.cpp')
| -rw-r--r-- | src/main.cpp | 90 |
1 files changed, 76 insertions, 14 deletions
diff --git a/src/main.cpp b/src/main.cpp index 343b9d8..e8f9dc0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,10 +1,54 @@ #include <filesystem> +#include <fstream> #include <unordered_map> #include "functions.hpp" #include "opts/cxxopts.hpp" +namespace fs = std::filesystem; + std::unordered_map<std::string, std::string> tasks = {}; +std::unordered_map<std::string, std::string> tasksDone = {}; +fs::path taskpath, donepath; +std::string taskdir; +std::string taskfile; + +std::string sha256_hash(std::string text) { + std::vector<unsigned char> hash(picosha2::k_digest_size); + picosha2::hash256(text.begin(), text.end(), hash.begin(), hash.end()); + + std::string hex_str = picosha2::bytes_to_hex_string(hash.begin(), hash.end()); + return hex_str; +} + +std::string prefix(std::string hash) { + std::string prefix; + for (size_t i = 1; i <= hash.length(); i++) { + prefix = hash.substr(0, i); + if (tasks.find(prefix) == tasks.end()) + return prefix; + } + return hash; +} + +void readFiles() { + std::cout << "taskfile: " << taskpath << std::endl; + std::ifstream intaskfile(taskpath); + + std::string line; + while (std::getline(intaskfile, line)) { + std::istringstream iss(line); + tasks[sha256_hash(line)] = line; + } + + std::cout << "done taskfile: " << donepath << std::endl; + std::ifstream indonefile(donepath); + + while (std::getline(indonefile, line)) { + std::istringstream iss(line); + tasksDone[sha256_hash(line)] = line; + } +} int main(int argc, char *argv[]) { cxxopts::Options options("MyProgram", "One line description of MyProgram"); @@ -31,6 +75,37 @@ int main(int argc, char *argv[]) { std::cout << options.help() << std::endl; exit(0); } + + if (result.count("taskdir")) { + taskdir = result["taskdir"].as<std::string>(); + } else { + namespace fs = std::filesystem; + taskdir = fs::current_path(); + } + + std::cout << "Current path is " << taskdir << '\n'; + + if (result.count("list")) { + taskfile = result["list"].as<std::string>(); + } else { + taskfile = "tasks"; + } + + std::cout << "Current list is " << taskfile << '\n'; + + taskpath = fs::path(taskdir) / fs::path(taskfile); + donepath = fs::path(taskdir) / fs::path("." + taskfile + ".done"); + + readFiles(); + for (const auto &n : tasks) { + std::cout << "Key:[" << n.first << "] Value:[" << n.second << "]\n"; + } + std::cout << std::endl; + for (const auto &n : tasksDone) { + std::cout << "Key:[" << n.first << "] Value:[" << n.second << "]\n"; + } + std::cout << std::endl; + if (result.count("positional")) { std::string str; auto &v = result["positional"].as<std::vector<std::string>>(); @@ -42,21 +117,8 @@ int main(int argc, char *argv[]) { std::string hash = sha256_hash(str); std::cout << "sha256('" << str << "'): " << hash << std::endl; - // for (const auto &n : tasks) { - // std::cout << "Key:[" << n.first << "] Value:[" << n.second << "]\n"; - // } - - std::string p = prefix(hash, tasks); + std::string p = prefix(hash); std::cout << "prefix: " << p << std::endl; } - std::string taskdir; - if (result.count("taskdir")) { - taskdir = result["taskdir"].as<std::string>(); - } else { - namespace fs = std::filesystem; - taskdir = fs::current_path(); - } - - std::cout << "Current path is " << taskdir << '\n'; return 0; }
\ No newline at end of file |