Changeset 8
- Timestamp:
- Oct 12, 2009, 7:59:16 AM (15 years ago)
- Location:
- ProtocolBuffers
- Files:
-
- 2 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
ProtocolBuffers/Demo.lpi
r7 r8 9 9 <Icon Value="0"/> 10 10 <UseXPManifest Value="True"/> 11 <ActiveEditorIndexAtStart Value=" 1"/>11 <ActiveEditorIndexAtStart Value="2"/> 12 12 </General> 13 13 <VersionInfo> … … 33 33 </Item1> 34 34 </RequiredPackages> 35 <Units Count=" 4">35 <Units Count="7"> 36 36 <Unit0> 37 37 <Filename Value="Demo.lpr"/> 38 38 <IsPartOfProject Value="True"/> 39 39 <UnitName Value="Demo"/> 40 <CursorPos X=" 49" Y="10"/>40 <CursorPos X="75" Y="10"/> 41 41 <TopLine Value="1"/> 42 <EditorIndex Value=" 3"/>43 <UsageCount Value="2 1"/>42 <EditorIndex Value="6"/> 43 <UsageCount Value="24"/> 44 44 <Loaded Value="True"/> 45 45 </Unit0> … … 50 50 <ResourceBaseClass Value="Form"/> 51 51 <UnitName Value="UMainForm"/> 52 <CursorPos X=" 41" Y="43"/>53 <TopLine Value=" 24"/>52 <CursorPos X="29" Y="86"/> 53 <TopLine Value="31"/> 54 54 <EditorIndex Value="0"/> 55 <UsageCount Value="2 1"/>55 <UsageCount Value="24"/> 56 56 <Loaded Value="True"/> 57 57 </Unit1> … … 60 60 <IsPartOfProject Value="True"/> 61 61 <UnitName Value="UProtocolBuffers"/> 62 <CursorPos X=" 14" Y="160"/>63 <TopLine Value=" 144"/>62 <CursorPos X="3" Y="87"/> 63 <TopLine Value="85"/> 64 64 <EditorIndex Value="1"/> 65 <UsageCount Value="2 1"/>65 <UsageCount Value="24"/> 66 66 <Loaded Value="True"/> 67 67 </Unit2> … … 70 70 <IsPartOfProject Value="True"/> 71 71 <UnitName Value="UMemoryStreamEx"/> 72 <CursorPos X="5" Y="18"/> 72 <CursorPos X="1" Y="1"/> 73 <TopLine Value="1"/> 74 <EditorIndex Value="5"/> 75 <UsageCount Value="24"/> 76 <Loaded Value="True"/> 77 </Unit3> 78 <Unit4> 79 <Filename Value="..\..\..\Programy\Lazarus_0.9.27\fpc\2.2.4\source\rtl\objpas\classes\classesh.inc"/> 80 <CursorPos X="19" Y="826"/> 81 <TopLine Value="810"/> 82 <EditorIndex Value="4"/> 83 <UsageCount Value="11"/> 84 <Loaded Value="True"/> 85 </Unit4> 86 <Unit5> 87 <Filename Value="Sample.proto"/> 88 <IsPartOfProject Value="True"/> 89 <CursorPos X="20" Y="4"/> 90 <TopLine Value="1"/> 91 <EditorIndex Value="3"/> 92 <UsageCount Value="22"/> 93 <Loaded Value="True"/> 94 <SyntaxHighlighter Value="None"/> 95 </Unit5> 96 <Unit6> 97 <Filename Value="UParser.pas"/> 98 <IsPartOfProject Value="True"/> 99 <UnitName Value="UParser"/> 100 <CursorPos X="66" Y="26"/> 73 101 <TopLine Value="2"/> 74 102 <EditorIndex Value="2"/> 75 103 <UsageCount Value="21"/> 76 104 <Loaded Value="True"/> 77 </Unit 3>105 </Unit6> 78 106 </Units> 79 107 <JumpHistory Count="30" HistoryIndex="29"> 80 108 <Position1> 81 <Filename Value="U MainForm.pas"/>82 <Caret Line=" 34" Column="14" TopLine="28"/>109 <Filename Value="UProtocolBuffers.pas"/> 110 <Caret Line="159" Column="49" TopLine="144"/> 83 111 </Position1> 84 112 <Position2> 85 113 <Filename Value="UProtocolBuffers.pas"/> 86 <Caret Line=" 55" Column="18" TopLine="39"/>114 <Caret Line="28" Column="14" TopLine="12"/> 87 115 </Position2> 88 116 <Position3> 89 117 <Filename Value="UProtocolBuffers.pas"/> 90 <Caret Line=" 41" Column="3" TopLine="19"/>118 <Caret Line="170" Column="60" TopLine="155"/> 91 119 </Position3> 92 120 <Position4> 93 121 <Filename Value="UProtocolBuffers.pas"/> 94 <Caret Line="1 9" Column="3" TopLine="9"/>122 <Caret Line="175" Column="10" TopLine="159"/> 95 123 </Position4> 96 124 <Position5> 97 125 <Filename Value="UProtocolBuffers.pas"/> 98 <Caret Line=" 96" Column="34" TopLine="67"/>126 <Caret Line="206" Column="5" TopLine="174"/> 99 127 </Position5> 100 128 <Position6> 101 <Filename Value="U MainForm.pas"/>102 <Caret Line=" 42" Column="5" TopLine="15"/>129 <Filename Value="UProtocolBuffers.pas"/> 130 <Caret Line="234" Column="1" TopLine="202"/> 103 131 </Position6> 104 132 <Position7> 105 133 <Filename Value="UProtocolBuffers.pas"/> 106 <Caret Line=" 49" Column="46" TopLine="23"/>134 <Caret Line="222" Column="34" TopLine="203"/> 107 135 </Position7> 108 136 <Position8> 109 <Filename Value="U MainForm.pas"/>110 <Caret Line=" 42" Column="5" TopLine="28"/>137 <Filename Value="UProtocolBuffers.pas"/> 138 <Caret Line="230" Column="37" TopLine="205"/> 111 139 </Position8> 112 140 <Position9> 113 141 <Filename Value="UProtocolBuffers.pas"/> 114 <Caret Line=" 97" Column="5" TopLine="65"/>142 <Caret Line="223" Column="56" TopLine="207"/> 115 143 </Position9> 116 144 <Position10> 117 <Filename Value="U MainForm.pas"/>118 <Caret Line=" 42" Column="9" TopLine="30"/>145 <Filename Value="UProtocolBuffers.pas"/> 146 <Caret Line="230" Column="55" TopLine="207"/> 119 147 </Position10> 120 148 <Position11> 121 149 <Filename Value="UProtocolBuffers.pas"/> 122 <Caret Line=" 110" Column="11" TopLine="84"/>150 <Caret Line="229" Column="27" TopLine="207"/> 123 151 </Position11> 124 152 <Position12> 125 153 <Filename Value="UProtocolBuffers.pas"/> 126 <Caret Line=" 107" Column="1" TopLine="85"/>154 <Caret Line="28" Column="15" TopLine="12"/> 127 155 </Position12> 128 156 <Position13> 129 <Filename Value="U MainForm.pas"/>130 <Caret Line=" 43" Column="41" TopLine="30"/>157 <Filename Value="UProtocolBuffers.pas"/> 158 <Caret Line="229" Column="28" TopLine="207"/> 131 159 </Position13> 132 160 <Position14> 133 161 <Filename Value="UProtocolBuffers.pas"/> 134 <Caret Line="1 04" Column="1" TopLine="72"/>162 <Caret Line="183" Column="65" TopLine="183"/> 135 163 </Position14> 136 164 <Position15> 137 165 <Filename Value="UProtocolBuffers.pas"/> 138 <Caret Line=" 103" Column="5" TopLine="71"/>166 <Caret Line="88" Column="5" TopLine="56"/> 139 167 </Position15> 140 168 <Position16> 141 169 <Filename Value="UProtocolBuffers.pas"/> 142 <Caret Line=" 38" Column="3" TopLine="19"/>170 <Caret Line="89" Column="27" TopLine="73"/> 143 171 </Position16> 144 172 <Position17> 145 <Filename Value="U ProtocolBuffers.pas"/>146 <Caret Line=" 80" Column="5" TopLine="48"/>173 <Filename Value="UMainForm.pas"/> 174 <Caret Line="40" Column="19" TopLine="31"/> 147 175 </Position17> 148 176 <Position18> 149 <Filename Value="U ProtocolBuffers.pas"/>150 <Caret Line="1 54" Column="1" TopLine="122"/>177 <Filename Value="UMainForm.pas"/> 178 <Caret Line="17" Column="22" TopLine="17"/> 151 179 </Position18> 152 180 <Position19> 153 <Filename Value="U ProtocolBuffers.pas"/>154 <Caret Line=" 96" Column="44" TopLine="90"/>181 <Filename Value="UMainForm.pas"/> 182 <Caret Line="21" Column="5" TopLine="17"/> 155 183 </Position19> 156 184 <Position20> 157 <Filename Value="U ProtocolBuffers.pas"/>158 <Caret Line=" 29" Column="17" TopLine="13"/>185 <Filename Value="UMainForm.pas"/> 186 <Caret Line="90" Column="5" TopLine="58"/> 159 187 </Position20> 160 188 <Position21> 161 <Filename Value="U ProtocolBuffers.pas"/>162 <Caret Line=" 53" Column="32" TopLine="37"/>189 <Filename Value="UMainForm.pas"/> 190 <Caret Line="21" Column="5" TopLine="5"/> 163 191 </Position21> 164 192 <Position22> 165 <Filename Value="U ProtocolBuffers.pas"/>166 <Caret Line=" 156" Column="18" TopLine="133"/>193 <Filename Value="UMainForm.pas"/> 194 <Caret Line="77" Column="1" TopLine="49"/> 167 195 </Position22> 168 196 <Position23> 169 <Filename Value="U ProtocolBuffers.pas"/>170 <Caret Line=" 117" Column="54" TopLine="117"/>197 <Filename Value="UMainForm.pas"/> 198 <Caret Line="49" Column="15" TopLine="35"/> 171 199 </Position23> 172 200 <Position24> 173 <Filename Value=" Demo.lpr"/>174 <Caret Line=" 10" Column="66" TopLine="1"/>201 <Filename Value="UProtocolBuffers.pas"/> 202 <Caret Line="63" Column="28" TopLine="43"/> 175 203 </Position24> 176 204 <Position25> 177 <Filename Value=" Demo.lpr"/>178 <Caret Line=" 19" Column="51" TopLine="1"/>205 <Filename Value="UMainForm.pas"/> 206 <Caret Line="49" Column="15" TopLine="35"/> 179 207 </Position25> 180 208 <Position26> 181 <Filename Value=" Demo.lpr"/>182 <Caret Line=" 10" Column="39" TopLine="1"/>209 <Filename Value="UMainForm.pas"/> 210 <Caret Line="51" Column="29" TopLine="35"/> 183 211 </Position26> 184 212 <Position27> 185 <Filename Value="U ProtocolBuffers.pas"/>186 <Caret Line=" 156" Column="39" TopLine="140"/>213 <Filename Value="UMainForm.pas"/> 214 <Caret Line="56" Column="8" TopLine="40"/> 187 215 </Position27> 188 216 <Position28> 189 <Filename Value="UP rotocolBuffers.pas"/>190 <Caret Line=" 55" Column="47" TopLine="39"/>217 <Filename Value="UParser.pas"/> 218 <Caret Line="14" Column="3" TopLine="1"/> 191 219 </Position28> 192 220 <Position29> 193 <Filename Value="UP rotocolBuffers.pas"/>194 <Caret Line=" 104" Column="5" TopLine="72"/>221 <Filename Value="UParser.pas"/> 222 <Caret Line="34" Column="5" TopLine="2"/> 195 223 </Position29> 196 224 <Position30> 197 <Filename Value="UP rotocolBuffers.pas"/>198 <Caret Line=" 103" Column="36" TopLine="101"/>225 <Filename Value="UParser.pas"/> 226 <Caret Line="7" Column="60" TopLine="3"/> 199 227 </Position30> 200 228 </JumpHistory> -
ProtocolBuffers/Demo.lpr
r6 r8 8 8 {$ENDIF}{$ENDIF} 9 9 Interfaces, // this includes the LCL widgetset 10 Forms, UMainForm, LResources, UProtocolBuffers, UMemoryStreamEx 10 Forms, UMainForm, LResources, UProtocolBuffers, UMemoryStreamEx, UParser 11 11 { you can add units after this }; 12 12 -
ProtocolBuffers/UMainForm.lfm
r6 r8 17 17 TabOrder = 0 18 18 end 19 object Button1: TButton 20 Left = 7 21 Height = 25 22 Top = 288 23 Width = 75 24 Caption = 'Runtime' 25 OnClick = Button1Click 26 TabOrder = 1 27 end 28 object Button2: TButton 29 Left = 88 30 Height = 25 31 Top = 289 32 Width = 75 33 Caption = 'From file' 34 OnClick = Button2Click 35 TabOrder = 2 36 end 19 37 end -
ProtocolBuffers/UMainForm.lrs
r6 r8 4 4 +#1#11'ClientWidth'#3#192#1#8'OnCreate'#7#10'FormCreate'#9'OnDestroy'#7#11'Fo' 5 5 +'rmDestroy'#10'LCLVersion'#6#6'0.9.29'#0#5'TMemo'#5'Memo1'#4'Left'#2#9#6'Hei' 6 +'ght'#3#16#1#3'Top'#2#9#5'Width'#3#177#1#8'TabOrder'#2#0#0#0#0 6 +'ght'#3#16#1#3'Top'#2#9#5'Width'#3#177#1#8'TabOrder'#2#0#0#0#7'TButton'#7'Bu' 7 +'tton1'#4'Left'#2#7#6'Height'#2#25#3'Top'#3' '#1#5'Width'#2'K'#7'Caption'#6#7 8 +'Runtime'#7'OnClick'#7#12'Button1Click'#8'TabOrder'#2#1#0#0#7'TButton'#7'But' 9 +'ton2'#4'Left'#2'X'#6'Height'#2#25#3'Top'#3'!'#1#5'Width'#2'K'#7'Caption'#6#9 10 +'From file'#7'OnClick'#7#12'Button2Click'#8'TabOrder'#2#2#0#0#0 7 11 ]); -
ProtocolBuffers/UMainForm.pas
r7 r8 9 9 StdCtrls, UProtocolBuffers, UMemoryStreamEx; 10 10 11 const 12 SampleProtoFileName = 'Sample.proto'; 13 11 14 type 12 15 { TMainForm } 13 16 TMainForm = class(TForm) 17 Button1: TButton; 18 Button2: TButton; 14 19 Memo1: TMemo; 20 procedure Button1Click(Sender: TObject); 21 procedure Button2Click(Sender: TObject); 15 22 procedure FormCreate(Sender: TObject); 16 23 procedure FormDestroy(Sender: TObject); 17 24 private 18 { private declarations }25 procedure DisplayStream(Stream: TStream); 19 26 public 20 27 PB: TProtocolBuffer; … … 29 36 30 37 procedure TMainForm.FormCreate(Sender: TObject); 38 begin 39 end; 40 41 procedure TMainForm.Button2Click(Sender: TObject); 42 var 43 Stream: TMemoryStream; 44 StringList: TStringList; 45 begin 46 Stream := TMemoryStreamEx.Create; 47 StringList := TStringList.Create; 48 StringList.LoadFromFile(SampleProtoFileName); 49 PB := TProtocolBuffer.Create; 50 with PB do begin 51 LoadFromProto(StringList); 52 SaveToStream(Stream); 53 end; 54 DisplayStream(Stream); 55 StringList.Free; 56 Stream.Free; 57 end; 58 59 procedure TMainForm.Button1Click(Sender: TObject); 31 60 var 32 61 Stream: TMemoryStreamEx; 33 Text: string;34 I: Integer;35 62 NewItem: TPBItem; 36 63 begin … … 45 72 end; 46 73 SaveToStream(Stream); 74 DisplayStream(Stream); 47 75 end; 48 49 Stream.Position := 0;50 Text := '';51 for I := 1 to Stream.Size do begin52 Text := Text + IntToHex(Stream.ReadByte, 2) + ' ';53 end;54 Memo1.Lines.Text := Text;55 76 Stream.Free; 56 77 end; … … 61 82 end; 62 83 84 procedure TMainForm.DisplayStream(Stream: TStream); 85 var 86 I: Integer; 87 Text: string; 88 begin 89 Stream.Position := 0; 90 Text := ''; 91 for I := 1 to Stream.Size do begin 92 Text := Text + IntToHex(Stream.ReadByte, 2) + ' '; 93 end; 94 Memo1.Lines.Text := Text; 95 end; 96 63 97 initialization 64 98 {$I UMainForm.lrs} -
ProtocolBuffers/UProtocolBuffers.pas
r7 r8 24 24 ItemMode: TPBItemMode; 25 25 procedure SaveVariantToStream(Stream: TStream; Value: Integer); 26 function LoadVariantFromStream(Stream: TStream): Integer; 26 27 procedure SaveToStream(Stream: TStream); virtual; 27 28 procedure LoadFromStream(Stream: TStream); virtual; 28 function LoadVariantFromStream(Stream: TStream);29 29 end; 30 30 … … 34 34 TPBStringItem = class(TPBItem) 35 35 Value: string; 36 procedure SaveToStream(Stream: TStream); override; 37 procedure LoadFromStream(Stream: TStream); override; 38 constructor Create; 36 39 end; 37 40 … … 40 43 Value: Integer; 41 44 procedure SaveToStream(Stream: TStream); override; 45 procedure LoadFromStream(Stream: TStream); override; 42 46 constructor Create; 43 47 end; … … 57 61 procedure LoadFromStream(Stream: TStream); 58 62 procedure SaveToStream(Stream: TStream); 63 procedure LoadFromProto(Source: TStringList); 59 64 constructor Create; 60 65 destructor Destroy; override; … … 76 81 begin 77 82 BaseMessage.SaveToStream(Stream); 83 end; 84 85 procedure TProtocolBuffer.LoadFromProto(Source: TStringList); 86 begin 87 78 88 end; 79 89 … … 101 111 102 112 procedure TPBMessageItem.LoadFromStream(Stream: TStream); 113 var 114 I: Integer; 103 115 begin 104 116 inherited LoadFromStream(Stream); 117 for I := 0 to Items.Count - 1 do 118 TPBItem(Items[I]).LoadFromStream(Stream); 105 119 end; 106 120 … … 168 182 while Data > $7f do begin 169 183 Data := TMemoryStreamEx(Stream).ReadByte; 170 Tag := Tag or ((Data and $7f) shl (ByteIndex * 7 + 4)) 184 Tag := Tag or ((Data and $7f) shl (ByteIndex * 7 + 4)); 171 185 Inc(ByteIndex); 172 186 end; 173 187 end; 174 188 175 function TPBItem.LoadVariantFromStream(Stream: TStream) ;189 function TPBItem.LoadVariantFromStream(Stream: TStream): Integer; 176 190 var 177 191 Data: Byte; … … 179 193 begin 180 194 Data := TMemoryStreamEx(Stream).ReadByte; 181 Tag:= Data and $7f;195 Result := Data and $7f; 182 196 ByteIndex := 1; 183 197 while Data > $7f do begin 184 198 Data := TMemoryStreamEx(Stream).ReadByte; 185 Tag := Tag or ((Data and $7f) shl (ByteIndex * 7))199 Result := Result or ((Data and $7f) shl (ByteIndex * 7)); 186 200 Inc(ByteIndex); 187 201 end; … … 196 210 end; 197 211 212 procedure TPBIntegerItem.LoadFromStream(Stream: TStream); 213 begin 214 inherited LoadFromStream(Stream); 215 LoadVariantFromStream(Stream); 216 end; 217 198 218 constructor TPBIntegerItem.Create; 199 219 begin … … 201 221 end; 202 222 223 { TPBStringItem } 224 225 procedure TPBStringItem.SaveToStream(Stream: TStream); 226 begin 227 inherited SaveToStream(Stream); 228 SaveVariantToStream(Stream, Length(Value)); 229 TMemoryStreamEx(Stream).Write(Value[1], Length(Value)); 230 end; 231 232 procedure TPBStringItem.LoadFromStream(Stream: TStream); 233 begin 234 inherited LoadFromStream(Stream); 235 SetLength(Value, LoadVariantFromStream(Stream)); 236 TMemoryStreamEx(Stream).Read(Value[1], Length(Value)); 237 end; 238 239 constructor TPBStringItem.Create; 240 begin 241 ItemType := itLengthDelimited; 242 end; 243 203 244 end. 204 245
Note:
See TracChangeset
for help on using the changeset viewer.