WordReplace: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
No edit summary
No edit summary
Line 13: Line 13:
____________________________________________________________________________
____________________________________________________________________________


                         WordReplace v2.0 (replace or delete)
                         WordReplace v2.1 (replace or delete)
____________________________________________________________________________
____________________________________________________________________________


Line 195: Line 195:
StrCpy $5 0
StrCpy $5 0
StrCpy $2 $R0 $7 $5
StrCpy $2 $R0 $7 $5
StrCmp $2$3 '' error1
StrCmp $2 '' +4
StrCmp $2 '' +4
StrCmp $2 $0 +5
StrCmp $2 $0 +6
IntOp $5 $5 + 1
IntOp $5 $5 + 1
goto -5
goto -4
StrCmp $R0 $R1 error1
StrCpy $R0 '$3$R0'
StrCpy $R0 '$3$R0'
goto end
goto end

Revision as of 08:16, 7 September 2005

Author: Instructor (talk, contrib)


Links

Latest version of headers "nsh.zip"
http://forums.winamp.com/showthread.php?s=&threadid=203228&goto=lastpost

If a function is used without header, put the function code in your script before calling it.

The Function Description

____________________________________________________________________________
 
                         WordReplace v2.1 (replace or delete)
____________________________________________________________________________
 
 
Replace or delete word from string.
 
 
Syntax:
${WordReplace} "[string]" "[word1]" "[word2]" "[E][options]" $var
 
"[string]"         ;[string]
                   ;  input string
"[word1]"          ;[word1]
                   ;  word to replace or delete
"[word2]"          ;[word2]
                   ;  replace with (if empty delete)
"[E][options]"     ;[options]
                   ;  +number  : word number from start
                   ;  -number  : word number from end
                   ;  +number* : word number from start multiple-replace
                   ;  -number* : word number from end multiple-replace
                   ;  +        : replace or delete all founded
                   ;  +*       : multiple-replace all founded
                   ;  {}       : if exists replace or delete all delimiters
                   ;               from edges (no errorlevel output)
                   ;  {}*      : if exists multiple-replace all delimiters
                   ;               from edges (no errorlevel output)
                   ;
                   ;[E]
                   ;  with errorlevel output
                   ;  IfErrors:
                   ;     $var=1  word to replace or delete not found
                   ;     $var=2  no such word number
                   ;     $var=3  syntax error (Use: +1,-1,+1*,-1*,+,+*,{},{}*)
                   ;[]
                   ;  no errorlevel output (default)
                   ;  If some errors found then (result=input string)
                   ;
$var               ;output (result)


Example (replace):

Section
	${WordReplace} "C:\io.sys C:\logo.sys C:\WINDOWS" "SYS" "bmp" "+2" $R0
	; $R0="C:\io.sys C:\logo.bmp C:\WINDOWS"
SectionEnd

Example (delete):

Section
	${WordReplace} "C:\io.sys C:\logo.sys C:\WINDOWS" "SYS" "" "+" $R0
	; $R0="C:\io. C:\logo. C:\WINDOWS"
SectionEnd

Example (multiple-replace 1):

Section
	${WordReplace} "C:\io.sys      C:\logo.sys   C:\WINDOWS" " " " " "+1*" $R0
	; +1* or +2* or +3* or +4* or +5* or +6*
	; $R0="C:\io.sys C:\logo.sys   C:\WINDOWS"
SectionEnd

Example (multiple-replace 2):

Section
	${WordReplace} "C:\io.sys C:\logo.sysSYSsys C:\WINDOWS" "sys" "bmp" "+*" $R0
	; $R0="C:\io.bmp C:\logo.bmp C:\WINDOWS"
SectionEnd

Example (multiple-replace 3):

Section
	${WordReplace} "sysSYSsysC:\io.sys C:\logo.sys C:\WINDOWSsysSYSsys" "sys" "|" "{}*" $R0
	; $R0="|C:\io.sys C:\logo.sys C:\WINDOWS|"
SectionEnd

Example (With errorlevel output):

Section
	${WordReplace} "C:\io.sys C:\logo.sys" "sys" "bmp" "E+3" $R0
	; $R0="2" (no such word number "+3")
 
	IfErrors 0 noerrors
	MessageBox MB_OK 'Errorlevel=$R0' IDOK end
 
	noerrors:
	MessageBox MB_OK 'No errors'
 
	end:
SectionEnd

The Function Code

Function WordReplace
	!define WordReplace `!insertmacro WordReplaceCall`
 
	!macro WordReplaceCall _STRING _WORD1 _WORD2 _NUMBER _RESULT
		Push `${_STRING}`
		Push `${_WORD1}`
		Push `${_WORD2}`
		Push `${_NUMBER}`
		Call WordReplace
		Pop ${_RESULT}
	!macroend
 
	Exch $2
	Exch
	Exch $1
	Exch
	Exch 2
	Exch $0
	Exch 2
	Exch 3
	Exch $R0
	Exch 3
	Push $3
	Push $4
	Push $5
	Push $6
	Push $7
	Push $8
	Push $9
	Push $R1
	ClearErrors
 
	StrCpy $R1 $R0
	StrCpy $9 ''
	StrCpy $3 $2 1
	StrCmp $3 'E' 0 +4
	StrCpy $9 E
	StrCpy $2 $2 '' 1
	goto -4
 
	StrLen $7 $0
 
	StrCpy $4 $2 3
	StrCpy $5 $2 2
	StrCmp $4 '{}*' +3
	StrCmp $5 '{}' +2
	goto errorchk
	StrCmp $7 0 end
	StrCpy $5 ''
	StrCpy $6 ''
	StrCpy $3 $R0 $7
	StrCmp $3 $0 0 +4
	StrCpy $R0 $R0 '' $7
	StrCpy $5 '$1$5'
	goto -4
	StrCpy $3 $R0 '' -$7
	StrCmp $3 $0 0 +4
	StrCpy $R0 $R0 -$7
	StrCpy $6 '$6$1'
	goto -4
	StrCmp $4 '{}*' 0 +5
	StrCmp $5 '' +2
	StrCpy $5 $1
	StrCmp $6 '' +2
	StrCpy $6 $1
	StrCpy $R0 '$5$R0$6'
	goto end
 
	errorchk:
	StrCpy $3 $2 1
	StrCpy $2 $2 '' 1
	StrCmp $3 '+' +2
	StrCmp $3 '-' 0 error3
	StrCmp $R0 '' error1
	StrCmp $7 0 error1
 
	StrCpy $4 $2 1 -1
	StrCpy $5 $2 1
	IntOp $2 $2 + 0
	StrCmp $2 0 0 one
	StrCmp $5 0 error2
	StrCpy $3 ''
 
	all:
	StrCpy $5 0
	StrCpy $2 $R0 $7 $5
	StrCmp $2 '' +4
	StrCmp $2 $0 +6
	IntOp $5 $5 + 1
	goto -4
	StrCmp $R0 $R1 error1
	StrCpy $R0 '$3$R0'
	goto end
	StrCpy $2 $R0 $5
	IntOp $5 $5 + $7
	StrCmp $4 '*' 0 +3
	StrCpy $6 $R0 $7 $5
	StrCmp $6 $0 -3
	StrCpy $R0 $R0 '' $5
	StrCpy $3 '$3$2$1'
	goto all
 
	one:
	StrCpy $5 0
	StrCpy $8 0
	goto loop
 
	preloop:
	IntOp $5 $5 + 1
 
	loop:
	StrCpy $6 $R0 $7 $5
	StrCmp $6$8 0 error1
	StrCmp $6 '' minus
	StrCmp $6 $0 0 preloop
	IntOp $8 $8 + 1
	StrCmp $3$8 +$2 found
	IntOp $5 $5 + $7
	goto loop
 
	minus:
	StrCmp $3 '-' 0 error2
	StrCpy $3 +
	IntOp $2 $8 - $2
	IntOp $2 $2 + 1
	IntCmp $2 0 error2 error2 one
 
	found:
	StrCpy $3 $R0 $5
	StrCmp $4 '*' 0 +5
	StrCpy $6 $3 '' -$7
	StrCmp $6 $0 0 +3
	StrCpy $3 $3 -$7
	goto -3
	IntOp $5 $5 + $7
	StrCmp $4 '*' 0 +3
	StrCpy $6 $R0 $7 $5
	StrCmp $6 $0 -3
	StrCpy $R0 $R0 '' $5
	StrCpy $R0 '$3$1$R0'
	goto end
 
	error3:
	StrCpy $R0 3
	goto error
	error2:
	StrCpy $R0 2
	goto error
	error1:
	StrCpy $R0 1
	error:
	StrCmp $9 'E' +3
	StrCpy $R0 $R1
	goto +2
	SetErrors
 
	end:
	Pop $R1
	Pop $9
	Pop $8
	Pop $7
	Pop $6
	Pop $5
	Pop $4
	Pop $3
	Pop $2
	Pop $1
	Pop $0
	Exch $R0
FunctionEnd