Recent

Author Topic: [solved] TIniFile takes first value for repeated key  (Read 1736 times)

kegge13

  • New Member
  • *
  • Posts: 45
    • the BistroMath project
[solved] TIniFile takes first value for repeated key
« on: June 04, 2020, 09:46:38 am »
When a key in a ini file has more than one version, Delhi would take the last value. FPC 3.0.4 takes the first value. Not a big deal, I tend to have multiple versions of a key for quick testing, but was this a deliberate design change?

In IniFiles ReadString uses KeyByName to find a key:
Code: Pascal  [Select][+][-]
  1.       for i := 0 to Count-1 do
  2.         if CompareText(Items[i].Ident, AName) = 0 then begin
  3.           Result := Items[i];
  4.           Break;
  5.  

Btw: imho using a break in a for-loop is a sloppy programming practice. Is that because of my old-skool background?

« Last Edit: June 16, 2020, 03:38:41 pm by kegge13 »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: TIniFile takes first value for repeated key
« Reply #1 on: June 04, 2020, 10:08:47 am »
Btw: imho using a break in a for-loop is a sloppy programming practice. Is that because of my old-skool background?

Yes. Using Break is perfectly legitimate and more often than not it's better to abort a loop early (e.g. if you search for a value in a large dataset why walk the whole list when you can abort early?).

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TIniFile takes first value for repeated key
« Reply #2 on: June 04, 2020, 10:31:51 am »
When a key in a ini file has more than one version, Delhi would take the last value. FPC 3.0.4 takes the first value. Not a big deal, I tend to have multiple versions of a key for quick testing, but was this a deliberate design change?
FPC has deliberately been implemented without reference to Delphi code.To provide - as far as possible - compatibility with Delphi, its published APIs and documentation are of course consulted, but not the sources, which are copyright.
So a clean-room approach is taken, and FPC sources are not derived from Delphi except for (largely) compatible naming/API and functionality.
Implementation specifics are consequently bound to differ from Delphi (if you have the Delphi sources and compare them, which many FPC users don't).

kegge13

  • New Member
  • *
  • Posts: 45
    • the BistroMath project
Re: TIniFile takes first value for repeated key
« Reply #3 on: June 04, 2020, 10:46:06 am »
Yes. Using Break is perfectly legitimate and more often than not it's better to abort a loop early (e.g. if you search for a value in a large dataset why walk the whole list when you can abort early?).
Therefore I would have used a while loop. But I see your point, which I always was aware of, it takes one test extra each loop:
Code: Pascal  [Select][+][-]
  1. while (i<Count) and (Length(Result)=0) do

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: TIniFile takes first value for repeated key
« Reply #4 on: June 04, 2020, 05:59:12 pm »
When a key in a ini file has more than one version, Delhi would take the last value. FPC 3.0.4 takes the first value. Not a big deal, I tend to have multiple versions of a key for quick testing, but was this a deliberate design change?

I'm not sure what you mean, but given a inifile like:
Code: [Select]
[multiples]
key=first
key=middle
key=last

then both Fpc and Delphi 7 return 'first' on Ini.ReadString('multiples','key','ERROR_KEY_NOT_FOUND')

Bart

kegge13

  • New Member
  • *
  • Posts: 45
    • the BistroMath project
Re: TIniFile takes first value for repeated key
« Reply #5 on: June 12, 2020, 04:11:23 pm »

I'm not sure what you mean, but given a inifile like:
Code: [Select]
[multiples]
key=first
key=middle
key=last

then both Fpc and Delphi 7 return 'first' on Ini.ReadString('multiples','key','ERROR_KEY_NOT_FOUND')


I checked (again) today with D7. With
Code: [Select]
SpecialMode[1]String[1]=selftest02_real.txt
SpecialMode[1]String[1]=selftest03_pdd.txt

my application (now ported to LCL) in D7 takes the second file. But as said, not a big deal at all. It was just convenient for me, not having to comment out the first line.

Thanks anyway for your comments.


Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: TIniFile takes first value for repeated key
« Reply #6 on: June 12, 2020, 07:16:46 pm »
Since the documentation TIniFile does not specify what the behaviour should be on a basically invalid section, the behaviour as such is by definition undefined (and can change at any time due to implementation details that are not part of the public API).

Bart

 

TinyPortal © 2005-2018