Archive: ReplaceOnline repeats line problem


ReplaceOnline repeats line problem
Hi,

I'm trying to use a simple ReplaceOnLine to replace the text on 1 line from a text file.

here's my code ;

!include LogicLib.nsh
!include TextFunc.nsh
!include functions.nsh
!insertmacro GetDrives
!insertmacro GetTime

Name "add_text_file"
OutFile "add_text_file.exe"
XPStyle on
ShowInstDetails show
InstallDir "C:\"

Page directory
Page instfiles

LoadLanguageFile "${NSISDIR}\Contrib\Language files\French.nlf"

Section "";

InitPluginsDir

SetOutPath $INSTDIR

ClearErrors
Push 'test1'
Push 'test2'
Push 1
Push "C:\file.txt"
Call ReplaceOnLine

SectionEnd


Content of file.txt before ;
test1


Content of file.txt after ;
test2test1test2test2test2test1test2test2test2test2test2test2test2test1test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test1test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2  test2test2test2test2test2test2test2test2test2test2test2test1test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2  test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test1test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2  test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2  test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2  test2test2test2test1test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2  test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2  test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2  test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2  test2test2test2test2test2test2test2test2tes


Content of functions.nsh ;
Function DriveType
StrCmp $9 $R0 0 +3
StrCpy $R1 $8
StrCpy $0 StopGetDrives

Push $0
FunctionEnd

Function ReplaceOnLine
Exch $R0 ;file
Exch
Exch $R1 ;line
Exch 2
Exch $R2 ;replace with
Exch 2
Exch 3
Exch $R3 ;string to replace
Exch 3
Push $R4
Push $R5
Push $R6
Push $R7
Push $R8
Push $R9
Push $9
FileOpen $R4 $R0 r
GetTempFileName $R5
FileOpen $R6 $R5 w
Top:
FileRead $R4 $R7
IntOp $R8 $R8 + 1
StrCmp $R8 $R1 +3
FileWrite $R6 $R7
Goto Top
StrLen $9 $R3
Loop_Top:
StrCpy $R8 0
Loop:
IntOp $R8 $R8 - 1
StrCpy $R9 $R7 $9 $R8
StrCmp $R9 "" Finish
StrCmp $R9 $R3 0 Loop
StrCpy $R9 $R7 $R8
IntOp $R8 $R8 + $9
StrCpy $R7 $R7 "" $R8
StrCpy $R7 $R9$R2$R7
;detailprint $R7
FileWrite $R6 $R7
Goto Loop_Top
Finish:
ClearErrors
FileRead $R4 $R7
IfErrors +3
FileWrite $R6 $R7
Goto Finish
FileClose $R4
FileClose $R6
SetDetailsPrint none
Delete $R0
Rename $R5 $R0
SetDetailsPrint both
Pop $9
Pop $R9
Pop $R8
Pop $R7
Pop $R6
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Pop $R1
Pop $R0
FunctionEnd

Function GetServicePack
Push $R0
Push $R1

ReadRegDWORD $R0 HKLM "System\CurrentControlSet\Control\Windows" "CSDVersion"
IntOp $R1 $R0 % 256 ;get minor version
IntOp $R0 $R0 / 256 ;get major version

Exch $R1
Exch
Exch $R0
FunctionEnd

Function GetWindowsVersion
Push $R0
Push $R1

ClearErrors

ReadRegStr $R0 HKLM \
"SOFTWARE\Microsoft\Windows NT\CurrentVersion" CurrentVersion

IfErrors 0 lbl_winnt

; we are not NT
ReadRegStr $R0 HKLM \
"SOFTWARE\Microsoft\Windows\CurrentVersion" VersionNumber

StrCpy $R1 $R0 1
StrCmp $R1 '4' 0 lbl_error

StrCpy $R1 $R0 3

StrCmp $R1 '4.0' lbl_win32_95
StrCmp $R1 '4.9' lbl_win32_ME lbl_win32_98

lbl_win32_95:
StrCpy $R0 '95'
Goto lbl_done

lbl_win32_98:
StrCpy $R0 '98'
Goto lbl_done

lbl_win32_ME:
StrCpy $R0 'ME'
Goto lbl_done

lbl_winnt:

StrCpy $R1 $R0 1

StrCmp $R1 '3' lbl_winnt_x
StrCmp $R1 '4' lbl_winnt_x

StrCpy $R1 $R0 3

StrCmp $R1 '5.0' lbl_winnt_2000
StrCmp $R1 '5.1' lbl_winnt_XP
StrCmp $R1 '5.2' lbl_winnt_2003
StrCmp $R1 '6.0' lbl_winnt_vista lbl_error

lbl_winnt_x:
;StrCpy $R0 "NT $R0" 6
StrCpy $R0 'NT'
Goto lbl_done

lbl_winnt_2000:
Strcpy $R0 '2000'
Goto lbl_done

lbl_winnt_XP:
Strcpy $R0 'XP'
Goto lbl_done

lbl_winnt_2003:
Strcpy $R0 '2003'
Goto lbl_done

lbl_winnt_vista:
Strcpy $R0 'Vista'
Goto lbl_done

lbl_error:
Strcpy $R0 ''
lbl_done:

Pop $R1
Exch $R0
FunctionEnd

Function FileSearch
Exch $R0 ;search for
Exch
Exch $R1 ;input file
Push $R2
Push $R3
Push $R4
Push $R5
Push $R6
Push $R7
Push $R8
Push $R9

StrLen $R4 $R0
StrCpy $R7 0
StrCpy $R8 0

ClearErrors
FileOpen $R2 $R1 r
IfErrors Done

LoopRead:
ClearErrors
FileRead $R2 $R3
IfErrors DoneRead

IntOp $R7 $R7 + 1
StrCpy $R5 -1
StrCpy $R9 0

LoopParse:
IntOp $R5 $R5 + 1
StrCpy $R6 $R3 $R4 $R5
StrCmp $R6 "" 0 +4
StrCmp $R9 1 LoopRead
IntOp $R7 $R7 - 1
Goto LoopRead
StrCmp $R6 $R0 0 LoopParse
StrCpy $R9 1
IntOp $R8 $R8 + 1
Goto LoopParse

DoneRead:
FileClose $R2
Done:
StrCpy $R0 $R8
StrCpy $R1 $R7

Pop $R9
Pop $R8
Pop $R7
Pop $R6
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Exch $R1 ;number of lines found on
Exch
Exch $R0 ;output count found
FunctionEnd


ANY help would be greatly appreciated!

Thanks

I just went through a similar problem in using ReplaceOnLine. Ultimately I realized that ReplaceOnLine's usage of memory registers was stepping on (or being stepped on by) my usage of memory registers. I'm no expert at NSIS, but I suggest you check your memory usage very carefully.

I was getting output that was repeating itself in various shapes...

pre-execution file contents:
line 1: blah blah
line 2: original content
line 3:

Push "original content"
Push "replacement"
Push 2
Push "some\path\file.txt"
Call ReplaceOnLine

post-execution file contents:

line 1: blah blah
line 2: replacement replacement
line 3: replacement replacement replacement
line 4: replacement replacement replacement replacement
etc.


ok thanks, I'll look into it.