/*************************************************************************\
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 2002 The Regents of the University of California, as
* Operator of Los Alamos National Laboratory.
* This file is distributed subject to a Software License Agreement found
* in the file LICENSE that is included with this distribution. 
\*************************************************************************/

/*
 * $Log: not supported by cvs2svn $
 * Revision 1.5  2004/07/30 15:39:43  borland
 * Added getUnits command.
 *
 * Revision 1.4  2003/09/05 21:21:09  soliday
 * Added EXTERN
 *
 * Revision 1.3  2003/08/08 20:44:44  soliday
 * Updated code so that this is a dynamically loadable extension.
 *
 * Revision 1.2  2002/08/14 20:44:48  soliday
 * Added Open License
 *
 * Revision 1.1  1999/06/10 19:45:24  soliday
 * Moved to oag tree.
 *
 * Revision 1.15  1999/06/08 21:18:59  soliday
 * Removed compiler warnings under Linux.
 *
 * Revision 1.14  1999/05/13 14:42:13  soliday
 * Added load and save commands
 *
 * Revision 1.13  1999/04/08 15:27:51  soliday
 * Added getDescription and setDescription
 *
 * Revision 1.12  1999/03/12 16:06:29  soliday
 * Added tclSDDSConvert
 *
 * Revision 1.11  1999/03/11 14:38:53  soliday
 * Added tclSDDSExpand
 *
 * Revision 1.10  1999/03/10 21:13:22  soliday
 * Added tclSDDSCollapse
 *
 * Revision 1.9  1999/03/10 16:55:50  soliday
 * Added tclSDDSCheck
 *
 * Revision 1.8  1999/03/09 16:43:08  soliday
 * Added getRowCount
 *
 * Revision 1.7  1997/02/16 19:38:28  saunders
 * Added getColumnInformation, getParameterInformation, getArrayInformation
 * commands.
 *
 * Revision 1.6  1996/12/10 16:40:40  saunders
 * Added getArray command.
 *
 * Revision 1.5  1996/12/09 22:23:55  saunders
 * Complete set of SDDS write commands added.
 *
 * Revision 1.4  1996/09/19 15:22:37  saunders
 * Initial tested release of tclSDDS extension.
 *
 * Revision 1.3  1996/09/05 20:27:39  saunders
 * Fixed bug which comes up because command list is not NULL terminated.
 *
 * Revision 1.2  1996/08/29 15:25:49  saunders
 * Initial release with open/close/getColumn/getParameter/getNames.
 *
 * Revision 1.1  1996/08/28 21:27:03  saunders
 * Initial prototype sdds extension for tcl.
 * */

#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <math.h>

#include <tclSDDS.h>

#define VERSION "4.0"

#if ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)) || (TCL_MAJOR_VERSION > 8)
#define SDDSCONST const
#else
#define SDDSCONST
#endif

static Tcl_HashTable tclSDDSCmdTable;

typedef struct {
  char *name;     /* sdds xxx command name */
  int (*proc)();  /* command handling function */
} CMD_INFO;

static CMD_INFO tclSDDSCommands[] = {
  {"open", tclSDDSOpen},
  {"close", tclSDDSClose},
  {"getRowCount", tclSDDSGetRowCount},
  {"getDescription", tclSDDSGetDescription},
  {"getColumn", tclSDDSGetColumn},
  {"getColumnInformation", tclSDDSGetColumnInformation},
  {"getParameter", tclSDDSGetParameter},
  {"getParameterInformation", tclSDDSGetParameterInformation},
  {"getNames", tclSDDSGetNames},
  {"getUnits", tclSDDSGetUnits},
  {"getArray", tclSDDSGetArray},
  {"getArrayInformation", tclSDDSGetArrayInformation}, 
  {"defineColumn", tclSDDSDefineColumn},
  {"defineParameter", tclSDDSDefineParameter},
  {"defineArray", tclSDDSDefineArray},
  {"startPage", tclSDDSStartPage},
  {"writePage", tclSDDSWritePage},
  {"writeLayout", tclSDDSWriteLayout},
  {"setDescription", tclSDDSSetDescription},
  {"setParameter", tclSDDSSetParameter},
  {"setArray", tclSDDSSetArray},
  {"setRowValues", tclSDDSSetRowValues},
  {"setColumn", tclSDDSSetColumn},
  {"check", tclSDDSCheck},
  {"collapse", tclSDDSCollapse},
  {"expand", tclSDDSExpand},
  {"convert", tclSDDSConvert},
  {"load", tclSDDSLoad},
  {"save", tclSDDSSave},
  {(char *)NULL, (int (*)())NULL}
};


/* Function Prototypes */
int tclSDDSCmd(ClientData dummy, Tcl_Interp *interp, int argc, SDDSCONST char *argv[]);
EXTERN int Tclsdds_Init(Tcl_Interp *interp);

int tclSDDSCmd(ClientData dummy, Tcl_Interp *interp, int argc, SDDSCONST char *argv[]) {

  Tcl_HashEntry *entryPtr;
  int (*handler)();

  if (argc < 2) {
    Tcl_SetResult(interp, "sdds: must specify a sub-command", 
		  (Tcl_FreeProc *) NULL); 
    return TCL_ERROR;
  }

  entryPtr = Tcl_FindHashEntry(&tclSDDSCmdTable, argv[1]);
  if (entryPtr == NULL) {
    Tcl_SetResult(interp, "Unknown sdds command ", (Tcl_FreeProc *) NULL);
    return TCL_ERROR;
  } else {
    handler = (int(*)())Tcl_GetHashValue(entryPtr);
    return ((*handler)(interp, argc, argv));
  }

}

int Tclsdds_Init(Tcl_Interp *interp) {
  CMD_INFO *cmd;
  if (Tcl_InitStubs(interp, "8.0", 0) == NULL) {
    return TCL_ERROR;
  }
  if (Tcl_PkgRequire(interp, "Tcl", TCL_VERSION, 0) == NULL) {
    if (TCL_VERSION[0] == '7') {
      if (Tcl_PkgRequire(interp, "Tcl", "8.0", 0) == NULL) {
	return TCL_ERROR;
      }
    }
  }
  if (Tcl_PkgProvide(interp, "sdds", VERSION) != TCL_OK) {
    return TCL_ERROR;
  }

  Tcl_CreateCommand(interp, "sdds", tclSDDSCmd, (ClientData *) NULL,
		    (Tcl_CmdDeleteProc *)NULL);

  Tcl_InitHashTable(&tclSDDSCmdTable, TCL_STRING_KEYS);
  for (cmd=tclSDDSCommands ; cmd->name != NULL ; cmd++) {
    int newEntry;
    Tcl_HashEntry *entryPtr;
    entryPtr = Tcl_CreateHashEntry(&tclSDDSCmdTable, cmd->name, &newEntry);
    Tcl_SetHashValue(entryPtr, cmd->proc);
  }
  tclSDDSInit(interp);
  
  return TCL_OK;
}




