Exemple #1
0
/** \brief Create a Zip archive from the specified FileCollection.
 *
 * This function is expected to be used with a DirectoryCollection
 * that you created to save the collection in an archive.
 *
 * \param[in,out] os  The output stream where the Zip archive is saed.
 * \param[in] collection  The collection to save in this output stream.
 * \param[in] zip_comment  The global comment of the Zip archive.
 */
void ZipFile::saveCollectionToArchive(std::ostream & os, FileCollection & collection, std::string const & zip_comment)
{
    try
    {
        ZipOutputStream output_stream(os);

        output_stream.setComment(zip_comment);

        FileEntry::vector_t entries(collection.entries());
        for(auto it(entries.begin()); it != entries.end(); ++it)
        {
            output_stream.putNextEntry(*it);
            // get an InputStream if available (i.e. directories do not have an input stream)
            if(!(*it)->isDirectory())
            {
                FileCollection::stream_pointer_t is(collection.getInputStream((*it)->getName()));
                if(is)
                {
                    output_stream << is->rdbuf();
                }
            }
        }

        // clean up mantually so we can get any exception
        // (so we avoid having exceptions gobbled by the destructor)
        output_stream.closeEntry();
        output_stream.finish();
        output_stream.close();
    }
    catch(...)
    {
        os.setstate(std::ios::failbit);
        throw;
    }
}
Exemple #2
0
// Save, change, and restore stream properties
void
saver_tests_1
(
    std::istream &  input,
    std::ostream &  output,
    std::ostream &  err
)
{
    using std::locale;
    using std::ios_base;
    using std::setw;

    boost::io::ios_flags_saver const      ifls( output );
    boost::io::ios_precision_saver const  iprs( output );
    boost::io::ios_width_saver const      iws( output );
    boost::io::ios_tie_saver const        its( input );
    boost::io::ios_rdbuf_saver const      irs( output );
    boost::io::ios_fill_saver const       ifis( output );
    boost::io::ios_locale_saver const     ils( output );
    boost::io::ios_iword_saver const      iis( output, my_index );
    boost::io::ios_pword_saver const      ipws( output, my_index );

    locale  loc( locale::classic(), new backward_bool_names );

    input.tie( &err );
    output.rdbuf( err.rdbuf() );
    output.iword( my_index ) = 69L;
    output.pword( my_index ) = &err;

    output << "The data is (again):\n";
    output.setf( ios_base::showpos | ios_base::boolalpha );
    output.setf( ios_base::internal, ios_base::adjustfield );
    output.fill( '@' );
    output.precision( 9 );
    output << '\t' << test_string << '\n';
    output << '\t' << setw( 10 ) << test_num1 << '\n';
    output << '\t' << setw( 15 ) << test_num2 << '\n';
    output.imbue( loc );
    output << '\t' << test_bool << '\n';

    BOOST_CHECK( &err == output.pword(my_index) );
    BOOST_CHECK( 69L == output.iword(my_index) );

    try
    {
        boost::io::ios_exception_saver const  ies( output );
        boost::io::ios_iostate_saver const    iis( output );

        output.exceptions( ios_base::eofbit );
        output.setstate( ios_base::eofbit );
        BOOST_ERROR( "previous line should have thrown" );
    }
    catch ( ios_base::failure &f )
    {
        err << "Got the expected I/O failure: \"" << f.what() << "\".\n";
        BOOST_CHECK( output.exceptions() == ios_base::goodbit );
    }
    catch ( ... )
    {
        err << "Got an unknown error when doing exception test!\n";
        throw;
    }
}