WordReplace: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Added category links.)
m (Updated by user: Instructor (talk, contrib).)
Line 6: Line 6:
; Latest version of headers "nsh.zip":
; Latest version of headers "nsh.zip":
: http://forums.winamp.com/showthread.php?s=&threadid=203228&goto=lastpost
: http://forums.winamp.com/showthread.php?s=&threadid=203228&goto=lastpost
If a function is used without an header, you should put the function below in your script before calling it.


== The Function ==
== The Function ==
Line 13: Line 15:
                         WordReplace v2.0 (replace or delete)
                         WordReplace v2.0 (replace or delete)
______________________________________________________________________________
______________________________________________________________________________
2004 Shengalts Aleksander (Shengalts@mail.ru)




Line 21: Line 21:


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 or delete all founded
                    ;  +*      : multiple-replace all founded
                  ;  +*      : multiple-replace all founded
                    ;  {}      : if exists replace or delete all delimiters
                  ;  {}      : if exists replace or delete all delimiters
                    ;              from edges (no errorlevel output)
                  ;              from edges (no errorlevel output)
                    ;  {}*      : if exists multiple-replace all delimiters
                  ;  {}*      : if exists multiple-replace all delimiters
                    ;              from edges (no errorlevel output)
                  ;              from edges (no errorlevel output)
                    ;
                  ;
                    ;[E]
                  ;[E]
                    ;  with errorlevel output
                  ;  with errorlevel output
                    ;  IfErrors:
                  ;  IfErrors:
                    ;    $var=1  word to replace or delete not found
                  ;    $var=1  word to replace or delete not found
                    ;    $var=2  no such word number
                  ;    $var=2  no such word number
                    ;    $var=3  syntax error
                  ;    $var=3  syntax error (Use: +1,-1,+1*,-1*,+,{},...)
                    ;            (Use: +1,-1,+1*,-1*,+,+*,{},{}*)
                  ;[]
                    ;[]
                  ;  no errorlevel output (default)
                    ;  no errorlevel output (default)
                  ;  If some errors found then (result=input string)
                    ;  If some errors found then (result=input string)
                  ;
                    ;
$var               ;output (result)
Call WordReplace    ;call function
                    ;
Pop $var             ;output (result)




Example (replace):
Example (replace):
Section
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


Example (delete):
Example (delete):
Section
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


Example (multiple-replace 1):
Example (multiple-replace 1):
Section
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


Example (multiple-replace 2):
Example (multiple-replace 2):
Section
Section
Push "C:\io.sys C:\logo.sysSYSsys C:\WINDOWS"
${WordReplace} "C:\io.sys C:\logo.sysSYS 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


Example (multiple-replace 3):
Example (multiple-replace 3):
Section
Section
Push "sysSYSsysC:\io.sys C:\logo.sys C:\WINDOWSsysSYSsys"
${WordReplace} "###C:\io.sys C:\logo.sys C:\WINDOWS###" "#" "|" "{}*" $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


Example (With errorlevel output):
Example (With errorlevel output):
Section
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 126: Line 101:


;---------------------------------------------------------------------------
;---------------------------------------------------------------------------
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

Revision as of 09:42, 24 June 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 an header, you should put the function below in your script before calling it.

The Function

/*
______________________________________________________________________________
 
                         WordReplace v2.0 (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.sysSYS C:\WINDOWS" "sys" "bmp" "+*" $R0
	; $R0="C:\io.bmp C:\logo.bmp C:\WINDOWS"
SectionEnd
 
Example (multiple-replace 3):
Section
	${WordReplace} "###C:\io.sys C:\logo.sys C:\WINDOWS###" "#" "|" "{}*" $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 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$3 '' error1
	StrCmp $2 '' +4
	StrCmp $2 $0 +5
	IntOp $5 $5 + 1
	goto -5
	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