/* Copyright 1984-2000 The MathWorks, Inc.  */

/*
 * hdfhx.c --- support file for HDF.MEX
 *
 * This module supports a *portion* of the HDF HX interface.  The only public
 * function is hdfHX(), which is called by mexFunction().
 * hdfHX looks at the second input argument to determine which
 * private function should get control.
 *
 * Syntaxes
 * ========
 * access_id = hdf('HX', 'create', file_id, tag, ref, extern_name,
 *                 offset, length)
 */

/* $Revision: 1.5 $  $Date: 2000/06/01 04:17:22 $ */

static char rcsid[] = "$Id: hdfhx.c,v 1.5 2000/06/01 04:17:22 joeya Exp $";

#include <string.h>

/* Main HDF library header file */
#include "hdf.h"

/* MATLAB API header file */
#include "mex.h"

/* HDFMEX utility functions */
#include "hdfutils.h"

#include "hdfhx.h"

/*
 * hdfHXcreate
 *
 * Purpose: gateway to HXcreate()
 *
 * MATLAB usage:
 * access_id = hdf('HX', 'create', file_id, tag, ref, extern_name,
 *                 offset, length)
 */
static void hdfHXcreate(int nlhs,
                        mxArray *plhs[],
                        int nrhs,
                        const mxArray *prhs[])
{
    int32 file_id;
    uint16 tag;
    uint16 ref;
    char *extern_name;
    int32 offset;
    int32 length;
    int32 H_id;
    intn status;

    haNarginChk(8, 8, nrhs);
    haNargoutChk(0, 1, nlhs);

    file_id = (int32) haGetDoubleScalar(prhs[2], "File identifier");
    tag = (uint16) haGetDoubleScalar(prhs[3], "Tag");
    ref = (uint16) haGetDoubleScalar(prhs[4], "Reference number");
    extern_name = haGetString(prhs[5], "External filename");
    offset = (int32) haGetDoubleScalar(prhs[6], "Offset");
    length = (int32) haGetDoubleScalar(prhs[7], "Length");

    H_id = HXcreate(file_id, tag, ref, extern_name, offset, length);
    if (H_id != FAIL)
    {
        status = haAddIDToList(H_id, Access_ID_List);
        if (status == FAIL)
        {
            /* Failed to add access id to the list. */
            /* This might cause data loss later, so we don't allow it. */
            Hendaccess(H_id);
            H_id = FAIL;
        }
    }

    plhs[0] = haCreateDoubleScalar((double) H_id);

}
    

/*
 * hdfHX
 *
 * Purpose: Function switchyard for the HX part of the HDF gateway.
 *
 * Inputs:  nlhs --- number of left-side arguments
 *          plhs --- left-side arguments
 *          nrhs --- number of right-side arguments
 *          prhs --- right-side arguments
 *          functionStr --- string specifying which HX function to call
 * Outputs: none
 * Return:  none
 */
void hdfHX(int nlhs,
          mxArray *plhs[],
          int nrhs,
          const mxArray *prhs[],
          char *functionStr
          )
{
    void (*func)(int nlhs, mxArray *plhs[], 
                 int nrhs, const mxArray *prhs[]);

    if (strcmp(functionStr, "create") == 0)
    {
        func = hdfHXcreate;
    }
    else
    {
        mexErrMsgTxt("Unknown HX interface function.");
    }

    (*func)(nlhs, plhs, nrhs, prhs);
}

