Skip to content

Commit

Permalink
Rename file according to class name, plus some refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Aug 31, 2017
1 parent 7ca9968 commit aa25da7
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 152 deletions.
10 changes: 5 additions & 5 deletions libs/DirectoryArchiveFile.h
@@ -1,7 +1,7 @@
#pragma once

#include "iarchive.h"
#include "stream/filestream.h"
#include "stream/FileInputStream.h"

namespace archive
{
Expand All @@ -12,19 +12,19 @@ class DirectoryArchiveFile :
{
private:
std::string _name;
FileInputStream _istream;
FileInputStream::size_type _size;
stream::FileInputStream _istream;
stream::FileInputStream::size_type _size;

public:
typedef FileInputStream::size_type size_type;
typedef stream::FileInputStream::size_type size_type;

DirectoryArchiveFile(const std::string& name, const std::string& filename) :
_name(name),
_istream(filename)
{
if (!failed())
{
_istream.seek(0, FileInputStream::end);
_istream.seek(0, stream::FileInputStream::end);
_size = _istream.tell();
_istream.seek(0);
}
Expand Down
9 changes: 7 additions & 2 deletions libs/stream/BufferInputStream.h
Expand Up @@ -3,6 +3,9 @@
#include "itextstream.h"
#include <algorithm>

namespace stream
{

class BufferInputStream :
public TextInputStream
{
Expand All @@ -18,7 +21,7 @@ class BufferInputStream :
_end(buffer + length)
{}

std::size_t read(char* buffer, std::size_t length)
std::size_t read(char* buffer, std::size_t length) override
{
std::size_t count = std::min(std::size_t(_end - _read), length);

Expand All @@ -35,7 +38,7 @@ class BufferInputStream :
// greebo: Override default std::streambuf::seekoff() method to provide buffer positioning capabilities
virtual std::streampos seekoff(std::streamoff off,
std::ios_base::seekdir way,
std::ios_base::openmode which = std::ios_base::in | std::ios_base::out)
std::ios_base::openmode which = std::ios_base::in | std::ios_base::out) override
{
if (way == std::ios_base::beg)
{
Expand Down Expand Up @@ -88,3 +91,5 @@ class BufferInputStream :
return std::streampos(_read - _begin);
}
};

}
111 changes: 111 additions & 0 deletions libs/stream/FileInputStream.h
@@ -0,0 +1,111 @@
#pragma once

#include "idatastream.h"
#include <algorithm>
#include <cstdio>

namespace stream
{

namespace detail
{

inline int whence_for_seekdir(SeekableStream::seekdir direction)
{
switch (direction)
{
case SeekableStream::cur:
return SEEK_CUR;
case SeekableStream::end:
return SEEK_END;
default:
break;
}
return SEEK_SET;
}

}

/// \brief A wrapper around a file input stream opened for reading in binary mode. Similar to std::ifstream.
///
/// - Maintains a valid file handle associated with a name passed to the constructor.
/// - Implements SeekableInputStream.
class FileInputStream :
public SeekableInputStream
{
private:
std::FILE* _file;
public:
FileInputStream(const std::string& name) :
_file(!name.empty() ? fopen(name.c_str(), "rb") : nullptr)
{}

~FileInputStream()
{
if (!failed())
{
fclose(_file);
}
}

bool failed() const
{
return _file == nullptr;
}

size_type read(byte_type* buffer, size_type length) override
{
return fread(buffer, 1, length, _file);
}

size_type seek(size_type position) override
{
return fseek(_file, static_cast<long>(position), SEEK_SET);
}

size_type seek(offset_type offset, seekdir direction) override
{
return fseek(_file, offset, detail::whence_for_seekdir(direction));
}

size_type tell() const override
{
return ftell(_file);
}

std::FILE* file()
{
return _file;
}
};

/// \brief A wrapper around a FileInputStream limiting access.
///
/// - Maintains an input stream.
/// - Provides input starting at an offset in the file for a limited range.
class SubFileInputStream :
public InputStream
{
private:
FileInputStream& _istream;
size_type _remaining;

public:
typedef FileInputStream::position_type position_type;

SubFileInputStream(FileInputStream& istream, position_type offset, size_type size) :
_istream(istream),
_remaining(size)
{
_istream.seek(offset);
}

size_type read(byte_type* buffer, size_type length) override
{
size_type result = _istream.read(buffer, std::min(length, _remaining));
_remaining -= result;
return result;
}
};

}
116 changes: 0 additions & 116 deletions libs/stream/filestream.h

This file was deleted.

12 changes: 6 additions & 6 deletions plugins/archivezip/DeflatedArchiveFile.h
@@ -1,7 +1,7 @@
#pragma once

#include "iarchive.h"
#include "stream/filestream.h"
#include "stream/FileInputStream.h"
#include "DeflatedInputStream.h"

namespace archive
Expand All @@ -12,14 +12,14 @@ class DeflatedArchiveFile :
{
private:
std::string _name;
FileInputStream _istream;
SubFileInputStream _substream; // provides a subset of _istream
stream::FileInputStream _istream;
stream::SubFileInputStream _substream; // provides a subset of _istream
DeflatedInputStream _zipstream; // inflates data from _subStream
FileInputStream::size_type _size;
stream::FileInputStream::size_type _size;

public:
typedef FileInputStream::size_type size_type;
typedef FileInputStream::position_type position_type;
typedef stream::FileInputStream::size_type size_type;
typedef stream::FileInputStream::position_type position_type;

DeflatedArchiveFile(const std::string& name,
const std::string& archiveName, // path to the ZIP file
Expand Down
8 changes: 4 additions & 4 deletions plugins/archivezip/DeflatedArchiveTextFile.h
Expand Up @@ -15,17 +15,17 @@ class DeflatedArchiveTextFile :
{
private:
std::string _name;
FileInputStream _istream;
SubFileInputStream _substream; // reads subset of _istream
stream::FileInputStream _istream;
stream::SubFileInputStream _substream; // reads subset of _istream
DeflatedInputStream _zipstream; // inflates data from _substream
stream::BinaryToTextInputStream<DeflatedInputStream> _textStream; // converts data from _zipstream

// Mod directory containing this file
const std::string _modName;

public:
typedef FileInputStream::size_type size_type;
typedef FileInputStream::position_type position_type;
typedef stream::FileInputStream::size_type size_type;
typedef stream::FileInputStream::position_type position_type;

/**
* Constructor.
Expand Down
10 changes: 5 additions & 5 deletions plugins/archivezip/StoreArchiveFile.h
Expand Up @@ -11,13 +11,13 @@ class StoredArchiveFile :
{
private:
std::string _name;
FileInputStream _filestream;
SubFileInputStream _substream; // provides a subset of _filestream
FileInputStream::size_type _size;
stream::FileInputStream _filestream;
stream::SubFileInputStream _substream; // provides a subset of _filestream
stream::FileInputStream::size_type _size;

public:
typedef FileInputStream::size_type size_type;
typedef FileInputStream::position_type position_type;
typedef stream::FileInputStream::size_type size_type;
typedef stream::FileInputStream::position_type position_type;

StoredArchiveFile(const std::string& name,
const std::string& archiveName, // full path to the archive file
Expand Down
10 changes: 5 additions & 5 deletions plugins/archivezip/StoredArchiveTextFile.h
Expand Up @@ -12,15 +12,15 @@ class StoredArchiveTextFile :
{
private:
std::string _name;
FileInputStream _filestream;
SubFileInputStream _substream; // provides a subset of _filestream
stream::BinaryToTextInputStream<SubFileInputStream> _textStream; // converts data from _substream
stream::FileInputStream _filestream;
stream::SubFileInputStream _substream; // provides a subset of _filestream
stream::BinaryToTextInputStream<stream::SubFileInputStream> _textStream; // converts data from _substream

// Mod directory
std::string _modName;
public:
typedef FileInputStream::size_type size_type;
typedef FileInputStream::position_type position_type;
typedef stream::FileInputStream::size_type size_type;
typedef stream::FileInputStream::position_type position_type;

/**
* Constructor.
Expand Down

0 comments on commit aa25da7

Please sign in to comment.