1
votes

I am trying to learn Free Pascal using Lazarus and one of my pet projects involves reading the 64 byte headers of a particular set of untyped files that cannot be read and displayed using text or ASCII related procedures (so cannot be outputted directly to Memo boxes etc).

So far, I have devised the following code which does, I think, read in the 64 bytes of the header and I am using TStreams and a "Select Directory" dialog box to do this, based on advice received via the Lazarus IRC. My question though is how to actually USE the data that is read into the buffer from the header? For example, in the headers, there are sequences of 8 bytes, then 16 bytes, then 2 bytes and so on that I want to "work on" to generate other output that will eventually be converted to a string to go into my string grid.

Some of what I have so far is based on what I found here written by Mason Wheeler near the end (http://stackoverflow.com/questions/455790/fast-read-write-from-file-in-delphi) but it only shows how to read it in, not how to use it. I also read this (http://stackoverflow.com/questions/4309739/best-way-to-read-parse-a-untyped-binary-file-in-delphi) but again, it shows you how to READ the data too, but not subsequently USE the data. Any guidance wamrly received! So far, the code below just outputs single value integer numbers to the edit box, as opposed to, say, a range of 8 hexadecimal values.

PS - I am new to programming so please be gentle! Nothing too complex.

procedure TForm1.ProbeFile(FileIterator: TFileIterator);

type
 TMyHeader = Array[1..64] of packed record
 First8Bytes,
 Next16Bytes,
 Next2Bytes: byte;
 end;

var

  FI : TFileIterator;                      //File Iterator class
  SG : TStringGrid;
  NumRead : SmallInt;
  FileToProbe: TStream;
  header: TMyHeader;

begin
  FI := TFileIterator.Create;
  SG := TStringGrid.Create(self);


  // Open the file and read the header
  FileToProbe := TFileStream.Create(FileIterator.FileName, fmOpenRead);
  try
    FileToProbe.seek(0, soFromBeginning);
    FileToProbe.ReadBuffer(header, SizeOf(header));
    edit1.text := IntToStr(header[0].First8Bytes);  // Just outputs '0' to the field?  If I try '10' it ooutputs '29' and so on
  finally
    FileToProbe.Free;
  end; 
1

1 Answers

2
votes

Please forgive me if I misunderstood your question.

As I understand it there is a header of 64 bytes. The first 8 bytes belong together, then the next 16 bytes and finally another 2 bytes.

To me it seems the declaration for this header should be:

   TMyHeader = packed record
     First8Bytes: array[0..7] of byte;  
     Next16Bytes: array [0..15] of byte;
     Next2Bytes: array [0..1] of byte;
     // add more if you like
   end;

This recordtype has a size of 8+16+2 = 26 bytes.

Your code that reads the header looks ok to me, So I won't repeat that.

The next16bytes in your header can be retrieved, for example, like this:

edit1.text:= '';
// needs a declaration of a variable "i" as integer
for i:= 0 to 15 do
  edit1.text:= edit1.text + IntToStr(header.next16bytes[i]) + '-';

Change the value of the first byte in the next2bytes part of your header as follows (again as an example):

header.next2bytes[0]:= 123;

Finally, you could write your changes back to the header of the file with help of the filetoprobe.writebuffer method.