Skip to content

Commit

Permalink
Adding header_size_ to class ClassicIndexHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
leoisl committed Oct 14, 2022
1 parent 9601c8e commit 4575ff8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
9 changes: 6 additions & 3 deletions cobs/file/classic_index_header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@ void ClassicIndexHeader::serialize(std::ostream& os) const {
}

void ClassicIndexHeader::deserialize(std::istream& is) {
deserialize_magic_begin(is, magic_word, version);
std::streamsize nb_of_bytes_read = 0;
nb_of_bytes_read += deserialize_magic_begin(is, magic_word, version);

uint32_t file_names_size;
stream_get(is, term_size_, canonicalize_,
nb_of_bytes_read += stream_get(is, term_size_, canonicalize_,
file_names_size, signature_size_, num_hashes_);
file_names_.resize(file_names_size);
for (auto& file_name : file_names_) {
std::getline(is, file_name);
nb_of_bytes_read += file_name.size() + 1;
}

deserialize_magic_end(is, magic_word);
nb_of_bytes_read += deserialize_magic_end(is, magic_word);
header_size_ = nb_of_bytes_read;
}

void ClassicIndexHeader::write_file(std::ostream& os,
Expand Down
2 changes: 2 additions & 0 deletions cobs/file/classic_index_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class ClassicIndexHeader
uint64_t num_hashes_;
//! list of document file names
std::vector<std::string> file_names_;
//! header size in bytes
std::streamsize header_size_;

public:
static const std::string magic_word;
Expand Down
17 changes: 10 additions & 7 deletions cobs/file/header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
namespace cobs {

static inline
void check_magic_word(std::istream& is, const std::string& magic_word) {
std::streamsize check_magic_word(std::istream& is, const std::string& magic_word) {
std::vector<char> mw_v(magic_word.size(), ' ');
is.read(mw_v.data(), magic_word.size());
std::string mw(mw_v.data(), mw_v.size());
assert_throw<FileIOException>(mw == magic_word, "invalid file type");
assert_throw<FileIOException>(is.good(), "input filestream broken");
return is.gcount();
}

static inline
Expand All @@ -43,19 +44,21 @@ void serialize_magic_end(
}

static inline
void deserialize_magic_begin(
std::streamsize deserialize_magic_begin(
std::istream& is, const std::string& magic_word, const uint32_t& version) {
check_magic_word(is, "COBS:");
check_magic_word(is, magic_word);
std::streamsize nb_of_bytes_read = 0;
nb_of_bytes_read += check_magic_word(is, "COBS:");
nb_of_bytes_read += check_magic_word(is, magic_word);
uint32_t v;
stream_get(is, v);
nb_of_bytes_read += stream_get(is, v);
assert_throw<FileIOException>(v == version, "invalid file version");
return nb_of_bytes_read;
}

static inline
void deserialize_magic_end(
std::streamsize deserialize_magic_end(
std::istream& is, const std::string& magic_word) {
check_magic_word(is, magic_word);
return check_magic_word(is, magic_word);
}

} // namespace cobs
Expand Down

0 comments on commit 4575ff8

Please sign in to comment.