Panda3D
copy_stream.cxx
1 // Filename: copy_stream.cxx
2 // Created by: drose (27Aug09)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "copy_stream.h"
16 
17 ////////////////////////////////////////////////////////////////////
18 // Function: copy_stream
19 // Access: Published
20 // Description: Reads the source stream from its current position to
21 // the end of the stream, and writes that data to the
22 // dest stream at its current position. Returns true on
23 // success, false on failure.
24 ////////////////////////////////////////////////////////////////////
25 bool
26 copy_stream(istream &source, ostream &dest) {
27  static const size_t buffer_size = 4096;
28  char buffer[buffer_size];
29 
30  source.read(buffer, buffer_size);
31  size_t count = source.gcount();
32  while (count != 0) {
33  dest.write(buffer, count);
34  source.read(buffer, buffer_size);
35  count = source.gcount();
36  }
37 
38  return (!source.fail() || source.eof()) && (!dest.fail());
39 }