aimsdata 6.0.0
Neuroimaging data handling
defaultItemR.h
Go to the documentation of this file.
1/* This software and supporting documentation are distributed by
2 * Institut Federatif de Recherche 49
3 * CEA/NeuroSpin, Batiment 145,
4 * 91191 Gif-sur-Yvette cedex
5 * France
6 *
7 * This software is governed by the CeCILL-B license under
8 * French law and abiding by the rules of distribution of free software.
9 * You can use, modify and/or redistribute the software under the
10 * terms of the CeCILL-B license as circulated by CEA, CNRS
11 * and INRIA at the following URL "http://www.cecill.info".
12 *
13 * As a counterpart to the access to the source code and rights to copy,
14 * modify and redistribute granted by the license, users are provided only
15 * with a limited warranty and the software's author, the holder of the
16 * economic rights, and the successive licensors have only limited
17 * liability.
18 *
19 * In this respect, the user's attention is drawn to the risks associated
20 * with loading, using, modifying and/or developing or reproducing the
21 * software by the user in light of its specific status of free software,
22 * that may mean that it is complicated to manipulate, and that also
23 * therefore means that it is reserved for developers and experienced
24 * professionals having in-depth computer knowledge. Users are therefore
25 * encouraged to load and test the software's suitability as regards their
26 * requirements in conditions enabling the security of their systems and/or
27 * data to be ensured and, more generally, to use and operate it in the
28 * same conditions as regards security.
29 *
30 * The fact that you are presently reading this means that you have had
31 * knowledge of the CeCILL-B license and that you accept its terms.
32 */
33
34/*
35 * Data item reader class
36 */
37#ifndef AIMS_IO_DEFAULTITEMR_H
38#define AIMS_IO_DEFAULTITEMR_H
39
40#include <aims/def/general.h>
41#include <aims/vector/vector.h>
42#include <aims/io/itemR.h>
44#include <algorithm>
45
46
47namespace aims
48{
49
54 template <class T>
55 class DefaultItemReader : public ItemReader<T>
56 {
57 public:
59 virtual ~DefaultItemReader() { }
60
61 virtual void read( std::istream & is, T& item ) const
62 { read( is, &item, 1 ); }
63 virtual void read( std::istream & is, T* pitem, size_t n ) const;
64 virtual ItemReader<T>* reader( const std::string & openmode = "binar",
65 bool bswap = false ) const;
66 inline void read( const std::string &istring, T &item ) const
67 { ItemReader<T>::read( istring, item ); }
68 inline void read( const std::string &istring, T *pitem, size_t n ) const
69 { ItemReader<T>::read( istring, pitem, n ); }
70 };
71
72
73 template <class T>
75 {
76 public:
79
80 virtual void read( std::istream & is, T& item ) const;
81 virtual void read( std::istream & is, T* pitem, size_t n ) const;
82 inline void read( const std::string &istring, T &item ) const
83 { ItemReader<T>::read( istring, item ); }
84 inline void read( const std::string &istring, T *pitem, size_t n ) const
85 { ItemReader<T>::read( istring, pitem, n ); }
86 };
87
88
89 template <class T>
91 {
92 public:
95
96 virtual void read( std::istream & is, T& item ) const
97 { read( is, &item, 1 ); }
98 virtual void read( std::istream & is, T* pitem, size_t n ) const;
99 inline void read( const std::string &istring, T &item ) const
100 { ItemReader<T>::read( istring, item ); }
101 inline void read( const std::string &istring, T *pitem, size_t n ) const
102 { ItemReader<T>::read( istring, pitem, n ); }
103 };
104
105
106#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
107
109 template <class T, int D>
111 : public ItemReader<AimsVector<T,D> >
112 {
113 public:
115 virtual ~DefaultItemReader() { }
116
117 virtual void read( std::istream & is, AimsVector<T,D> & item ) const
118 { read( is, &item, 1 ); }
119 virtual void read( std::istream & is, AimsVector<T,D> * pitem,
120 size_t n ) const;
121 virtual ItemReader<AimsVector<T,D> >* reader( const std::string & openmode
122 = "binar",
123 bool bswap = false ) const;
124 inline void read( const std::string &istring, T &item ) const
125 { ItemReader<T>::read( istring, item ); }
126 inline void read( const std::string &istring, T *pitem, size_t n ) const
127 { ItemReader<T>::read( istring, pitem, n ); }
128 };
129
130
132 template <class T, int D>
134 : public DefaultItemReader<AimsVector<T,D> >
135 {
136 public:
139
140 virtual void read( std::istream & is, AimsVector<T,D> & item ) const
141 { read( is, &item, 1 ); }
142 virtual void read( std::istream & is, AimsVector<T,D> * pitem,
143 size_t n ) const;
144 inline void read( const std::string &istring, T &item ) const
145 { ItemReader<T>::read( istring, item ); }
146 inline void read( const std::string &istring, T *pitem, size_t n ) const
147 { ItemReader<T>::read( istring, pitem, n ); }
148 };
149
150#endif // DOXYGEN_HIDE_INTERNAL_CLASSES
151
152
153 template<class T> ItemReader<T>*
154 DefaultItemReader<T>::reader( const std::string & openmode,
155 bool bswap ) const
156 {
157 if( openmode == "ascii" )
158 return( new DefaultAsciiItemReader<T> );
159 if( bswap )
160 return( new DefaultBSwapItemReader<T> );
161 return( new DefaultItemReader<T> );
162 }
163
164
165 template <class T> inline void
166 DefaultItemReader<T>::read( std::istream & is, T* pitem, size_t n ) const
167 {
168 is.read( (char *) pitem, sizeof(T) * n );
169 }
170
171
172 template <class T> inline
173 void DefaultAsciiItemReader<T>::read( std::istream & is, T& item ) const
174 {
175 carto::StreamUtil::skip( is, " \t\n\r,", true );
176 is >> item;
177 }
178
179
180 template <class T> inline
181 void DefaultAsciiItemReader<T>::read( std::istream & is, T* pitem,
182 size_t n ) const
183 {
184 T* ptr = pitem;
185 for( size_t i=0; i<n; ++i, ++ptr )
186 read( is, *ptr );
187 }
188
189
190 template <class T> inline
191 void DefaultBSwapItemReader<T>::read( std::istream & is, T* pitem,
192 size_t n ) const
193 {
194 is.read( (char *) pitem, sizeof(T) * n );
195 byte* ptr = (byte*) pitem;
196 for( size_t k=0; k<n*sizeof(T); k+=sizeof(T) )
197 for( size_t b=0; b<sizeof(T)/2; ++b )
198 std::swap( ptr[k+b], ptr[k+sizeof(T)-1-b] );
199 }
200
201
202 // partial specializations
203
204 template<class T, int D> ItemReader<AimsVector<T, D> >*
205 DefaultItemReader<AimsVector<T, D> >::reader( const std::string & openmode,
206 bool bswap ) const
207 {
208 if( openmode == "ascii" )
210 if( bswap )
212 return( new DefaultItemReader<AimsVector<T, D> > );
213 }
214
215
216 template <class T, int D> inline
218 AimsVector<T, D>* pitem,
219 size_t n ) const
220 {
222 AimsVector<T,D> *ptr = pitem;
223
224 for( size_t p=0; p<n; ++p, ++ptr )
225 //for( i=0; i<D; ++i )
226 ir.read( is, &(*ptr)[0], D );
227 }
228
229
230 template <class T, int D> inline void
232 AimsVector<T, D>* pitem,
233 size_t n ) const
234 {
236 AimsVector<T,D> *ptr = pitem;
237
238 for( size_t p=0; p<n; ++p, ++ptr )
239 //for( i=0; i<D; ++i )
240 ir.read( is, &(*ptr)[0], D );
241 }
242
243
244 // specializations
245
246 template <> inline
247 void DefaultItemReader<Void>::read( std::istream &, Void & ) const
248 {
249 }
250
251
252 template <> inline
253 void DefaultItemReader<Void>::read( std::istream &, Void*, size_t ) const
254 {
255 }
256
257
258 template <> inline void
259 DefaultAsciiItemReader<Void>::read( std::istream & , Void & ) const
260 {
261 }
262
263
264 template <> inline void
265 DefaultAsciiItemReader<Void>::read( std::istream & , Void*, size_t ) const
266 {
267 }
268
269
270 template <> inline void
271 DefaultBSwapItemReader<Void>::read( std::istream &, Void & ) const
272 {
273 }
274
275
276 template <> inline void
277 DefaultBSwapItemReader<Void>::read( std::istream &, Void*, size_t ) const
278 {
279 }
280
281
282 template <> inline
283 void DefaultBSwapItemReader<int8_t>::read( std::istream & is, int8_t* pitem,
284 size_t n ) const
285 {
286 is.read( (char *) pitem, n );
287 }
288
289
290 template <> inline
292 uint8_t* pitem,
293 size_t n ) const
294 {
295 is.read( (char *) pitem, n );
296 }
297
298
299 // ### remove after everything has been moved to intN_t/uintN_t
300#if !defined(__sun__) || !defined(_CHAR_IS_SIGNED)
301 template <> inline
302 void DefaultBSwapItemReader<char>::read( std::istream & is,
303 char* pitem,
304 size_t n ) const
305 {
306 is.read( (char *) pitem, n );
307 }
308#endif
309
310
311 template <> inline
312 void DefaultBSwapItemReader<bool>::read( std::istream & is, bool* pitem,
313 size_t n ) const
314 {
315 is.read( (char *) pitem, n );
316 }
317
318
319 template <> inline
320 void DefaultBSwapItemReader<cfloat>::read( std::istream & is, cfloat* pitem,
321 size_t n ) const
322 {
323 is.read( (char *) pitem, n );
324 byte *ptr = (byte*) pitem;
325 for( size_t k=0; k<n*sizeof(cfloat); k+=sizeof(float) )
326 for( size_t b=0; b<sizeof(float)/2; ++b )
327 std::swap( ptr[k+b], ptr[k+sizeof(float)-1-b] );
328 }
329
330
331 template <> inline void
333 size_t n ) const
334 {
335 is.read( (char *) pitem, n );
336 byte* ptr = (byte*) pitem;
337 for( size_t k=0; k<n*sizeof(cdouble); k+=sizeof(float) )
338 for( size_t b=0; b<sizeof(double)/2; ++b )
339 std::swap( ptr[k+b], ptr[k+sizeof(double)-1-b] );
340 }
341
342
343}
344
345
346#endif
347
virtual void read(std::istream &is, T *pitem, size_t n) const
void read(const std::string &istring, T *pitem, size_t n) const
void read(const std::string &istring, T &item) const
virtual void read(std::istream &is, T &item) const
void read(const std::string &istring, T &item) const
void read(const std::string &istring, T *pitem, size_t n) const
virtual void read(std::istream &is, AimsVector< T, D > &item) const
virtual void read(std::istream &is, T *pitem, size_t n) const
virtual void read(std::istream &is, T &item) const
void read(const std::string &istring, T *pitem, size_t n) const
void read(const std::string &istring, T &item) const
void read(const std::string &istring, T *pitem, size_t n) const
void read(const std::string &istring, T &item) const
virtual void read(std::istream &is, AimsVector< T, D > &item) const
virtual ItemReader< AimsVector< T, D > > * reader(const std::string &openmode="binar", bool bswap=false) const
void read(const std::string &istring, T *pitem, size_t n) const
virtual void read(std::istream &is, T *pitem, size_t n) const
virtual ItemReader< T > * reader(const std::string &openmode="binar", bool bswap=false) const
void read(const std::string &istring, T &item) const
virtual void read(std::istream &is, T &item) const
Low-level "small item" reader, used by higher-level file readers.
Definition itemR.h:99
virtual void read(std::istream &is, T &item) const
Definition itemR.h:103
The class for EcatSino data write operation.
std::complex< double > cdouble
std::complex< float > cfloat