From ea1510d19263ff435f6aa2462f2b9a725994f20b Mon Sep 17 00:00:00 2001 From: cxl Date: Thu, 16 Oct 2014 21:20:10 +0000 Subject: [PATCH] .uppdev git-svn-id: svn://ultimatepp.org/upp/trunk@7793 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppdev/ZSerialize/ZSerialize.cpp | 112 +++++++++++++++++++++++++++++++ uppdev/ZSerialize/ZSerialize.upp | 9 +++ 2 files changed, 121 insertions(+) create mode 100644 uppdev/ZSerialize/ZSerialize.cpp create mode 100644 uppdev/ZSerialize/ZSerialize.upp diff --git a/uppdev/ZSerialize/ZSerialize.cpp b/uppdev/ZSerialize/ZSerialize.cpp new file mode 100644 index 000000000..6b32cd68a --- /dev/null +++ b/uppdev/ZSerialize/ZSerialize.cpp @@ -0,0 +1,112 @@ +#include + +using namespace Upp; + +bool ZLoad(Callback1 serialize, Stream& stream, int version = Null) +{ + String h = stream.GetLine(); + if(h != "COMPRESSED") + return false; + int64 sz = stream.Get64(); + Zlib zlib; + InFilterStream in(stream, zlib); + zlib.Decompress(); + in.SetSize(sz); + return Load(serialize, in, version); +} + +bool ZStore(Callback1 serialize, Stream& stream, int version = Null) +{ + stream.Put("COMPRESSED\n"); + int sz_pos = stream.GetPos(); + stream.Put64(0); + Zlib zlib; + OutFilterStream out(stream, zlib); + zlib.Compress(); + if(!Store(serialize, out, version)) + return false; + out.Close(); + int64 sz = out.GetPos(); + stream.Seek(sz_pos); + stream.Put64(sz); +} + +template +bool ZLoad(T& x, Stream& s, int version = Null) { + return ZLoad(SerializeCb(x), s, version); +} + +template +bool ZStore(T& x, Stream& s, int version = Null) { + return ZStore(SerializeCb(x), s, version); +} + +/* +template +bool ZLoadFromFile(T& x, const char *name = NULL, int version = Null) { + return LoadFromFile(SerializeCb(x), name, version); +} + +template +bool StoreToFile(T& x, const char *name = NULL, int version = Null) { + return StoreToFile(SerializeCb(x), name, version); +} +*/ +template +String ZStoreAsString(T& x) { + StringStream ss; + ZStore(x, ss); + return ss; +} + +template +bool ZLoadFromString(T& x, const String& s) { + StringStream ss(s); + return ZLoad(x, ss); +} + +CONSOLE_APP_MAIN +{ + String data; + for(int i = 0; i < 10000; i++) + data << AsString(i) << ": " << AsString(Uuid::Create()) << '\n'; + + String path = GetHomeDirFile("test.z"); + { + FileOut fout(path); + DDUMP(data.GetLength()); + int64 sz_pos = fout.GetPos(); + fout.Put64(0); + Zlib zlib; + OutFilterStream out(fout, zlib); + zlib.Compress(); + out % data; + int64 sz = out.GetPos(); + out.Close(); + fout.Seek(sz_pos); + fout.Put64(sz); + } + + String data2; + { + FileIn fin(path); + int64 sz = fin.Get64(); + Zlib zlib; + InFilterStream in(fin, zlib); + zlib.Decompress(); + in.SetSize(sz); + in % data2; + } + + ASSERT(data == data2); + + DLOG("========================"); + data = LoadFile(GetDataFile("FilterStream.cpp")); + String h = ZStoreAsString(data); + DDUMP(data.GetCount()); + DDUMP(ZCompress(data).GetCount()); + DDUMP(h.GetCount()); + ZLoadFromString(data2, h); + + ASSERT(data == data2); +} diff --git a/uppdev/ZSerialize/ZSerialize.upp b/uppdev/ZSerialize/ZSerialize.upp new file mode 100644 index 000000000..7503b16c7 --- /dev/null +++ b/uppdev/ZSerialize/ZSerialize.upp @@ -0,0 +1,9 @@ +uses + Core; + +file + ZSerialize.cpp; + +mainconfig + "" = ""; +