miguez
17th December 2008 04:31 UTC
Inserting line halfway down an INI
First post.
I've done searches here and in the wiki, and asked questions (to no avail) in the IRC.
First off, NSIS rocks! I've been at it for a week now and I really like the scripting language, very simple yet powerful.
My installer needs to open an existing INI file, search for a section, then add a new section immediately after that. That means that everything else that already existed after the section I search for should be shifted down to make room for this new section.
By using WriteINIStr I can add my section, but it shows at the end of the INI file.
By using FileWrite I can add it where I want the section to be, but it overwrites the existing text from that point on.
Can anyone help me with this?
Thanks,
Comperio
17th December 2008 05:05 UTC
An INI file doesn't have to be in any particular order since any INI file parser will find the proper section no matter where it lies in the file.
But, if you want to force it that way, here's a rough outline of what you might do:
- Make copy of the file. (Use $PLUGINSDIR for this)
- open the copy as read-only; open the real file in either append or write mode.
- Read the copy and write to the real file line by line until you get to the point where you want to insert your section.
- Write your new section to the file.
- Continue then reading from the copy and writing to the real file.
- Close both files and delete your copy.
(All you really need to do with the steps above is get the section header in the right place. Once you've done that, you can then use WriteINIStr for the rest of the data.)
miguez
20th December 2008 00:29 UTC
Comperio,
This worked like a charm! I first had to figure out how to copy a file, which I did with:
Rename $AircraftCFG "$PLUGINSDIR\aircraft.cfg"
Where aircraft.cfg is the file to be copied, the original residing in a variable
$AircraftCFG, which holds a path. As you can see, I followed your recommendation of copying it to the
$PLUGINSDIR.
Then I read through the copy line by line, writing to the original as I went, until I found the place to add the new text. I added the new text, then resumed the writing of the original file.
By the way, I agree with you, in an INI file the order or location of the sections does not matter. In my case, though, the file is commonly edited by end users, so it needed to look tidy.
Here's the final function code, if anyone is interested. I am open to any suggestions on improving how I do this, as I am very far from being a good programmer.
Oh, and for those doing about the same thing, don't forget to delete the copy of the INI file at the end!
Function UpdateFSXINI
Push $R0
Push $R1
Push $R2
Push $R3
Push $R4
StrCpy $AircraftCFG "$INSTDIR\SimObjects\Airplanes\PMDG747-400\aircraft.cfg"
Rename $AircraftCFG "$PLUGINSDIR\aircraft.cfg"
Call FindNumberPlanes
FileOpen $R0 $AircraftCFG a
FileOpen $R2 "$PLUGINSDIR\aircraft.cfg" r
StrCpy $FSXINIsearch "[General]"
Loop:
FileRead $R2 $R3
StrCpy $R4 $R3 9
StrCmp $R4 "[General]" +1 +2
Call AddFSXAoAAircraft
StrCmp $R3 "" +3 +1
FileWrite $R0 $R3
Goto Loop
FileClose $R0
FileClose $R2
Delete "$PLUGINSDIR\aircraft.cfg"
FlushINI "$PLUGINSDIR\aircraft.cfg"
Pop $R4
Pop $R3
Pop $R2
Pop $R1
Pop $R0
FunctionEnd
Thanks for NSIS, and for all the help!!
Best regards,