Question

When Value is a quoted-string, quotation marks (") are removed automatically.

It means that, two following statement, A.WriteString('Section','Key','"abcde"') and A.WriteString('Section','Key','abcde') are not different.

Please see my code (it is quite clearly):

program project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, IniFiles, sysutils
  { you can add units after this };

var
  List: TIniFile;
  A, B: String;

begin
  List := TIniFile.Create('file.ini');
  A := '"abcde"';
  List.WriteString('Section', 'Key', A);
  List.Free;

  List := TIniFile.Create('file.ini');
  B := List.ReadString('Section', 'Key', '');
  List.Free;

  if A<>B then raise Exception.Create(Format('A<>B (A=[%s] but B=[%s])', [A, B]));
end.

The previous code raise the following exception: A<>B (A=["abcde"] but B=[abcde])

I want to code something like this: A.WriteString('Section', 'Key', List.CommaText); Because List.CommaText may be a quoted-string, I have no solution to code as above.

Is it a bug or features? How can I save a TStrings into a TIniFile?

Was it helpful?

Solution 2

Too much effort to find the answer.

TCustomIniFile.StripQuotes

http://www.freepascal.org/docs-html/fcl/inifiles/tcustominifile.stripquotes.html

OTHER TIPS

Before writing, change the quotes (if any) with something else, which guaranteed cannot appear in the string. After reading, change it back to quotes. For example:

begin
  List := TIniFile.Create('file.ini');
  A := '"abcde"';
  List.WriteString('Section', 'Key', ReplaceStr(A, '"', #1));
  List.Free;

  List := TIniFile.Create('file.ini');
  B := ReplaceStr(List.ReadString('Section', 'Key', ''), #1, '"');
  List.Free;

  if A<>B then raise Exception.Create(Format('A<>B (A=[%s] but B=[%s])', [A, B]));
end.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top