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 » Community » Newbie corner » How to use StringStream?
How to use StringStream? [message #38961] Tue, 29 January 2013 22:51 Go to next message
lectus is currently offline  lectus
Messages: 329
Registered: September 2006
Location: Brazil
Senior Member
I csn't find any example of it.

I'm trying to use it together with ZCompress.

Thanks

[Updated on: Tue, 29 January 2013 22:52]

Report message to a moderator

Re: How to use StringStream? [message #38979 is a reply to message #38961] Sat, 02 February 2013 21:56 Go to previous message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

What exactly are you trying to do?
If you want to make "streaming" zip/unzip operation, then you may meet some problems here. Anyway, I really tried to use StringStream as simple FIFO buffer for the same thing, and it didn't work as I expected.

So I had to write my own string buffer class which is String-based Stream compatible FIFO-buffer.

This is simplified code how to use it for streaming unzip operation:

FileUnZip unzip(filename);
StrIOStream unzipStream; //my simple String-based FIFO class
unzip.ReadFile(unzipStream, callback(this, &MyClass::OnZipData));
while (unzipStream.GetSize())
    ProcessChunk(&unzipStream);
...

bool MyClass::OnZipData(int op1, int op2)
{
    ProcessChunk(&unzipStream);
    return isShutdown; //usually false
}

void MyClass::ProcessChunk(Stream *stream)
{
    String data = stream->Get(static_cast<int>(stream->GetSize())); //get unzipped data
    ... //process the chunk of unzipped data
}


The idea is that ProcessChunk() is called from within unzip cycle for streaming. For the last part of unzipped data, the ProcessChunk() is not called automatically, so we call it manually AFTER unzipping.

My tests for the BIG files (about 3 GB in unzipped size) had shown that the maximum size of unzipped chunk could be ~60 MB. So you have to take it into account.

Finally, if you need it, here is the source code of my Stream-based class:
class StrIOStream : public Stream 
{
protected:
	virtual  void  _Put(int w)
	{
		data.Cat(w);
	}
	
	virtual  int   _Term()
	{
		return data.IsEmpty() ? -1 : data[0];
	}
	
	virtual  int   _Get()
	{
		if (data.IsEmpty())
		return -1;
		
		int w = data[0];
		data.Remove(0);
		return w;
	}
	
	virtual  void  _Put(const void *_data, dword size)
	{
		data.Cat((const char *) _data,size);
	}
	
	virtual  dword _Get(void *_data, dword size)
	{
		dword l = min(size,(dword)data.GetLength());
		memcpy(_data,~data,l);
		data.Remove(0,l);
		return l;
	}

public:
	virtual  void  Seek(int64 pos) {}
	virtual  int64 GetSize() const {return data.GetLength();}
	virtual  void  SetSize(int64 size)
	{
		if (size == data.GetLength())
			return;
		if (size > data.GetLength())
			data.Cat('?', static_cast<int>(size-data.GetLength()));
		else
			data.Trim(static_cast<int>(size));
	}
	virtual  bool  IsOpen() const {return true;}

protected:
	String         data;
	
public:
	void        Open(const String& _data) {data = _data;}
	void        Create() {}
	void        Reserve(int n) {data.Reserve(n);}

	String      GetResult() {return data;}
	operator    String()                     { return data; }
};

[Updated on: Sat, 02 February 2013 21:56]

Report message to a moderator

Previous Topic: Deploying U++ Application
Next Topic: LineEdit event on click on scrollbar
Goto Forum:
  


Current Time: Fri Apr 19 01:23:40 CEST 2024

Total time taken to generate the page: 0.04605 seconds