Skip to content
⌘ NSIS Forum Archive

Does anyone have a function to parse connection strings?

2 posts

Ilitirit#

Does anyone have a function to parse connection strings?

eg. Something that will parse:
"Data Source=server;Initial Catalog=database;User ID=user;Password=password"

into 4 variables:
$DataSource = "server"
$InitialCatalog = "database"
$UserId="user"
$Password="password"
Ilitirit#edited
This seems to do the trick...

push "Data Source=server;Initial Catalog=database;User ID=user;Password=password"
pop $0
  
${WordFind} $0 "Data Source=" "-1" $R0
${WordFind} $R0 ";" "+1" $DataSource
${WordFind} $0 "Initial Catalog=" "-1" $R0
${WordFind} $R0 ";" "+1" $InitialCatalog
${WordFind} $0 "User ID=" "-1" $R0
${WordFind} $R0 ";" "+1" $UserId
  
${WordFind} $0 "Password=" "-1" $R0
${WordFind} $R0 ";" "+1" $Password
  
MessageBox MB_OK "[$DataSource|$InitialCatalog|$UserId|$Password]" 

Edit:
I put it into a function. It seems to work, but am I doing it correctly?

!define ParseConnectionString "!insertmacro ParseConnectionString"
!macro ParseConnectionString ConnectionString Server Database UserId Password
    Push "${ConnectionString}"
    Call ParseConnectionString
    Pop "${Password}"
    Pop "${UserId}"
    Pop "${Database}"
    Pop "${Server}"
!macroend
Function ParseConnectionString
     Exch $0     ; Connection string
     push $1     ; Temp variable
     push $R0    ; Server
     push $R1    ; Database
     push $R2    ; User Id
     push $R3    ; Password
     ${WordFind} $0 "Data Source=" "E-1" $1
     IfErrors +1 +3
     StrCpy $R0 ""
     Goto GetDatabase
     ${WordFind} $1 ";" "+1" $R0
     GetDatabase:
     ${WordFind} $0 "Initial Catalog=" "E-1" $1
     IfErrors +1 +3
     StrCpy $R1 ""
     Goto GetUserId
     ${WordFind} $1 ";" "+1" $R1
     GetUserId:
     ${WordFind} $0 "User ID=" "E-1" $1
     IfErrors +1 +3
     StrCpy $R2 ""
     Goto GetPassword
     ${WordFind} $1 ";" "+1" $R2
     GetPassword:
     ${WordFind} $0 "Password=" "E-1" $1
     IfErrors +1 +3
     StrCpy $R3 ""
     Goto End
     ${WordFind} $1 ";" "+1" $R3
     End:
     Exch 5      ; Move the previous value of $0 to the top of the stack
     Pop $0      ; Restore $0 variable
     Exch 4      ; Move the previous value of $1 to the top of the stack
     Pop $1      ; Restore $1 variable
     Exch $R1    ; Restore $R2 and put the database at the top of the stack
     Exch        ; Swap the database with the server output variable
     Exch $R0    ; Restore $R0 and put the server at the top of the stack
     Exch 2      ; Swap the server with the user id output variable
     
     Exch $R2    ; Restore $R2 and put the user id at the top of the stack
     Exch 3      ; Swap the user id with the password output variable
     
     Exch $R3    ;  Restore $R3 and put the password at the top of the stack
     ; Stack looks like this (bottom to top):
     ; Password|Database|Server|User Id
     
     Exch 3      ; User Id|Password|Database|Server
     Exch 2      ; Password|User Id|Database|Server
     Exch 3      ; Server|User Id|Database|Password
     Exch        ; User Id|Server|Database|Password
     Exch 2      ; Database|Server|User Id|Password
     Exch        ; Server|Database|User Id|Password
     
FunctionEnd