
! script name: vproperty.d/searchstring
! var_topic: search an info packet class directory for a string

!A
!A NAME:
!A
!A 	String Search External Packets
!A
!A DESCRIPTION:
!A
!A 	This script searches an info packet class directory for a specified 
!A 	string.  This is useful when looking for a specific piece of 
!A 	information that may be contained in a large info packet or a large
!A 	group of info packets.
!A
!B INSTRUCTIONS:
!B
!B 	PACKET CLASS DIRECTORY: Select the packet class directory.
!B
!B 	SELECT DATA FIELD: Select the data field of the class that
!B 	you wish to search.
!B
!B 	SEARCH STRING: Enter the string that you wish to search for.
!B
!B 	MATCHING PACKET FILE NAME: Select the name of the matching 
!B 	info packet file that you wish to see.
!B
!C NOTE:
!C
!C 	This function creates a group of info packets. The group contains all
!C 	of the elements which satisfy the search criteria.  Your search can be
!C 	refined by applying additional criteria and searching this group.
!C 	Narrow your search with each new group of packets.
!C
!C 	This script contains the following internal settings.
!C
!C 		Perfect_match= WsTrue: String must match exactly.
!C 		Perfect_match= WsFalse: String need only contain search string.
!C 		First_match= WsTrue: Returns name of first match found.
!C 		First_match= WsFalse: Returns all matching names.
!C
!# see also - htdocs/help.d/query.htm
!# see also - htdocs/help.d/property.htm

Stat=ChangeLabelStr("ScriptNameStatus","Function: property string search")

DIM Destination$[512]
DIM Answer$[512]
DIM A$[512]
DIM Name$[512]
DIM Field_name$[512]
DIM Search_str$[512]
DIM Prop_dir$[512]
DIM Class_dir$[512]
DIM Class_dir_name$[512]
DIM Filename$[512]
DIM Class_name$[512]
DIM Prop_file$[512]
DIM Namelist$[32000]
DIM D_label$[512]
DIM Choice$[512]
DIM Mask$[32]
DIM Var_name$[512]
DIM Current_dir$[512]
DIM Ques$[512]

Var_object=CheckVarObject
IF ExitScript(Destination$) THEN Leave

Stat= NamedVarStr("properties dir",WsSystemVars,"/usr/weber/database/properties/",Prop_dir$)

Perfect_match= WsTrue
First_match= WsFalse

Start:!
DeActivateUndo
D_label$="PACKET CLASSES"
Choice$="CLASS SELECTED"
Ques$= "PACKET CLASS DIRECTORY  List  "
Answer$= SelectDirectory(Prop_dir$,"*",Ques$,D_label$,Choice$,"",Current_dir$)
IF (ExitScript(Destination$)) THEN Leave
IF  Answer$= "CANCEL" THEN Leave
Answer$= TRIM$(Answer$)
Class_dir$= Answer$
Class_dir_name$= BaseNamePath(Answer$,Prop_dir$)

List_fields:!
Namelist$=DirectoryNamesStr(Class_dir$,"*")
Prop_file$= DataFieldStr(Namelist$,",",1)
Filename$= FileNameString(Class_dir$,Prop_file$)
Namelist$= ExtFieldListStr(Filename$,":",",",WsAscending)

D_label$="DATA FIELDS"
Choice$="DATA FIELD SELECTED"
Mask$="*"
Stat=GeneralListBox(D_label$,Choice$,Namelist$)

Data_field:stat= SetInputFormat("a99","")
stat= GetValue("SELECT DATA FIELD ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF NOT LEN(Answer$) THEN Data_field
LowerSelectBox
SELECT Answer$
CASE "CANCEL"
	GOTO Start
END SELECT
Field_name$= Answer$&":"

Search_string:stat= SetInputFormat("a99","")
stat= GetValue("SEARCH STRING , Variable ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF NOT LEN(Answer$) THEN Search_str
Answer$= TRIM$(Answer$)
SELECT UPC$(Answer$)
CASE "VARIABLE","V"
        Stat=UserSelectBox("VARIABLES","selected variable",WsVariationVars,WsVarSelect)
        IF Stat=1 THEN Leave
        Stat=SetInputFormat("a99","")
        Stat=GetValue("NAME OF VARIABLE",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        IF (Answer$="CANCEL") THEN Start

        N=POS(Answer$,"=")
        IF (N>1) THEN
                 Answer$=Answer$[1,N-1]
                 Answer$=TRIM$(Answer$)
        END IF
        Var_name$=Answer$
        V1=FindNamedVar(Var_name$,WsVariationVars)
        IF NOT ValidGeometry(V1) THEN Start
        Variable_input=1
        LowerSelectBox
CASE ELSE
	Search_str$= Answer$
END SELECT


Make_prop_record:!
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat=RecordDescr("INFO PACKET SEARCH GROUP: "&Class_dir_name$&" : "&Name$)
V1= StringConstant(Prop_dir$)
V2= StringConstant(Class_dir_name$)
V1= StringConstant(FileNameString(V1,V2))
V2= StringConstant(Field_name$)
IF Variable_input THEN
	V3=FindNamedVar(Var_name$,WsVariationVars)
ELSE
	V3= StringConstant(Search_str$)
END IF
V4= FreeParameter("Perfect match",Perfect_match)
G1= PDirSearchStrGrp(V1,V2,V3,V4)
Stat= AutoNameGroup(G1,Name$)
RecordFunctionOff
CloseVarRecord
IF NOT ValidGeometry(G1) THEN
        DelLastVarRecord
END IF

IF GroupCount(G1) THEN 
Namelist$=""
FOR I=1 TO GroupCount(G1)
        E1= GroupElement(G1,I)
        A$= UserDataStr(E1,"Class name:")
        B$= UserDataStr(E1,"Name:")
        Namelist$= Namelist$&VAL$(I)&")  "&A$&":  "&B$&CHR$(10)
NEXT I
stat= OutputString("tempdata",Namelist$,WsNewFile)
Stat=MessageFileBox("tempdata",Class_dir_name$&" : "&Name$)
END IF


GOTO Start

Leave:!
LowerSelectBox
Stat=SetInputFormat("n4","")
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END


