Commit 5e8cf8d2 authored by Ludovic Courtès's avatar Ludovic Courtès

data: 'load' automatically dispatches according to the 'FORMAT' header entry.

parent de0e9344
......@@ -184,6 +184,8 @@ unsigned int timer::current_time() const
}
static const std::string _text_string("text");
const header::value header::_text_value(_text_string);
const header::value header::value::_undefined("");
header::header(std::istream& input)
......
......@@ -444,7 +444,10 @@ class header
i = _alist.find(key);
if (i == _alist.end())
{
return value::undefined();
// For backward compatibility, when the 'FORMAT' entry is
// missing, we assume it means 'text'.
return key == "FORMAT" ?
_text_value : value::undefined();
}
return value(i->second);
}
......@@ -452,6 +455,8 @@ class header
protected:
std::string _kind;
std::map<std::string, std::string> _alist;
static const value _text_value;
};
......
......@@ -78,7 +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);
friend void load_data_from_binary(std::istream& in, const header& header,
data& data);
protected: // data
} ;
......
......@@ -21,6 +21,7 @@
#endif
void vertical_segment::load_data_from_text(std::istream& input,
const header& header,
vertical_segment& result,
const arguments& args)
{
......@@ -29,8 +30,6 @@ void vertical_segment::load_data_from_text(std::istream& input,
result._nX = 0 ; result._nY = 0 ;
header header(input);
std::pair<int, int> dim = header["DIM"];
result._nX = dim.first;
result._nY = dim.second;
......@@ -255,10 +254,8 @@ 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)
void load_data_from_binary(std::istream& in, const header& header, data& data)
{
header header(in);
// FIXME: For now we make a number of assumptions.
assert(header["FORMAT"].string() == "binary");
assert(header["VERSION"] == 0);
......
......@@ -12,6 +12,7 @@
#include <iostream>
#include "data.h"
#include "common.h"
// Write DATA to OUT in ALTA's text format.
void save_data_as_text(std::ostream& out, const data &data);
......@@ -20,4 +21,5 @@ void save_data_as_text(std::ostream& out, const data &data);
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);
void load_data_from_binary(std::istream& in, const header& header,
data &data);
......@@ -68,7 +68,13 @@ void vertical_segment::load(const std::string& filename, const arguments& args)
file.open(filename.c_str());
file.exceptions (std::ios::goodbit);
load_data_from_text(file, *this, args);
header header(file);
if (header["FORMAT"].string() == "binary")
load_data_from_binary(file, header, *this);
else if (header["FORMAT"].string() == "text")
load_data_from_text(file, header, *this, args);
else throw; // FIXME: Throw a usable exception.
file.close();
}
......
......@@ -132,7 +132,9 @@ class vertical_segment : public data
private: // method
void initializeToZero( unsigned int number_of_data_elements );
static void load_data_from_text(std::istream& input, vertical_segment& result,
static void load_data_from_text(std::istream& input,
const header& header,
vertical_segment& result,
const arguments& args);
private: // data
......
......@@ -142,10 +142,8 @@ int main(int argc, char** argv)
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();
// This should automatically load using the binary format loader.
sample3.load(temp_file3);
}
CATCH_FILE_IO_ERROR(input_file);
......
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