aimsdata 6.0.0
Neuroimaging data handling
bckW.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 AIMS_IO_BCKW_H
35#define AIMS_IO_BCKW_H
36
37#include <aims/io/bckItemW.h>
38#include <aims/bucket/bucket.h>
39#include <aims/io/bckheader.h>
41#include <cartobase/exception/ioexcept.h>
42#include <fstream>
43
44
45namespace aims
46{
47
48 template<class T> class BckWriter
49 {
50 public:
51 BckWriter( const std::string& name, bool ascii = false )
52 : _name( name ), _itemw( 0 ), _ascii( ascii ) {}
53 ~BckWriter() { delete _itemw; }
54
55 void write( const AimsBucket<T> & thing );
57 { delete _itemw; _itemw = iw; }
58
59 private:
60 std::string _name;
62 bool _ascii;
63 };
64
65
66 template<class T> inline
67 void BckWriter<T>::write( const AimsBucket<T> & thing )
68 {
69 std::string openmode = ( _ascii ? "ascii" : "binar" );
71 BckHeader hdr( _name, dt.name(), thing.sizeX(), thing.sizeY(),
72 thing.sizeZ(), thing.sizeT(), AIMS_MAGIC_NUMBER,
73 openmode );
74 hdr.copy( thing.header() );
75 hdr.write();
76
77 std::ios::openmode omd = std::ios::app;
78 if ( !_ascii )
79 omd |= std::ios::binary;
80 std::ofstream os( hdr.filename().c_str(), omd );
81 if ( !os )
83
84 if ( !_itemw )
86 std::unique_ptr< ItemWriter<AimsBucketItem<T> > > iw( _itemw->writer( openmode, false ) );
88
89 typename AimsBucket<T>::const_iterator ib, eb = thing.end();
90 typename std::list<AimsBucketItem<T> >::const_iterator ibi, ebi;
91
92 if ( _ascii )
93 os << "-dimt " << thing.size() << std::endl;
94 else
95 sw.write( os, (uint32_t)thing.size() );
96
97 for ( ib=thing.begin(); ib!=eb; ++ib )
98 {
99 typename std::list< AimsBucketItem<T> >::size_type n = ib->second.size();
100 if ( _ascii )
101 {
102 os << "-time " << ib->first << std::endl;
103 os << "-dim " << n << std::endl;
104 }
105 else
106 {
107 sw.write( os, (uint32_t)ib->first );
108 sw.write( os, (uint32_t)n );
109 }
110
111 const std::list<AimsBucketItem<T> > & items = ib->second;
112 for ( ibi=items.begin(), ebi=items.end(); ibi!=ebi; ++ibi )
113 iw->write( os, *ibi );
114 }
115 }
116
117}
118
119
120template<class T> inline
122 const AimsBucket<T> & thing )
123{
124 writer.write( thing );
125 return writer;
126}
127
128
129#endif
aims::BckWriter< T > & operator<<(aims::BckWriter< T > &writer, const AimsBucket< T > &thing)
Definition bckW.h:121
The bucket base class to manage packages of points associated to their value during time.
Definition bucket.h:70
float sizeX() const
returns the X resolution in mm
Definition bucket.h:218
float sizeY() const
returns the Y resolution in mm
Definition bucket.h:228
float sizeZ() const
returns the Z resolution in mm
Definition bucket.h:238
float sizeT() const
returns the T resolution in mm
Definition bucket.h:248
std::map< int, std::list< AimsBucketItem< T > > >::const_iterator const_iterator
Definition bucket.h:76
const aims::PythonHeader & header() const
Definition bucket.h:147
Descriptor class for the .bck BUCKET file format header.
Definition bckheader.h:52
std::string filename() const
void setItemWriter(ItemWriter< T > *iw)
Definition bckW.h:56
BckWriter(const std::string &name, bool ascii=false)
Definition bckW.h:51
void write(const AimsBucket< T > &thing)
Definition bckW.h:67
Default low-levels writers.
virtual void write(std::ostream &os, const T &item) const
Low-level "small item" writer, used by higher-level file readers.
Definition itemW.h:51
virtual void copy(const PythonHeader &, bool keepUuid=false)
std::string name()
static void launchErrnoExcept(const std::string &filename="")
#define AIMS_MAGIC_NUMBER
Definition general.h:51
The class for EcatSino data write operation.