libGDF
Writer.h
00001 //
00002 // This file is part of libGDF.
00003 //
00004 // libGDF is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU Lesser General Public License as
00006 // published by the Free Software Foundation, either version 3 of
00007 // the License, or (at your option) any later version.
00008 //
00009 // libGDF is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with libGDF.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // Copyright 2010 Martin Billinger
00018 
00019 #ifndef __WRITER_H_INCLUDED__
00020 #define __WRITER_H_INCLUDED__
00021 
00022 #include "GDF/RecordBuffer.h"
00023 #include "GDF/EventHeader.h"
00024 #include "GDF/GDFHeaderAccess.h"
00025 #include <string>
00026 #include <fstream>
00027 #include <iostream>
00028 #include <sstream>
00029 
00030 namespace gdf
00031 {
00032     enum WriterFlags
00033     {
00034         writer_ev_file      = 0,
00035         writer_ev_memory    = 1,
00036         writer_overwrite    = 2
00037     };
00038 
00040 
00044     class Writer : public RecordBuffer::RecordFullHandler
00045     {
00046     public:
00048         Writer( );
00049 
00051         virtual ~Writer( );
00052 
00054 
00063         void open( const int flags = writer_ev_file );
00064 
00066 
00072         void open( const std::string filename, const int flags = writer_ev_file );
00073 
00075 
00076         void close( );
00077 
00079         bool isOpen( );
00080 
00082 
00084         void setFilename( std::string filename );
00085 
00087 
00088         void setMaxFullRecords( size_t num );
00089 
00091 
00096         bool createSignal( size_t index, bool throwexc = false );
00097 
00099 
00103         void swapSignals( size_t a, size_t b );
00104 
00106 
00110         void relocateSignal( size_t src, size_t dst );
00111 
00113         size_t getFirstFreeSignalIndex( );
00114 
00116 
00123         void blitFromSerialBufferPhys( const double *buf, const std::vector<size_t> &samples_per_channel );
00124 
00126 
00132         void addSamplePhys( const size_t channel_idx, const float64 value );
00133 
00135 
00140         template<typename T> void addSampleRaw( const size_t channel_idx, const T value )
00141         {
00142             m_recbuf.addSampleRaw<T>( channel_idx, value );
00143         }
00144 
00146 
00153         void blitSamplesPhys( const size_t channel_idx, const float64 *values, size_t num );
00154 
00156 
00162         void blitSamplesPhys( const size_t channel_idx, const std::vector<float64> &values );
00163 
00165 
00171         template<typename T> void blitSamplesRaw( const size_t channel_idx, const T *values, size_t num )
00172         {
00173             m_recbuf.blitSamplesRaw<T>( channel_idx, values, num );
00174         }
00175 
00177 
00182         template<typename T> void blitSamplesRaw( const size_t channel_idx, const std::vector<T> &values )
00183         {
00184             m_recbuf.blitSamplesRaw<T>( channel_idx, &values[0], values.size() );
00185         }
00186 
00188         void addRecord( Record *r );
00189 
00191         Record *acquireRecord( );
00192 
00194         void writeRecordDirect( Record *r );
00195 
00197         void flush( );
00198 
00200 
00206         void setEventMode( uint8 mode );
00207 
00209 
00214         void setEventSamplingRate( float32 fs = -1 );
00215 
00217         void addEvent( const Mode1Event &ev );
00218 
00220         void addEvent( uint32 position, uint16 type );
00221 
00223         void addEvent( const Mode3Event &ev );
00224 
00226         void addEvent( uint32 position, uint16 type, uint16 channel, uint32 duration );
00227 
00229         void addEvent( uint32 position, uint16 type, uint16 channel, float32 value );
00230 
00232         const GDFHeaderAccess &getHeaderAccess_readonly( ) const { return m_header; }
00233 
00235         GDFHeaderAccess &getHeaderAccess( ) { return m_header; }
00236 
00238         const MainHeader &getMainHeader_readonly( ) const { return m_header.getMainHeader_readonly( ); }
00239 
00241         MainHeader &getMainHeader( ) { return m_header.getMainHeader( ); }
00242 
00244         const SignalHeader &getSignalHeader_readonly( size_t idx ) const { return m_header.getSignalHeader_readonly(idx); }
00245 
00246         inline size_t getNumSignals( ) const { return m_header.getNumSignals( ); }
00247 
00249         SignalHeader &getSignalHeader( size_t idx ) { return m_header.getSignalHeader(idx); }
00250 
00251     private:
00252 
00254         void writeRecord( );
00255 
00257         void writeEvents( );
00258 
00260         virtual void triggerRecordFull( Record *rec );
00261 
00262         RecordBuffer m_recbuf;
00263         GDFHeaderAccess m_header;
00264         std::fstream m_file;
00265         std::iostream m_eventbuffer;
00266         std::fstream m_evbuf_file;
00267         std::stringstream m_evbuf_memory;
00268         int m_eventbuffermemory;
00269         std::string m_filename;
00270         int64 m_num_datarecords;
00271         size_t max_full_records;
00272     };
00273 }
00274 
00275 #endif // __WRITER_H_INCLUDED__
 All Data Structures Functions Variables Friends