http://stxxl.sourceforge.net
<dementiev@ira.uka.de>
http://www.boost.org/LICENSE_1_0.txt
#include <stxxl/io>
#include <stxxl/vector>
#include <stxxl/bits/mng/buf_ostream.h>
template <class ExtIterator>
class write_vector
{
typedef typename ExtIterator::size_type size_type;
typedef typename ExtIterator::value_type value_type;
typedef typename ExtIterator::block_type block_type;
typedef typename ExtIterator::const_iterator ConstExtIterator;
typedef stxxl::buf_ostream<block_type, typename ExtIterator::bids_container_iterator> buf_ostream_type;
ExtIterator it;
unsigned nbuffers;
buf_ostream_type * outstream;
public:
write_vector(ExtIterator begin,
unsigned nbuffers_
) : it(begin), nbuffers(nbuffers_)
{
outstream = new buf_ostream_type(it.bid(), nbuffers);
}
value_type & operator * ()
{
if (it.block_offset() == 0)
it.block_externally_updated();
return **outstream;
}
write_vector & operator ++ ()
{
++it;
++(*outstream);
return *this;
}
void flush()
{
ConstExtIterator const_out = it;
while (const_out.block_offset())
{
**outstream = *const_out;
++const_out;
++(*outstream);
}
it.flush();
delete outstream;
outstream = NULL;
}
virtual ~write_vector()
{
if (outstream)
flush();
}
};
typedef unsigned char my_type;
using stxxl::syscall_file;
using stxxl::file;
int main(int argc, char * argv[])
{
if (argc < 3)
{
std::cout << "Usage: " << argv[0] << " input_file output_file " << std::endl;
return -1;
}
unlink(argv[2]);
syscall_file InputFile(argv[1], file::RDONLY);
syscall_file OutputFile(argv[2], file::RDWR | file::CREAT);
typedef stxxl::vector<my_type> vector_type;
std::cout << "Copying file " << argv[1] << " to " << argv[2] << std::endl;
vector_type InputVector(&InputFile);
vector_type OutputVector(&OutputFile);
OutputVector.resize(InputVector.size());
std::cout << "File " << argv[1] << " has size " << InputVector.size() << " bytes." << std::endl;
vector_type::const_iterator it = InputVector.begin();
write_vector<vector_type::iterator> Writer(OutputVector.begin(), 2);
for ( ; it != InputVector.end(); ++it, ++Writer)
{
*Writer = *it;
}
Writer.flush();
return 0;
}