WordReplace: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Wikipedia python library)
 
No edit summary
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Links ==
{{PageAuthor|Instructor}}
; Latest version of headers "nsh.zip":
: http://forums.winamp.com/showthread.php?s=&threadid=203228&goto=lastpost


== The Function ==
{{User:Instructor/Headers/Template}}
<highlight-nsis>/*
______________________________________________________________________________


                        WordReplace v2.0 (replace or delete)
== Function Description ==
______________________________________________________________________________


2004 Shengalts Aleksander (Shengalts@mail.ru)
<highlight-nsis>
____________________________________________________________________________
 
                        WordReplace v2.2
____________________________________________________________________________




Line 17: Line 16:


Syntax:
Syntax:
${WordReplace} "[string]" "[word1]" "[word2]" "[E][options]" $var


Push "[string]"     ;[string]
"[string]"         ;[string]
                    ;  input string
                  ;  input string
Push "[word1]"       ;[word1]
"[word1]"         ;[word1]
                    ;  word to replace or delete
                  ;  word to replace or delete
Push "[word2]"       ;[word2]
"[word2]"         ;[word2]
                    ;  replace with (if empty delete)
                  ;  replace with (if empty delete)
Push "[E][options]" ;[options]
"[E][options]"     ;[options]
                    ;  +number  : word number from start
                  ;  +number  : word number from start
                    ;  -number  : word number from end
                  ;  -number  : word number from end
                    ;  +number* : word number from start multiple-replace
                  ;  +number* : word number from start multiple-replace
                    ;  -number* : word number from end multiple-replace
                  ;  -number* : word number from end multiple-replace
                    ;  +        : replace or delete all founded
                  ;  +        : replace all founded
                    ;  +*      : multiple-replace all founded
                  ;  +*      : multiple-replace all founded
                    ;  {}      : if exists replace or delete all delimiters
                  ;  {        : if exists replace all delimiters
                    ;              from edges (no errorlevel output)
                  ;              from left edge
                    ;  {}*      : if exists multiple-replace all delimiters
                  ;  }        : if exists replace all delimiters
                    ;              from edges (no errorlevel output)
                  ;              from right edge
                    ;
                  ;  {}      : if exists replace all delimiters
                    ;[E]
                  ;              from edges
                    ;  with errorlevel output
                  ;  {*      : if exists multiple-replace all
                    ;  IfErrors:
                  ;              delimiters from left edge
                    ;    $var=1  word to replace or delete not found
                  ;  }*      : if exists multiple-replace all
                    ;    $var=2  no such word number
                  ;              delimiters from right edge
                    ;    $var=3  syntax error
                  ;  {}*      : if exists multiple-replace all
                    ;            (Use: +1,-1,+1*,-1*,+,+*,{},{}*)
                  ;              delimiters from edges
                    ;[]
                  ;
                    ;  no errorlevel output (default)
                  ;[E]
                    ;  If some errors found then (result=input string)
                  ;  with errorlevel output
                    ;
                  ;  IfErrors:
Call WordReplace    ;call function
                  ;    $var=1  word to replace not found
                    ;
                  ;    $var=2  no such word number
Pop $var             ;output (result)
                  ;    $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
Push "C:\io.sys C:\logo.sys C:\WINDOWS"
${WordReplace} "C:\io.sys C:\logo.sys C:\WINDOWS" "SYS" "bmp" "+2" $R0
Push "SYS"
; $R0="C:\io.sys C:\logo.bmp C:\WINDOWS"
Push "bmp"
Push "+2"
Call WordReplace
Pop $R0     ;now contain: "C:\io.sys C:\logo.bmp C:\WINDOWS"
SectionEnd
SectionEnd
</highlight-nsis>


Example (delete):
<b>Example (delete):</b>
Section
<highlight-nsis>Section
Push "C:\io.sys C:\logo.sys C:\WINDOWS"
${WordReplace} "C:\io.sys C:\logo.sys C:\WINDOWS" "SYS" "" "+" $R0
Push "SYS"
; $R0="C:\io. C:\logo. C:\WINDOWS"
Push ""
Push "+"
Call WordReplace
Pop $R0     ;now contain: "C:\io. C:\logo. C:\WINDOWS"
SectionEnd
SectionEnd
</highlight-nsis>


Example (multiple-replace 1):
<b>Example (multiple-replace 1):</b>
Section
<highlight-nsis>Section
Push "C:\io.sys      C:\logo.sys  C:\WINDOWS"
${WordReplace} "C:\io.sys      C:\logo.sys  C:\WINDOWS" " " " " "+1*" $R0
Push " "
; +1* or +2* or +3* or +4* or +5* or +6*
Push " "
; $R0="C:\io.sys C:\logo.sys  C:\WINDOWS"
Push "+1*"   ; +1* or +2* or +3* or +4* or +5* or +6*
Call WordReplace
Pop $R0     ;now contain: "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
Push "C:\io.sys C:\logo.sysSYSsys C:\WINDOWS"
${WordReplace} "C:\io.sys C:\logo.sysSYSsys C:\WINDOWS" "sys" "bmp" "+*" $R0
Push "sys"
; $R0="C:\io.bmp C:\logo.bmp C:\WINDOWS"
Push "bmp"
Push "+*"
Call WordReplace
Pop $R0     ;now contain: "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
Push "sysSYSsysC:\io.sys C:\logo.sys C:\WINDOWSsysSYSsys"
${WordReplace} "sysSYSsysC:\io.sys C:\logo.sys C:\WINDOWSsysSYSsys" "sys" "|" "{}*" $R0
Push "sys"
; $R0="|C:\io.sys C:\logo.sys C:\WINDOWS|"
Push "|"
Push "{}*"
Call WordReplace
Pop $R0     ;now contain: "|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
Push "C:\io.sys C:\logo.sys"
${WordReplace} "C:\io.sys C:\logo.sys" "sys" "bmp" "E+3" $R0
Push "sys"
; $R0="2" (no such word number "+3")
Push "bmp"
Push "E+3"
Call WordReplace
Pop $R0     ;now contain: "2" (no such word number "+3")


IfErrors 0 noerrors
IfErrors 0 noerrors
Line 118: 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 146: Line 147:
StrCpy $9 ''
StrCpy $9 ''
StrCpy $3 $2 1
StrCpy $3 $2 1
StrCmp $3 'E' 0 +4
StrCpy $2 $2 '' 1
StrCmp $3 'E' 0 +3
StrCpy $9 E
StrCpy $9 E
StrCpy $2 $2 '' 1
goto -4
goto -4


StrCpy $4 $2 1 -1
StrCpy $5 ''
StrCpy $6 ''
StrLen $7 $0
StrLen $7 $0


StrCpy $4 $2 3
StrCmp $7 0 error1
StrCpy $5 $2 2
StrCmp $R0 '' error1
StrCmp $4 '{}*' +3
StrCmp $3 '{' beginning
StrCmp $5 '{}' +2
StrCmp $3 '}' ending errorchk
goto errorchk
 
StrCmp $7 0 end
beginning:
StrCpy $5 ''
StrCpy $8 $R0 $7
StrCpy $6 ''
StrCmp $8 $0 0 +4
StrCpy $3 $R0 $7
StrCmp $3 $0 0 +4
StrCpy $R0 $R0 '' $7
StrCpy $R0 $R0 '' $7
StrCpy $5 '$1$5'
StrCpy $5 '$5$1'
goto -4
goto -4
StrCpy $3 $R0 '' -$7
StrCpy $3 $2 1
StrCmp $3 $0 0 +4
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 '{}*' 0 +5
 
merge:
StrCmp $4 '*' 0 +5
StrCmp $5 '' +2
StrCmp $5 '' +2
StrCpy $5 $1
StrCpy $5 $1
Line 180: Line 188:


errorchk:
errorchk:
StrCpy $3 $2 1
StrCpy $2 $2 '' 1
StrCmp $3 '+' +2
StrCmp $3 '+' +2
StrCmp $3 '-' 0 error3
StrCmp $3 '-' 0 error3
StrCmp $R0 '' error1
StrCmp $7 0 error1


StrCpy $4 $2 1 -1
StrCpy $5 $2 1
StrCpy $5 $2 1
IntOp $2 $2 + 0
IntOp $2 $2 + 0
Line 197: Line 200:
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
Line 283: Line 286:
</highlight-nsis>
</highlight-nsis>


 
[[Category:String Functions]]
Page author: Instructor

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