You are not using the streaming nature of streams, so why bother using streams? This is the simplest solution you can use:
class Buffer {
public:
Buffer(int size = 256) : m_size(0), m_used(0), m_data(0){ resize(size); }
~Buffer() { if(m_data) free((void*)m_data); }
void write(const char *buf, int len) {
while(m_used+len<m_size) resize(m_size*2);
memcpy(m_data+m_used, buf, len);
m_used += len;
}
void dumpToStream(...) { /* implement yourself */ }
protected:
void resize(int s) {
if(!m_data) m_data = (char*)malloc(s); else m_data = (char*)realloc((void*)m_data, s);
}
private:
int m_size;
int m_used;
};
class Buffer {
public:
Buffer(int size = 256) : m_size(0), m_used(0), m_data(0){ resize(size); }
~Buffer() { if(m_data) free((void*)m_data); }
void write(const char *buf, int len) {
while(m_used+len<m_size) resize(m_size*2);
memcpy(m_data+m_used, buf, len);
m_used += len;
}
void dumpToStream(...) { /* implement yourself */ }
protected:
void resize(int s) {
if(!m_data) m_data = (char*)malloc(s); else m_data = (char*)realloc((void*)m_data, s);
}
private:
int m_size;
int m_used;
};
To copy to clipboard, switch view to plain text mode
And your solution might fail on \0 chars, as usual in such situations.
Bookmarks