WordAdd: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Updated author links.)
m (Added category links.)
Line 236: Line 236:
FunctionEnd
FunctionEnd
</highlight-nsis>
</highlight-nsis>
[[{{ns:14}}:String Functions]]

Revision as of 22:02, 30 April 2005

Author: Instructor (talk, contrib)


Links

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

The Function

/*
______________________________________________________________________________
 
                         WordAdd v1.8 (add or delete)
______________________________________________________________________________
 
2004 Shengalts Aleksander (Shengalts@mail.ru)
 
Function "WordFind" required.
 
 
Add words to string1 from string2 if not exist or
delete words if exist.
 
 
Syntax:
 
Push "[string1]"          ;[string1]
                          ;  string for addition or removing
Push "[delimiter]"        ;[delimiter]
                          ;  one or several symbols
Push "[E][+-string2]"     ;[+-string2]
                          ;  +string2 : words to add
                          ;  -string2 : words to delete
                          ;
                          ;[E]
                          ;  with errorlevel output
                          ;  IfErrors:
                          ;     $var=1  delimiter is empty
                          ;     $var=3  syntax error (use: +text,-text)
                          ;[]
                          ;  no errorlevel output (default)
                          ;  If some errors found then (result=input string)
                          ;
Call WordAdd              ;call function
                          ;
Pop $var                  ;output (result)
 
 
Example (add):
Section
	Push "C:\io.sys C:\WINDOWS"
	Push " "
	Push "+C:\WINDOWS C:\config.sys"
	Call WordAdd
	Pop $R0     ;now contain: "C:\io.sys C:\WINDOWS C:\config.sys"
SectionEnd
 
Example (delete):
Section
	Push "C:\io.sys C:\logo.sys C:\WINDOWS"
	Push " "
	Push "-C:\WINDOWS C:\config.sys C:\IO.SYS"
	Call WordAdd
	Pop $R0     ;now contain: "C:\logo.sys"
SectionEnd
 
Example (add to one):
Section
	Push "C:\io.sys"
	Push " "
	Push "+C:\WINDOWS C:\config.sys C:\IO.SYS"
	Call WordAdd
	Pop $R0     ;now contain: "C:\io.sys C:\WINDOWS C:\config.sys"
SectionEnd
 
Example (delete one):
Section
	Push "C:\io.sys C:\logo.sys C:\WINDOWS"
	Push " "
	Push "-C:\WINDOWS"
	Call WordAdd
	Pop $R0     ;now contain: "C:\io.sys C:\logo.sys"
SectionEnd
 
Example (No new words found):
Section
	Push "C:\io.sys C:\logo.sys"
	Push " "
	Push "+C:\logo.sys"
	Call WordAdd
	Pop $R0
 
	StrCmp $R0 "C:\io.sys C:\logo.sys" 0 +2
	MessageBox MB_OK "No new words found to add"
SectionEnd
 
Example (No words deleted):
Section
	Push "C:\io.sys C:\logo.sys"
	Push " "
	Push "-C:\config.sys"
	Call WordAdd
	Pop $R0
 
	StrCmp $R0 "C:\io.sys C:\logo.sys" 0 +2
	MessageBox MB_OK "No words found to delete"
SectionEnd
 
Example (With errorlevel output):
Section
	Push "C:\io.sys C:\logo.sys"
	Push ""
	Push "E-C:\logo.sys"
	Call WordAdd
	Pop $R0     ;now contain: "1" (delimiter is empty "")
 
	IfErrors 0 noerrors
	MessageBox MB_OK 'Errorlevel=$R0' IDOK end
 
	noerrors:
	MessageBox MB_OK 'No errors'
 
	end:
SectionEnd*/
 
 
;---------------------------------------------------------------------------
Function WordAdd
	Exch $1
	Exch
	Exch $0
	Exch
	Exch 2
	Exch $R0
	Exch 2
	Push $2
	Push $3
	Push $4
	Push $5
	Push $6
	Push $7
	Push $R1
	ClearErrors
 
	StrCpy $7 ''
	StrCpy $2 $1 1
	StrCmp $2 'E' 0 +4
	StrCpy $7 E
	StrCpy $1 $1 '' 1
	goto -4
 
	StrCpy $5 0
	StrCpy $R1 $R0
	StrCpy $2 $1 '' 1
	StrCpy $1 $1 1
	StrCmp $1 '+' +2
	StrCmp $1 '-' 0 error3
 
	StrCmp $0 '' error1
	StrCmp $2 '' end
	StrCmp $R0 '' 0 +5
	StrCmp $1 '-' end
	StrCmp $1 '+' 0 +3
	StrCpy $R0 $2
	goto end
 
	loop:
	IntOp $5 $5 + 1
	Push `$2`
	Push `$0`
	Push `E+$5`
	Call WordFind
	Pop $3
	IfErrors 0 /word
	StrCmp $3 2 +4
	StrCmp $3$5 11 0 +3
	StrCpy $3 $2
	goto /word
	StrCmp $1 '-' end preend
 
	/word:
	Push `$R0`
	Push `$0`
	Push `E/$3`
	Call WordFind
	Pop $4
	IfErrors +2
	StrCmp $1 '-' delete loop
	StrCmp $1$4 '-1' +2
	StrCmp $1 '-' loop +4
	StrCmp $R0 $3 0 loop
	StrCpy $R0 ''
	goto end
	StrCmp $1$4 '+1' 0 +2
	StrCmp $R0 $3 loop
	StrCmp $R0 $R1 +3
	StrCpy $R1 '$R1$0$3'
	goto loop
	StrLen $6 $0
	StrCpy $6 $R0 '' -$6
	StrCmp $6 $0 0 -4
	StrCpy $R1 '$R1$3'
	goto loop
 
	delete:
	Push `$R0`
	Push `$0`
	Push `E+$4{}`
	Call WordFind
	Pop $R0
	goto /word
 
	error3:
	StrCpy $R1 3
	goto error
	error1:
	StrCpy $R1 1
	error:
	StrCmp $7 'E' 0 end
	SetErrors
 
	preend:
	StrCpy $R0 $R1
 
	end:
	Pop $R1
	Pop $7
	Pop $6
	Pop $5
	Pop $4
	Pop $3
	Pop $2
	Pop $1
	Pop $0
	Exch $R0
FunctionEnd