Another string tokenizer
I recently did some work on an installer and came across the need to tokenize a list of strings. I first attempted to use the macro as defined here: http://nsis.sourceforge.net/Split_strings which tokenizes strings of the form "string1" "string2" ... "stringN". One problem with the macro is that it doesn't allow your strings to contain spaces. So I wrote a simple function to parse a string based on a delimiter instead. You may want to convert it into a macro, or even optimize it a bit, but I thought it might be useful for someone.
Function gettoken
Pop $0 ;list of strings
Pop $1 ;desired token number
StrCpy $R0 0 ;base index
StrCpy $R1 -1 ;string index
StrCpy $R3 1 ;iteration
StrLen $R5 $0
token_nextchar:
IntOp $R1 $R1 + 1
IntCmp $R1 $R5 token_empty 0 0
StrCpy $R2 $0 1 $R1
StrCmp $R2 '|' 0 token_nextchar
IntCmp $R3 $1 token_return token_nexttoken 0
token_nexttoken:
IntOp $R0 $R1 + 1
IntOp $R3 $R3 + 1
Goto token_nextchar
token_return:
IntOp $R4 $R1 - $R0
StrCpy $R2 $0 $R4 $R0
Goto token_done
token_empty:
StrCpy $R2 "_empty_"
token_done:
Push $R2
FunctionEnd
Currently the delimiter is hard-coded to '|' but you could easily make that a parameter. Before calling the function, you need to push both the token number you want returned and the list of strings onto the stack. One caveat to note is that the the list of string needs to have a delimiter at the very end. So an example of a working list of strings would be:
"The United States of America|Japan|Korea|China|"
You can iterate from 1 to N calling the function at each index to tokenize the entire list. The return value is pushed onto the stack. Your iteration can terminate if the return value was "_empty_".