Commit d5abe79f authored by hbristow's avatar hbristow

untracked io for initial pull request

parent 9726346b
#ifndef OPENCV_FILEBUFFER_HPP_
#define OPENCV_FILEBUFFER_HPP_
#include <vector>
#include <streambuf>
class EndianFileBuffer : public std::streambuf {
private:
const int fd_;
const size_t put_back_;
std::vector<char> buffer_;
// prevent copy construction
EndianFileBuffer(const EndianFileBuffer&);
EndianFileBuffer& operator=(const EndianFileBuffer&);
public:
explicit EndianFileBuffer(int fd, size_t buffer_sz, size_t put_back) :
fd_(fd), put_back_(max(put_back, 1)), buffer_(max(buffer_sz, put_back_) + put_back_) {
char *end = &buffer_.front() + buffer_.size();
setg(end, end, end);
}
std::streambuf::int_type underflow() {
if (gptr() < egptr()) // buffer not exhausted
return traits_type::to_int_type(*gptr());
char *base = &buffer_.front();
char *start = base;
if (eback() == base) { // true when this isn't the first fill
std::memmove(base, egptr() - put_back_, put_back_);
start += put_back_;
}
// start is now the start of the buffer
// refill from the file
read(fd_, start, buffer_.size() - (start - base));
if (n == 0) return traits_type::eof();
// set buffer pointers
setg(base, start, start + n);
return traits_type::to_int_type(*gptr());
}
};
#endif
#include <ctime>
#include <stringstream>
using namespace std;
using namespace cv;
const char* day[] = { "Sun", "Mon", "Tue", "Wed", "Thurs", "Fri", "Sat" };
const char* month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
const char* arch = "${MEX_ARCH}";
// ----------------------------------------------------------------------------
// BASIC OPERATIONS
// ----------------------------------------------------------------------------
string MatlabIO::filename(void) const { return filename_; }
bool open(const string& filename, ios_base::openmode mode);
filename_ = filename;
stream_.open(filename, mode);
}
bool isOpen() const { return stream_.valid(); }
void close() { stream_.close(); }
std::ifstream::pos_type filesize() {
std::ifstream::pos_type current = stream_.tellg();
stream_.seekg(0, std::ifstream::end);
std::ifstream::pos_type end = stream_.tellg();
stream_.seekg(current, std::ifstream::beg);
return end;
}
void pushStreamPosition() {
stream_pos_ = stream_.tellg();
}
void popStreamPosition() {
stream_.seekg(stream_pos_, std::ifstream::beg);
}
void setStreamPosition(std::ifstream::pos_type position) {
stream_.seekg(position, std::ifstream::beg);
}
// ----------------------------------------------------------------------------
// HEADERS
// ----------------------------------------------------------------------------
void getFileHeader() {
// store the current stream position
pushStreamPosition();
setStreamPosition(0);
stream_.read(const_cast<char *>(header_.data()), sizeof(char)*HEADER_LENGTH);
stream_.read(const_cast<char *>(subsys_.data()), sizeof(char)*SUBSYS_LENGTH);
stream_.read((char *)&version_, sizeof(int16_t));
stream_.read(const_cast<char *>(endian_.data()), sizeof(char)*ENDIAN_LENGTH);
// get the actual version
if (version_ == 0x0100) version_ = Matlab::IO::Version5;
if (version_ == 0x0200) version_ = Matlab::IO::Version73;
// get the endianness
if (endian_.compare("IM") == 0) byte_swap_ = false;
if (endian_.compare("MI") == 0) byte_swap_ = true;
// restore the current stream position
popStreamPosition();
}
// ----------------------------------------------------------------------------
// INDEXING OPERATIONS
// ----------------------------------------------------------------------------
void MatlabIO::indexNode(const MappingIndex& current) {
}
void MatlabIO::indexNode(const SequenceIndex& current) {
}
void MatlabIO::index() {
// if there is no open file, do nothing
if (!isOpen()) return;
// read the global header
getFileHeader();
// change the endianness if need be
// manually index the top-level node
MappingIndex root(filename_, vector<size_t>(), stream_.tellg(), filesize(), 0, 0, stream_);
indexNode(root);
index_ = root;
}
// ----------------------------------------------------------------------------
// FORMATTING / PRINTING
// ----------------------------------------------------------------------------
template <typename Iterable>
string delimitedStringFromIterable(const Iterable& obj, const string& delimiter=string(" ")) {
string cache = "";
ostringstream oss;
for (Iterable::iterator it = obj.begin(); it != obj.end(); ++it) {
// flush the cache and insert the next element
oss << cache << *it;
cache = delimiter;
}
return oss.str();
}
string formatCurrentTime() {
ostringstream oss;
time_t rawtime;
struct tm* timeinfo;
int dom, hour, min, sec, year;
// compute the current time
time(&rawtime);
timeinfo = localtime(&rawtime);
// extract the components of interest
dom = timeinfo->tm_mday;
hour = timeinfo->tm_hour;
min = timeinfo->tm_min;
sec = timeinfo->tm_sec;
year = timeinfo->year + 1900;
oss << day[timeinfo->tm_wday] << " " << month[timeinfo->tm_mon]
<< " " << dom << " " << hour << ":" << min << ":" << sec << " " << year;
return oss.str();
}
void MatlabIO::printRootIndex() const {
cout << "--------------- top level file index ------------------" << endl;
cout << "Filename: " << filename() << endl;
cout << "File size: " << filesize() << "MB" << endl << endl;
cout << "Name size bytes type" << endl;
for (Map<String, Index>::iterator it = index_.mapping.begin(); it != index_.mapping.end(); ++it) {
cout << it->name << " ";
cout << delimitedStringFromIterable(it->size, "x") << " ";
cout << it->end - it->begin << " ";
cout << endl;
}
cout << "-------------------------------------------------------" << endl;
}
void printIndex(const Index& index, const int indentation) {
cout << string(2*indentation - 1, ' ') << "|" << endl;
cout << string(2*indentation - 1, ' ') << "|-- ";
cout << index.name << " (" << delimitedStringFromIterable(index.size, "x") << ")" << endl;
if (index.leaf) return;
if (index.associative) {
for (Map<string, Index>::iterator it = index.mapping.begin(); it != index.mapping.end(); ++it) {
printIndex(it->second, indentation+1);
}
} else {
for (vector<Index>::iterator it = index.sequence.begin(); it != index.sequence.end(); ++it) {
printIndex(it->second, indentation+1);
}
}
}
void MatlabIO::printFullIndex() const {
int indentation = 0;
cout << "----------------- full file index ---------------------" << endl;
printIndex(index_, indentation);
cout << "-------------------------------------------------------" << endl;
}
#ifndef MATLAB_IO_HPP_
#define MATLAB_IO_HPP_
#include <sstream>
#include <fstream>
#include <zlib.h>
#include <opencv2/core.hpp>
#include <opencv2/core/persistence.hpp>
#include "primitives.hpp"
#include "bridge.hpp"
#include "mxarray.hpp"
#include "map.hpp"
namespace Matlab {
namespace IO {
class RandomAccessRead {};
class SequentialWrite {};
static const int Version5 = 5;
static const int Version73 = 73;
}
// predeclarations
class IONode;
class IONodeIterator;
class MatlabIO;
// ----------------------------------------------------------------------------
// FILE AS A DATA STRUCTURE
// ----------------------------------------------------------------------------
class IONode {
protected:
//! the name of the field (if associative container)
std::string name_;
//! the size of the field
std::vector<size_t> size_;
//! beginning of the data field in the file
size_t begin_;
//! address after the last data field
size_t end_;
//! Matlab stored-type
int stored_type_;
//! Matlab actual type (sometimes compression is used)
int type_;
//! is the field compressed?
bool compressed_;
//! are the descendents associative (mappings)
bool associative_;
//! is this a leaf node containing data, or an interior node
bool leaf_;
//! the data stream from which the file was indexed
cv::Ptr<std::istream> stream_;
//! valid if the container is a sequence (list)
std::vector<IONode> sequence_;
//! valid if the container is a mapping (associative)
Map<std::string, IONode> mapping_;
IONode(const std::string& name, const std::Vector<size_t>& size, size_t begin, size_t end,
int stored_type, int type, bool compressed, bool associative, bool leaf, istream& stream) :
name_(name), size_(size), begin_(begin), end_(end), stored_type_(stored_type), type_(type),
compressed_(compressed), associative_(associative), leaf_(leaf), stream_(stream) {}
public:
std::string name() const { return name_; }
std::vector<size_t> size() const { return size_; }
size_t begin() const { return begin_; }
size_t end() const { return end_; }
int stored_type() const { return stored_type_; }
int type() const { return type_; }
bool compressed() const { return compressed_; }
bool associative() const { return associative_; }
bool leaf() const { return leaf_; }
IONode() : begin_(0), end_(0), stored_type_(0), type_(0), leaf_(true) {}
#if __cplusplus >= 201103L
// conversion operators
template <typename T> void operator=(const T& obj) { static_assert(0, "Unimplemented specialization for given type"); }
template <typename T> operator T() { static_assert(0, "Unimplemented specialization for given type"); }
#else
// conversion operators
template <typename T> void operator=(const T& obj) { T::unimplemented_specialization; }
template <typename T> operator T() { T::unimplemented_specialization; }
#endif
void swap(const IONode& other) {
using std::swap;
swap(name_, other.name_);
swap(size_, other.size_);
swap(begin_, other.begin_);
swap(end_, other.end_);
swap(stored_type_, other.stored_type_);
swap(type_, other.type_);
swap(compressed_, other.compressed_);
swap(associative_, other.associative_);
swap(leaf_, other.leaf_);
swap(stream_, other.stream_);
swap(sequence_, other.sequence_);
swap(mapping_, other.mapping_);
}
};
class SequenceIONode : public IONode {
public:
std::vector<IONode>& sequence() { return sequence_; }
SequenceIONode(const std::string& name, const std::Vector<size_t>& size, size_t begin, size_t end,
int stored_type, int type, const std::istream& stream) :
IONode(name, size, begin, end, stored_type, type, false, false, false, stream) {}
};
class MappingIONode : public IONode {
public:
Map<std::string, IONode>& mapping() { return mapping_; }
MappingIONode(const std::string& name, const std::Vector<size_t>& size, size_t begin, size_t end,
int stored_type, int type, const std::istream& stream) :
IONode(name, size, begin, end, stored_type, type, false, true, false, stream) {}
};
class LeafIONode : public IONode {
LeafIONode(const std::string& name, const std::Vector<size_t>& size, size_t begin, size_t end,
int stored_type, int type, const std::istream& stream) :
IONode(name, size, begin, end, stored_type, type, false, false, true, stream) {}
};
class CompressedIONode : public IONode {
private:
std::istringstream uncompressed_stream_;
std::vector<char> data_;
public:
CompressedIONode(const std::string& name, const std::Vector<size_t>& size, size_t begin, size_t end,
int stored_type, int type, const std::stream& stream) :
IONode(name, size, begin, end, stored_type, type, true, false, false, stream) {}
};
class Header : public IONode {
Header(const std::string& name, const std::Vector<size_t>& size, size_t begin, size_t end,
int stored_type, int type, const std::stream& stream) :
IONode(name, size, begin, end, stored_type, type, true, false, false, stream) {}
// ----------------------------------------------------------------------------
// FILE NODE
// ----------------------------------------------------------------------------
class IONodeIterator : public std::iterator<std::random_access_iterator_tag, MatlabIONode> {
};
// ----------------------------------------------------------------------------
// MATLABIO
// ----------------------------------------------------------------------------
class MatlabIO {
private:
// member variables
static const int HEADER_LENGTH = 116;
static const int SUBSYS_LENGTH = 8;
static const int ENDIAN_LENGTH = 2;
std::string header_;
std::string subsys_;
std::string endian_;
int version_;
bool byte_swap_;
std::string filename_;
// uses a custom stream buffer for fast memory-mapped access and endian swapping
std::fstream stream_;
std::ifstream::pos_type stream_pos_;
//! the main file index. The top-level index must be associative
IONode index_;
// internal methods
void getFileHeader();
void setFileHeader();
void getHeader();
void setHeader();
CompressedIONode uncompress(const IONode& node);
public:
// construct/destruct
MatlabIO() : header_(HEADER_LENGTH+1, '\0'), subsys_(SUBSYS_LENGTH+1, '\0'),
endian_(ENDIAN_LENGTH+1, '\0'), byte_swap(false), stream_pos_(0) {}
~MatlabIO {}
// global read and write routines
std::string filename(void);
bool open(const std::string& filename, std::ios_base::openmode mode);
bool isOpen() const;
void close();
void clear();
// index the contents of the file
void index();
// print all of the top-level variables in the file
void printRootIndex() const;
void printFullIndex() const;
// FileNode operations
IONode root() const;
IONode operator[](const String& nodename) const;
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment