Skip to content
⌘ NSIS Forum Archive

Iterate List of Strings

5 posts

zewari#

Iterate List of Strings

I'm sure this must be a super simple thing, but I could not figure out how to iterate through a list of strings in NSIS. Any suggestions?
Anders#
It would be helpful if you provided some more information. Where do the strings come from? Their format? Separators?

All you need is just StrCpy and StrCmp in a loop...
zewari#
I am creating an installer for a plugin that copies different dlls based on the version of the requisite application that is installed. To do this, I have a section for each supported version of the requisite application that provides the appropriate DLLs and required steps in order to make the plugin work for the main app. All of the sections are disabled by default, and do the following in .onInit:
  1. Read the registry to determine if the required app is present.
  2. Determine if a supported version of the required app is present
  3. Enable the section that corresponds to the supported version


I'm using a bunch of "If" statements to check each version supported by my plugin and would like to reduce the redundant code by using a for loop to check for each version. I take this approach because the registry hive for the required app is a little funky. For the sake of argument, let's say the supported versions are:
  • 1.2.1
  • 1.2.2
  • 1.3.0
  • 1.3.1


Versions 1.2.1 and 1.2.2 would be under a hive named 'Software1.2', whereas versions 1.3.0, 1.3.1 would be under 1.3.

Instead of having a bunch of If statements where I have the same redundant code that checks the registry value like I do now, I would like to have a list of strings and a for statement that steps through each value.

If this were Python, what I want to do would look something like this:

supported_versions = ['1.2.1', '1.2.2', '1.3.0', '1.3.1']
for value in [1.2, 1.3]:
if ReadRegistry(<hive>, <key>) in supported_versions:
break
Anders#
I don't speak python but here is how you can split a string into substrings:

Function StrSplitOne
Exch $0 ; Separator
Exch 
Exch $1 ; String
Push $2
Push $3
StrCpy $2 0
loop:
    StrCpy $3 $1 1 $2
    IntOp $2 $2 + 1
    StrCmp $3 "" +3
    StrCmp $3 $0 0 loop
    IntOp $2 $2 - 1
    StrCpy $0 $1 $2
    IntOp $2 $2 + 1
    StrCpy $1 $1 "" $2
    
done:
Pop $3
Pop $2
Exch $1 ; Remaining
Exch
Exch $0 ; Item
FunctionEnd
Section 
StrCpy $0 "1.2.1|1.2.2|1.3.0|1.3.1"
loop:
    StrCmp $0 "" done
    Push $0
    Push '|'
    Call StrSplitOne
    Pop $1
    StrCmp $1 "" next ; Skip empty
    DetailPrint "Item:$1!"
next:
    Pop $0
    Goto loop
done:
SectionEnd