/*****************************************************************************
******************************************************************************
**<AUTO>
**
** FILE:	tclFieldFile.c
**
**<HTML>
**	This file contains TCL functions to write tsFieldFiles.
**</HTML>
**</AUTO>
**
**
** ENVIRONMENT:
**	ANSI C.
**
******************************************************************************
******************************************************************************
*/

#include <dervish.h>

TBLCOL* taFieldTblColInit(int nRows);


/*============================================================================
**<AUTO EXTRACT>
**
** TCL VERB: fieldTblColInit
**
**<HTML>
**	Create a TBLCOL for a "tsField" file, containing all information
**	for a single field that is to be exported to the SX.  The number of
**	rows in the TBLCOL is specified by the argument "nRows".
**	The handle to the newly created TBLCOL is returned.
**</HTML>
**
**</AUTO>
**============================================================================
*/
static char fieldTblColInitCmd[] = "fieldTblColInit";
static int fieldTblColInitFlg = FTCL_ARGV_NO_LEFTOVERS;
static ftclArgvInfo fieldTblColInitTbl[] = {
   {NULL, FTCL_ARGV_HELP, NULL, NULL,
    "Create an empty tsField file TBLCOL\n"},
   {"[nRows]", FTCL_ARGV_INT, NULL, NULL,
    "Number of rows in the TBLCOL (defaults to one)"},
   {NULL, FTCL_ARGV_END, NULL, NULL, NULL}
};

static int
tclFieldTblColInit (ClientData clientData,
		    Tcl_Interp *interp,
		    int argc,
		    char **argv)
{
  int nRows = 1;
  int status;

  TBLCOL *tblCol;
  char vName[HANDLE_NAMELEN];

  /* Parse the arguments */
  fieldTblColInitTbl[1].dst = &nRows;
  if ((status = shTclParseArgv(interp, &argc, argv, fieldTblColInitTbl,
			       fieldTblColInitFlg, fieldTblColInitCmd))
      != FTCL_ARGV_SUCCESS)
    return status;
   
  /* Do it */
  if ((tblCol = taFieldTblColInit(nRows)) == NULL)
    {
      shTclInterpAppendWithErrStack(interp);
      return TCL_ERROR;
    }

  /* Return a handle to the TBLCOL */
  if (shTclHandleNew(interp, vName, "TBLCOL", (void *)tblCol) != TCL_OK)
    {
      /* Error.  Delete the TBLCOL and return an error. */
      shTblcolDel(tblCol);
      Tcl_AppendResult(interp, fieldTblColInitCmd,
		       ": error allocating TBLCOL TCL handle", NULL);
      return TCL_ERROR;
    }
  Tcl_SetResult(interp, vName, TCL_VOLATILE);
  return TCL_OK;
}

/*****************************************************************************
 *
 * Declare my new tcl verbs to tcl
 */
void
taTclFieldFileDeclare(Tcl_Interp *interp) 
{
  char *tclHelpFacil = "ta";
  shTclDeclare(interp, fieldTblColInitCmd,
	       (Tcl_CmdProc *)tclFieldTblColInit,
	       (ClientData) 0,	(Tcl_CmdDeleteProc *)NULL, tclHelpFacil,
	       shTclGetArgInfo(interp, fieldTblColInitTbl,
			       fieldTblColInitFlg,
			       fieldTblColInitCmd),
	       shTclGetUsage(interp, fieldTblColInitTbl,
			     fieldTblColInitFlg,
			     fieldTblColInitCmd));
}

