Urd
18th September 2004 16:12 UTC
Parcing a file for a variable
Is there anyway to search inside a specific file to look for a certain line? Im trying to set up an installer to only work for people with certain ids, and the id is located inside of a file with a bunch of other stuff.
CsComm Session Jan-24-2004 21:12:15.311 [1364] CreateSession(test@test.com,11,6)=0 attempt 1
CsComm ConnectionPool Jan-24-2004 21:12:15.334 [1364] Did not find an existing connection, attempting new connection 0,69.28.158.130:27030 for 0:1:1514131
CsComm Connection Jan-24-2004 21:12:15.334 [1364] {Cnx=0,0,69.28.158.130:27030} : Attempting new connection
Im trying to find a way to turn the 0:1:1514131 at the end of the second CsComm line into a variable so it can be matched against others built into the installer, kind of like a password. Can NSIS search for specific lines inside of other files? Even if they are in a random order and the id at the end is of a different length (like 0:1:123 or 0:0:35135)?
Afrow UK
18th September 2004 18:51 UTC
Use FileOpen, FileRead to read a file line by line.
There are various string functions on the archive which will allow you to get access to the string chunk you want.
Something like this:
Function GetCodeThingy
Push $R0
Push $R1
Push $R2
FileOpen $R0 "path_to_file.ext"
readTop:
ClearErrors
FileRead $R0 $R1
IfErrors readDone
StrLen $R2 $R1
StrCpy $R2 $R1 21 -$R2
StrCmp $R2 "CsComm ConnectionPool" 0 readTop
Push $R1
Call GetLastStrPart
Pop $R1
Push $R1
Call TrimNewLines
Pop $R1
readDone:
FileClose $R0
StrCpy $R0 $R1
Pop $R2
Pop $R1
Exch $R0
FunctionEnd
Function TrimNewlines
Exch $R0
Push $R1
Push $R2
StrCpy $R1 0
loop:
IntOp $R1 $R1 - 1
StrCpy $R2 $R0 1 $R1
StrCmp $R2 "$\r" loop
StrCmp $R2 "$\n" loop
IntOp $R1 $R1 + 1
IntCmp $R1 0 no_trim_needed
StrCpy $R0 $R0 $R1
no_trim_needed:
Pop $R2
Pop $R1
Exch $R0
FunctionEnd
Function GetLastStrPart
Exch $0
Push $1
Push $2
StrCpy $1 0
loop:
IntOp $1 $1 - 1
StrCpy $2 $0 1 $1
StrCmp $2 "" exit2
StrCmp $2 " " exit1
Goto loop
exit1:
IntOp $1 $1 + 1
StrCpy $0 $0 "" $1
exit2:
Pop $2
Pop $1
Exch $0
FunctionEnd
This should work.
Use:
[code]
Call GetCodeThingy
Pop $R0
$R0 == 0:1:1514131
-Stu
Urd
19th September 2004 00:28 UTC
Problem with function. I got this error when i tried to compile.
FileOpen expects 3 parameters, got 2.
Usage: FileOpen $(user_var: handle output) filename openmode
openmode=r|w|a
Afrow UK
19th September 2004 10:57 UTC
Oops.
Should be
FileOpen $R0 "path_to_file.ext" r
-Stu
Urd
20th September 2004 00:35 UTC
Well it compiled this time, but when I try to run it the exe aborts. I'm attaching the script and the file so you can see if I'm using it right.