Home » Extra libraries, Code snippets, applications etc. » U++ users applications in progress and useful code snippets, including reference examples! » Simple generic Copy function for e.g. copying between a Stream and a TcpSocket or anything
Simple generic Copy function for e.g. copying between a Stream and a TcpSocket or anything [message #58755] |
Sat, 20 August 2022 02:13 |
zsolt
Messages: 702 Registered: December 2005 Location: Budapest, Hungary
|
Contributor |
|
|
... anything, implementing Get(), Put() and IsError().
I'm using it to write data coming from a socket to a file or vice versa.
template<class DEST, class SRC>
int64 CopyGeneric(DEST& dest, SRC& src, int64 count = INT64_MAX, Gate<int64, int64> progress = Null, int chunk_size = 1024 * 64)
{
int block = (int)min<int64>(count, chunk_size);
Buffer<byte> temp(block);
int loaded;
int64 done = 0;
int64 total = count;
while(count > 0 && (loaded = src.Get(~temp, (int)min<int64>(count, block))) > 0) {
dest.Put(~temp, loaded);
if(dest.IsError() || src.IsError())
return -1;
count -= loaded;
done += loaded;
if(progress(done, total))
return -1;
}
return done;
}
|
|
|
Goto Forum:
Current Time: Fri May 09 12:03:09 CEST 2025
Total time taken to generate the page: 0.02871 seconds
|