Archive: Problem modifying a string in a .cfg file


Problem modifying a string in a .cfg file
I seem to have run into a problem during the creation of my installer. One of the steps I need performed, involves the manipulation of a string of text within a .cfg file. However, the config file is not devided into sections, it is all one big section, without a section header such as

[Main]

Instead, the config file flows sort of like:

HazeColor = 1.2 1.3 .56
LightLevel = 2.0 1.3 0.5
AtmAlt = 64e3

etc, etc.

I need to have the installer modify one of those lines, ex., HazeColor = 1.0 1.0 1.0

Without there being any Sections defined in brackets, how can I get the value in that string to be changed by the installer?

I've tried to research the forums on this, and I am currently also in the IRC channel in case anyone in there can give some insight, but so far, I havent been able to find a problem similar to mine, and I will admit, I am brand new at scripting, and have maybe a total of 10 hours in Visual C++, so Im most definitely not skilled in this kind of thing, and would need a pretty decent explaination. Any help is appreciated greatly.

Thanks,

EndeavourCmdr.


Re-format the file.cfg to a new structured file.cfg and you'll be able to modify any string.


Unfortunately, thats not an option, beceause the program I am having installed is simply a modification to an existing program, of which I do not have any access or rights to, other then just being an addon developer for it. Thus, the config file structure has to remain the same, and it is not even feasible to have the structure changed. Every single config however does have the same line of text in it which is:

AtmHazeColor = valuehere

Is it possible to have the script just find that string without a section defined and replace the value?


Hold on a second I'll be back


!include TextFunc.nsh
!insertmacro TrimNewLines

!define STR_TO_FIND "HazeColor = 1.2 1.3 .56"
!define STR_TO_REPLACE "HazeColor = 1.0 1.0 1.0"


Section
#......
FileOpen $0 "path_to_file\file.cfg" r ; file to read
GetTempFileName '$R2'
FileOpen $R2 w ; file to write
ClearErrors

start:
FileRead $0 $1 ${NSIS_MAX_STRLEN}
IfErrors end
${TrimNewLines} '$1' '$1'
StrCmp '$1' '${STR_TO_FIND}' 0 next
StrCpy '$1' '${STR_TO_REPLACE}'
FileWrite $R2 '$\r$\n$\r$\n'
FileWrite $R2 '$1'

next:
FileWrite $R2 '$\r$\n$\r$\n'
FileWrite $R2 '$1'
goto start

end:
FileClose $0
FileClose $R2
Rename "path_to_file\file.cfg" "path_to_file\file.cfg.bak"
Rename $R2 "path_to_file\file.cfg"
#......
SectionEnd

Well thanks! That is alot more complex then I expected. Are there any good tutorials out there on how do learn some of this stuff, other then the manual, which seems to be just a reference?


Hmm, actually on a second look, that code wouldnt quite work, because everyone needs to have

AtmHazeColor = 1.0 1.0 1.0

after the install.

Before the install, the values may be different on every users machine.

So user 1 might have

AtmHazeColor = 0.535 0.76 1.0

User 2 might have

AtmHazeColor = 0.7 0.85 1.0

etc, etc.

But in the end, they all need to read,

AtmHazeColor = 1.0 1.0 1.0

So, now I am once again where I started Lol.


You may find everything in the forum, just point your search to the required keywords. Also browse to wiki http://nsis.sourceforge.net there are about 1000 articles with functions and code examples, simply, everything is possible within NSIS :-)


!define STR_TO_FIND "HazeColor"

StrLen $2 ${STR_TO_FIND}

start:
FileRead $0 $1 ${NSIS_MAX_STRLEN}
IfErrors end
${TrimNewLines} '$1' '$1'
StrCpy '$3' '$1' $2
StrCmp '$3' '${STR_TO_FIND}' 0 next

StrCpy '$1' '${STR_TO_REPLACE}'
FileWrite $R2 '$\r$\n$\r$\n'
FileWrite $R2 '$1'

I dont quite understand. The following is the contents of my script. Where did I mess up?


Name "EndeavourCmdr's Environment Enhancement"
OutFile "EEEo2006.exe"
InstallDir $PROGRAMFILES

;--------------------------------

Page components
Page directory
Page instfiles

;--------------------------------

Section "EEE (required)"

SectionIn RO

SetOutPath $INSTDIR\Textures
File "Horizon.dds"

FileOpen $0 $INSTDIR\file.dat a
!define STR_TO_FIND "AmtHazeColor"

StrLen $2 ${STR_TO_FIND}
start:
FileRead $0 $1 ${NSIS_MAX_STRLEN}
IfErrors end
${TrimNewLines} '$1' '$1'
StrCpy '$3' '$1' $2
StrCmp '$3' '${STR_TO_FIND}' 0 next
StrCpy '$1' '${STR_TO_REPLACE}'
FileWrite $R2 '$\r$\n$\r$\n'
FileWrite $R2 '$1'

FileClose $0

SectionEnd


Sorry to be a bother, but this is very new and confusing, and I am a hand on learner, so I will get the hang of this all by having someone explain it a bit and then doing it myself, but it's something I want to learn.

Replace/add the lines in my upper post with the red lines in the followed post.
Don't forget on the top of your script the defines and the includes.

Now you should have this:


!define STR_TO_FIND "HazeColor"
!define STR_TO_REPLACE "HazeColor = 1.0 1.0 1.0"


Section
#......
FileOpen $0 "path_to_file\file.cfg" r ; file to read
GetTempFileName '$R2'
FileOpen $R2 w ; file to write
ClearErrors

StrLen $2 ${STR_TO_FIND}

start:
FileRead $0 $1 ${NSIS_MAX_STRLEN}
IfErrors end
${TrimNewLines} '$1' '$1'
StrCpy '$3' '$1' $2
StrCmp '$3' '${STR_TO_FIND}' 0 next
StrCpy '$1' '${STR_TO_REPLACE}'
FileWrite $R2 '$\r$\n$\r$\n'
FileWrite $R2 '$1'

next:
FileWrite $R2 '$\r$\n$\r$\n'
FileWrite $R2 '$1'
goto start

end:
FileClose $0
FileClose $R2
Rename "path_to_file\file.cfg" "path_to_file\file.cfg.bak"
Rename $R2 "path_to_file\file.cfg"
#......
SectionEnd

I'm so sorry...
I messed things a little :-)
here is the right one, please doenload and test.


Sigh...

I'm trying hard to get this, but here is my script, which you gave me most of.



Name "EndeavourCmdr's Environment Enhancement"
OutFile "EEEo2006.exe"
InstallDir $PROGRAMFILES

!include TextFunc.nsh
!insertmacro TrimNewLines

!define STR_TO_FIND "AtmHazeColor"
!define STR_TO_REPLACE "AtmHazeColor = 1.0 1.0 1.0"

;-----------------------------------------------------

Section
#......
SetOutPath $INSTDIR\Textures
File "Horizon.dds"

FileOpen $0 "$INSTDIR\config\earth.cfg" r ; file to read
GetTempFileName '$R2'
FileOpen $R2 w ; file to write
ClearErrors

StrLen $2 $(STR_TO_FIND)

start:
FileRead $0 $1 ${NSIS_MAX_STRLEN}
IfErrors end
${TrimNewLines} '$1' '$1'
StrCpy '$3' '$1' $2
StrCmp '$3' '${STR_TO_FIND}' 0 next
StrCpy '$1' '${STR_TO_REPLACE}'
FileWrite $R2 '$\r$\n$\r$\n'
FileWrite $R2 '$1'

next:
FileWrite $R2 '$\r$\n$\r$\n'
FileWrite $R2 '$1'
goto start

end:
FileClose $0
FileClose $R2
Rename "$INSTDIR\config\earth.cfg" "$INSTDIR\config\earth.cfg.bak"
Rename $R2 "$INSTDIR\config\earth.cfg"
#......
SectionEnd



But after trying to compile that, I get this...


MakeNSIS v2.22 - Copyright 1995-2006 Contributors
See the file COPYING for license details.
Credits can be found in the Users Manual.

Processing config:
Processing plugin dlls: "C:\Program Files\NSIS\Plugins\*.dll"
- AdvSplash::show
- Banner::destroy
- Banner::getWindow
- Banner::show
- BgImage::AddImage
- BgImage::AddText
- BgImage::Clear
- BgImage::Destroy
- BgImage::Redraw
- BgImage::SetBg
- BgImage::SetReturn
- BgImage::Sound
- Dialer::AttemptConnect
- Dialer::AutodialHangup
- Dialer::AutodialOnline
- Dialer::AutodialUnattended
- Dialer::GetConnectedState
- InstallOptions::dialog
- InstallOptions::initDialog
- InstallOptions::show
- LangDLL::LangDialog
- Math::Script
- NSISdl::download
- NSISdl::download_quiet
- Splash::show
- StartMenu::Init
- StartMenu::Select
- StartMenu::Show
- System::Alloc
- System::Call
- System::Copy
- System::Free
- System::Get
- System::Int64Op
- System::Store
- TypeLib::GetLibVersion
- TypeLib::Register
- TypeLib::UnRegister
- UserInfo::GetAccountType
- UserInfo::GetName
- VPatch::vpatchfile
- nsExec::Exec
- nsExec::ExecToLog
- nsExec::ExecToStack

!define: "MUI_INSERT_NSISCONF"=""

Changing directory to: "C:\Documents and Settings\Adm.EL-081FF12C7FDC\Desktop\Orbiter Environment Installer"

Processing script file: "C:\Documents and Settings\Adm.EL-081FF12C7FDC\Desktop\Orbiter Environment Installer\Copy of In Progress.nsi"
Name: "EndeavourCmdr's Environment Enhancement"
OutFile: "EEEo2006.exe"
InstallDir: "$PROGRAMFILES"
!include: "C:\Program Files\NSIS\Include\TextFunc.nsh"
!define: "TEXTFUNC_INCLUDED"=""
!include: closed: "C:\Program Files\NSIS\Include\TextFunc.nsh"
!insertmacro: TrimNewLines
!insertmacro: end of TrimNewLines
!define: "STR_TO_FIND"="AtmHazeColor"
!define: "STR_TO_REPLACE"="AtmHazeColor = 1.0 1.0 1.0"
Section: ""
SetOutPath: "$INSTDIR\Textures"
File: "Horizon.dds" [compress] 4890/65664 bytes
FileOpen: $INSTDIR\config\earth.cfg as r -> $0
GetTempFileName -> $R2
FileOpen expects 3 parameters, got 2.
Usage: FileOpen $(user_var: handle output) filename openmode
openmode=r|w|a
Error in script "C:\Documents and Settings\Adm.EL-081FF12C7FDC\Desktop\Orbiter Environment Installer\Copy of In Progress.nsi" on line 21 -- aborting creation process


I cant understand what part is wrong there, but im assuming its the:

FileOpen $R2 w ; file to write

line, that is missing something?

Thanks btw for all your help so far. It's actually making sence, but I dont understand all those variables yet.

Excellent! The file you sent works perfectly. Im going to study it thouroughly and incorporate it into the rest with my new values. Thank you so much.


You're welcome! and sorry for the mess...
This happens to me often when I try to write code straight in the reply window :-) A full screen editor is always required.