http://stxxl.sourceforge.net
<dementiev@mpi-sb.mpg.de>
<tb@panthema.net>
http://www.boost.org/LICENSE_1_0.txt
#include <stxxl/io>
#include <stxxl/vector>
#include <stxxl/stream>
void copy_file(const char* input_path, const char* output_path, unsigned int method)
{
using stxxl::file;
file::unlink(output_path);
stxxl::timer tm(true);
stxxl::syscall_file InputFile(input_path, file::RDONLY | file::DIRECT);
stxxl::syscall_file OutputFile(output_path, file::RDWR | file::CREAT | file::DIRECT);
typedef stxxl::vector<unsigned char> vector_type;
std::cout << "Copying file " << input_path << " to " << output_path << std::endl;
vector_type InputVector(&InputFile);
vector_type OutputVector(&OutputFile);
std::cout << "File " << input_path << " has size " << InputVector.size() << " bytes." << std::endl;
if (method == 1)
{
std::cout << "Using first method: copying vector elements." << std::endl;
for (vector_type::const_iterator it = InputVector.begin();
it != InputVector.end(); ++it)
{
OutputVector.push_back(*it);
}
}
else if (method == 2)
{
std::cout << "Using second method: vector_iterator2stream and vector_bufwriter." << std::endl;
OutputVector.resize(InputVector.size());
stxxl::stream::vector_iterator2stream<vector_type::const_iterator>
input(InputVector.begin(), InputVector.end());
vector_type::bufwriter_type writer(OutputVector.begin());
while (!input.empty())
{
writer << *input;
++input;
}
writer.finish();
}
else if (method == 3)
{
std::cout << "Using third method: vector_iterator2stream and materialize." << std::endl;
OutputVector.resize(InputVector.size());
stxxl::stream::vector_iterator2stream<vector_type::const_iterator>
input(InputVector.begin(), InputVector.end());
stxxl::stream::materialize(input, OutputVector.begin(), OutputVector.end());
}
std::cout << "Copied in " << tm.seconds() << " at "
<< (double)InputVector.size() / tm.seconds() / 1024 / 1024 << " MiB/s" << std::endl;
}
int main(int argc, char* argv[])
{
if (argc < 3)
{
std::cout << "Usage: " << argv[0] << " input_file output_file [method 1-3]" << std::endl;
return -1;
}
int method = (argc >= 4) ? atoi(argv[3]) : 3;
copy_file(argv[1], argv[2], method);
return 0;
}