Recent

Author Topic: Reading\writing formal params partially  (Read 1212 times)

cch

  • Newbie
  • Posts: 6
Reading\writing formal params partially
« on: April 16, 2019, 01:33:00 pm »
Hi!
My method recieves the formal parameter Buffer. I can't figure out how to read and write it partially.
Example
Code: Pascal  [Select][+][-]
  1. procedure my_proc( var Buffer; count: int64 ) //let count be 100
  2. var
  3. strm1, strm2: tStream;
  4. begin
  5. ...
  6. //read\write 20 bytes from strm1
  7. //read\write 80 bytes from strm2
  8.  
I do it via temporary stream but it is not a nice way I think, and it must decrease the performance.
Somewhere on this forum people say that such untyped variable is a pointer itself. But pointer logic like
Code: Pascal  [Select][+][-]
  1. strm2.Read( buffer+20, 80 )
gives a compilation-time error.
Am I to cast Buffer to pointer explicitly? Or maybe I have to cast Buffer to something iterable like
Code: Pascal  [Select][+][-]
  1. type tByteArray = array of byte;
  2. ...
  3. strm2.Read( tByteArray( Buffer )[20], 80 )
  4.  
? Or maybe there's another solution?
Thx

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11458
  • FPC developer.
Re: Reading\writing formal params partially
« Reply #1 on: April 16, 2019, 01:44:24 pm »
There are various ways. I usually use

strm1.read(pbyte(buffer)[20],20);

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: Reading\writing formal params partially
« Reply #2 on: April 16, 2019, 01:51:21 pm »
You can use extra local variable with absolute and specify the data type:

Code: Pascal  [Select][+][-]
  1. procedure ShowBuffer(var ABuffer; ACount: Integer);
  2. var
  3.   Bytes: TByteArray absolute ABuffer;
  4. begin
  5.   {..}

and use this variable without casting, instead of the parameter. Short example:

Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}{$LONGSTRINGS ON}
  2.  
  3. uses
  4.   SysUtils;
  5.  
  6.   procedure ShowBuffer(var ABuffer; ACount: Integer);
  7.   var
  8.     Bytes: TByteArray absolute ABuffer;
  9.     I: Integer;
  10.   begin
  11.     WriteLn('Buffer size: ', ACount);
  12.     Write('Buffer data:');
  13.  
  14.     for I := 0 to ACount - 1 do
  15.       Write('$' + HexStr(Bytes[I], 2):4);
  16.  
  17.     Write(LineEnding, LineEnding);
  18.   end;
  19.  
  20. var
  21.   Bytes: TBytes;
  22.   Text: String;
  23.   Number: Integer;
  24. begin
  25.   // bytes array
  26.   Bytes := TBytes.Create(2, 4, 8, 16, 32, 64, 128);
  27.   ShowBuffer(Bytes[0], Length(Bytes));
  28.  
  29.   // string
  30.   Text := 'ABCDE';
  31.   ShowBuffer(Text[1], Length(Text));
  32.  
  33.   // integer
  34.   Number := $AABBCCDD;
  35.   ShowBuffer(Number, SizeOf(Number));
  36. end.

Output:

Code: Pascal  [Select][+][-]
  1. Buffer size: 7
  2. Buffer data: $02 $04 $08 $10 $20 $40 $80
  3.  
  4. Buffer size: 5
  5. Buffer data: $41 $42 $43 $44 $45
  6.  
  7. Buffer size: 4
  8. Buffer data: $DD $CC $BB $AA
« Last Edit: April 16, 2019, 01:57:05 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

cch

  • Newbie
  • Posts: 6
Re: Reading\writing formal params partially
« Reply #3 on: April 16, 2019, 02:54:58 pm »
wow, I love the "absolute" magic))
I'll try both variants though)
thank you, guys!

 

TinyPortal © 2005-2018