
! script name: vproperty.d/extpropvar
! var_topic: make a variable from a value in a external info packet field

!A
!A NAME:
!A
!A 	Variable from an External Field
!A
!A DESCRIPTION:
!A
!A 	An external info packet field is an info packet that is not currently 
!A 	being used as an info packet entity.  It remains external to the program
!A 	as an ASCII data file.  This script assigns a dependent variable to an
!A 	external info packet.  Numeric data becomes a numeric variable, String
!A 	data becomes a string variable. This allows you to variationally
!A 	reference data not currently being used by the system.
!A
!B INSTRUCTIONS:
!B
!B 	VARIABLE NAME: Enter the name of the variable.
!B
!B 	INFO PACKET CLASS DIRECTORY: Select the packet class directory.
!B
!B 	EXISTING INFO PACKET FILE NAME: Select the info packet file you want 
!B 	referenced from the selection box.
!B
!B 	SELECT DATA FIELD: Pick the data field from the selection box.
!B
!# see also - htdocs/help.d/property.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: external property field variable ")

DIM Answer$[512]
DIM A$[512]
DIM B$[512]
DIM Name$[512]
DIM Var_name$[512]
DIM Descr$[512]
DIM Element$[512]
DIM Destination$[512]
DIM Prop_name$[512]
DIM Field$[512]
DIM Namelist$[32000]
DIM Field_id$[512]
DIM Field_data$[512]
DIM D_label$[512]
DIM Choice$[512]
DIM Filename$[512]
DIM Prop_class$[512]
DIM Class_dir$[512]
DIM Prop_dir$[512]
DIM Prop_file$[512]
DIM Current_dir$[512]
DIM Ques$[512]
DEG

Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

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

Start:!
DeActivateUndo

Name_loop:!
stat= SetInputFormat("a99","")
Stat=GetValue("VARIABLE NAME",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Var_name$= Answer$
IF NOT ValidVariableName(Var_name$) THEN
        stat= SetInputFormat("a99","")
        Stat=GetValue("INVALID VARIABLE NAME, Proceed",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        GOTO Name_loop
END IF


Ask_class:!
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$= BaseNamePath(Answer$,Prop_dir$)
Prop_class$= PathNameFile(Answer$)

Ask_file:!
D_label$= Prop_class$
Choice$= Prop_class$&" SELECTED"
Stat= DirListBox(FileNameString(Prop_dir$,Class_dir$),"*",D_label$,Choice$,WsAscending)

File_name:stat= SetInputFormat("a99","")
stat= GetValue("PACKET FILE NAME ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Start
IF NOT LEN(Answer$) THEN File_name
Prop_file$= Answer$
Filename$= FileNameString(Prop_dir$,Class_dir$,Prop_file$)

Id_number=GetExtUserValue(Filename$,"Class id number:")
Prop_name$= ExtUserDataStr(Filename$,"Name:")
Prop_ent= ReadUserData(Id_number,Name$,Filename$)


List_fields:!
Namelist$= DataFieldListStr(Prop_ent,":",",",WsNoOrder,WsTrue)
D_label$="DATA FIELDS"
Choice$="DATA FIELD SELECTED"
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
IF POS(Answer$,"=") THEN
        Field_id$= DataFieldStr(Answer$,"=",1)
	Field_id$= Field_id$&":"
        Field_data$= DataFieldStr(Answer$,"=",2)
        Field_data$= TRIM$(Field_data$)
ELSE
        Field_data$=""
END IF
GOTO Make_variable

Make_variable:!
IF IsStringNumeric(Field_data$) THEN
	Numeric_entry=1
ELSE
	Numeric_entry=0
END IF

A$= UserDataStr(Prop_ent,"Class name:")
B$= UserDataStr(Prop_ent,"Name:")
Descr$= "VARIABLE: "&A$&":"&B$&":"&Field_id$
Stat= DeleteEntity(Prop_ent)

RecordFunctionOn
Stat= RecordDescr(Descr$)
V1= StringConstant(Prop_dir$)
V2= StringConstant(Class_dir$)
V3= StringConstant(Prop_file$)
V1= StringConstant(FileNameString(V1,V2,V3))
V2= StringConstant(Field_id$)
IF Numeric_entry THEN
        V3=NumericVariable(Var_name$,WsVariationVars,GetExtUserValue(V1,V2))
ELSE
        V3=CreateVariable(Var_name$,WsVariationVars,ExtUserDataStr(V1,V2))
END IF
stat= SetVariableDescr(V3,"External Property field")
RecordFunctionOff
CloseVarRecord

GOTO Start




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

