Old 26th October 2015, 14:40   #1
SADiE
Junior Member
 
Join Date: Oct 2015
Posts: 8
Can you search for multiple strcontains?

Me again:

Can you look for multiple strcontains in a file?

I've tried a couple of variations, what I want to do is below, but it only ever seems to find the first ${strcontains}, and then skips over the rest.

The file itself has multiple lines. Some of the lines will have the text I am searching for, and some won't.
A typical file will look like this:

STAGES.TXT

Stage1true
Stage2true
pmp123456
EXP000479
cusMarkatSADiE
Stage3true
EOF


iffileexists "$Temp\stages.txt" openfile nofile

openfile:

Fileopen $9 "$Temp\Stages.txt" r

${ForEach} $i 1 $R0 + 1
Fileread $9 $R4 10
pop $R4


${strcontains} $serial "EXP" $R4
STRCMP $serial "" notfound
strcpy $serial $R4
messagebox mb_ok " Serial number is: $serial"


${strcontains} $order "pmp" $R4
STRCMP $order "" notfound
strcpy $order $R4
messagebox mb_ok " Order number is: $order"

${strcontains} $customer "cus" $R4
STRCMP $customer "" notfound
strcpy $customer $R4
messagebox mb_ok " Customer is: $customer"

goto done

notfound:
; I never found nothin'

done:

${Next}


What am I missing?
SADiE is offline   Reply With Quote
Old 26th October 2015, 15:44   #2
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,442
And strcontains is a macro you wrote? found somewhere? from the wiki?

If the line always starts with the part of the string you are looking for it is better to just use StrCpy with the maxlen parameter to copy the first 3 characters and then StrCmp that...

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 26th October 2015, 16:06   #3
SADiE
Junior Member
 
Join Date: Oct 2015
Posts: 8
Oh Lord, I can barely write my own name, let alone write a macro... It's one I found ages ago http://nsis.sourceforge.net/StrContains

Yes you're right Anders, I wasn't thinking.

I already have the strcontains function loaded to check for the occurance of a vendor Id (To see if a particular USB device is plugged in), so I figured I'd just reuse the function, but it only seems to find the first instance... i.e. it finds the serial number, and ignors the rest.

I'll try it another way. Thanks
SADiE is offline   Reply With Quote
Old 26th October 2015, 17:16   #4
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,442
pop $R4 in your script does not look right to me. Passing 10 as the maxlen also seems strange unless STAGES.TXT does not contain any newlines at all but then "cusMarkatSADiE" would be too long?

If each entry is on their own line I would just do:

PHP Code:
!include LogicLib.nsh
Section
InitPluginsDir
FileOpen 
$"$PluginsDir\Stages.txt" Generate example file
FileWrite 
$"Stage1true$\r$\n"
FileWrite $"Stage2true$\n" Unix newline
FileWrite 
$"pmp123456$\r" Mac newline
FileWrite 
$"EXP000479$\r$\n"
FileWrite $"$\r$\n" empty line for testing
FileWrite 
$"cusMarkatSADiE$\r$\n"
FileWrite $"Stage3true$\r$\n"
FileClose $0

Test file has been createdwill open and parse it now:
FileOpen $"$PluginsDir\Stages.txt" r
loop
:
    
FileRead $$1
    StrCmp 
$"" done
    killnewlines
:
        
StrCpy $$"" -1
        StrCmp 
$"$\n" +3
        StrCpy 
$"$1" -1
        
Goto killnewlines
        StrCpy 
$$"" -1
        StrCmp 
$"$\r" +3
        StrCpy 
$$-1
        
Goto killnewlines
    DetailPrint 
"Parsing line:|$1|"
    
StrCpy $$1 3 Copy the first 3 characters in the line
    
${If} $== "EXP"
        
StrCpy $$"" 3
        DetailPrint EXP
=$3
    
${ElseIf} $== "cus"
        
StrCpy $$"" 3
        DetailPrint cus
=$3
    
${EndIf}
    Goto 
loop
done
:
FileClose $0
SectionEnd 

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 26th October 2015, 18:14   #5
SADiE
Junior Member
 
Join Date: Oct 2015
Posts: 8
Thanks Anders, that makes sense. I should have increased the maxlen. It was initially there when I was on;y looking for stagextrue.

I'll have another go at this when I get back from holiday Yay Holiday.

Thanks for your help.

Mark
SADiE is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump