Changeset 8 for ProtocolBuffers


Ignore:
Timestamp:
Oct 12, 2009, 7:59:16 AM (15 years ago)
Author:
george
Message:
  • Added: Uncompleted proto file loading support.
  • Modified: String type serialization implmentation.
Location:
ProtocolBuffers
Files:
2 added
7 edited

Legend:

Unmodified
Added
Removed
  • ProtocolBuffers/Demo.lpi

    r7 r8  
    99      <Icon Value="0"/>
    1010      <UseXPManifest Value="True"/>
    11       <ActiveEditorIndexAtStart Value="1"/>
     11      <ActiveEditorIndexAtStart Value="2"/>
    1212    </General>
    1313    <VersionInfo>
     
    3333      </Item1>
    3434    </RequiredPackages>
    35     <Units Count="4">
     35    <Units Count="7">
    3636      <Unit0>
    3737        <Filename Value="Demo.lpr"/>
    3838        <IsPartOfProject Value="True"/>
    3939        <UnitName Value="Demo"/>
    40         <CursorPos X="49" Y="10"/>
     40        <CursorPos X="75" Y="10"/>
    4141        <TopLine Value="1"/>
    42         <EditorIndex Value="3"/>
    43         <UsageCount Value="21"/>
     42        <EditorIndex Value="6"/>
     43        <UsageCount Value="24"/>
    4444        <Loaded Value="True"/>
    4545      </Unit0>
     
    5050        <ResourceBaseClass Value="Form"/>
    5151        <UnitName Value="UMainForm"/>
    52         <CursorPos X="41" Y="43"/>
    53         <TopLine Value="24"/>
     52        <CursorPos X="29" Y="86"/>
     53        <TopLine Value="31"/>
    5454        <EditorIndex Value="0"/>
    55         <UsageCount Value="21"/>
     55        <UsageCount Value="24"/>
    5656        <Loaded Value="True"/>
    5757      </Unit1>
     
    6060        <IsPartOfProject Value="True"/>
    6161        <UnitName Value="UProtocolBuffers"/>
    62         <CursorPos X="14" Y="160"/>
    63         <TopLine Value="144"/>
     62        <CursorPos X="3" Y="87"/>
     63        <TopLine Value="85"/>
    6464        <EditorIndex Value="1"/>
    65         <UsageCount Value="21"/>
     65        <UsageCount Value="24"/>
    6666        <Loaded Value="True"/>
    6767      </Unit2>
     
    7070        <IsPartOfProject Value="True"/>
    7171        <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"/>
    73101        <TopLine Value="2"/>
    74102        <EditorIndex Value="2"/>
    75103        <UsageCount Value="21"/>
    76104        <Loaded Value="True"/>
    77       </Unit3>
     105      </Unit6>
    78106    </Units>
    79107    <JumpHistory Count="30" HistoryIndex="29">
    80108      <Position1>
    81         <Filename Value="UMainForm.pas"/>
    82         <Caret Line="34" Column="14" TopLine="28"/>
     109        <Filename Value="UProtocolBuffers.pas"/>
     110        <Caret Line="159" Column="49" TopLine="144"/>
    83111      </Position1>
    84112      <Position2>
    85113        <Filename Value="UProtocolBuffers.pas"/>
    86         <Caret Line="55" Column="18" TopLine="39"/>
     114        <Caret Line="28" Column="14" TopLine="12"/>
    87115      </Position2>
    88116      <Position3>
    89117        <Filename Value="UProtocolBuffers.pas"/>
    90         <Caret Line="41" Column="3" TopLine="19"/>
     118        <Caret Line="170" Column="60" TopLine="155"/>
    91119      </Position3>
    92120      <Position4>
    93121        <Filename Value="UProtocolBuffers.pas"/>
    94         <Caret Line="19" Column="3" TopLine="9"/>
     122        <Caret Line="175" Column="10" TopLine="159"/>
    95123      </Position4>
    96124      <Position5>
    97125        <Filename Value="UProtocolBuffers.pas"/>
    98         <Caret Line="96" Column="34" TopLine="67"/>
     126        <Caret Line="206" Column="5" TopLine="174"/>
    99127      </Position5>
    100128      <Position6>
    101         <Filename Value="UMainForm.pas"/>
    102         <Caret Line="42" Column="5" TopLine="15"/>
     129        <Filename Value="UProtocolBuffers.pas"/>
     130        <Caret Line="234" Column="1" TopLine="202"/>
    103131      </Position6>
    104132      <Position7>
    105133        <Filename Value="UProtocolBuffers.pas"/>
    106         <Caret Line="49" Column="46" TopLine="23"/>
     134        <Caret Line="222" Column="34" TopLine="203"/>
    107135      </Position7>
    108136      <Position8>
    109         <Filename Value="UMainForm.pas"/>
    110         <Caret Line="42" Column="5" TopLine="28"/>
     137        <Filename Value="UProtocolBuffers.pas"/>
     138        <Caret Line="230" Column="37" TopLine="205"/>
    111139      </Position8>
    112140      <Position9>
    113141        <Filename Value="UProtocolBuffers.pas"/>
    114         <Caret Line="97" Column="5" TopLine="65"/>
     142        <Caret Line="223" Column="56" TopLine="207"/>
    115143      </Position9>
    116144      <Position10>
    117         <Filename Value="UMainForm.pas"/>
    118         <Caret Line="42" Column="9" TopLine="30"/>
     145        <Filename Value="UProtocolBuffers.pas"/>
     146        <Caret Line="230" Column="55" TopLine="207"/>
    119147      </Position10>
    120148      <Position11>
    121149        <Filename Value="UProtocolBuffers.pas"/>
    122         <Caret Line="110" Column="11" TopLine="84"/>
     150        <Caret Line="229" Column="27" TopLine="207"/>
    123151      </Position11>
    124152      <Position12>
    125153        <Filename Value="UProtocolBuffers.pas"/>
    126         <Caret Line="107" Column="1" TopLine="85"/>
     154        <Caret Line="28" Column="15" TopLine="12"/>
    127155      </Position12>
    128156      <Position13>
    129         <Filename Value="UMainForm.pas"/>
    130         <Caret Line="43" Column="41" TopLine="30"/>
     157        <Filename Value="UProtocolBuffers.pas"/>
     158        <Caret Line="229" Column="28" TopLine="207"/>
    131159      </Position13>
    132160      <Position14>
    133161        <Filename Value="UProtocolBuffers.pas"/>
    134         <Caret Line="104" Column="1" TopLine="72"/>
     162        <Caret Line="183" Column="65" TopLine="183"/>
    135163      </Position14>
    136164      <Position15>
    137165        <Filename Value="UProtocolBuffers.pas"/>
    138         <Caret Line="103" Column="5" TopLine="71"/>
     166        <Caret Line="88" Column="5" TopLine="56"/>
    139167      </Position15>
    140168      <Position16>
    141169        <Filename Value="UProtocolBuffers.pas"/>
    142         <Caret Line="38" Column="3" TopLine="19"/>
     170        <Caret Line="89" Column="27" TopLine="73"/>
    143171      </Position16>
    144172      <Position17>
    145         <Filename Value="UProtocolBuffers.pas"/>
    146         <Caret Line="80" Column="5" TopLine="48"/>
     173        <Filename Value="UMainForm.pas"/>
     174        <Caret Line="40" Column="19" TopLine="31"/>
    147175      </Position17>
    148176      <Position18>
    149         <Filename Value="UProtocolBuffers.pas"/>
    150         <Caret Line="154" Column="1" TopLine="122"/>
     177        <Filename Value="UMainForm.pas"/>
     178        <Caret Line="17" Column="22" TopLine="17"/>
    151179      </Position18>
    152180      <Position19>
    153         <Filename Value="UProtocolBuffers.pas"/>
    154         <Caret Line="96" Column="44" TopLine="90"/>
     181        <Filename Value="UMainForm.pas"/>
     182        <Caret Line="21" Column="5" TopLine="17"/>
    155183      </Position19>
    156184      <Position20>
    157         <Filename Value="UProtocolBuffers.pas"/>
    158         <Caret Line="29" Column="17" TopLine="13"/>
     185        <Filename Value="UMainForm.pas"/>
     186        <Caret Line="90" Column="5" TopLine="58"/>
    159187      </Position20>
    160188      <Position21>
    161         <Filename Value="UProtocolBuffers.pas"/>
    162         <Caret Line="53" Column="32" TopLine="37"/>
     189        <Filename Value="UMainForm.pas"/>
     190        <Caret Line="21" Column="5" TopLine="5"/>
    163191      </Position21>
    164192      <Position22>
    165         <Filename Value="UProtocolBuffers.pas"/>
    166         <Caret Line="156" Column="18" TopLine="133"/>
     193        <Filename Value="UMainForm.pas"/>
     194        <Caret Line="77" Column="1" TopLine="49"/>
    167195      </Position22>
    168196      <Position23>
    169         <Filename Value="UProtocolBuffers.pas"/>
    170         <Caret Line="117" Column="54" TopLine="117"/>
     197        <Filename Value="UMainForm.pas"/>
     198        <Caret Line="49" Column="15" TopLine="35"/>
    171199      </Position23>
    172200      <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"/>
    175203      </Position24>
    176204      <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"/>
    179207      </Position25>
    180208      <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"/>
    183211      </Position26>
    184212      <Position27>
    185         <Filename Value="UProtocolBuffers.pas"/>
    186         <Caret Line="156" Column="39" TopLine="140"/>
     213        <Filename Value="UMainForm.pas"/>
     214        <Caret Line="56" Column="8" TopLine="40"/>
    187215      </Position27>
    188216      <Position28>
    189         <Filename Value="UProtocolBuffers.pas"/>
    190         <Caret Line="55" Column="47" TopLine="39"/>
     217        <Filename Value="UParser.pas"/>
     218        <Caret Line="14" Column="3" TopLine="1"/>
    191219      </Position28>
    192220      <Position29>
    193         <Filename Value="UProtocolBuffers.pas"/>
    194         <Caret Line="104" Column="5" TopLine="72"/>
     221        <Filename Value="UParser.pas"/>
     222        <Caret Line="34" Column="5" TopLine="2"/>
    195223      </Position29>
    196224      <Position30>
    197         <Filename Value="UProtocolBuffers.pas"/>
    198         <Caret Line="103" Column="36" TopLine="101"/>
     225        <Filename Value="UParser.pas"/>
     226        <Caret Line="7" Column="60" TopLine="3"/>
    199227      </Position30>
    200228    </JumpHistory>
  • ProtocolBuffers/Demo.lpr

    r6 r8  
    88  {$ENDIF}{$ENDIF}
    99  Interfaces, // this includes the LCL widgetset
    10   Forms, UMainForm, LResources, UProtocolBuffers, UMemoryStreamEx
     10  Forms, UMainForm, LResources, UProtocolBuffers, UMemoryStreamEx, UParser
    1111  { you can add units after this };
    1212
  • ProtocolBuffers/UMainForm.lfm

    r6 r8  
    1717    TabOrder = 0
    1818  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
    1937end
  • ProtocolBuffers/UMainForm.lrs

    r6 r8  
    44  +#1#11'ClientWidth'#3#192#1#8'OnCreate'#7#10'FormCreate'#9'OnDestroy'#7#11'Fo'
    55  +'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
    711]);
  • ProtocolBuffers/UMainForm.pas

    r7 r8  
    99  StdCtrls, UProtocolBuffers, UMemoryStreamEx;
    1010
     11const
     12  SampleProtoFileName = 'Sample.proto';
     13
    1114type
    1215  { TMainForm }
    1316  TMainForm = class(TForm)
     17    Button1: TButton;
     18    Button2: TButton;
    1419    Memo1: TMemo;
     20    procedure Button1Click(Sender: TObject);
     21    procedure Button2Click(Sender: TObject);
    1522    procedure FormCreate(Sender: TObject);
    1623    procedure FormDestroy(Sender: TObject);
    1724  private
    18     { private declarations }
     25    procedure DisplayStream(Stream: TStream);
    1926  public
    2027    PB: TProtocolBuffer;
     
    2936
    3037procedure TMainForm.FormCreate(Sender: TObject);
     38begin
     39end;
     40
     41procedure TMainForm.Button2Click(Sender: TObject);
     42var
     43  Stream: TMemoryStream;
     44  StringList: TStringList;
     45begin
     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;
     57end;
     58
     59procedure TMainForm.Button1Click(Sender: TObject);
    3160var
    3261  Stream: TMemoryStreamEx;
    33   Text: string;
    34   I: Integer;
    3562  NewItem: TPBItem;
    3663begin
     
    4572    end;
    4673    SaveToStream(Stream);
     74    DisplayStream(Stream);
    4775  end;
    48 
    49   Stream.Position := 0;
    50   Text := '';
    51   for I := 1 to Stream.Size do begin
    52     Text := Text + IntToHex(Stream.ReadByte, 2) + ' ';
    53   end;
    54   Memo1.Lines.Text := Text;
    5576  Stream.Free;
    5677end;
     
    6182end;
    6283
     84procedure TMainForm.DisplayStream(Stream: TStream);
     85var
     86  I: Integer;
     87  Text: string;
     88begin
     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;
     95end;
     96
    6397initialization
    6498  {$I UMainForm.lrs}
  • ProtocolBuffers/UProtocolBuffers.pas

    r7 r8  
    2424    ItemMode: TPBItemMode;
    2525    procedure SaveVariantToStream(Stream: TStream; Value: Integer);
     26    function LoadVariantFromStream(Stream: TStream): Integer;
    2627    procedure SaveToStream(Stream: TStream); virtual;
    2728    procedure LoadFromStream(Stream: TStream); virtual;
    28     function LoadVariantFromStream(Stream: TStream);
    2929  end;
    3030
     
    3434  TPBStringItem = class(TPBItem)
    3535    Value: string;
     36    procedure SaveToStream(Stream: TStream); override;
     37    procedure LoadFromStream(Stream: TStream); override;
     38    constructor Create;
    3639  end;
    3740
     
    4043    Value: Integer;
    4144    procedure SaveToStream(Stream: TStream); override;
     45    procedure LoadFromStream(Stream: TStream); override;
    4246    constructor Create;
    4347  end;
     
    5761    procedure LoadFromStream(Stream: TStream);
    5862    procedure SaveToStream(Stream: TStream);
     63    procedure LoadFromProto(Source: TStringList);
    5964    constructor Create;
    6065    destructor Destroy; override;
     
    7681begin
    7782  BaseMessage.SaveToStream(Stream);
     83end;
     84
     85procedure TProtocolBuffer.LoadFromProto(Source: TStringList);
     86begin
     87
    7888end;
    7989
     
    101111
    102112procedure TPBMessageItem.LoadFromStream(Stream: TStream);
     113var
     114  I: Integer;
    103115begin
    104116  inherited LoadFromStream(Stream);
     117  for I := 0 to Items.Count - 1 do
     118    TPBItem(Items[I]).LoadFromStream(Stream);
    105119end;
    106120
     
    168182  while Data > $7f do begin
    169183    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));
    171185    Inc(ByteIndex);
    172186  end;
    173187end;
    174188
    175 function TPBItem.LoadVariantFromStream(Stream: TStream);
     189function TPBItem.LoadVariantFromStream(Stream: TStream): Integer;
    176190var
    177191  Data: Byte;
     
    179193begin
    180194  Data := TMemoryStreamEx(Stream).ReadByte;
    181   Tag := Data and $7f;
     195  Result := Data and $7f;
    182196  ByteIndex := 1;
    183197  while Data > $7f do begin
    184198    Data := TMemoryStreamEx(Stream).ReadByte;
    185     Tag := Tag or ((Data and $7f) shl (ByteIndex * 7))
     199    Result := Result or ((Data and $7f) shl (ByteIndex * 7));
    186200    Inc(ByteIndex);
    187201  end;
     
    196210end;
    197211
     212procedure TPBIntegerItem.LoadFromStream(Stream: TStream);
     213begin
     214  inherited LoadFromStream(Stream);
     215  LoadVariantFromStream(Stream);
     216end;
     217
    198218constructor TPBIntegerItem.Create;
    199219begin
     
    201221end;
    202222
     223{ TPBStringItem }
     224
     225procedure TPBStringItem.SaveToStream(Stream: TStream);
     226begin
     227  inherited SaveToStream(Stream);
     228  SaveVariantToStream(Stream, Length(Value));
     229  TMemoryStreamEx(Stream).Write(Value[1], Length(Value));
     230end;
     231
     232procedure TPBStringItem.LoadFromStream(Stream: TStream);
     233begin
     234  inherited LoadFromStream(Stream);
     235  SetLength(Value, LoadVariantFromStream(Stream));
     236  TMemoryStreamEx(Stream).Read(Value[1], Length(Value));
     237end;
     238
     239constructor TPBStringItem.Create;
     240begin
     241  ItemType := itLengthDelimited;
     242end;
     243
    203244end.
    204245
Note: See TracChangeset for help on using the changeset viewer.