Split strings: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
No edit summary
Line 89: Line 89:
</highlight-nsis>
</highlight-nsis>


May be helpful!


Note: You cannot use multiple !insertmacro SPLIT_STRING within the same function/section using the same part.
Note: You cannot use multiple !insertmacro SPLIT_STRING within the same function/section using the same part.

Revision as of 14:13, 19 December 2007

Author: Lyra IT (talk, contrib)


Description

This script (macro) is an improved version of SPLIT_STRING macro Save_on_variables. It will allow you to split up a string into separate sub-strings for use. Differently from SPLIT_STRING, it works with more than 2 sub-strings. It can be useful to get single parameters from a command line. The format of the string is:

"string1" "string2"..."stringN"

The macro will then split it up to use one piece of the string (e.g. stringN)

Usage

!insertmacro GET_STRING_TOKEN $0 2 ;$0 = input string, 2 = get 2nd sub-string
Pop $1

Usage Example

; in $0 is stored the string
 
  StrCpy $1 0
top:
  IntOp $1 $1 + 1
  !insertmacro GET_STRING_TOKEN $0 $1
  Pop $2
  StrCmp $2 "error" go_ahead 0
  MessageBox MB_OK $2
  Goto top
go_ahead:

The Macro

!macro GET_STRING_TOKEN INPUT PART
  Push $R0
  Push $R1
  Push $R2
 
; R0 = indice di scorrimento stringa
; R0 = index of current position in the string
  StrCpy 	$R0 -1
; R1 = indice del carattere " da trovare  
; R1 = index of '"' character to be found
  IntOp  	$R1 ${PART} * 2
  IntOp  	$R1 $R1 - 1
 
; cerco il " che indica l'inizio della sottostringa di interesse
; searching '"' character beginning the substring
findStart_loop_${PART}:						
 
  IntOp  	$R0 $R0 + 1 					; i++
  StrCpy	$R2 ${INPUT} 1 $R0				; getting next character
  StrCmp 	$R2 "" error_${PART}
  StrCmp 	$R2 '"' 0 findStart_loop_${PART}
 
  IntOp 	$R1 $R1 - 1
  IntCmp 	$R1 0 0 0 findStart_loop_${PART}		
 
; salvo in R1 l'indice di inizio della sottostringa di interesse 
; storing in R1 the index beginning the substring
  IntOp 	$R1 $R0 + 1
 
; cerco il " successivo, che indica la fine della stringa di interesse
; searching '"' character ending the substring
findEnd_loop_${PART}:						
  IntOp  	$R0 $R0 + 1 					; i++
  StrCpy	$R2 ${INPUT} 1 $R0				; getting next character
  StrCmp 	$R2 "" error_${PART}
  StrCmp 	$R2 '"' 0 findEnd_loop_${PART}
 
; R0 = indice di fine della sottostringa di interesse
; R0 = the index ending the substring
  IntOp 	$R0 $R0 - $R1					
; salvo in R0 la lunghezza della sottostringa di interesse						; storing in R0 the substring's length
  StrCpy 	$R0 ${INPUT} $R0 $R1
  Goto 		done_${PART}
 
 
error_${PART}:
  StrCpy 	$R0 error
 
 
done_${PART}:
  Pop 		$R2
  Pop 		$R1
  Exch 		$R0
!macroend


Note: You cannot use multiple !insertmacro SPLIT_STRING within the same function/section using the same part. Doing so will stop your script compiling. e.g.

Function ThisFunctionIsFine
!insertmacro GET_STRING_TOKEN $0 1
!insertmacro GET_STRING_TOKEN $0 2
FunctionEnd
 
Function ThisFunctionIs'''NOT'''Fine
!insertmacro GET_STRING_TOKEN $0 1
!insertmacro GET_STRING_TOKEN $0 1
FunctionEnd

-Lyra