Recent

Author Topic: How to pass multiple lines of text to a variable in the easiest way?  (Read 2114 times)

loaded

  • Sr. Member
  • ****
  • Posts: 310
Hi All,
Here's how I'm assigning multiple lines of text to a variable of type String.
Code: Pascal  [Select][+][-]
  1. var
  2.   kml:String='<?xml version="1.0" encoding="UTF-8"?>'+
  3.              '<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">'+
  4.              '<Document>';  

Similarly, I use {} to comment multiple lines. Sample ;
Code: Pascal  [Select][+][-]
  1. {
  2. <?xml version="1.0" encoding="UTF-8"?>
  3.   <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
  4.   <Document>
  5. }  
  6.  

Now my aim is; I want to be able to transfer a text containing multiple lines to a String variable, even if it contains single quotation marks ', without making any corrections or additions, such as the text inside the {} marks above, is this possible? Or is there an easier way? If you can help it would be greatly appreciated. Respects.


Lazarus V2.0.10 32 Bit + Intel I5 8250U + 12GB RAM + Win 10 Home 64 Bit = red pill
Morpheus: "You take the blue pill...the story ends, you wake up in your bed and believe whatever you want to believe. You take the red pill...you stay in Wonderland, and I show you how deep the rabbit hole goes."

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1053
Re: How to pass multiple lines of text to a variable in the easiest way?
« Reply #1 on: September 21, 2021, 04:39:14 pm »
hello,
you can try to use the macroEditor with the multlineStr Macro (see here)
you paste your multiline text in your code and  launch the macro ( click on the attachment to see animation).
Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

loaded

  • Sr. Member
  • ****
  • Posts: 310
Re: How to pass multiple lines of text to a variable in the easiest way?
« Reply #2 on: September 21, 2021, 05:19:00 pm »
Jurassic Pork, thank you very much for your hard work. If I can't find one step easier, I'll use this one.
Lazarus V2.0.10 32 Bit + Intel I5 8250U + 12GB RAM + Win 10 Home 64 Bit = red pill
Morpheus: "You take the blue pill...the story ends, you wake up in your bed and believe whatever you want to believe. You take the red pill...you stay in Wonderland, and I show you how deep the rabbit hole goes."

Kays

  • Sr. Member
  • ****
  • Posts: 367
  • Whasup!?
    • KaiBurghardt.de
Re: How to pass multiple lines of text to a variable in the easiest way?
« Reply #3 on: September 21, 2021, 05:57:53 pm »
[…] I want to be able to transfer a text containing multiple lines to a String variable, even if it contains single quotation marks ', without making any corrections or additions, […]
I myself tend to forget these tools, but the FPC is shipped with data2inc(1) and bin2obj.
Code: Bash  [Select][+][-]
  1. bin2obj -a -c head head.xml
This will require support for and make a typed “constant” which actually is a variable.
Yours Sincerely
Kai Burghardt

loaded

  • Sr. Member
  • ****
  • Posts: 310
Re: How to pass multiple lines of text to a variable in the easiest way?
« Reply #4 on: September 21, 2021, 07:54:16 pm »
Thank you very much for your answer, brother Kays, but I am not smart enough to understand what you wrote. I would appreciate it if you could be a little more descriptive.
Lazarus V2.0.10 32 Bit + Intel I5 8250U + 12GB RAM + Win 10 Home 64 Bit = red pill
Morpheus: "You take the blue pill...the story ends, you wake up in your bed and believe whatever you want to believe. You take the red pill...you stay in Wonderland, and I show you how deep the rabbit hole goes."

Kays

  • Sr. Member
  • ****
  • Posts: 367
  • Whasup!?
    • KaiBurghardt.de
Re: How to pass multiple lines of text to a variable in the easiest way?
« Reply #5 on: September 22, 2021, 06:00:14 pm »
Thank you very much for your answer, brother Kays, but I am not smart enough to understand what you wrote. I would appreciate it if you could be a little more descriptive.
Well, I suppose your XML preamble comes from an external source. If you don’t want to read a file containing the XML preamble during run-time, you can write yourself a script automatically creating a Pascal source code unit file with the aide of data2inc.
Yours Sincerely
Kai Burghardt

loaded

  • Sr. Member
  • ****
  • Posts: 310
Re: How to pass multiple lines of text to a variable in the easiest way?
« Reply #6 on: September 22, 2021, 09:11:44 pm »
Yes, you are right, there is a fragment xml in the example I gave. But while I was preparing the post, the kml file came up and I put a piece of it, otherwise don't think I'm using just XML, there may be many different types of text.
Regarding the matter, I was looking for an easier way because I didn't want to deal with the memo object in the simplest way.
Lazarus V2.0.10 32 Bit + Intel I5 8250U + 12GB RAM + Win 10 Home 64 Bit = red pill
Morpheus: "You take the blue pill...the story ends, you wake up in your bed and believe whatever you want to believe. You take the red pill...you stay in Wonderland, and I show you how deep the rabbit hole goes."

PascalDragon

  • Hero Member
  • *****
  • Posts: 3406
  • Compiler Developer
Re: How to pass multiple lines of text to a variable in the easiest way?
« Reply #7 on: September 23, 2021, 09:09:40 am »
Now my aim is; I want to be able to transfer a text containing multiple lines to a String variable, even if it contains single quotation marks ', without making any corrections or additions, such as the text inside the {} marks above, is this possible? Or is there an easier way? If you can help it would be greatly appreciated. Respects.

My suggestion is to store the text as an external file and use resources to include them in your binary:

test.xml:

Code: XML  [Select][+][-]
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.   <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
  3.   <Document>

data.rc:

Code: [Select]
testxml RCDATA test.xml
tres.pp:

Code: Pascal  [Select][+][-]
  1. program tres;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes, resource;
  7.  
  8. {$R data.rc}
  9.  
  10. var
  11.   kml: String;
  12.   ss: TStringStream;
  13.   r: TResourceStream;
  14. begin
  15.   ss := TStringStream.Create;
  16.   try
  17.     r := TResourceStream.Create(HINSTANCE, 'testxml', PChar(RT_RCDATA));
  18.     try
  19.       ss.CopyFrom(r, r.Size);
  20.     finally
  21.       r.Free;
  22.     end;
  23.  
  24.     kml := ss.DataString;
  25.   finally
  26.     ss.Free;
  27.   end;
  28.  
  29.   Writeln(kml);
  30. end.

loaded

  • Sr. Member
  • ****
  • Posts: 310
Re: How to pass multiple lines of text to a variable in the easiest way?
« Reply #8 on: September 23, 2021, 05:15:07 pm »
First of all, thank you very much PascalDragon for the answer.
God bless you, I am learning a lot of new things thanks to you.
I have a lot of text blocks to use and there will be expressions in these blocks that I need to change or renew, so it doesn't seem like a very practical method for this job, but I have a different situation in mind, and this method is perfect for that situation.
Lazarus V2.0.10 32 Bit + Intel I5 8250U + 12GB RAM + Win 10 Home 64 Bit = red pill
Morpheus: "You take the blue pill...the story ends, you wake up in your bed and believe whatever you want to believe. You take the red pill...you stay in Wonderland, and I show you how deep the rabbit hole goes."

 

TinyPortal © 2005-2018