aboutsummaryrefslogtreecommitdiff
path: root/src/index.cc
blob: c8dc6de7357e9af482b61a4d8aa8baab20f27de2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "index.h"

#include "util/query.h"
#include "util/base64.h"

#include <algorithm>


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::Entry> IndexFile::get(const std::string& word) {
	const std::vector<std::string> lines = get_lines_starting_with(this->path_, word);

	std::vector<Entry> entries;
	entries.reserve(lines.size());

	std::for_each(
		lines.begin(),
		lines.end(),
		[&entries](const std::string& line) {
			entries.emplace_back(line);
		}
	);

	return entries;
}

}