
! script name: vdatatable.d/exporttable
! var_topic: export a table of data

!A
!A NAME:
!A
!A 	Export Table ASCII    VARIATIONAL
!A
!A DESCRIPTION:
!A
!A	Exports a table of data as a tab separated ascii file.
!A
!B INSTRUCTIONS:
!B
!B 	EXPORT FILE NAME: Enter the name of the file.
!B
!B 	FILE EXISTS: OVERWRITE  Yes or No:
!B
!B 		Y - Enter Y to overwrite the existing file.
!B 		N - Enter N to enter a different file name.
!B
!C NOTE:
!C	
!C	The default location for exported ASCII tables is:	
!C
!C		/usr/weber/database/tables
!C
!# see also - htdocs/help.d/datatables.htm

Stat=ChangeLabelStr("ScriptNameStatus","Function: export table")

DIM Answer$[256]
DIM A$[4096]
DIM B$[4096]
DIM Filename$[256]
DIM Destination$[256]
DIM Table$[256]
DIM Tab$[32]
DIM Lf$[32]
DIM Sdevice$[256]
DEG

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

ActivateUndo
Tab$= CHR$(9)
Lf$= CHR$(10)

Stat= NamedVarStr("datatables dir",WsSystemVars,"/usr/weber/database/tables/",Sdevice$)

Start:!
Filename$=""

Ask_file:!
Stat=SetInputFormat("a99","")
Stat=GetValue("EXPORT FILE NAME",Answer$)
IF ExitScript(Destination$) THEN Leave
Filename$=Answer$

A$= FileNameString(Sdevice$,Answer$)
F_type= DoesFileExist(A$)
SELECT F_type
CASE 1
        Stat=SetInputFormat("a1",",Y,N,")
        Stat=GetValue("FILE EXISTS: OVERWRITE  Yes or No",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        IF Answer$= "N" THEN
                GOTO Ask_file
        END IF
CASE 2
        Stat=SetInputFormat("a1",",P,")
        Stat=GetValue("COPY NAME IS A DIRECTORY: Proceed ",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        GOTO Ask_file
END SELECT


Pick_table:stat= SetInputFormat("n4","")
Table1=PickEntity("PICK TABLE")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Table1) THEN Pick_table
Stat= EntityNameCheck(Table1,Table$)


RecordFunctionOn
Stat= RecordDescr("EXPORT TABLE: "&Filename$)
V1= StringConstant(Sdevice$)
V2= StringConstant(Filename$)
V3= StringConstant(FileNameString(V1,V2))
Stat= RemoveFile(V3)
Stat= ExportTable(Table$,V3)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

ActivateUndo
Stat= AdvanceUndoNumber("export table")
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
GOTO Start

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