Skip to content

Commit

Permalink
Removed unnecessary indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
veloman-yunkan committed Sep 3, 2020
1 parent ac857be commit 1c2a7b9
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 136 deletions.
142 changes: 71 additions & 71 deletions src/cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,90 +105,90 @@ Cluster::read(const Reader& zimReader, offset_t clusterOffset)
// NonCompressedCluster
////////////////////////////////////////////////////////////////////////////////

NonCompressedCluster::NonCompressedCluster(std::shared_ptr<const Reader> reader_, bool isExtended)
: Cluster(isExtended),
reader(reader_),
startOffset(0)
{
auto d = reader->offset();
if (isExtended) {
startOffset = read_header<uint64_t>();
} else {
startOffset = read_header<uint32_t>();
}
reader = reader->sub_reader(startOffset, zsize_t(offsets.back().v));
auto d1 = reader->offset();
ASSERT(d+startOffset, ==, d1);
NonCompressedCluster::NonCompressedCluster(std::shared_ptr<const Reader> reader_, bool isExtended)
: Cluster(isExtended),
reader(reader_),
startOffset(0)
{
auto d = reader->offset();
if (isExtended) {
startOffset = read_header<uint64_t>();
} else {
startOffset = read_header<uint32_t>();
}
reader = reader->sub_reader(startOffset, zsize_t(offsets.back().v));
auto d1 = reader->offset();
ASSERT(d+startOffset, ==, d1);
}

/* This return the number of char read */
template<typename OFFSET_TYPE>
offset_t NonCompressedCluster::read_header()
{
// read first offset, which specifies, how many offsets we need to read
OFFSET_TYPE offset = reader->read_uint<OFFSET_TYPE>(offset_t(0));

size_t n_offset = offset / sizeof(OFFSET_TYPE);
const offset_t data_address(offset);

// read offsets
offsets.clear();
offsets.reserve(n_offset);
offsets.push_back(offset_t(0));

auto buffer = reader->get_buffer(offset_t(0), zsize_t(offset));
offset_t current = offset_t(sizeof(OFFSET_TYPE));
while (--n_offset)
{
OFFSET_TYPE new_offset = buffer->as<OFFSET_TYPE>(current);
ASSERT(new_offset, >=, offset);
ASSERT(new_offset, <=, reader->size().v);

offset = new_offset;
offsets.push_back(offset_t(offset - data_address.v));
current += sizeof(OFFSET_TYPE);
}
return data_address;
}
/* This return the number of char read */
template<typename OFFSET_TYPE>
offset_t NonCompressedCluster::read_header()
{
// read first offset, which specifies, how many offsets we need to read
OFFSET_TYPE offset = reader->read_uint<OFFSET_TYPE>(offset_t(0));

size_t n_offset = offset / sizeof(OFFSET_TYPE);
const offset_t data_address(offset);

zsize_t NonCompressedCluster::getBlobSize(blob_index_t n) const
// read offsets
offsets.clear();
offsets.reserve(n_offset);
offsets.push_back(offset_t(0));

auto buffer = reader->get_buffer(offset_t(0), zsize_t(offset));
offset_t current = offset_t(sizeof(OFFSET_TYPE));
while (--n_offset)
{
if (blob_index_type(n)+1 >= offsets.size()) throw ZimFileFormatError("blob index out of range");
return zsize_t(offsets[blob_index_type(n)+1].v - offsets[blob_index_type(n)].v);
OFFSET_TYPE new_offset = buffer->as<OFFSET_TYPE>(current);
ASSERT(new_offset, >=, offset);
ASSERT(new_offset, <=, reader->size().v);

offset = new_offset;
offsets.push_back(offset_t(offset - data_address.v));
current += sizeof(OFFSET_TYPE);
}
return data_address;
}

Blob NonCompressedCluster::getBlob(blob_index_t n) const
{
if (n < count()) {
auto blobSize = getBlobSize(n);
if (blobSize.v > SIZE_MAX) {
return Blob();
}
auto buffer = reader->get_buffer(offsets[blob_index_type(n)], blobSize);
return Blob(buffer);
} else {
zsize_t NonCompressedCluster::getBlobSize(blob_index_t n) const
{
if (blob_index_type(n)+1 >= offsets.size()) throw ZimFileFormatError("blob index out of range");
return zsize_t(offsets[blob_index_type(n)+1].v - offsets[blob_index_type(n)].v);
}

Blob NonCompressedCluster::getBlob(blob_index_t n) const
{
if (n < count()) {
auto blobSize = getBlobSize(n);
if (blobSize.v > SIZE_MAX) {
return Blob();
}
auto buffer = reader->get_buffer(offsets[blob_index_type(n)], blobSize);
return Blob(buffer);
} else {
return Blob();
}
}

Blob NonCompressedCluster::getBlob(blob_index_t n, offset_t offset, zsize_t size) const
{
if (n < count()) {
const auto blobSize = getBlobSize(n);
if ( offset.v > blobSize.v ) {
return Blob();
}
size = std::min(size, zsize_t(blobSize.v-offset.v));
if (size.v > SIZE_MAX) {
return Blob();
}
offset += offsets[blob_index_type(n)];
auto buffer = reader->get_buffer(offset, size);
return Blob(buffer);
} else {
Blob NonCompressedCluster::getBlob(blob_index_t n, offset_t offset, zsize_t size) const
{
if (n < count()) {
const auto blobSize = getBlobSize(n);
if ( offset.v > blobSize.v ) {
return Blob();
}
size = std::min(size, zsize_t(blobSize.v-offset.v));
if (size.v > SIZE_MAX) {
return Blob();
}
offset += offsets[blob_index_type(n)];
auto buffer = reader->get_buffer(offset, size);
return Blob(buffer);
} else {
return Blob();
}
}

////////////////////////////////////////////////////////////////////////////////
// CompressedCluster
Expand Down
132 changes: 67 additions & 65 deletions src/cluster.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2009 Tommi Maekitalo
* Copyright (C) 2020 Veloman Yunkan
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
Expand Down Expand Up @@ -35,93 +36,94 @@

namespace zim
{
class Blob;
class Reader;

class Cluster : public std::enable_shared_from_this<Cluster> {
public:
const bool isExtended;
class Blob;
class Reader;

protected:
explicit Cluster(bool isExtended) : isExtended(isExtended) {}
class Cluster : public std::enable_shared_from_this<Cluster> {
public:
const bool isExtended;

public:
virtual ~Cluster() {}
protected:
explicit Cluster(bool isExtended) : isExtended(isExtended) {}

virtual bool isCompressed() const = 0;
virtual CompressionType getCompression() const = 0;
public:
virtual ~Cluster() {}

virtual blob_index_t count() const = 0;
virtual bool isCompressed() const = 0;
virtual CompressionType getCompression() const = 0;

virtual zsize_t getBlobSize(blob_index_t n) const = 0;
virtual offset_t getBlobOffset(blob_index_t n) const = 0;
virtual Blob getBlob(blob_index_t n) const = 0;
virtual Blob getBlob(blob_index_t n, offset_t offset, zsize_t size) const = 0;
virtual blob_index_t count() const = 0;

static std::shared_ptr<Cluster> read(const Reader& zimReader, offset_t clusterOffset);
};
virtual zsize_t getBlobSize(blob_index_t n) const = 0;
virtual offset_t getBlobOffset(blob_index_t n) const = 0;
virtual Blob getBlob(blob_index_t n) const = 0;
virtual Blob getBlob(blob_index_t n, offset_t offset, zsize_t size) const = 0;

class NonCompressedCluster : public Cluster {
typedef std::vector<offset_t> Offsets;
static std::shared_ptr<Cluster> read(const Reader& zimReader, offset_t clusterOffset);
};

private:
std::shared_ptr<const Reader> reader;
class NonCompressedCluster : public Cluster {
typedef std::vector<offset_t> Offsets;

// offset of the first blob of this cluster relative to the beginning
// of the (uncompressed) cluster data
offset_t startOffset;
private:
std::shared_ptr<const Reader> reader;

// offsets of the blob boundaries relative to the start of the first
// blob in this cluster. For a cluster with N blobs, this collection
// contains N+1 entries - the start of the first blob (which is always
// 0) and the end of the last blob are also included.
Offsets offsets;
// offset of the first blob of this cluster relative to the beginning
// of the (uncompressed) cluster data
offset_t startOffset;

template<typename OFFSET_TYPE>
offset_t read_header();
// offsets of the blob boundaries relative to the start of the first
// blob in this cluster. For a cluster with N blobs, this collection
// contains N+1 entries - the start of the first blob (which is always
// 0) and the end of the last blob are also included.
Offsets offsets;

public:
NonCompressedCluster(std::shared_ptr<const Reader> reader, bool isExtended);
template<typename OFFSET_TYPE>
offset_t read_header();

bool isCompressed() const override { return false; }
CompressionType getCompression() const override { return zimcompNone; }
public:
NonCompressedCluster(std::shared_ptr<const Reader> reader, bool isExtended);

blob_index_t count() const override { return blob_index_t(offsets.size() - 1); }
bool isCompressed() const override { return false; }
CompressionType getCompression() const override { return zimcompNone; }

zsize_t getBlobSize(blob_index_t n) const override;
offset_t getBlobOffset(blob_index_t n) const override { return startOffset + offsets[blob_index_type(n)]; }
Blob getBlob(blob_index_t n) const override;
Blob getBlob(blob_index_t n, offset_t offset, zsize_t size) const override;
};
blob_index_t count() const override { return blob_index_t(offsets.size() - 1); }

class CompressedCluster : public Cluster
{
public: // functions
CompressedCluster(std::shared_ptr<const Reader> reader, CompressionType comp, bool isExtended);
zsize_t getBlobSize(blob_index_t n) const override;
offset_t getBlobOffset(blob_index_t n) const override { return startOffset + offsets[blob_index_type(n)]; }
Blob getBlob(blob_index_t n) const override;
Blob getBlob(blob_index_t n, offset_t offset, zsize_t size) const override;
};

bool isCompressed() const override;
CompressionType getCompression() const override;
blob_index_t count() const override;
zsize_t getBlobSize(blob_index_t n) const override;
offset_t getBlobOffset(blob_index_t n) const override;
Blob getBlob(blob_index_t n) const override;
Blob getBlob(blob_index_t n, offset_t offset, zsize_t size) const override;
class CompressedCluster : public Cluster
{
public: // functions
CompressedCluster(std::shared_ptr<const Reader> reader, CompressionType comp, bool isExtended);

bool isCompressed() const override;
CompressionType getCompression() const override;
blob_index_t count() const override;
zsize_t getBlobSize(blob_index_t n) const override;
offset_t getBlobOffset(blob_index_t n) const override;
Blob getBlob(blob_index_t n) const override;
Blob getBlob(blob_index_t n, offset_t offset, zsize_t size) const override;

private: // functions
template<typename OFFSET_TYPE>
void readHeader(IDataStream& ds);
private: // functions
template<typename OFFSET_TYPE>
void readHeader(IDataStream& ds);

void ensureBlobIsDecompressed(blob_index_t i) const;
void ensureBlobIsDecompressed(blob_index_t i) const;

private: // data
const std::unique_ptr<IDataStream> ds_;
const CompressionType compression_;
std::vector<size_t> blobSizes_;
private: // data
const std::unique_ptr<IDataStream> ds_;
const CompressionType compression_;
std::vector<size_t> blobSizes_;

mutable std::mutex blobAccessMutex_;
mutable std::vector<IDataStream::Blob> blobs_;
};
mutable std::mutex blobAccessMutex_;
mutable std::vector<IDataStream::Blob> blobs_;
};

}
} // unnamed namespace

#endif // ZIM_CLUSTER_H

0 comments on commit 1c2a7b9

Please sign in to comment.