20 #include <tensor/sdf.h>
25 template <
class number>
26 void read_raw_with_endian(std::ifstream &s, number *data,
size_t n)
28 s.read((
char *)data, n *
sizeof(number));
30 std::cerr <<
"I/O error when reading from stream " << s;
35 template <
class number>
36 void read_raw_with_endian(std::ifstream &s, number *data,
size_t n)
38 const int size =
sizeof(number);
40 s.read((
char *)data, n *
sizeof(number));
42 std::cerr <<
"I/O error when reading from stream " << s;
48 const int buffer_size = 1024;
49 char *alias = (
char *)data;
50 char buffer[buffer_size];
53 size_t now = min<size_t>(n*size, buffer_size);
56 std::cerr <<
"I/O error when reading from stream " << s;
59 for (
size_t i = 0; i < now; i+=size) {
61 *(alias++) = buffer[i+3];
62 *(alias++) = buffer[i+2];
63 *(alias++) = buffer[i+1];
64 *(alias++) = buffer[i];
65 }
else if (size == 8) {
66 *(alias++) = buffer[i+7];
67 *(alias++) = buffer[i+6];
68 *(alias++) = buffer[i+5];
69 *(alias++) = buffer[i+4];
70 *(alias++) = buffer[i+3];
71 *(alias++) = buffer[i+2];
72 *(alias++) = buffer[i+1];
73 *(alias++) = buffer[i];
75 for (
size_t j = size; j--; ) {
76 *(alias++) = buffer[i+j];
85 InDataFile::InDataFile(
const std::string &a_filename,
int flags) :
86 DataFile(a_filename, flags), _stream(actual_filename().c_str())
92 InDataFile::read_raw(
char *data,
size_t n)
95 read_raw_with_endian(_stream, data, n);
99 InDataFile::read_raw(
int *data,
size_t n)
102 read_raw_with_endian(_stream, data, n);
106 InDataFile::read_raw(
long *data,
size_t n)
109 read_raw_with_endian(_stream, data, n);
113 InDataFile::read_raw(
size_t *data,
size_t n)
116 read_raw_with_endian(_stream, data, n);
120 InDataFile::read_raw(
double *data,
size_t n)
123 read_raw_with_endian(_stream, data, n);
127 InDataFile::read_raw(cdouble *data,
size_t n)
130 read_raw_with_endian(_stream, (
double*)data, 2*n);
134 InDataFile::read_tag_code()
142 InDataFile::read_variable_name()
144 char buffer[var_name_size];
145 read_raw(buffer, var_name_size);
146 return std::string(buffer);
150 InDataFile::read_tag(
const std::string &name,
size_t type)
152 std::string other_name = read_variable_name();
153 if (name.size() && (name != other_name)) {
154 std::cerr <<
"While reading file " << _filename <<
", variable "
155 << name <<
" was expected but found "
156 << other_name <<
'\n';
159 size_t other_type = read_tag_code();
160 if (type != other_type) {
161 std::cerr <<
"While reading file " << _filename <<
", an object of type "
162 << tag_to_name(type) <<
" was expected but found a "
163 << tag_to_name(other_type) <<
'\n';
168 template<
class Vector>
169 const Vector InDataFile::load_vector()
174 read_raw(v.begin(), length);
179 InDataFile::load(
RTensor *t,
const std::string &name) {
180 read_tag(name, TAG_RTENSOR);
181 Indices dims = load_vector<Indices>();
182 *t =
RTensor(dims, load_vector<RTensor>());
186 InDataFile::load(
CTensor *t,
const std::string &name) {
187 read_tag(name, TAG_CTENSOR);
188 Indices dims = load_vector<Indices>();
189 *t =
CTensor(dims, load_vector<CTensor>());
193 InDataFile::load(std::vector<RTensor> *m,
const std::string &name)
195 read_tag(name, TAG_RTENSOR_VECTOR);
199 for (
size_t k = 0; k < l; k++) {
205 InDataFile::load(std::vector<CTensor> *m,
const std::string &name)
207 read_tag(name, TAG_CTENSOR_VECTOR);
211 for (
size_t k = 0; k < l; k++) {
217 InDataFile::load(
double *value,
const std::string &name)
222 std::cerr <<
"While reading file " << _filename <<
" found a tensor of size "
223 << t.
size() <<
" while a single value was expected.";
230 InDataFile::load(cdouble *value,
const std::string &name)
235 std::cerr <<
"While reading file " << _filename <<
" found a tensor of size "
236 << t.
size() <<
" while a single value was expected.";
243 InDataFile::load(
size_t *v,
const std::string &name)
251 InDataFile::load(
int *v,
const std::string &name)
259 InDataFile::read_header()
261 std::string var_name = read_variable_name();
263 if (var_name[0] !=
's' || var_name[1] !=
'd' || var_name[2] !=
'f') {
264 std::cerr <<
"Bogus SDF file";
267 int file_int_size = var_name[3] -
'0';
268 int file_long_size = var_name[4] -
'0';
269 int file_endianness = var_name[5] -
'0';
270 if (file_int_size !=
sizeof(
int) ||
271 file_long_size !=
sizeof(
long) ||
272 file_endianness != endian)
274 std::cerr <<
"File " << _filename <<
" has word sizes (" << file_int_size
275 <<
',' << file_long_size <<
") and cannot be read by this computer";
Real Tensor with elements of type "double".
Complex Tensor with elements of type "cdouble".
index size() const
Returns total number of elements in Tensor.
Vector of 'index' type, where 'index' fits the indices of a tensor.