WordInsert

From NSIS Wiki
Jump to navigationJump to search
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

____________________________________________________________________________
 
                         WordInsert v1.3
____________________________________________________________________________
 
 
Insert word in string.
 
 
Syntax:
${WordInsert} "[string]" "[delimiter]" "[word]" "[E][options]" $var
 
"[string]"          ;[string]
                    ;  input string
"[delimiter]"       ;[delimiter]
                    ;  one or several symbols
"[word]"            ;[word]
                    ;  word to insert
"[E][options]"      ;[options]
                    ;  +number  : word number from start
                    ;  -number  : word number from end
                    ;
                    ;[E]
                    ;  with errorlevel output
                    ;  IfErrors:
                    ;     $var=1  delimiter is empty
                    ;     $var=2  wrong word number
                    ;     $var=3  syntax error (Use: +1,-1)
                    ;[]
                    ;  no errorlevel output (default)
                    ;  If some errors found then (result=input string)
                    ;
$var                ;output (result)


Example (1):

Section
	${WordInsert} "C:\io.sys C:\WINDOWS" " " "C:\logo.sys" "-2" $R0
	; $R0="C:\io.sys C:\logo.sys C:\WINDOWS"
SectionEnd

Example (2):

Section
	${WordInsert} "C:\io.sys" " " "C:\WINDOWS" "+2" $R0
	; $R0="C:\io.sys C:\WINDOWS"
SectionEnd

Example (3):

Section
	${WordInsert} "" " " "C:\WINDOWS" "+1" $R0
	; $R0="C:\WINDOWS "
SectionEnd

Example (With errorlevel output):

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

Function Code

Function WordInsert
	!define WordInsert `!insertmacro WordInsertCall`
 
	!macro WordInsertCall _STRING _DELIMITER _WORD _NUMBER _RESULT
		Push `${_STRING}`
		Push `${_DELIMITER}`
		Push `${_WORD}`
		Push `${_NUMBER}`
		Call WordInsert
		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 $5 ''
	StrCpy $6 $0
	StrCpy $7 }
 
	StrCpy $9 ''
	StrCpy $R1 $R0
	StrCpy $3 $2 1
	StrCpy $2 $2 '' 1
	StrCmp $3 'E' 0 +3
	StrCpy $9 'E'
	goto -4
 
	StrCmp $3 '+' +2
	StrCmp $3 '-' 0 error3
	IntOp $2 $2 + 0
	StrCmp $2 0 error2
	StrCmp $0 '' error1
 
	StrCmp $2 1 0 two
	GetLabelAddress $8 oneback
	StrCmp $3 '+' call
	StrCpy $7 {
	goto call
	oneback:
	IfErrors 0 +2
	StrCpy $4 $R0
	StrCmp $3 '+' 0 +3
	StrCpy $R0 '$1$0$4'
	goto end
	StrCpy $R0 '$4$0$1'
	goto end
 
	two:
	IntOp $2 $2 - 1
	GetLabelAddress $8 twoback
	StrCmp $3 '+' 0 call
	StrCpy $7 {
	goto call
	twoback:
	IfErrors 0 tree
	StrCmp $2$4 11 0 error2
	StrCmp $3 '+' 0 +3
	StrCpy $R0 '$R0$0$1'
	goto end
	StrCpy $R0 '$1$0$R0'
	goto end
 
	tree:
	StrCpy $7 }
	StrCpy $5 $4
	IntOp $2 $2 + 1
	GetLabelAddress $8 treeback
	StrCmp $3 '+' call
	StrCpy $7 {
	goto call
	treeback:
	IfErrors 0 +3
	StrCpy $4 ''
	StrCpy $6 ''
	StrCmp $3 '+' 0 +3
	StrCpy $R0 '$5$0$1$6$4'
	goto end
	StrCpy $R0 '$4$6$1$0$5'
	goto end
 
	call:	
	Push '$R0'
	Push '$0'
	Push 'E$3$2*$7'
	Call WordFind
	Pop $4
	goto $8
 
	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