From 51a422f161e7e7cc5b1093a12e004165754ec1b9 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 7 Oct 2017 15:45:37 +0200 Subject: Introduce IndexFile class --- src/index.cc | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/index.cc (limited to 'src/index.cc') diff --git a/src/index.cc b/src/index.cc new file mode 100644 index 0000000..c8dc6de --- /dev/null +++ b/src/index.cc @@ -0,0 +1,50 @@ +#include "index.h" + +#include "util/query.h" +#include "util/base64.h" + +#include + + +namespace dictzip { + +IndexFile::Entry parse_from_line(const std::string& line) { + const std::size_t start = line.find_first_of('\t'); + const std::size_t end = line.find_last_of('\t'); + + return IndexFile::Entry( + line.substr(0, start), + base64_decode(line.substr(start + 1, end - (start + 1))), + base64_decode(line.substr(end + 1))); +} + +IndexFile::Entry::Entry(const std::string& line): + IndexFile::Entry{parse_from_line(line)} { } + +IndexFile::Entry::Entry( + const std::string& word, std::size_t offset, std::size_t length): + word(word), + offset(offset), + length(length) { } + +IndexFile::IndexFile(const std::string& path): + path_(path) { } + +std::vector IndexFile::get(const std::string& word) { + const std::vector lines = get_lines_starting_with(this->path_, word); + + std::vector entries; + entries.reserve(lines.size()); + + std::for_each( + lines.begin(), + lines.end(), + [&entries](const std::string& line) { + entries.emplace_back(line); + } + ); + + return entries; +} + +} -- cgit v1.2.3