4
votes

I am trying to make a basic Hex viewer out of a TMemo, I know this is probably not ideal but it will be only me personally using it so that does not really matter.

(1)

Firstly, suppose a Memo is filled with Hex information like so:

enter image description here

How could I get a count of all the text blocks shown, ignoring the white space? So using the image the result in this case would be 28.

This is what I tried and I know it is completely wrong as I am accessing the Memo lines but I don't know how to access each character.

I cant seem to solve this simple problem :(

function CountWordBlocks(Memo: TMemo): Integer;
var
  i: Integer;
  vCount: Integer;
begin
  for i := 0 to Memo.Lines.Count - 1 do
  begin
    if Length(Memo.Lines.Strings[i]) = 2 then
    begin
      Inc(vCount);
    end;
  end;

  Result := vCount;
end;

Here is the code I am using to display the Hex values in the Memo:

procedure ReadFileAsHex(const AFileName: string; ADestination: TStrings);
var
  fs: TFileStream;
  buff: Byte;
  linecount: Byte;
  line: string;
begin
  linecount := 0;
  line := '';
  fs := TFileStream.Create(AFileName, fmOpenRead);

  try
    ADestination.BeginUpdate;
    try
      while fs.Position < fs.Size do
      begin
        fs.Read(buff, 1);
        line := line + IntToHex(buff, 2) + ' ';
        Inc(linecount);
        if linecount = 16 then
        begin
          ADestination.Add(line);
          line := '';
          linecount := 0;
        end;
      end;
      if Length(line) <> 0 then
        ADestination.Add(line);
    finally
      ADestination.EndUpdate;
    end;
  finally
    fs.Free;
  end;
end;

(2)

If I click onto the Memo and a text block is under the cursor, how could I know which number the selected block is out of all the others?

So using the same first image, the caret is at the top line next to 68, so the result would be 3 as it is the third text block out of 28.

This should be so easy but I cannot think clearly, I don't have the right programming mind yet and so really struggle with basic logic and solving problems!

(3)

Finally I would like to select a block at runtime by passing a block number value. I tried this without much success:

procedure FindBlock(Memo: TMemo; BlockNumber: Integer);
var
  i: Integer;
  txt: string;
  ThisWhite, PrevWhite: boolean;
  vRead: Integer;
begin
  txt := Memo.Text;
  vRead:= 0;
  PrevWhite := True;
  for i := 1 to Length(txt) do
  begin
    ThisWhite := Character.IsWhiteSpace(txt[i]);
    if PrevWhite and not ThisWhite then
    begin
      Inc(vRead);
      PrevWhite := False;
    end;
    PrevWhite := ThisWhite;

    if vRead = BlockNumber then
    begin
      Memo.SelStart := vRead;
      Memo.SetFocus;
      Exit;
    end;
  end;
end;
2

2 Answers

6
votes

(1)

This works:

function TForm1.CountBlocks: integer;
var
  i: Integer;
  txt: string;
  ThisWhite, PrevWhite: boolean;
begin
  txt := Memo1.Text;
  result:= 0;
  PrevWhite := true;
  for i := 1 to Length(txt) do
  begin
    ThisWhite := Character.IsWhiteSpace(txt[i]);
    if PrevWhite and not ThisWhite then
    begin
      inc(result);
      PrevWhite := false;
    end;
    PrevWhite := ThisWhite;
  end;
end;

However, it can be optimized if more detailed information about the memo contents is available. For instance, if you know that each line consists of four blocks, then the number of blocks is simply 4*Memo1.Lines.Count. My code above will even accept blocks of different width.

(2)

Simply replace

for i := 1 to Length(txt) do

by

for i := 1 to Memo1.SelStart + 1 do
2
votes

Since you are in control of the formatting of your lines, and the lines have a fixed format, it is very easy to calculate the number of bytes being displayed without resorting to looping through the individual lines one a time. Every line displays 3 characters per byte, and every line other than the last line displays 16 bytes, thus 48 characters per complete 16-byte line. Use those facts to your advantage to calculate the number of bytes based on the number of complete 16-byte lines present, and then you can add on the number of remaining bytes from just the last line:

function CountWordBlocks(Memo: TMemo): Integer; 
var 
  Count: Integer; 
begin 
  Count := Memo.Lines.Count;
  if Count > 0 then
    Result := (16 * (Count-1)) + (Length(Memo.Lines[Count-1]) div 3);
  else
    Result := 0;
end;

You can do something similar to translate a character offset within the Memo into a work block number:

function GetCurrentWordBlock(Memo: TMemo): Integer;
var
  SelStart, LineStart, LineNum: Integer
begin
  Result := 0;
  SelStart := Memo.SelStart;
  if SelStart < 0 then Exit;
  LineStart := Memo.Perform(EM_LINEINDEX, SelStart, 0);
  if LineStart < 0 then Exit;
  LineNum := Memo.Perform(EM_LINEFROMCHAR, LineStart, 0);
  Result := (16 * LineNum) + ((SelStart - LineStart) div 3) + 1;
end;

To select a given block number, you can do this:

procedure FindBlock(Memo: TMemo; BlockNumber: Integer); 
var
  LineNum, LineStart: Integer;
begin 
  if BlockNumber < 1 then Exit;
  LineNum = (BlockNumber - 1) div 16;
  LineStart = Memo.Perform(EM_LINEINDEX, LineNum, 0);
  if LineStart < 0 then Exit;
  Memo.SelStart = LineStart + (((BlockNumber - 1) - (16 * LineNum)) * 3);
  Memo.SelLength := 2;
  Memo.SetFocus; 
end;