Recent

Author Topic: Position in a Memo  (Read 983 times)

justnewbie

  • Full Member
  • ***
  • Posts: 199
Re: Position in a Memo
« Reply #15 on: April 18, 2019, 07:17:38 pm »
Yes, of course, the exe file is in the program's directory.
Memo1 is on the form.
Did not work: I got an empty Memo.
« Last Edit: April 18, 2019, 07:23:08 pm by justnewbie »

lucamar

  • Hero Member
  • *****
  • Posts: 1483
Re: Position in a Memo
« Reply #16 on: April 18, 2019, 08:05:09 pm »
Try this for testing:
Code: Pascal  [Select]
  1. procedure ReadBinaryToMemo(const AFilename: String; const Memo: TMemo);
  2. var
  3.   AString: RawByteString;
  4.   i; Integer;
  5. begin
  6.   if AFilename.IsEmpty then
  7.     ShowMessage('AFilename is empty!')
  8.   else if not FileExists(AFileName) then
  9.     ShowMessageFmt('File: %s doesn''t exits!', [AFilename])
  10.   else begin
  11.     AString := ReadFileToString(AFilename);
  12.     if AString = '' then begin
  13.       ShowMessage('The file is empty?!')
  14.     end else begin
  15.       for i := 1 to Length(AString) do
  16.         if AString[i] in ProblemChars then
  17.           AString[i] := Space;
  18.       Memo.Text := AString;
  19.     end;
  20.   end;
  21. end;

and tell me if any of the mesage boxes is shown.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

justnewbie

  • Full Member
  • ***
  • Posts: 199
Re: Position in a Memo
« Reply #17 on: April 18, 2019, 08:28:55 pm »
Extremely strange. Nothing happens. No message at all and the Memo is empty.
I have a form and a Button and a Memo on it.
I used this code:
Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   StrUtils;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Memo1: TMemo;
  18.     procedure Button1Click(Sender: TObject);
  19.     procedure ReadBinaryToMemo(const AFilename: String; const Memo: TMemo);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35.  
  36.  
  37. procedure TForm1.Button1Click(Sender: TObject);
  38. begin
  39.   ReadBinaryToMemo('notepad.exe', Memo1);
  40. end;
  41.  
  42. procedure TForm1.ReadBinaryToMemo(const AFilename: String; const Memo: TMemo);
  43. const
  44.   ProblemChars = [#0..#31];
  45.   Space = #32;
  46. var
  47.   AString: RawByteString;
  48.   i: Integer;
  49. begin
  50.   if AFilename.IsEmpty then
  51.     ShowMessage('AFilename is empty!')
  52.   else if not FileExists(AFileName) then
  53.     ShowMessageFmt('File: %s doesn''t exits!', [AFilename])
  54.   else begin
  55.     AString := ReadFileToString(AFilename);
  56.     if AString = '' then begin
  57.       ShowMessage('The file is empty?!')
  58.     end else begin
  59.       for i := 1 to Length(AString) do
  60.         if AString[i] in ProblemChars then
  61.           AString[i] := Space;
  62.       Memo.Text := AString;
  63.     end;
  64.   end;
  65. end;
  66.  
  67. end.

Obviously I did something wrong, but cannot find.
It seems that "ReadBinaryToMemo" doesn't run when I click the button?  :(
« Last Edit: April 18, 2019, 08:32:13 pm by justnewbie »

lucamar

  • Hero Member
  • *****
  • Posts: 1483
Re: Position in a Memo
« Reply #18 on: April 18, 2019, 08:54:14 pm »
Extremely strange. Nothing happens. No message at all and the Memo is empty.
I have a form and a Button and a Memo on it.
I used this code:
Code: Pascal  [Select]
  1. {... code here ...}

Obviously I did something wrong, but cannot find.

You did nothing wrong and you're right: it doesn't work, although I don't know why. It worked before in a simpler CLI test (which unfortunately I've deleted :( ).

Let me try a couple things and let's see if I can make it work somehow.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

justnewbie

  • Full Member
  • ***
  • Posts: 199
Re: Position in a Memo
« Reply #19 on: April 18, 2019, 09:07:34 pm »
OK, thank you for your effort!
(Maybe it has nothing to do with this thing, but I found earlier that the Memo component cannot load files that are in UTF-16 format, only with UTF-8. I don't know if it does matter in this case?)

lucamar

  • Hero Member
  • *****
  • Posts: 1483
Re: Position in a Memo
« Reply #20 on: April 19, 2019, 12:06:44 am »
Ok, it has been a couple funny (not!) hours of trying this and that and finally managed to ascertain that it's not the transposition that fails: that works well, one just have to save the result string to a file to see it.

What's failing is adding the string to the memo! Nothing works: Memo.Append(), Memo.Lines.Add(), Memo.Lines.AddText(), assigning Memo.Text, assigning Memo.Lines.Text, ... nothing!

And here is the even more curious thing: if the file constains only text, it works: the transposition simply replaces all line-endings with dots/spaces and the whole appears as a single line. But with a binary file, which in theory should do the same, nothing appears in the memo. >:D

I'm completely surprised, atonit, flaberglasted.

Only explanation I can think of (and is as wild a guess as they come) is that the memo is trying to interpret the string as UTF-8 and failing miserably.

Next test: After converting the string, save it to a temp file and open that file in the memo with LoadFromFile(). But I'll leave that to you ... ok, no: here it is, in all its untested "glory":
Code: Pascal  [Select]
  1. const
  2.   ProblemChars = [#0..#31];
  3.   Placeholder = '.';
  4.  
  5. procedure ReadBinaryToMemo(const AFilename: String; const AMemo: TMemo);
  6. var
  7.   AString: RawByteString;
  8.   i; Integer;
  9. begin
  10.   AString := ReadFileToString(AFilename);
  11.   if AString = '' then
  12.     ShowMessage('Seems to be an empty file?')
  13.   else begin
  14.     { Replace problematic chars by dots }
  15.     for i := 1 to Length(AString) do
  16.       if AString[i] in ProblemChars then
  17.         AString[i] := Placeholder;
  18.     { Save the converted string to a temp file }
  19.     if FileExists('memotest.temp') then
  20.       DeleteFile('memotest.temp');
  21.     with TFileStream.Create('memotest.temp', fmCreate) do
  22.     try
  23.       { Extremely simplistic, naïve approach }
  24.       if Write(AString[1], Length(AString)) <> Length(AString) then
  25.         ShowMessage('Didn''t write the full string :( ');
  26.     finally
  27.       Free;
  28.     end;
  29.     {and load that file into the memo}
  30.     try
  31.       AMemo.LoadFromFile('memotest.temp');
  32.     finally
  33.       DeleteFile('memotest.temp');
  34.     end;
  35.   end;
  36. end;

(Maybe it has nothing to do with this thing, but I found earlier that the Memo component cannot load files that are in UTF-16 format, only with UTF-8. I don't know if it does matter in this case?)

No, it doesn't matter in this case but ... are you sure of that? I kind of remember having opened UTF16 files w/out problems. Of course my memory may be at fault here; I don' use many UTF16 files. :)
« Last Edit: April 19, 2019, 12:09:20 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

ASBzone

  • Full Member
  • ***
  • Posts: 225
  • Automation leads to relaxation...
    • BrainWaveCC Utilities
Re: Position in a Memo
« Reply #21 on: April 19, 2019, 03:41:52 am »
What's failing is adding the string to the memo! Nothing works: Memo.Append(), Memo.Lines.Add(), Memo.Lines.AddText(), assigning Memo.Text, assigning Memo.Lines.Text, ... nothing!

And here is the even more curious thing: if the file constains only text, it works: the transposition simply replaces all line-endings with dots/spaces and the whole appears as a single line. But with a binary file,


Does it have anything to do with data being flushed to disk?
-ASB: https://www.BrainWaveCC.com

Lazarus v2.0.3 r61159 / FPC v3.2.0-beta-r42011 (via FpcUpDeluxe) -- Windows 64-bit install w/32-bit cross-compile
Primary System: Windows 10 Pro x64, Version 1809 (Build 17763.379)
Other Systems: Windows 10 Pro x64, Version 1803 or greater

lucamar

  • Hero Member
  • *****
  • Posts: 1483
Re: Position in a Memo
« Reply #22 on: April 19, 2019, 03:34:16 pm »
Does it have anything to do with data being flushed to disk?

No, I don't think so. Whatever you do with it the string already has he data (printabl data, at that) and the memo should accept it whithout more ado ... but it doesn't. It behaves as if the string where empty, which it isn't. A real to goodness mistery :)

Have you been able to test saving to a temp file and loading from the it? I'm mired in another project and can't dedicate to this the time it merits.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

justnewbie

  • Full Member
  • ***
  • Posts: 199
Re: Position in a Memo
« Reply #23 on: April 19, 2019, 10:45:38 pm »
Hi lucamar, very sorry for not answering, but I cannot try it for 2 days. Will be back then and will look at it.

lucamar

  • Hero Member
  • *****
  • Posts: 1483
Re: Position in a Memo
« Reply #24 on: April 21, 2019, 10:25:40 am »
Hi lucamar, very sorry for not answering, but I cannot try it for 2 days. Will be back then and will look at it.

I have finally found some little time to try it and it doesn't work but I think it may be something with the file I'm "converting" here (the executable of the test itself) because I've been unable to open it with any other editor (and I have quite some of them installed: GEdit, Leafpad, Scribes, X2, ...)

If you manage to test it and it works tell me about it, will you? I'll keep testing when I can.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

justnewbie

  • Full Member
  • ***
  • Posts: 199
Re: Position in a Memo
« Reply #25 on: April 21, 2019, 11:18:48 am »
Here again.
Tested, doesn't work.

Some remarks (I am on Linux).
The 'notepad.exe' cannot be opened by the usual text editors (xed, medit ...).
But, it can be opened in the Notepad (running via Wine).
Other thing:
As wrote earlier, I found that UTF-16 encoded texts cannot be loaded into the TMemo component by the LoadFromFile. (Why?)
I have to convert/save it to UTF-8 in an external text editor and then Memo will accept it.
Also, if I copy/paste a UTF-16 text directly into the Memo, the text will appear in the Memo flawlessy.
Is it a bug or is it normal?


lucamar

  • Hero Member
  • *****
  • Posts: 1483
Re: Position in a Memo
« Reply #26 on: April 21, 2019, 12:12:38 pm »
Here again.
Tested, doesn't work.

Some remarks (I am on Linux).
The 'notepad.exe' cannot be opened by the usual text editors (xed, medit ...).
But, it can be opened in the Notepad (running via Wine).

I must confess I didn't try that. My Wine installation has some peculiar ideas as to what constitutes a good font size which I don't share at all :)

Quote
As wrote earlier, I found that UTF-16 encoded texts cannot be loaded into the TMemo component by the LoadFromFile. (Why?)
I have to convert/save it to UTF-8 in an external text editor and then Memo will accept it.
Also, if I copy/paste a UTF-16 text directly into the Memo, the text will appear in the Memo flawlessy.
Is it a bug or is it normal?

It may be normal: TMemo is a very basic editor (basically a multiline TEdit) and may not be ready to accept UTF16 text. Pasting from the clipboard is different: IIRC, the clipboard itself does the change.

By the way, you don't need to convert/save in an external editor: you can load your file to an UnicodeString and convert it to UTF-8 in your program, and viceversa for saving. Somewhat wasteful of memory but that that doesn't seem to matter much nowadays.

Of course, it means writing your own routines to Load/Save files but that is not very difficult: you could even write a type helper for TMemo with them :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.