Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 is currently offline  zsolt
Messages: 696
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;
}
Previous Topic: SUniGuiCreator
Next Topic: Using CoFor with SSH package to parallelize sftp file downloads.
Goto Forum:
  


Current Time: Sat Apr 20 01:24:34 CEST 2024

Total time taken to generate the page: 0.03099 seconds