Recent

Author Topic: Old chestnut: including a big block of raw text into a program  (Read 13063 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 3502
Is there a way that works with as-installed FPC on Linux of preloading a big block of raw text into a string? For the purpose of the question please assume that this is a multi-line script which includes arbitrary quote characters etc.

If this were Perl or C++ I'd use a here-document. If using Lazarus I have in the past put it into a TStringList component, but the editor is primitive and modal so it's impossible to consult this while working elsewhere in the program.

I've reverted to defining it as a multiline constant, but either have to use the double-' escape or define a substitute and preprocess the text... the latter works out better but is still hardly ideal:

Code: Pascal  [Select][+][-]
  1. const
  2.   qq= LineEnding;                       (* Prefix is same size as 8-spaces tab  *)
  3.  
  4.   syntax='.QUOTE `'
  5. + qq + '`MetaScriptSyntax` <<EOF'
  6. + qq + '.SYNTAX MetaScriptSyntax'
  7. + qq + ''
  8. + qq + '(* This is the master representation of the syntax of the embedded      *)'
  9. + qq + '(* script compiler.                                                     *)'
  10. ...
  11.  

MarkMLl

MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9700
  • FPC developer.
Re: Old chestnut: including a big block of raw text into a program
« Reply #1 on: July 31, 2021, 12:42:05 pm »
Simply include the textfile as a resource, and load it from the resource? Afaik resources work with Linux too.


MarkMLl

  • Hero Member
  • *****
  • Posts: 3502
Re: Old chestnut: including a big block of raw text into a program
« Reply #2 on: July 31, 2021, 01:35:18 pm »
Simply include the textfile as a resource, and load it from the resource? Afaik resources work with Linux too.

https://wiki.freepascal.org/Lazarus_Resources says that doing so needs windres or GoRC, neither of which comes as standard. The current manual concurs, and the examples still have to be quoted.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Bart

  • Hero Member
  • *****
  • Posts: 4510
    • Bart en Mariska's Webstek
Re: Old chestnut: including a big block of raw text into a program
« Reply #3 on: July 31, 2021, 01:50:37 pm »
Or LazRes (using old style *.lrs resources)...

Bart

MarkMLl

  • Hero Member
  • *****
  • Posts: 3502
Re: Old chestnut: including a big block of raw text into a program
« Reply #4 on: July 31, 2021, 02:21:59 pm »
Or LazRes (using old style *.lrs resources)...

Except that's not part of FPC.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 793
  • Professional amateur ;-P
Re: Old chestnut: including a big block of raw text into a program
« Reply #5 on: July 31, 2021, 02:25:49 pm »
Hey Mark,

Is there a way that works with as-installed FPC on Linux of preloading a big block of raw text into a string? For the purpose of the question please assume that this is a multi-line script which includes arbitrary quote characters etc.

First of all I don't know if you want this on a program you're compiling with the fp console IDE or with the Lazarus IDE, so I'll try and give an answer to both:

1. Using fp

I spent about 20m looking in all the menus for something that would remotely mention resources. Alas I failed.
If there is a way, I need more time to investigate.

2. Using Lazarus

If you go to the project options(CTRL+SHIFT+F11) under the entry Resources on the tree view you can add all types of resources that are automatically linked to your binary.
This way you use the same procedure that creates the *.res file that is usually created and contains, AT LEAST, the default icon for the application when in GUI mode.
And from any normal Linux install, you're able to have that *.res file produced and linked, right?

I'm guessing that even if you're doing a purely console app, Lazarus will still link the resources you've added there, since I think that any binary, GUI or not can have resources linked to it.

If that works for you, do you then need assistance on how to retrieve the saved resource?
I haven't done that in a long while but I do remember that it wasn't hard.

Cheers,
Gus
Lazarus 2.3.0(trunk) FPC 3.3.1(trunk) Ubuntu 21.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.2(stable) Ubuntu 21.10 64b Dark Theme
http://github.com/gcarreno

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9700
  • FPC developer.
Re: Old chestnut: including a big block of raw text into a program
« Reply #6 on: July 31, 2021, 02:33:59 pm »
Simply include the textfile as a resource, and load it from the resource? Afaik resources work with Linux too.

https://wiki.freepascal.org/Lazarus_Resources says that doing so needs windres or GoRC, neither of which comes as standard. The current manual concurs, and the examples still have to be quoted.

Yes, until FPC 3.2.2 and fpcres.

The syntax is

fpcres -i logo.rc -of res  -o logo.res

« Last Edit: July 31, 2021, 03:07:10 pm by marcov »

MarkMLl

  • Hero Member
  • *****
  • Posts: 3502
Re: Old chestnut: including a big block of raw text into a program
« Reply #7 on: July 31, 2021, 03:17:16 pm »
Yes, until FPC 3.2.2 and fpcres.

The syntax is

fpcres -i logo.rc -of res  -o logo.res

So how does embedded text look? Because if it still has to be quoted there really isn't much point from my POV.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9700
  • FPC developer.
Re: Old chestnut: including a big block of raw text into a program
« Reply #8 on: July 31, 2021, 03:32:35 pm »
Yes, until FPC 3.2.2 and fpcres.

The syntax is

fpcres -i logo.rc -of res  -o logo.res

So how does embedded text look? Because if it still has to be quoted there really isn't much point from my POV.

You store the text in a textfile, and then make a .rc file containing this:

Defaults RCDATA defaults.xml

this makes the contents of the file "defaults.xml" binary (RCDATA is binary) available using the resource name "Defaults". You can include multi files using one .rc file. I do this e.g. with shader source files.

I however load the data over windows unit functions, but it should be possible portably too (Lazarus uses it afaik)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Old chestnut: including a big block of raw text into a program
« Reply #9 on: July 31, 2021, 03:38:27 pm »
So how does embedded text look? Because if it still has to be quoted there really isn't much point from my POV.

The text is embedded as-is, and can be retrieved without any conversion. Think of it as if loading a text file into a String, only instead of loading from an external file you get it from the resource in the executable.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

jamie

  • Hero Member
  • *****
  • Posts: 5020
Re: Old chestnut: including a big block of raw text into a program
« Reply #10 on: July 31, 2021, 04:06:12 pm »
Code: Pascal  [Select][+][-]
  1.   S := JSonStringTostring('Hello hackers \n');  
  2.  

You should be able to construct your strings this way..
you could use the complete \r\n too.. what ever combo you need.
The only true wisdom is knowing you know nothing

Bart

  • Hero Member
  • *****
  • Posts: 4510
    • Bart en Mariska's Webstek
Re: Old chestnut: including a big block of raw text into a program
« Reply #11 on: July 31, 2021, 05:33:17 pm »
Except that's not part of FPC.

Sorry, you're right of course.

Barr

MarkMLl

  • Hero Member
  • *****
  • Posts: 3502
Re: Old chestnut: including a big block of raw text into a program
« Reply #12 on: July 31, 2021, 06:13:52 pm »
You store the text in a textfile, and then make a .rc file containing this:

Defaults RCDATA defaults.xml

this makes the contents of the file "defaults.xml" binary (RCDATA is binary) available using the resource name "Defaults". You can include multi files using one .rc file. I do this e.g. with shader source files.

Thanks for that. So it's doable, but hardly painless.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9700
  • FPC developer.
Re: Old chestnut: including a big block of raw text into a program
« Reply #13 on: July 31, 2021, 06:33:02 pm »
Thanks for that. So it's doable, but hardly painless.

That is a very opinionated and biassed statement. There are multiple sides to this, and if you have e.g. external editors or viewers for such included files, then suddenly the in-source view is extremely cumbersome and a poor man's solution.

But many languages actively limit themselves to the in source solution,, because they can't standardize tooling outside of the core language due to oppressive requirements for formal language definitions. Then doing it in source becomes the "my only tool is a hammer" kind of solution.

FPC and Delphi don't have that problem.   

engkin

  • Hero Member
  • *****
  • Posts: 2977
Re: Old chestnut: including a big block of raw text into a program
« Reply #14 on: July 31, 2021, 06:46:35 pm »
I've reverted to defining it as a multiline constant, but either have to use the double-' escape or define a substitute and preprocess the text... the latter works out better but is still hardly ideal:

Code: Pascal  [Select][+][-]
  1. const
  2.   qq= LineEnding;                       (* Prefix is same size as 8-spaces tab  *)
  3.  
  4.   syntax='.QUOTE `'
  5. + qq + '`MetaScriptSyntax` <<EOF'
  6. + qq + '.SYNTAX MetaScriptSyntax'
  7. + qq + ''
  8. + qq + '(* This is the master representation of the syntax of the embedded      *)'
  9. + qq + '(* script compiler.                                                     *)'
  10. ...
  11.  


There is a patch for FPC to add multi-line strings. Discussed here.

I don't know why I have to sign in to gitlab to view the bug report %)

 

TinyPortal © 2005-2018