Logo Search packages:      
Sourcecode: libgdf version File versions  Download package

void gdf::Writer::open ( const int  flags = writer_ev_file )

Opens file for writing and writes Header.

Prior to opening the file, GDFHeaderAccess::sanitize() is called. Exceptions thrown by GDFHeaderAccess::sanitize() are checked if there are errors and/or warnings. In the presence of errors the exception is forwarded immediately, and in case of warnings the file is opened but the exception is still forwarded. If the file exists, it is not opened unless the file_overwrite flag is set.

Parameters:
[in]flagsFlags...
Exceptions:
exception::header_issues
exception::file_exists

Definition at line 47 of file Writer.cpp.

References gdf::RecordBuffer::reset(), gdf::GDFHeaderAccess::sanitize(), and gdf::GDFHeaderAccess::setLock().

Referenced by open().

    {
        if( m_file.is_open() )
            throw exception::file_open( "" );

        assert( !m_file.is_open() );
        m_header.setLock( true );

        bool warn = false;
        std::list< std::string > wmsg;
        try {
            m_header.sanitize( );
        } catch( exception::header_issues &e )
        {
            wmsg = e.warnings;
            if( e.errors.size() != 0 )
                throw e;
            if( e.warnings.size() != 0 )
                warn = true;
        }

        if( !(flags & writer_overwrite) )
        {
            m_file.open( m_filename.c_str(), std::ios_base::in );
            if( !m_file.fail() )
            {
                m_header.setLock( false );
                m_file.close( );
                throw exception::file_exists( m_filename );
            }
        }

        m_file.clear( );

        m_file.open( m_filename.c_str(), std::ios_base::out | std::ios_base::binary | std::ios_base::trunc );

        if( m_file.fail() )
        {
            throw std::invalid_argument( "Error opening file for writing." );
        }

        m_eventbuffermemory = flags & writer_ev_memory;
        if( m_eventbuffermemory )
        {
            m_eventbuffer.rdbuf( m_evbuf_memory.rdbuf() );
            m_evbuf_memory.clear( );
        }
        else
        {
            m_evbuf_file.open( (m_filename+".events").c_str(), std::ios_base::out | std::ios_base::binary | std::ios_base::trunc );
            m_eventbuffer.rdbuf( m_evbuf_file.rdbuf() );
        }

        m_recbuf.reset( );
        m_num_datarecords = 0;

        m_file << m_header;
        m_file.flush( );

        if( warn )
            throw exception::header_issues( wmsg );
    }

Here is the call graph for this function:

Here is the caller graph for this function:


Generated by  Doxygen 1.6.0   Back to index