Recent

Author Topic: [SOLVED] How does TStringList and TMemo deal with EOL (line break) characters?  (Read 1988 times)

AMJF

  • New Member
  • *
  • Posts: 27
What I mean is line breaks like Line Feed and Carriage Return.

I am dealing with text files that use LF characters only, which is compatible with Unix, and text files that use LF and CR which works with Windows.

What I would like to know is, does TStringList and TMemo use whatever's in use in the file already as line breaks, or does it imposed Windows-style line breaks? Or can I change the line breaks throughout the file with a function?
« Last Edit: August 04, 2021, 10:25:40 am by AMJF »

GAN

  • Sr. Member
  • ****
  • Posts: 335
Re: How does TStringList and TMemo deal with EOL (line break) characters?
« Reply #1 on: August 04, 2021, 01:34:38 am »
Or can I change the line breaks throughout the file with a function?
Yes, you can using LineBreak property of TString.

https://www.freepascal.org/docs-html/rtl/classes/tstrings.linebreak.html
Lazarus 2.0.8 FPC 3.0.4 Linux Mint Mate 19.3
Zeos 7̶.̶2̶.̶6̶ 7.1.3a-stable - Sqlite 3.32.3 - LazReport

https://searchlazarus.blogspot.com/

jamie

  • Hero Member
  • *****
  • Posts: 4832
Re: How does TStringList and TMemo deal with EOL (line break) characters?
« Reply #2 on: August 04, 2021, 02:19:54 am »
What I mean is line breaks like Line Feed and Carriage Return.

I am dealing with text files that use LF characters only, which is compatible with Unix, and text files that use LF and CR which works with Windows.

What I would like to know is, does TStringList and TMemo use whatever's in use in the file already as line breaks, or does it imposed Windows-style line breaks? Or can I change the line breaks throughout the file with a function?

TStringlist does its best at reading each line from a source attempting to resolve the lines as they come in but the bulk of the text isn't stored as it is on file. Each line read creates a string, more accurately there is a record created for each line and in that record is the string and optional object pointer for each line.

 The LineEding property comes into play when you get ready to save it back to file or generate a complete bulk text using the TEXT property, otherwise all lines are simply an array of RECORD that contains the string and object to ecah.

 Writing it back or assigning it is where you may need to be concerned if you need to keep with standards of some sort.

 I wrote myself a TLinesString class that is based around a Tstrings and does basically the same thing but it auto detects the line endings and uses that from there on.

 It also does other things too, not related to this of course
The only true wisdom is knowing you know nothing

AMJF

  • New Member
  • *
  • Posts: 27
Re: How does TStringList and TMemo deal with EOL (line break) characters?
« Reply #3 on: August 04, 2021, 09:18:40 am »
Or can I change the line breaks throughout the file with a function?
Yes, you can using LineBreak property of TString.

https://www.freepascal.org/docs-html/rtl/classes/tstrings.linebreak.html

I succeeded in saving a stringlist to a file with linebreak set at #10, but I wonder if the usual #13#10 is also supported? The link indicates a single character is used, even if the variable is declared as a string.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How does TStringList and TMemo deal with EOL (line break) characters?
« Reply #4 on: August 04, 2021, 09:52:50 am »
I succeeded in saving a stringlist to a file with linebreak set at #10, but I wonder if the usual #13#10 is also supported? The link indicates a single character is used, even if the variable is declared as a string.

No, it is a string so you can set whatever line-break you want. You could even set it to "[a break here]" and that would be used. :D

For example, this code:
Code: Pascal  [Select][+][-]
  1.   with TStringList.Create do begin
  2.     LineBreak := '<br>';
  3.     Add('Line1');
  4.     Add('Line2');
  5.     Add('Line3');
  6.     Add('Line4');
  7.     Add('Line5');
  8.     SaveToFile('LINEBR.txt');
  9.     Free;
  10.   end;
produces this single line in LINEBR.txt:
Code: Text  [Select][+][-]
  1. Line1<br>Line2<br>Line3<br>Line4<br>Line5<br>
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.

AMJF

  • New Member
  • *
  • Posts: 27
Re: How does TStringList and TMemo deal with EOL (line break) characters?
« Reply #5 on: August 04, 2021, 10:25:23 am »
I see now.

Thanks!  :)

 

TinyPortal © 2005-2018