Recent

Author Topic: Best way to include lots of text?  (Read 2866 times)

domasz

  • Hero Member
  • *****
  • Posts: 553
Best way to include lots of text?
« on: June 04, 2023, 12:03:01 pm »
How can I include lots of text?

I can define strings like this:
Code: Pascal  [Select][+][-]
  1. Str := 'line' + #13#10 + 'second line';
but that's terrible when I have thousands of line.
Code: Pascal  [Select][+][-]
  1. I can do {$INCLUDE text.txt}
but it needs to be a valid Pascal code.
I can do:
Code: Pascal  [Select][+][-]
  1. StringList.LoadFromFile('test.txt');
but then I need to distribute this file with my exe.

Is there a way to easily insert huge files of text into source code, like in PHP where I can just write:

Code: Pascal  [Select][+][-]
  1. $Str := 'line
  2. second line
  3. third line';
?

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: Best way to include lots of text?
« Reply #1 on: June 04, 2023, 12:31:14 pm »
create a unit that houses just your string and use a "ResourceString"

It will be loaded in the resource but not in the binary and you can access when needed.
The only true wisdom is knowing you know nothing

domasz

  • Hero Member
  • *****
  • Posts: 553
Re: Best way to include lots of text?
« Reply #2 on: June 04, 2023, 12:38:29 pm »
But with ResourceString I still need to define value in this retarded way:

Code: Pascal  [Select][+][-]
  1. 'line' + #13#10 + 'second line';

wp

  • Hero Member
  • *****
  • Posts: 12461
Re: Best way to include lots of text?
« Reply #3 on: June 04, 2023, 01:00:39 pm »
Add the text as a resource:
  • Project Options > Resources > Add > pick your text file and make sure that the resource type is RCDATA. Note that the filename will become the name of the resource - we need that in the code.
  • In the OnCreate handler of the form (or when the text is needed), read the file from the resource into a TResourceStream and display it in a memo, or do whatever what you want to do with the text.
Code: Pascal  [Select][+][-]
  1. uses
  2.   LCLType;  // for RT_RCDATA
  3.  
  4. procedure TForm1.FormCreate(Sender: TObject);
  5. var
  6.   s: TResourceStream;
  7.   resName: String;
  8. begin
  9.   resName := '<name of the resource as displayed>';
  10.   s := TResourceStream.Create(HINSTANCE, resName, RT_RCDATA);
  11.   try
  12.     Memo1.Lines.LoadFromStream(s);
  13.   finally
  14.     s.Free;
  15.   end;
  16. end;

This way you can edit the text file whenever you need to, and it is always included automatically (well - you will have to do "run" > "build" to link the new version, though...).

Swami With The Salami

  • Guest
Re: Best way to include lots of text?
« Reply #4 on: June 04, 2023, 05:22:05 pm »
Lots of text can mean a number of things - do you need to present hundreds of different pages in different areas or all of it at once?
« Last Edit: June 04, 2023, 05:45:26 pm by swts »

VTwin

  • Hero Member
  • *****
  • Posts: 1224
  • Former Turbo Pascal 3 user
Re: Best way to include lots of text?
« Reply #5 on: June 04, 2023, 09:42:11 pm »
Add the text as a resource:
...

Nice solution. I just rewrote a help dialog to use it.
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 14.5: Lazarus 3.4 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 3.4 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 3.4 (64 bit on VBox)

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Best way to include lots of text?
« Reply #6 on: June 04, 2023, 10:54:51 pm »
Include a database with whatever text, easy to update IMHO.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

domasz

  • Hero Member
  • *****
  • Posts: 553
Re: Best way to include lots of text?
« Reply #7 on: June 05, 2023, 07:45:07 pm »
Add the text as a resource:
Not great but thank you!

domasz

  • Hero Member
  • *****
  • Posts: 553
Re: Best way to include lots of text?
« Reply #8 on: June 05, 2023, 07:46:12 pm »
Include a database with whatever text, easy to update IMHO.
Interesting, but a lot slower solution.

440bx

  • Hero Member
  • *****
  • Posts: 4737
Re: Best way to include lots of text?
« Reply #9 on: June 05, 2023, 08:04:27 pm »
Add the text as a resource:
Not great but thank you!
Actually that solution is basically what you likened it to in PHP

Is there a way to easily insert huge files of text into source code, like in PHP where I can just write:

Code: Pascal  [Select][+][-]
  1. $Str := 'line
  2. second line
  3. third line';
?
Once you get the pointer to the RCDATA resource you basically have a pointer to a string that is the entire text (probably a good idea to ensure the text is null terminated to make things simpler.)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

domasz

  • Hero Member
  • *****
  • Posts: 553
Re: Best way to include lots of text?
« Reply #10 on: June 07, 2023, 12:36:51 pm »
Once you get the pointer to the RCDATA resource you basically have a pointer to a string that is the entire text (probably a good idea to ensure the text is null terminated to make things simpler.)
My problem here is with that "once". It would be so much better to just declare multiline strings in the source code and not add into resources and load.
But this solution still seems like the best one.

robert rozee

  • Full Member
  • ***
  • Posts: 182
Re: Best way to include lots of text?
« Reply #11 on: June 07, 2023, 02:31:13 pm »
place your text in a file called mytext.txt then process it with the bin2obj utility provided with FPC:
bin2obj -o mytext.inc -c mytext mytext.txt

near the top of your program place the line:
Code: Pascal  [Select][+][-]
  1. {$I mytext.inc}

then within the body of your code have something like:
Code: Pascal  [Select][+][-]
  1. textstring:='';
  2. for I:=low(mytext) to high(mytext) do
  3.   textstring:=textstring+chr(mytext[I]);
where textstring is a string, and I is an integer.

textstring will now contain your text, including line breaks. the downside of this approach is that your data will be held in memory twice - once as mytext[] and a second time as textstring.


cheers,
rob   :-)
« Last Edit: June 07, 2023, 02:33:35 pm by robert rozee »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11942
  • FPC developer.
Re: Best way to include lots of text?
« Reply #12 on: June 07, 2023, 02:37:25 pm »
The fact that you describe the strings as "huge", makes resources the best option IMHO.

Nicole

  • Hero Member
  • *****
  • Posts: 1009
Re: Best way to include lots of text?
« Reply #13 on: June 07, 2023, 08:12:01 pm »
Depends of what you want to do with it and how huge is huge.
I like to save the text as txt and import it by loadfromfile into a TStringList.

*.rtf works fine as well.
Search the forum for a really great tutorial how to implement it.

Or you can use the odt-Format. Well, this is not easy, but very small as file.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5755
  • Compiler Developer
Re: Best way to include lots of text?
« Reply #14 on: June 08, 2023, 12:43:17 pm »
Once you get the pointer to the RCDATA resource you basically have a pointer to a string that is the entire text (probably a good idea to ensure the text is null terminated to make things simpler.)
My problem here is with that "once". It would be so much better to just declare multiline strings in the source code and not add into resources and load.
But this solution still seems like the best one.

Even if FPC would support multiline strings, resources should still be preferred, because it allows you to use whatever suitable editor there is for your data while that isn't easily possible if the data is embedded in a Pascal source file.

 

TinyPortal © 2005-2018