Recent

Author Topic: Save the same key ini file  (Read 856 times)

BIT

  • Full Member
  • ***
  • Posts: 158
Save the same key ini file
« on: March 31, 2023, 07:50:17 pm »
Sorry for the translation!
Tell me how to store values with the same key?
Code: Pascal  [Select][+][-]
  1. [Section]
  2. Key = xxxx
  3. Key = xxxx
  4. Key = xxxx
  5. Key = xxxx
  6.  

It is clear that it overwrites the first key)
Code: Pascal  [Select][+][-]
  1. Inif.WriteString('Section', 'Key', 'xxxx');
  2. Inif.WriteString('Section', 'Key', 'xxxx');
  3. Inif.WriteString('Section', 'Key', 'xxxx');
  4. Inif.WriteString('Section', 'Key', 'xxxx');
  5.  

How can I make it so that I can write a value with the same key, ini the file is not mine, I can’t influence the content.
« Last Edit: March 31, 2023, 09:03:20 pm by BIT »

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Save the same key ini file
« Reply #1 on: March 31, 2023, 08:30:07 pm »
And how you will read the data from the file?

If I have to, I would create more sections:
Code: Pascal  [Select][+][-]
  1. [Section]
  2. Key = xxxx
  3.  
  4. [Section1]
  5. Key = xxxx
  6.  
  7. [Section2]
  8. Key = xxxx
  9.  
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

BIT

  • Full Member
  • ***
  • Posts: 158
Re: Save the same key ini file
« Reply #2 on: March 31, 2023, 08:38:00 pm »
This ini file is not used by my application, I can't create new sections, etc.(

domasz

  • Sr. Member
  • ****
  • Posts: 423
Re: Save the same key ini file
« Reply #3 on: March 31, 2023, 08:46:46 pm »
Code: Pascal  [Select][+][-]
  1. L := TStringList.Create;
  2. L.LoadFromFile('file.ini');
  3. L.Add('Key=xx');
  4. L.Add('Key=xx');
  5. L.Add('Key=xx');
  6. L.Add('Key=xx');
  7. L.SaveToFile('file2.ini');
  8. L.Free;

BIT

  • Full Member
  • ***
  • Posts: 158
Re: Save the same key ini file
« Reply #4 on: March 31, 2023, 08:52:57 pm »
Code: Pascal  [Select][+][-]
  1. L := TStringList.Create;
  2. L.LoadFromFile('file.ini');
  3. L.Add('Key=xx');
  4. L.Add('Key=xx');
  5. L.Add('Key=xx');
  6. L.Add('Key=xx');
  7. L.SaveToFile('file2.ini');
  8. L.Free;

This ini file has its own sections before and after the one I need to write to, this method will not work

BIT

  • Full Member
  • ***
  • Posts: 158
Re: Save the same key ini file
« Reply #5 on: March 31, 2023, 08:59:19 pm »
ini file looks like this

domasz

  • Sr. Member
  • ****
  • Posts: 423
Re: Save the same key ini file
« Reply #6 on: March 31, 2023, 09:26:26 pm »
If the file is small then you can do this:
Code: Pascal  [Select][+][-]
  1.    
  2.  
  3.     L := TStringList.Create;
  4.     L.LoadFromFile('file.ini');
  5.    
  6. L2 := TStringList.Create;
  7.  
  8.    for i:=0 to L.Count-1 do begin
  9.     if Pos('Key', L[i]) > 0 then begin //instead of 'Key' you can try to locate different string
  10.       Index := i;
  11.       break;
  12.    end;
  13.    end;
  14.  
  15. for i:=0 to Index-1 do L2.Add(L[i]);
  16.  
  17.  
  18.     L2.Add('Key=xx');
  19.     L2.Add('Key=xx');
  20.     L2.Add('Key=xx');
  21.     L2.Add('Key=xx');
  22.  
  23. for i:=Index-1 to L.Count-1 do do L2.Add(L[i]);
  24.  
  25.     L2.SaveToFile('file2.ini');
  26.     L2.Free;
  27.     L.Free;
  28.  

BIT

  • Full Member
  • ***
  • Posts: 158
Re: Save the same key ini file
« Reply #7 on: March 31, 2023, 11:05:02 pm »
If the file is small then you can do this:
It is a pity, of course, that it is impossible to make an ini function while doing so.
Code: Pascal  [Select][+][-]
  1. var
  2.   SL, Sk: TStringList;
  3.   i, j, sIndex: integer;
  4.   ink: integer;
  5. begin
  6.   SL := TStringList.Create;
  7.   SL.LoadFromFile('file.ini');
  8.   Sk := TStringList.Create;
  9.   Sk.add('EditPackages=Core');
  10.   Sk.add('EditPackages=Engine');
  11.   Sk.add('EditPackages=NWindow');
  12.   Sk.add('EditPackages=Interface');
  13.   Sk.add('EditPackages=Mytext');
  14.   Sk.add('EditPackages=1234');
  15.  
  16.   for I := SL.Count - 1 downto 0 do
  17.   begin
  18.     if Pos('EditPackages', SL[i]) > 0 then
  19.     begin
  20.       SL.Delete(i);
  21.       sIndex := i;
  22.     end;
  23.   end;
  24.  
  25.   ink := Sk.Count - 1;
  26.   for j := sIndex to sIndex + Sk.Count - 1 do
  27.   begin
  28.     SL.Insert(sIndex, Sk[ink]);
  29.     Dec(ink);
  30.   end;
  31.  
  32.   SL.SaveToFile('file.ini');
  33.  
  34. end;                      
  35.  

domasz

  • Sr. Member
  • ****
  • Posts: 423
Re: Save the same key ini file
« Reply #8 on: March 31, 2023, 11:14:00 pm »
It is a pity, of course, that it is impossible to make an ini function while doing so.
I don't think your INI is a valid INI. From my experience INI files cannot have more than 1 identical keys in a section. Seems someone just invented own format based on INI.
Anyway your code looks fine, so problem solved.

BIT

  • Full Member
  • ***
  • Posts: 158
Re: Save the same key ini file
« Reply #9 on: March 31, 2023, 11:45:04 pm »
It is a pity, of course, that it is impossible to make an ini function while doing so.
I don't think your INI is a valid INI. From my experience INI files cannot have more than 1 identical keys in a section. Seems someone just invented own format based on INI.
Anyway your code looks fine, so problem solved.
Thanks for the help, I hoped to the end that this was possible, this ini file is from UnrealEd.

 

TinyPortal © 2005-2018