mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-07-11 22:03:01 -06:00
.uppdev
git-svn-id: svn://ultimatepp.org/upp/trunk@7793 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
2975f23442
commit
ea1510d192
2 changed files with 121 additions and 0 deletions
112
uppdev/ZSerialize/ZSerialize.cpp
Normal file
112
uppdev/ZSerialize/ZSerialize.cpp
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
#include <Core/Core.h>
|
||||
|
||||
using namespace Upp;
|
||||
|
||||
bool ZLoad(Callback1<Stream&> 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<Stream&> 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 <class T>
|
||||
bool ZLoad(T& x, Stream& s, int version = Null) {
|
||||
return ZLoad(SerializeCb(x), s, version);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
bool ZStore(T& x, Stream& s, int version = Null) {
|
||||
return ZStore(SerializeCb(x), s, version);
|
||||
}
|
||||
|
||||
/*
|
||||
template <class T>
|
||||
bool ZLoadFromFile(T& x, const char *name = NULL, int version = Null) {
|
||||
return LoadFromFile(SerializeCb(x), name, version);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
bool StoreToFile(T& x, const char *name = NULL, int version = Null) {
|
||||
return StoreToFile(SerializeCb(x), name, version);
|
||||
}
|
||||
*/
|
||||
template <class T>
|
||||
String ZStoreAsString(T& x) {
|
||||
StringStream ss;
|
||||
ZStore(x, ss);
|
||||
return ss;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
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);
|
||||
}
|
||||
9
uppdev/ZSerialize/ZSerialize.upp
Normal file
9
uppdev/ZSerialize/ZSerialize.upp
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
uses
|
||||
Core;
|
||||
|
||||
file
|
||||
ZSerialize.cpp;
|
||||
|
||||
mainconfig
|
||||
"" = "";
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue