
! script name: vproperty.d/grpsearchstr
! var_topic: search a group of info packets for a string

!A
!A NAME:
!A
!A 	String Search Packet Group
!A
!A DESCRIPTION:
!A
!A 	This script searches a group of info packets for a specific string.
!A
!B INSTRUCTIONS:
!B
!B 	PACKET GROUP NAME: Select the info packet group.
!B
!B 	SELECT DATA FIELD: Select the data field of the info packet class that
!B 	you wish to search.
!B
!B 	SEARCH STRING: Enter the string that you wish to search for.
!B
!C NOTES:
!C
!C 	This function creates a new group of info packets that satisfy the
!C 	search criteria.  This script will delete all info packets which do
!C 	not meet the search criteria. It will also delete the container group.
!C 	This script should be used to filter a group of info packets until only
!C 	the desired info packets remain. Narrow your search with each new group
!C 	of packets.
!C
!C 	The 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 group search")

DIM Destination$[256]
DIM Answer$[256]
DIM A$[256]
DIM Name$[256]
DIM Group_name$[256]
DIM Field_name$[256]
DIM Search_str$[256]
DIM Class_name$[256]
DIM Namelist$[32000]
DIM D_label$[256]
DIM Choice$[256]
DIM Mask$[32]
DIM Var_name$[256]

Var_object=CheckVarObject
IF ExitScript(Destination$) THEN Leave

Perfect_match= WsTrue
First_match= WsFalse

Start:!
DeActivateUndo
Stat=ListGrpNames("PACKET GROUPS","selected group","",WsGrpOfProperties)
IF Stat=1 THEN Leave
Stat=SetInputFormat("a99","")
Stat=GetValue("PACKET GROUP NAME",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Leave
Prop_group=FindNamedGrp(Answer$)
IF NOT ValidDependency(Prop_group) THEN Start
Stat= EntityNameCheck(Prop_group,Group_name$)
Stat=SetInputFormat("n4","")
LowerSelectBox

List_fields:!
E1= GroupElement(Prop_group,1)
IF NOT ValidGeometry(E1) THEN Start
Namelist$= DataFieldListStr(E1,":",",",WsNoOrder)

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_string
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("PACKET SEARCH GROUP: "&Group_name$)
V1= StringConstant(Field_name$)
IF Variable_input THEN
	V2=FindNamedVar(Var_name$,WsVariationVars)
ELSE
	V2= StringConstant(Search_str$)
END IF
V3= FreeParameter("Perfect match",Perfect_match)
G1= PGrpSearchStrGrp(Group_name$,V1,V2,V3)
Stat= AutoNameGroup(G1,Name$)
RecordFunctionOff
IF ValidGeometry(G1) THEN
RecordFunctionOn
	G2= SubtractGrps(Group_name$,G1)
	Stat= DeleteEntity(Group_name$)
	Stat= DeleteGroup(G2,WsTrue)
RecordFunctionOff
END IF
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",Group_name$)
END IF


GOTO Start

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


