aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt2
-rw-r--r--example.cc23
-rw-r--r--src/istream/buffer.cc12
-rw-r--r--src/util/base64.cc10
4 files changed, 26 insertions, 21 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index da53cc3..f67bc42 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3,7 +3,7 @@ project(DictzipQuery)
set(
CMAKE_CXX_FLAGS
- "-std=c++0x -W -Wall -Wextra -Winline -pedantic"
+ "-std=c++17 -W -Wall -Wextra -Winline -pedantic"
)
include_directories(
diff --git a/example.cc b/example.cc
index 5af1975..f2a091d 100644
--- a/example.cc
+++ b/example.cc
@@ -4,19 +4,24 @@
#include <string>
#include <iostream>
-int main() {
- dictzip::Istream stream("gcide.dict.dz");
+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.data(), length);
+
+ return result;
+}
+int main() {
// Decode location of _Accession_
+ // `gcide.index[1089]: "Accession 8Aw Wt"
const std::size_t offset = dictzip::base64_decode("8Aw");
const std::size_t length = dictzip::base64_decode("Wt");
// Print the GCIDE definition of _Accession_
- std::string data;
- data.reserve(length);
-
- stream.seekg(offset);
- stream.read(const_cast<char*>(data.data()), length);
-
- std::cout << data.c_str() << std::endl;
+ std::cout << get("gcide.dict.dz", offset, length) << std::endl;
}
diff --git a/src/istream/buffer.cc b/src/istream/buffer.cc
index aaa93ff..26697f4 100644
--- a/src/istream/buffer.cc
+++ b/src/istream/buffer.cc
@@ -168,27 +168,27 @@ int IstreamBuf::underflow() {
// From C++ annotations 8.1.0~pre1, chapter 23.
std::streamsize IstreamBuf::xsgetn(char *dest, std::streamsize n) {
int nread = 0;
-
+
while ( n ) {
if ( !this->in_avail() ) {
if ( this->underflow() == EOF ) {
break;
}
}
-
+
int avail = this->in_avail();
-
+
if (avail > n) {
avail = n;
}
-
+
std::memcpy(dest + nread, gptr(), avail);
this->gbump(avail);
-
+
nread += avail;
n -= avail;
}
-
+
return nread;
}
diff --git a/src/util/base64.cc b/src/util/base64.cc
index 4e8d0a4..4a4f095 100644
--- a/src/util/base64.cc
+++ b/src/util/base64.cc
@@ -8,22 +8,22 @@ namespace dictzip {
std::size_t base64_decode(const std::string& encoded) {
std::vector<std::int8_t> map(256,-1);
- for ( int i = 0; i < 64; ++i ) {
+ for ( int i = 0; i < 64; ++i ) {
map["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[i]] = i;
}
std::size_t value = 0;
- for ( std::uint8_t c : encoded ) {
- if ( map[c] == -1 ) {
+ for ( std::uint8_t c : encoded ) {
+ if ( map[c] == -1 ) {
throw std::invalid_argument("Invalid character in BASE64 string.");
} else {
value *= 64;
value += map[c];
}
- }
+ }
- return value;
+ return value;
}
}