Commit 04df04de authored by Ludovic Courtès's avatar Ludovic Courtès

data_storage: Add 'load_data_from_binary'.

parent 4874873c
......@@ -78,6 +78,8 @@ class data : public parametrized
virtual bool equals(const data& data,
double epsilon = std::numeric_limits<float>::min());
friend void load_data_from_binary(std::istream& in, data& data);
protected: // data
} ;
......
......@@ -14,6 +14,7 @@
#include <iostream>
#include <limits>
#include <cassert>
#ifdef __GLIBC__
# include <endian.h>
......@@ -261,3 +262,50 @@ void save_data_as_binary(std::ostream &out, const data& data)
out << std::endl << "#END_STREAM" << std::endl;
}
void load_data_from_binary(std::istream& in, data& data)
{
header header(in);
// FIXME: For now we make a number of assumptions.
assert(header["FORMAT"].string() == "binary");
assert(header["VERSION"] == 0);
assert(header["PRECISION"].string() == "ieee754-double");
#if __BYTE_ORDER == __LITTLE_ENDIAN
assert(header["ENDIAN"].string() == "little");
#else
assert(header["ENDIAN"].string() == "big");
#endif
std::pair<int, int> dim = header["DIM"];
data.setDimX(dim.first);
data.setDimY(dim.second);
data._min.resize(dim.first);
data._max.resize(dim.first);
data.setParametrizations(params::parse_input(header["PARAM_IN"]),
params::parse_output(header["PARAM_OUT"]));
in.exceptions(std::ios_base::failbit);
int sample_count = header["SAMPLE_COUNT"];
// TODO: Arrage to use mmap and make it zero-alloc and zero-copy.
for (int i = 0; i < sample_count; i++)
{
vec row = vec::Zero(data.dimX() + data.dimY());
std::streamsize expected = row.size() * sizeof(double);
for (std::streamsize total = 0;
total < expected && !in.eof();
total += in.gcount())
{
in.read((char *)row.data() + total, expected);
}
data.set(row);
}
}
......@@ -18,3 +18,6 @@ void save_data_as_text(std::ostream& out, const data &data);
// Write DATA to OUT in a compact binary format.
void save_data_as_binary(std::ostream& out, const data& data);
// Initialize DATA from the binary-formatted stream IN.
void load_data_from_binary(std::istream& in, data &data);
......@@ -16,6 +16,7 @@
#include <core/args.h>
#include <core/data.h>
#include <core/data_storage.h>
#include <core/vertical_segment.h>
#include <string>
......@@ -97,8 +98,8 @@ static bool files_are_equal(const std::string &file1, const std::string &file2)
int main(int argc, char** argv)
{
std::string input_file;
temporary_file temp_file1, temp_file2;
vertical_segment sample1, sample2;
temporary_file temp_file1, temp_file2, temp_file3;
vertical_segment sample1, sample2, sample3;
if (argc > 1)
// Process the user-specified file.
......@@ -120,10 +121,22 @@ int main(int argc, char** argv)
sample2.load(temp_file1);
sample2.save(temp_file2);
// Now use the binary output format.
std::ofstream out;
out.open(temp_file3.name().c_str());
save_data_as_binary(out, sample2);
out.close();
std::ifstream in;
in.open(temp_file3.name().c_str());
load_data_from_binary(in, sample3);
in.close();
}
CATCH_FILE_IO_ERROR(input_file);
return (sample1.equals(sample2)
&& files_are_equal(temp_file1, temp_file2))
&& files_are_equal(temp_file1, temp_file2)
&& sample2.equals(sample3))
? EXIT_SUCCESS : EXIT_FAILURE;
}
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