这几个小时既然在做服务端就把数据库一起做一下吧(话说前端后端数据库界面应用图像网络音效底层窗口winapi的调用都是我写的,开发测试产品软件工程师码农运营都是我一个人真的好吗?)
然后就发现mysq1
和0racle
都太垃圾了,于是打算自己开发一个,并且重定义一些操作
然后很自然的(一点也不自然)就有了把所有数据压缩成string
的想法
单元库zip.pas
:
unit zip;
interface
function encode(a:longint):ansistring;
function encode(a:char):ansistring;
function encode(a:ansistring):ansistring;
function encode(a:boolean):ansistring;
function decode_longint(var a:ansistring):longint;
function decode_char(var a:ansistring):char;
function decode_ansistring(var a:ansistring):ansistring;
function decode_boolean(var a:ansistring):boolean;
implementation
function encode(a:longint):ansistring;
var
b:int64;
begin
b:=a;
b:=b+2147483648;
exit(chr(b div 16777216)+chr(b div 65536 mod 256)+chr(b div 256 mod 256)+chr(b mod 256));
end;
function encode(a:char):ansistring;
begin
exit(a);
end;
function encode(a:ansistring):ansistring;
begin
exit(encode(longint(length(a)))+a);
end;
function encode(a:boolean):ansistring;
begin
if a then
exit('T')
else
exit('F');
end;
function decode_longint(var a:ansistring):longint;
begin
if length(a)<4 then
begin
writeln('Message lost');
exit;
end;
decode_longint:=int64(ord(a[1])*16777216+ord(a[2])*65536+ord(a[3])*256+ord(a[4]))-2147483648;
delete(a,1,4);
end;
function decode_char(var a:ansistring):char;
begin
if length(a)<1 then
begin
writeln('Message lost');
exit;
end;
decode_char:=a[1];
delete(a,1,1);
end;
function decode_ansistring(var a:ansistring):ansistring;
var
len:longint;
begin
len:=decode_longint(a);
if length(a)<len then
begin
writeln('Message lost');
exit;
end;
decode_ansistring:=copy(a,1,len);
delete(a,1,len);
end;
function decode_boolean(var a:ansistring):boolean;
begin
if length(a)<1 then
begin
writeln('Message lost');
exit;
end;
if a[1]='T' then
decode_boolean:=true;
if a[1]='F' then
decode_boolean:=false;
if not(a[1]in ['T','F']) then
begin
writeln('Illegal decode');
exit;
end;
delete(a,1,1);
end;
end.
那么问题来了:怎么使用呢?
自己写函数!
uses zip;
type
rec=record
a,b:longint;
c:boolean;
d:char;
e:ansistring;
end;
var
a:rec;
function encode_rec(s:rec):ansistring;
begin
with s do
exit(encode(a)+encode(b)+encode(c)+encode(d)+encode(e));
end;
function decode_rec(s:ansistring):rec;
begin
with decode_rec do
begin
a:=decode_longint(s);
b:=decode_longint(s);
c:=decode_boolean(s);
d:=decode_char(s);
e:=decode_ansistring(s);
end;
end;
procedure read(var a:rec);
var
ch:char;
begin
with a do
begin
readln(a);
readln(b);
readln(ch);
if ch='T' then
c:=true
else
c:=false;
readln(d);
readln(e);
end;
end;
procedure write(a:rec);
begin
with a do
begin
writeln(a);
writeln(b);
writeln(c);
writeln(d);
writeln(e);
end;
end;
begin
read(a);
{ writeln(length(encode_rec(a)),' ',encode_rec(a)); }
a:=decode_rec(encode_rec(a));
write(a);
end.