soma-io 6.0.6
itemreader.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#ifndef SOMAIO_READER_ITEMREADER_H
35#define SOMAIO_READER_ITEMREADER_H
36//--- soma-io ----------------------------------------------------------------
40//--- cartobase --------------------------------------------------------------
41#include <cartobase/config/cartobase_config.h>
42//--- system -----------------------------------------------------------------
43#include <algorithm>
44//----------------------------------------------------------------------------
45
46namespace soma
47{
48
83 template<typename T>
85 {
86 public:
87 virtual ~ItemReader() {}
88
90 virtual ItemReader<T>* reader( bool binary = true,
91 bool bswap = false ) const = 0;
92
98 virtual long read( DataSource & ds, T* pitem, size_t n = 1 ) const = 0;
99 };
100
104 template <typename T>
106 {
107 public:
109 virtual ~DefaultItemReader() { }
110
111 virtual ItemReader<T>* reader( bool binary = true,
112 bool bswap = false ) const;
113
114 virtual long read( DataSource & ds, T* pitem, size_t n = 1 ) const;
115 };
116
117
118 template <typename T>
120 {
121 public:
124
125 virtual long read( DataSource & ds, T* pitem, size_t n = 1 ) const;
126 bool readOne( DataSource & ds, T* pitem ) const;
127 };
128
129
130 template <typename T>
132 {
133 public:
136
137 virtual long read( DataSource & ds, T* pitem, size_t n = 1 ) const;
138 };
139
140
141 //==========================================================================
142 // I M P L E M
143 //==========================================================================
144
145 template <typename T>
146 inline
147 ItemReader<T>* DefaultItemReader<T>::reader( bool binary, bool bswap ) const
148 {
149 if( !binary )
150 return new DefaultAsciiItemReader<T>;
151 if( bswap )
152 return new DefaultBSwapItemReader<T>;
153 return new DefaultItemReader<T>;
154 }
155
156
157 template <>
158 inline
160 bool ) const
161 {
162 if( !binary )
164 return new DefaultItemReader<int8_t>;
165 }
166
167
168 template <>
169 inline
171 bool ) const
172 {
173 if( !binary )
176 }
177
178
179 template <typename T>
180 inline
182 size_t n ) const
183 {
184 // warning: alignment problems not taken into account here
185 long len = ds.readBlock( reinterpret_cast<char*>( pitem ),
186 n * sizeof(T) );
187 return len / sizeof(T);
188 }
189
190
191 template <typename T>
192 inline
194 size_t n ) const
195 {
196 // warning: alignment problems not taken into account here
197 long len = ds.readBlock( reinterpret_cast<char*>( pitem ), n * sizeof(T) );
198 uint8_t *ptr = reinterpret_cast<uint8_t*>( pitem );
199 for( long k=0; k<len; k+=sizeof(T) )
200 for( size_t b=0; b<sizeof(T)/2; ++b )
201 std::swap( ptr[k+b], ptr[k+sizeof(T)-1-b] );
202 return len / sizeof(T);
203 }
204
205
206 template <typename T>
207 inline
209 size_t n ) const
210 {
211 size_t i;
212 int c;
213 T *ptr = pitem;
214
215 for( i=0; i<n; ++i, ++ptr )
216 {
217 // skip formatting chars
218 do
219 {
220 c = ds.getch();
221 }
222 while( ( c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == ',' )
223 && ds.isOpen() );
224 if( !ds.isOpen() )
225 break;
226 ds.ungetch( c );
227 if( !readOne( ds, ptr ) )
228 break;
229 }
230 return i;
231 }
232
233
234 template <typename T>
235 inline
237 {
238 return AsciiDataSourceTraits<T>::read( ds, *pitem );
239 }
240
241
242}
243
244
245#endif
static bool read(DataSource &ds, T &item)
Abstraction layer for various data sources (file, buffer, socket...).
Definition datasource.h:65
virtual int getch()=0
virtual bool ungetch(int ch)=0
virtual long readBlock(char *data, unsigned long maxlen)=0
virtual bool isOpen() const =0
virtual long read(DataSource &ds, T *pitem, size_t n=1) const
Reading on an arbitrary DataSource.
Definition itemreader.h:208
bool readOne(DataSource &ds, T *pitem) const
Definition itemreader.h:236
virtual long read(DataSource &ds, T *pitem, size_t n=1) const
Reading on an arbitrary DataSource.
Definition itemreader.h:193
virtual ItemReader< T > * reader(bool binary=true, bool bswap=false) const
Factory function.
Definition itemreader.h:147
virtual long read(DataSource &ds, T *pitem, size_t n=1) const
Reading on an arbitrary DataSource.
Definition itemreader.h:181
Low-level "small item" reader, used by higher-level file readers.
Definition itemreader.h:85
virtual ItemReader< T > * reader(bool binary=true, bool bswap=false) const =0
Factory function.
virtual ~ItemReader()
Definition itemreader.h:87
virtual long read(DataSource &ds, T *pitem, size_t n=1) const =0
Reading on an arbitrary DataSource.