Old 26th July 2018, 23:09   #1
MyPC8MyBrain
Member
 
Join Date: Nov 2015
Posts: 96
Red face loop issue

Hi everyone
im having a small loop issue i need little help with,
i am trying to search in file first to find a specific entry; followed by another search and replace,

my start test.txt file looks like this
PHP Code:
mod={1234}
size=11

mod
={1235}
size=11 

i am trying to search for "mod={1234}" than replace the "size=*" that follows,
as a starting point the number after "size=" can be any number,
i am searching for the next line start and replacing the entire line after the first match is found,
it seem to work accept its looping on me; it keeps finding "size=" and replacing it throughout the file,

i am trying to get to this
PHP Code:
mod={1234}
size=22

mod
={1235}
size=11 
but i keep ending up with this
PHP Code:
mod={1234}
size=22

mod
={1235}
size=22 
why is it looping instead of replacing just the first match?
this is the code i am currently using,

PHP Code:
!include "TextFunc.nsh"
!insertmacro LineFind

!define FIND1 "mod={1234}"
!define FINDNEXT "size="
!define REPL1 "size=22"

outFile "test.exe"

Function LineFindCallback
        StrLen 
$"${FIND1}"
        
StrLen $"${FINDNEXT}"
        
StrCpy $"$R9$0
        StrCmp 
$"${FINDNEXT}0 End
        StrCpy $R9 
"${REPL1}$\r$\n"

        
End:
        
Push $0
FunctionEnd

Section Install
        
${LineFind"test.txt" "test.txt" "1:-1" "LineFindCallback"

        
IfErrors 0 +2
        MessageBox MB_OK 
"Error"
SectionEnd 

TIA
Chris
MyPC8MyBrain is offline   Reply With Quote
Old 27th July 2018, 01:20   #2
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 4,927
You are doing StrLen $3 "${FIND1}" and then ignore the result of it?

It seems to me like you should look for the "mod" line and when you find it you store that state somewhere so you know which "size" line you should modify.

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 27th July 2018, 01:55   #3
MyPC8MyBrain
Member
 
Join Date: Nov 2015
Posts: 96
Thank you Anders <3
indeed i am ignoring $3 as its the encore only,
i need to find and replace the line that comes right after $3,
as $0 string will show up many times; always below a distinct but similar entry to $3 format,
i first find my "encore" line with $3; from there i find again $0 and replace,

the problem is that it keeps looping finding $0 and replacing it multiple times instead of a single time edit followed by exiting,

i need to find string 1, immediately followed by the first instance found of string 2 following string 1 and replace it,

am i going at it the wrong way?

TIA
Chris
MyPC8MyBrain is offline   Reply With Quote
Reply
Go Back   Winamp & SHOUTcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump