Recent

Author Topic: String consts from HTML or SQL files  (Read 1960 times)

ydd

  • Jr. Member
  • **
  • Posts: 78
String consts from HTML or SQL files
« on: January 04, 2021, 07:27:09 pm »
I need to use many files with HTML or SQL templates. Now I load them at run-time and need to keep near executable.
I want to compile them into executable, but keep editing these files in external HTML or SQL editors - with highlight & options to validate or run.

I know that I can use Lazarus Resources (https://wiki.freepascal.org/Lazarus_Resources) and load content using TResourceStream.

But I'm sure that using string consts will work much faster and be easier to use.

So what I need:
- enum all *.htm & *.sql files in directory of current unit
- create file with string consts (const name should be based on template file name)
- include this file into project
- when any of *.htm & *.sql is changed - file should be regenerated
- OS - Windows & Linux

Is something like this already available in FPC/Lazarus?
Maybe there is better idea?
Or I should create on my own solution, run it manually or connect using Project Options / Compiler Commands, Execute before?
« Last Edit: January 04, 2021, 07:37:46 pm by ydd »

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: String consts from HTML or SQL files
« Reply #1 on: January 04, 2021, 07:33:42 pm »
I think that the standard answer would be to put the strings into resource files, but you'd get a far better class of response if you told people what OSes etc. you were expecting to run.

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

ydd

  • Jr. Member
  • **
  • Posts: 78
Re: String consts from HTML or SQL files
« Reply #2 on: January 04, 2021, 07:39:13 pm »
I think that the standard answer would be to put the strings into resource files

I know about such solution, but idea with string consts - is much better, faster. Also if const is not used - it will not be compiled into executable

but you'd get a far better class of response if you told people what OSes etc. you were expecting to run.

updated, Windows & Linux

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: String consts from HTML or SQL files
« Reply #3 on: January 04, 2021, 07:58:23 pm »
I'm sure that you noticed that there was a recent thread regarding multiline strings in standard FPC source, where it was made abundantly clear that the core developers didn't like the idea. While you could obviously use Perl etc. to parse HTML/SQL into custom Pascal source files, I'm pretty sure you won't get much official encouragement.

My own preference would be to be able to embed SQL etc. into high-level language sources, and this is of course fairly standard in various development environments: but I'm very much in a minority. At the same time I have grave misgivings about introducing yet more complexity into the syntax, e.g. coopting double-quoted strings for multiline text.

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

ydd

  • Jr. Member
  • **
  • Posts: 78
Re: String consts from HTML or SQL files
« Reply #4 on: January 04, 2021, 08:56:51 pm »
I'm sure that you noticed that there was a recent thread regarding multiline strings in standard FPC source

for me it is clear - that the way how multi-line string consts are implemented in FPC - is not good, for example in PHP - they are much better

My own preference would be to be able to embed SQL etc. into high-level language sources, and this is of course fairly standard in various development environments

I agree. Also important is syntax highlight, and for html - highlight that closing tag is missing

main reason why I decided to move sql & html to external files - is highlight

Rainbow6

  • New Member
  • *
  • Posts: 25
Re: String consts from HTML or SQL files
« Reply #5 on: January 05, 2021, 06:51:45 pm »
My own preference would be to be able to embed SQL etc. into high-level language sources, and this is of course fairly standard in various development environments: but I'm very much in a minority.

Hi Mark,

you mean something like a SQL pre-processor with „EXEC SQL“?

That would be great - I‘m using this every day with RPG, and it is so much easier for database access than any API can be.

If there would be any effort to implement something like that, I would be glad to participate - even when I‘m not the best Pascal programmer, I can help with standard syntax or docs.

Kind regards,
Daniel

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: String consts from HTML or SQL files
« Reply #6 on: January 05, 2021, 06:58:54 pm »
Maybe there is better idea?

Yeah, use resources.

A single file to constant is available as bin2obj and/or data2inc.

So then you either have to expand those to support multiple files, and find some way to rerun them each time.

But as said, just use resources, that is what they are meant for.

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: String consts from HTML or SQL files
« Reply #7 on: January 05, 2021, 07:25:35 pm »
Or I should create on my own solution, run it manually ...

I had similar problem too, not HTML nor SQL format but graphics files. The graphics files needed to be compiled into the binary but they're frequently changed when developing the program. I did not use TImageList because those images are different in sizes.

So I wrote a module to convert binary files into hexstring and the opposite. Then I wrote a small tool to convert any image I select to const hexstring in Pascal format. That file was loaded as a normal unit in the main project, so I can simply recompile the project to refresh the images that are 'hardcoded' in the binary.

In your case, you do not need to convert the data to hexstring. You can simply write the tool to convert the data to a Pascal format file.

I did not consider using resources because my way is simpler than using resources. I meant I do not need to remember which files need to be refreshed on the project setting. The downside is I had to spend some time for writing that function but that was good for learning.
« Last Edit: January 05, 2021, 07:41:19 pm by Handoko »

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: String consts from HTML or SQL files
« Reply #8 on: January 05, 2021, 08:43:35 pm »
I both sympathise with OP- since after all it's his project and he understands his workflow- and don't want to upset the core developers.

I think that if I were doing this I'd put SQL queries etc. in the source demarcated by something that the compiler would tolerate, and write a preprocessor to extract stuff from the original files into resource files without attempting to change the original. Based on one of the wiki examples:

Code: Pascal  [Select][+][-]
  1. procedure exampleproc;
  2. var
  3.   Image: TImage
  4. begin
  5.   Image := TImage.Create;
  6.   Image.Picture.LoadFromLazarusResource('image'); // note that there is no need for the extension
  7. {$ifdef USERRESOURCE_image }
  8.  
  9. The content of the image resource is defined here in arbitrary format.
  10.  
  11. {$endif USERRESOURCE_image }
  12.  
  13. end;
  14.  

Obviously USERRESOURCE_image is never defined in the context of the Pascal compiler and IDE, it's there as a tag that an external preprocessor can recognise.

Frankly I'd prefer it if there were some properly-agreed here-document mechanism, or at least an agreed $directive that could be used to tag stuff for an out-of-IDE preprocessor.

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

ydd

  • Jr. Member
  • **
  • Posts: 78
Re: String consts from HTML or SQL files
« Reply #9 on: January 19, 2021, 03:18:09 am »
I think that if I were doing this I'd put SQL queries etc. in the source demarcated by something that the compiler would tolerate, and write a preprocessor to extract stuff from the original files into resource files without attempting to change the original. Based on one of the wiki examples:

I like your idea very much, thanks!

And already implemented simple parser. I will test it more for real usage. And post here the results!

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: String consts from HTML or SQL files
« Reply #10 on: January 22, 2021, 10:31:45 pm »
I can't come up with a better solution than {$ifdef USERRESOURCE_image } etc., since even if we were to ask the developers for a directive that identified a block of "here-document" text it would- from their POV- be equivalent to an $ifdef so there is really very little point.

Marco (or anybody else still reading): what are the restrictions on the token that follows $ifdef, i.e. length and acceptable characters? Is there anything that can appear in that token, for example <> or {} characters, that can't easily be set by a $define etc. making certain sequences a safe choice for external preprocessors?

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

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: String consts from HTML or SQL files
« Reply #11 on: January 22, 2021, 11:11:33 pm »
Marco (or anybody else still reading): what are the restrictions on the token that follows $ifdef, i.e. length and acceptable characters? Is there anything that can appear in that token, for example <> or {} characters, that can't easily be set by a $define etc. making certain sequences a safe choice for external preprocessors?

IIRC, the token must be a valid Pascal identifier, precisely because it has to be setable by a {$define...} (and I guess also to simplify parsing).
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.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: String consts from HTML or SQL files
« Reply #12 on: January 23, 2021, 10:40:55 am »
IIRC, the token must be a valid Pascal identifier, precisely because it has to be setable by a {$define...} (and I guess also to simplify parsing).

Thanks for that, but OTOH the classic preprocessor directives (until declared() came along) were broadly outside the scope of Pascal itself... I've had fun in the past getting code working which had to compile with two different compilers which had completely different directive forms.

Whatever, "make it a Pascal identifier" sounds fairly safe and unambiguous.

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

 

TinyPortal © 2005-2018