STIR 6.4.0
read_data_1d.inl
Go to the documentation of this file.
1
9/*
10 Copyright (C) 2004- 2009, Hammersmith Imanet Ltd
11 Copyright (C) 2024, University College London
12 This file is part of STIR.
13
14 SPDX-License-Identifier: Apache-2.0
15
16 See STIR/LICENSE.txt for details
17*/
18#include "stir/Array.h"
19#include "stir/Succeeded.h"
20#include "stir/ByteOrder.h"
21#include "stir/warning.h"
22#include <fstream>
23
24START_NAMESPACE_STIR
25
26namespace detail
27{
28
29/***************** version for istream *******************************/
30
31template <int num_dimensions, class elemT>
32Succeeded
33read_data_1d(std::istream& s, ArrayType<num_dimensions, elemT>& data, const ByteOrder byte_order)
34{
35 if (!s || (dynamic_cast<std::ifstream*>(&s) != 0 && !dynamic_cast<std::ifstream*>(&s)->is_open())
36 || (dynamic_cast<std::fstream*>(&s) != 0 && !dynamic_cast<std::fstream*>(&s)->is_open()))
37 {
38 warning("read_data: error before reading from stream.\n");
39 return Succeeded::no;
40 }
41
42 // note: find num_to_read (using size()) outside of s.read() function call
43 // otherwise Array::check_state() in size() might abort if
44 // get_data_ptr() is called before size() (which is compiler dependent)
45 const std::streamsize num_to_read = static_cast<std::streamsize>(data.size_all()) * sizeof(elemT);
46 s.read(reinterpret_cast<char*>(data.get_full_data_ptr()), num_to_read);
48
49 if (!s)
50 {
51 warning("read_data: error after reading from stream.\n");
52 return Succeeded::no;
53 }
54
55 if (!byte_order.is_native_order())
56 {
57 for (auto iter = data.begin_all(); iter != data.end_all(); ++iter)
59 }
60
61 return Succeeded::yes;
62}
63
64/***************** version for FILE *******************************/
65// largely a copy of above, but with calls to stdio function
66
67template <int num_dimensions, class elemT>
69read_data_1d(FILE*& fptr_ref, ArrayType<num_dimensions, elemT>& data, const ByteOrder byte_order)
70{
71 FILE* fptr = fptr_ref;
72 if (fptr == NULL || ferror(fptr))
73 {
74 warning("read_data: error before reading from FILE.\n");
75 return Succeeded::no;
76 }
77
78 // note: find num_to_read (using size()) outside of s.read() function call
79 // otherwise Array::check_state() in size() might abort if
80 // get_data_ptr() is called before size() (which is compiler dependent)
81 const std::size_t num_to_read = static_cast<std::size_t>(data.size_all());
82 const std::size_t num_read = fread(reinterpret_cast<char*>(data.get_full_data_ptr()), sizeof(elemT), num_to_read, fptr);
84
85 if (ferror(fptr) || num_to_read != num_read)
86 {
87 warning("read_data: error after reading from FILE.\n");
88 return Succeeded::no;
89 }
90
91 if (!byte_order.is_native_order())
92 {
93 for (auto iter = data.begin_all(); iter != data.end_all(); ++iter)
94 ByteOrder::swap_order(*iter);
95 }
96
97 return Succeeded::yes;
98}
99
100} // end of namespace detail
101END_NAMESPACE_STIR
defines the stir::Array class for multi-dimensional (numeric) arrays
This file declares the stir::ByteOrder class.
Declaration of class stir::Succeeded.
full_iterator begin_all()
start value for iterating through all elements in the array, see full_iterator
Definition Array.inl:213
size_t size_all() const
return the total number of elements in this array
Definition Array.inl:262
void release_full_data_ptr()
signal end of access to elemT*
Definition Array.inl:329
elemT * get_full_data_ptr()
member function for access to the data via a elemT*
Definition Array.inl:292
full_iterator end_all()
end value for iterating through all elements in the array, see full_iterator
Definition Array.inl:185
This class provides member functions to find out what byte-order your machine is and to swap numbers.
Definition ByteOrder.h:100
bool is_native_order() const
check if the object refers to the native order.
Definition ByteOrder.inl:39
static void swap_order(NUMBER &value)
swap the byteorder of the argument
Definition ByteOrder.h:122
a class containing an enumeration type that can be used by functions to signal successful operation o...
Definition Succeeded.h:44
Succeeded read_data_1d(std::istream &s, ArrayType< num_dimensions, elemT > &data, const ByteOrder byte_order)
This is an internal function called by read_data(). It does the actual reading to std::istream.
Definition read_data_1d.inl:33
void warning(const char *const s,...)
Print warning with format string a la printf.
Definition warning.cxx:41
Array< num_dimensions, elemT > ArrayType
type alias for future-proofing for "large" rectangular arrays
Definition ArrayFwd.h:25
Declaration of stir::warning()