11 Vote

Delphi: Ini Files: Problems with Quotes

Question by Chematik | 2013-01-06 at 09:58

I am using Ini-Files in Delphi in order to store some settings of my program. The writing and reading of the file is working quite well, only with values containing quotation marks, it comes to some strange behaviour.

The problem affects both, double and single quotes (" and ') and the problem seems to appear only occasionally, logic is not apparent to me. My first idea was to double the quote, but that only works sometimes.

If that is important, here are a few code snippets of my program:

uses IniFiles;
...
 
procedure ...
var
  ini: TIniFile;
  str: string;
begin
  ini:=TIniFile.Create(filename);
 
  //writing
  ini.WriteString('Section', 'Identifier', 'Value');
 
  //reading
  str:=ini.ReadString('Section', 'Identifier', 'Default');
 
  ini.free;
end;

But I do not think, that this code causes the problems, since this code can be found on the internet in each INI-Tutorial. But how to solve the issue?

ReplyPositiveNegative
1Best Answer1 Vote

The functions WriteString and ReadString used in your code are using the Windows API functions ReadPrivateProfileString and GetPrivateProfileString internally.

With these functions, the following have to be observed: If the string value is enclosed in single or double quotes, the function returns the string without the quotes. So, the value "string" is retrieved as string. If there is only one quotation mark in the value or the quotes are appearing in the middle, the function returns them as they are stored in the INI file. That explains the behavior, you have described.

The solution is as follows:

function DoubleQuotes(s: string): string;
var
  k: integer;
begin
  k:=length(s);
  if k>1 then begin
    if (s[1])='''') and (s[k]='''') then s:=''''+s+'''';
    if (s[1])='"') and (s[k]='"') then s:='"'+s+'"';
  end;
  result:=s;
end;
 
...
 
s = '"VALUE"';
ini.WriteString('Section','Identifier',DoubleQuotes(s));

So, before writing the string into the INI file, we check, whether there are quotes of the same type at the front or the rear of the string. If so, we double both. Otherwise, the string is kept as it is.
2013-01-06 at 19:56

ReplyPositive Negative
Reply

Related Topics

PHP: Upload of large Files

Tutorial | 0 Comments

MIME Type of INI Files

Question | 1 Answer

Program in ZIP-Folder

Info | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.