tags:

views:

347

answers:

4

Is it possible to have line breaks in an ini file using Delphi?

I have a string value that I'd like to use, but it needs to have the ability for line breaks. I don't need any other formatting of the text.

How can I do this?

+2  A: 

Add \n in your string.

Örjan Jämte
\n is in C, in delphi we use #13#10 isn't it ? :)
avar
+5  A: 

It's been a while since I did Delphi, but I think you can't directly include linefeeds - a line is a line.

But as Örjan says, you can include characters in your string that can be interpreted by your program as line breaks.

I doubt that "\n" is automatically treated specially in any way in a .ini file, but you could include some other rarely used character, such as the pipe (|) or tilde (~) and just let your app translate that to a line break.

Carl Smotricz
I've used #7 to replace #13#10 when saving and convert it back when reading. It's certainly a character that won't occur naturally. :)
Bruce McGee
+8  A: 

It's up to you to figure out a method to encode the line break.

Depending on the use you want to do, and the strategy you want to apply to decode it.

I use url encoding. This way I can cover a much broader range of possible values. There are many URL Decode implementations available.

Or you may follow Orjan suggestion. Or invent your own.

PA
Which URL Encoding function do you use?
Bruce McGee
I use my own, dated back from the times of Turbo Pascal.
PA
+2  A: 

I also need this when a value is stored in a TStringList. To solve this issue I have used TStringList.DelimitedText property, instead of TStringList.Text:

Define the Delimiter:

Items.StrictDelimiter:= True;
Items.Delimiter:= ';';

Save:

IniFile.WriteString('Session', 'Key', Items.DelimitedText);

Load:

Items.DelimitedText:= IniFile.ReadString('Session', 'Key', '');
Cesar Romero
Be careful. There is a bug in the Delphi 7 implementation of delimitedText that always use the blank character #32 as a delimiter. I don't know if it has been fixed in more recent versions. Test extensively.
PA
Thank you for the heads up PA. I don't have to bother about that, I don't use Delphi 7 since Delphi 2007 was released.
Cesar Romero
@PA: It isn't a bug - it is intended, but often undesirable behaviour. Later versions of Delphi (2006+) added the "StrictDelimiter" property to avoid this.
Gerry
@PA: ...and it definitely won't get "fixed" since that would break backward-compatibility.
Smasher