diff --git a/bazaar/BufferStream/BufferStream.cpp b/bazaar/BufferStream/BufferStream.cpp new file mode 100644 index 000000000..de48ec979 --- /dev/null +++ b/bazaar/BufferStream/BufferStream.cpp @@ -0,0 +1,52 @@ +#include "BufferStream.h" + + +void BufferStream::_Put(const void *data, dword size) +{ + if(size > (dword)(uintptr_t)(wrlim - ptr)) { + Reserve(size + 128); + } + memcpy(ptr, data, size); + ptr += size; +} + +void BufferStream::SetSize(int64 asize) +{ + dword size = (dword)asize; + dword p = (dword)(uintptr_t)(ptr - buffer); + data.SetCount(size); + Open(data); + SetStoring(); + Seek(min(p, size)); +} + +void BufferStream::Seek(int64 pos) +{ + dword size = (dword)GetSize(); + if(pos > size) { + size = (dword)pos; + SetSize(size + 100); + } + ptr = buffer + min(pos, int64(rdlim - buffer)); +} + +void BufferStream::Open(Vector & d) +{ + if(&data != &d) data = d; //pick + MemStream::Create((byte*)data, data.GetCount()); +} + +void BufferStream::Create() +{ + data.Clear(); + Open(data); + SetStoring(); +} + +Vector BufferStream::GetResult() +{ + data.SetCount((dword)(uintptr_t)(ptr - buffer)); + Vector d = data; //pick + Create(); + return d; +} diff --git a/bazaar/BufferStream/BufferStream.h b/bazaar/BufferStream/BufferStream.h new file mode 100644 index 000000000..944220f53 --- /dev/null +++ b/bazaar/BufferStream/BufferStream.h @@ -0,0 +1,34 @@ +#ifndef _BufferStream_BufferStream_h +#define _BufferStream_BufferStream_h + +#include + +using namespace Upp; + +class BufferStream : public MemStream { +protected: + virtual void _Put(int w) { byte h = w; _Put(&h, 1); } + virtual void _Put(const void *data, dword size); + +public: + virtual void Seek(int64 pos); + virtual void SetSize(int64 asize); + +protected: + Vector data; + +public: + void Open(Vector & d); + void Create(); + void Reserve(int n) { SetSize((int)GetSize() + n); } + + Vector GetResult(); + operator Vector() { return GetResult(); } + + BufferStream() { Create(); } + BufferStream(Vector& d) { Open(d); } +}; + +typedef BufferStream VectorStream; + +#endif diff --git a/bazaar/BufferStream/BufferStream.upp b/bazaar/BufferStream/BufferStream.upp new file mode 100644 index 000000000..816396833 --- /dev/null +++ b/bazaar/BufferStream/BufferStream.upp @@ -0,0 +1,11 @@ +uses + Core; + +file + BufferStream.h, + BufferStream.cpp, + src.tpp; + +mainconfig + "" = ""; + diff --git a/bazaar/BufferStream/init b/bazaar/BufferStream/init new file mode 100644 index 000000000..5d154e2e9 --- /dev/null +++ b/bazaar/BufferStream/init @@ -0,0 +1,4 @@ +#ifndef _BufferStream_icpp_init_stub +#define _BufferStream_icpp_init_stub +#include "Core/init" +#endif diff --git a/bazaar/BufferStream/src.tpp/BufferStream$en-us.tpp b/bazaar/BufferStream/src.tpp/BufferStream$en-us.tpp new file mode 100644 index 000000000..f3a32f6b9 --- /dev/null +++ b/bazaar/BufferStream/src.tpp/BufferStream$en-us.tpp @@ -0,0 +1,50 @@ +topic "class BufferStream : public MemStream"; +[ $$0,0#00000000000000000000000000000000:Default] +[i448;a25;kKO9; $$1,0#37138531426314131252341829483380:structitem] +[l288;2 $$2,0#27521748481378242620020725143825:desc] +[0 $$3,0#96390100711032703541132217272105:end] +[H6;0 $$4,0#05600065144404261032431302351956:begin] +[i448;a25;kKO9;2 $$5,0#37138531426314131252341829483370:codeitem] +[{_} +[s1;:BufferStream`:`:class: [@(0.0.255) class]_[* BufferStream]_:_[@(0.0.255) public]_[*@3 Mem +Stream]&] +[s2;%% A Stream based on a Vector buffer, beeing able to open +such a Vector to Loading or directly serializing to such +a Vector.It automatically increases the underlying Vector. It +uses `_pick semantics to open a Vector and returns the +underlying Vector as `_pick (leaving a new instance under +hood, so new buffering is possible as well)&] +[s3; &] +[s4; &] +[s5;:BufferStream`:`:Open`(Vector``&`): [@(0.0.255) void]_[* Open]([_^Vector^ Vector +]<[_^byte^ byte]>_`&_[*@3 d])&] +[s2;%% Picks the Vector [%-*@3 d].as underlying buffer. doesnt change +the current Stream mode, only updates the pointers od the Stream, +resetting read position.&] +[s3;%% &] +[s4; &] +[s5;:BufferStream`:`:Create`(`): [@(0.0.255) void]_[* Create]()&] +[s2;%% resets underlying buffer and sets Stream to Storing mode. +rewinds current ptr to beginning.&] +[s3; &] +[s4; &] +[s5;:BufferStream`:`:Reserve`(int`): [@(0.0.255) void]_[* Reserve]([@(0.0.255) int]_[*@3 n])&] +[s2;%% reserves additional [%-*@3 n].bytes in the Stream. sets Storing +mode.can speed up things if you know how much is to come.&] +[s3;%% &] +[s4; &] +[s5;:BufferStream`:`:GetResult`(`): [_^Vector^ Vector]<[_^byte^ byte]>_[* GetResult]()&] +[s2;%% picks internal Vector, leaving an initialized Vector for new +operations.&] +[s3; &] +[s4; &] +[s5;:BufferStream`:`:BufferStream`(`): [* BufferStream]()&] +[s2;%% creates a BufferStream in Storing Mode&] +[s3; &] +[s4; &] +[s5;:BufferStream`:`:BufferStream`(Vector``&`): [* BufferStream]([_^Vector^ Vector +]<[_^byte^ byte]>`&_[*@3 d])&] +[s2;%% Creates a BufferStream in Loading mode. Loads Vector [%-*@3 d].picking +it.&] +[s3;%% &] +[s0; ] \ No newline at end of file diff --git a/bazaar/BufferStreamTest/BufferStreamTest.cpp b/bazaar/BufferStreamTest/BufferStreamTest.cpp new file mode 100644 index 000000000..4e2f3136d --- /dev/null +++ b/bazaar/BufferStreamTest/BufferStreamTest.cpp @@ -0,0 +1,51 @@ +#include "BufferStreamTest.h" + + + +CONSOLE_APP_MAIN +{ + Vector vi; + Array ai; + + VectorMap vmi; + ArrayMap ami; + + Index vii; + ArrayIndex aii; + + int & iv = vi.Add(123); + int & ia = ai.Add(123); + + int & imv = vmi.Add(0, 123); + int & ima = ami.Add(0, 123); + + int & iiv = vii.Add(123); + int & iia = aii.Add(123); + + + BufferStream vs; + + vs % String("Hallo"); + for(int i = 0; i < 1000; i++) + vs % i; + + Vector vb; + vb = vs.GetResult(); + + BufferStream vsd(vb); + + String s; + vsd % s; + ASSERT(s == "Hallo"); + + for(int i = 0; i < 1000; i++) + { + int ii; + vsd % ii; + ASSERT(ii == i); + int a = 0; + } + + int abc = 123; +} + diff --git a/bazaar/BufferStreamTest/BufferStreamTest.h b/bazaar/BufferStreamTest/BufferStreamTest.h new file mode 100644 index 000000000..a1f288b45 --- /dev/null +++ b/bazaar/BufferStreamTest/BufferStreamTest.h @@ -0,0 +1,6 @@ +#ifndef _ContTest_ContTest_h +#define _ContTest_ContTest_h + +#include + +#endif diff --git a/bazaar/BufferStreamTest/BufferStreamTest.upp b/bazaar/BufferStreamTest/BufferStreamTest.upp new file mode 100644 index 000000000..2b0aa25b7 --- /dev/null +++ b/bazaar/BufferStreamTest/BufferStreamTest.upp @@ -0,0 +1,11 @@ +uses + Core, + BufferStream; + +file + BufferStreamTest.h, + BufferStreamTest.cpp; + +mainconfig + "" = "MT"; + diff --git a/bazaar/BufferStreamTest/init b/bazaar/BufferStreamTest/init new file mode 100644 index 000000000..a6e574c1e --- /dev/null +++ b/bazaar/BufferStreamTest/init @@ -0,0 +1,5 @@ +#ifndef _BufferStreamTest_icpp_init_stub +#define _BufferStreamTest_icpp_init_stub +#include "Core/init" +#include "BufferStream/init" +#endif