有没有人知道有一个在Delphi中实现Google Protocol Buffers的项目?
这个项目包含了Delphi的Protocol Buffers实现。该项目仅实现了特定项目所需的有限功能。当时,我认为没有必要转移整个项目代码。
http://sourceforge.net/projects/protobuf-delphi/你最好找/制作一个C++/Delphi桥接程序,而不是重新实现Protocol Buffers。 这个代码库相当庞大。
我在github上发现了另一个项目。(该项目从2014年2月开发到2016年7月,截至2017年6月)
它似乎具有初步的proto3支持。
我还没有测试过,但可能是目前最好的选择。
https://github.com/stijnsanders/DelphiProtocolBuffer
编辑: 我测试过了,但似乎是用旧版 Delphi 编写的,不支持 Unicode。
我可以编译生成器(使用 10 Seattle),但编译后的可执行文件无法生成 pas 文件 :-(
编辑2:
代码生成器只需将 TStream 替换为 TStreamReader/Writer 即可正常工作。 我确认生成器可以转换最近的 地址簿示例。
diff --git a/ProtBufParse.pas b/ProtBufParse.pas
index f29d7c7..cdd734d 100644
--- a/ProtBufParse.pas
+++ b/ProtBufParse.pas
@@ -236,16 +236,13 @@ var
procedure LoadCode;
var
- f:TFileStream;
+ sr:TStreamReader;
begin
- f:=TFileStream.Create(FilePath,fmOpenRead or fmShareDenyWrite);
+ sr:=TStreamReader.Create(FilePath, True{DetectBOM});
try
- //TODO: UTF-8? UTF-16?
- CodeL:=f.Size;
- SetLength(Code,CodeL);
- if f.Read(Code[1],CodeL)<>CodeL then RaiseLastOSError;
+ Code := sr.ReadToEnd;
finally
- f.Free;
+ sr.Free;
end;
end;
diff --git a/dpbp.dpr b/dpbp.dpr
index 4049480..b6dab90 100644
--- a/dpbp.dpr
+++ b/dpbp.dpr
@@ -22,7 +22,7 @@ var
p:TProtocolBufferParser;
s,t,InputFN,OutputFN,RelPath:string;
i,j,l,l1:integer;
- f:TFileStream;
+ sw:TStreamWriter;
fv:TProtocolBufferParserValue;
ff:TProtocolBufferParserFlag;
Flags:TProtocolBufferParserFlags;
@@ -134,11 +134,12 @@ begin
writeln('Writing '+OutputFN);
s:=p.GenerateUnit(Flags);
- f:=TFileStream.Create(OutputFN,fmCreate);
+
+ sw:=TStreamWriter.Create(OutputFN,False,TEncoding.UTF8);
try
- f.Write(s[1],Length(s));
+ sw.Write(s);
finally
- f.Free;
+ sw.Free;
end;
finally