Archive: string search question


string search question
I have a question on how to do this. I am a n00b still so sorry for this sounding a little dumb.

I have a string, "C:\Windows;C:\windows\system;c:\blarg;c:\foobar"

i want to do is check if c:\foobar is in the string.
if it is go on.
if it isnt, do something; then go on.

thanks for any help..


There is a handy function in the User Manual (Appendix C) called StrStr which can be used to find the first occurance of a string. See example


Section ""

Push "this is a string with foobar in it"
Push "foobar"
Call StrStr
Pop $R0

StrCpy $R0 $R0 6 ; truncate string to first 6 chrs
StrCmp "foobar" $R0 true false ;compare strings

true:
DetailPrint "foobar is present"
Goto End

false:
DetailPrint "foobar is not present"
Goto End

End:
SectionEnd


; StrStr
; input, top of stack = string to search for
; top of stack-1 = string to search in
; output, top of stack (replaces with the portion of the string remaining)
; modifies no other variables.
;
; Usage:
; Push "this is a long ass string"
; Push "ass"
; Call StrStr
; Pop $R0
; ($R0 at this point is "ass string")

Function StrStr
Exch $R1 ; st=haystack,old$R1, $R1=needle
Exch ; st=old$R1,haystack
Exch $R2 ; st=old$R1,old$R2, $R2=haystack
Push $R3
Push $R4
Push $R5
StrLen $R3 $R1
StrCpy $R4 0
; $R1=needle
; $R2=haystack
; $R3=len(needle)
; $R4=cnt
; $R5=tmp
loop99:
StrCpy $R5 $R2 $R3 $R4
StrCmp $R5 $R1 done99
StrCmp $R5 "" done99
IntOp $R4 $R4 + 1
Goto loop99
done99:
StrCpy $R1 $R2 "" $R4
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Exch $R1
FunctionEnd


okay i am still unsure.

Push $R9
ReadRegStr $R9 HKLM \
"SYSTEM\CurrentControlSet\Control\Session Manager\Environment" Path

if $R9 contains c:\mypath
then go on
if it doesnt contain it
doMyFunction
then go on

thx sorry i am really a noob here i still use HM NIS edit to autogenerate the code. and now i got stuck on this here.


If the string doesn't have foobar in it the StrStr will return "".

-Stu


okay i get error with StrStr....

this is what i got now

Push $R9
ReadRegStr $R9 HKLM \
"SYSTEM\CurrentControlSet\Control\Session Manager\Environment" Path

${StrStr} $0 $R9 "foo"
StrCmp $0 "" true false

true:
; do nothing go on
Goto End

false:
Push "PATH"
Push "%SystemRoot%\foo"
Call AddToPath
Goto End

End:
File "help.tgz"
SetDetailsPrint textonly
DetailPrint "Installing Help System..."
SetDetailsPrint none
...snip....
SectionEnd


error:

Error in script "C:\Documents and Settings\jweinraub\Desktop\Installers\636.26\636_26.nsi" on line 27 -- aborting creation process


line 27 of my installer: !include "StrStr.nsh"

line 3 of strstr.nsh: !macro StrStr ResultVar String SubString

thnx


i mean this but it still doesnt work, i use the AddToPath function found on the nsis.sf.net wiki.


Push $R9
ReadRegStr $R9 HKLM \
"SYSTEM\CurrentControlSet\Control\Session Manager\Environment" Path

Push $R9
Push "foo"
Call StrStr
Pop $R0

; ${StrStr} $0 $R9 "foo"

StrCmp $R0 "" true false

true:
; doesn't have foo so add it
Push "PATH"
Push "%SystemRoot%\foo"
Call AddToPath
Goto End

false:
; do nothing go on
Goto End

End:
File "help.tgz"
SetDetailsPrint textonly
...

the adding isnt happening so i dunno if the addtopath function isnt working or my logic above isnt working.