WordReplace: Difference between revisions
From NSIS Wiki
Jump to navigationJump to search
m (Added category links.) |
Instructor (talk | contribs) No edit summary |
||
(5 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{ | {{PageAuthor|Instructor}} | ||
{{User:Instructor/Headers/Template}} | |||
== Function Description == | |||
<highlight-nsis> | |||
____________________________________________________________________________ | |||
WordReplace v2.2 | |||
____________________________________________________________________________ | |||
Line 21: | Line 16: | ||
Syntax: | 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 all founded | |||
; +* : multiple-replace all founded | |||
; { : if exists replace all delimiters | |||
; from left edge | |||
; } : if exists replace all delimiters | |||
; from right edge | |||
; {} : if exists replace all delimiters | |||
; from edges | |||
; {* : if exists multiple-replace all | |||
; delimiters from left edge | |||
; }* : if exists multiple-replace all | |||
; delimiters from right edge | |||
; {}* : if exists multiple-replace all | |||
; delimiters from edges | |||
; | |||
;[E] | |||
; with errorlevel output | |||
; IfErrors: | |||
; $var=1 word to replace 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) | |||
</highlight-nsis> | |||
Example (replace): | <b>Example (replace):</b> | ||
Section | <highlight-nsis>Section | ||
${WordReplace} "C:\io.sys C:\logo.sys C:\WINDOWS" "SYS" "bmp" "+2" $R0 | |||
; $R0="C:\io.sys C:\logo.bmp C:\WINDOWS" | |||
SectionEnd | SectionEnd | ||
</highlight-nsis> | |||
Example (delete): | <b>Example (delete):</b> | ||
Section | <highlight-nsis>Section | ||
${WordReplace} "C:\io.sys C:\logo.sys C:\WINDOWS" "SYS" "" "+" $R0 | |||
; $R0="C:\io. C:\logo. C:\WINDOWS" | |||
SectionEnd | SectionEnd | ||
</highlight-nsis> | |||
Example (multiple-replace 1): | <b>Example (multiple-replace 1):</b> | ||
Section | <highlight-nsis>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 | SectionEnd | ||
</highlight-nsis> | |||
Example (multiple-replace 2): | <b>Example (multiple-replace 2):</b> | ||
Section | <highlight-nsis>Section | ||
${WordReplace} "C:\io.sys C:\logo.sysSYSsys C:\WINDOWS" "sys" "bmp" "+*" $R0 | |||
; $R0="C:\io.bmp C:\logo.bmp C:\WINDOWS" | |||
SectionEnd | SectionEnd | ||
</highlight-nsis> | |||
Example (multiple-replace 3): | <b>Example (multiple-replace 3):</b> | ||
Section | <highlight-nsis>Section | ||
${WordReplace} "sysSYSsysC:\io.sys C:\logo.sys C:\WINDOWSsysSYSsys" "sys" "|" "{}*" $R0 | |||
; $R0="|C:\io.sys C:\logo.sys C:\WINDOWS|" | |||
SectionEnd | SectionEnd | ||
</highlight-nsis> | |||
Example (With errorlevel output): | <b>Example (With errorlevel output):</b> | ||
Section | <highlight-nsis>Section | ||
${WordReplace} "C:\io.sys C:\logo.sys" "sys" "bmp" "E+3" $R0 | |||
; $R0="2" (no such word number "+3") | |||
IfErrors 0 noerrors | IfErrors 0 noerrors | ||
Line 122: | Line 106: | ||
end: | end: | ||
SectionEnd | SectionEnd | ||
</highlight-nsis> | |||
== Function Code == | |||
<highlight-nsis> | |||
Function WordReplace | 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 $2 | ||
Exch | Exch | ||
Line 150: | Line 147: | ||
StrCpy $9 '' | StrCpy $9 '' | ||
StrCpy $3 $2 1 | StrCpy $3 $2 1 | ||
StrCmp $3 'E' 0 + | StrCpy $2 $2 '' 1 | ||
StrCmp $3 'E' 0 +3 | |||
StrCpy $9 E | StrCpy $9 E | ||
goto -4 | goto -4 | ||
StrCpy $4 $2 1 -1 | |||
StrCpy $5 '' | |||
StrCpy $6 '' | |||
StrLen $7 $0 | StrLen $7 $0 | ||
StrCmp $7 0 error1 | |||
StrCmp $R0 '' error1 | |||
StrCmp $ | StrCmp $3 '{' beginning | ||
StrCmp $ | StrCmp $3 '}' ending errorchk | ||
beginning: | |||
StrCpy $8 $R0 $7 | |||
StrCmp $8 $0 0 +4 | |||
StrCpy $ | |||
StrCmp $ | |||
StrCpy $R0 $R0 '' $7 | StrCpy $R0 $R0 '' $7 | ||
StrCpy $5 '$1 | StrCpy $5 '$5$1' | ||
goto -4 | goto -4 | ||
StrCpy $3 $R0 '' -$7 | StrCpy $3 $2 1 | ||
StrCmp $ | StrCmp $3 '}' 0 merge | ||
ending: | |||
StrCpy $8 $R0 '' -$7 | |||
StrCmp $8 $0 0 +4 | |||
StrCpy $R0 $R0 -$7 | StrCpy $R0 $R0 -$7 | ||
StrCpy $6 '$6$1' | StrCpy $6 '$6$1' | ||
goto -4 | goto -4 | ||
StrCmp $4 ' | |||
merge: | |||
StrCmp $4 '*' 0 +5 | |||
StrCmp $5 '' +2 | StrCmp $5 '' +2 | ||
StrCpy $5 $1 | StrCpy $5 $1 | ||
Line 184: | Line 188: | ||
errorchk: | errorchk: | ||
StrCmp $3 '+' +2 | StrCmp $3 '+' +2 | ||
StrCmp $3 '-' 0 error3 | StrCmp $3 '-' 0 error3 | ||
StrCpy $5 $2 1 | StrCpy $5 $2 1 | ||
IntOp $2 $2 + 0 | IntOp $2 $2 + 0 | ||
Line 201: | Line 200: | ||
StrCpy $5 0 | StrCpy $5 0 | ||
StrCpy $2 $R0 $7 $5 | StrCpy $2 $R0 $7 $5 | ||
StrCmp $2 '' +4 | StrCmp $2 '' +4 | ||
StrCmp $2 $0 + | StrCmp $2 $0 +6 | ||
IntOp $5 $5 + 1 | IntOp $5 $5 + 1 | ||
goto - | goto -4 | ||
StrCmp $R0 $R1 error1 | |||
StrCpy $R0 '$3$R0' | StrCpy $R0 '$3$R0' | ||
goto end | goto end | ||
Line 287: | Line 286: | ||
</highlight-nsis> | </highlight-nsis> | ||
[[ | [[Category:String Functions]] |
Latest revision as of 07:34, 7 February 2006
Author: Instructor (talk, contrib) |
Page for NSIS 2.07 and below users
You can use the latest version of headers (recommended) or the following function code (put the function code in your script before calling it)
Function Description
____________________________________________________________________________ WordReplace v2.2 ____________________________________________________________________________ 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 all founded ; +* : multiple-replace all founded ; { : if exists replace all delimiters ; from left edge ; } : if exists replace all delimiters ; from right edge ; {} : if exists replace all delimiters ; from edges ; {* : if exists multiple-replace all ; delimiters from left edge ; }* : if exists multiple-replace all ; delimiters from right edge ; {}* : if exists multiple-replace all ; delimiters from edges ; ;[E] ; with errorlevel output ; IfErrors: ; $var=1 word to replace 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
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 StrCpy $2 $2 '' 1 StrCmp $3 'E' 0 +3 StrCpy $9 E goto -4 StrCpy $4 $2 1 -1 StrCpy $5 '' StrCpy $6 '' StrLen $7 $0 StrCmp $7 0 error1 StrCmp $R0 '' error1 StrCmp $3 '{' beginning StrCmp $3 '}' ending errorchk beginning: StrCpy $8 $R0 $7 StrCmp $8 $0 0 +4 StrCpy $R0 $R0 '' $7 StrCpy $5 '$5$1' goto -4 StrCpy $3 $2 1 StrCmp $3 '}' 0 merge ending: StrCpy $8 $R0 '' -$7 StrCmp $8 $0 0 +4 StrCpy $R0 $R0 -$7 StrCpy $6 '$6$1' goto -4 merge: StrCmp $4 '*' 0 +5 StrCmp $5 '' +2 StrCpy $5 $1 StrCmp $6 '' +2 StrCpy $6 $1 StrCpy $R0 '$5$R0$6' goto end errorchk: StrCmp $3 '+' +2 StrCmp $3 '-' 0 error3 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