aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt1
-rw-r--r--example.cc19
-rw-r--r--src/archive.cc26
-rw-r--r--src/archive.h19
-rw-r--r--src/index.cc4
5 files changed, 52 insertions, 17 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6c96f1d..61dc3a5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,6 +14,7 @@ add_library(
DictzipQuery
SHARED
src/index.cc
+ src/archive.cc
src/util/base64.cc
src/util/query.cc
src/istream/stream.cc
diff --git a/example.cc b/example.cc
index 3727434..3ce640c 100644
--- a/example.cc
+++ b/example.cc
@@ -1,31 +1,18 @@
#include "index.h"
-#include "istream/stream.h"
+#include "archive.h"
#include <string>
#include <iostream>
-std::string get(const std::string& path, std::size_t offset, std::size_t length) {
- dictzip::Istream stream(path.c_str());
-
- std::string result;
- result.resize(length);
-
- stream.seekg(offset);
- stream.read(&result[0], length);
-
- return result;
-}
-
int main(int argc, char** argv) {
if ( argc != 2 ) {
std::cerr << "Empty query." << std::endl;
} else {
dictzip::IndexFile index("gcide.index");
+ dictzip::ArchiveFile archive("gcide.dict.dz");
- // Get index entries of requested word definitions
for ( auto& entry : index.get(argv[1]) ) {
- // Print the GCIDE definition
- std::cout << get("gcide.dict.dz", entry.offset, entry.length) << std::endl;
+ std::cout << archive.get(entry) << std::endl;
}
}
}
diff --git a/src/archive.cc b/src/archive.cc
new file mode 100644
index 0000000..6a97bdf
--- /dev/null
+++ b/src/archive.cc
@@ -0,0 +1,26 @@
+#include "archive.h"
+
+#include "istream/stream.h"
+
+namespace dictzip {
+
+ArchiveFile::ArchiveFile(const std::string& path):
+ path_(path) { }
+
+std::string ArchiveFile::get(std::size_t offset, std::size_t length) const {
+ Istream stream(this->path_.c_str());
+
+ std::string result;
+ result.resize(length);
+
+ stream.seekg(offset);
+ stream.read(&result[0], length);
+
+ return result;
+}
+
+std::string ArchiveFile::get(const IndexFile::Entry& entry) const {
+ return this->get(entry.offset, entry.length);
+}
+
+}
diff --git a/src/archive.h b/src/archive.h
new file mode 100644
index 0000000..fcf19b7
--- /dev/null
+++ b/src/archive.h
@@ -0,0 +1,19 @@
+#pragma once
+
+#include "index.h"
+
+namespace dictzip {
+
+class ArchiveFile {
+public:
+ ArchiveFile(const std::string& path);
+
+ std::string get(std::size_t offset, std::size_t length) const;
+ std::string get(const IndexFile::Entry& entry) const;
+
+private:
+ const std::string path_;
+
+};
+
+}
diff --git a/src/index.cc b/src/index.cc
index c8dc6de..b22ae05 100644
--- a/src/index.cc
+++ b/src/index.cc
@@ -5,7 +5,6 @@
#include <algorithm>
-
namespace dictzip {
IndexFile::Entry parse_from_line(const std::string& line) {
@@ -13,8 +12,11 @@ IndexFile::Entry parse_from_line(const std::string& line) {
const std::size_t end = line.find_last_of('\t');
return IndexFile::Entry(
+ // word
line.substr(0, start),
+ // offset
base64_decode(line.substr(start + 1, end - (start + 1))),
+ // length
base64_decode(line.substr(end + 1)));
}