/*
 *----------------------------------------------------------------------
 *
 * Tcl_NatRow --
 *    Implements the data-mode table commands...
 *    usage: nli_row handle
 *      NOTE this will only work if you are in DATA mode, not TEXT mode
 *              (see Tcl_NatMode)
 *      results:  return list of row values
 *
 *    results:
 *      TCL_OK - handle is opened
 *      TCL_ERROR - wrong # args, or handle not opened
 */
int
Tcl_NatRow (clientData, interp, argc, argv)
    ClientData   clientData;
    Tcl_Interp  *interp;
    int          argc;
    char       **argv;
{
int hand, ncols, i, j;
Nl_status nlret;
Nl_col_type type;
char    buf[NLI_BUFF_SIZE];
char colval[256];
char line[32];
char *data;

/*
	fprintf(stderr,"start NatRow: addrs buf %x line %x colval %x START \n",
			buf, line, colval);
*/

	*buf = '\0';				
        *colval = '\0';
        *line = '\0';

/*
	fprintf(stderr,"addrs buf %x line %x colval %x bufINIT \n",
			buf, line, colval);
        fprintf(stderr,"Put a space into buf\n");	
*/

    if ((hand = nli_prologue(interp, argc, argv, 2, " handle")) == -1) {
	return TCL_ERROR;
    }

    nlret = nl_fetchrow(NatProcs[hand].nlc);

    if(nlret == NL_SUCCESS) {

/*
	fprintf(stderr,"Got a success on row fetch\n");
	fprintf(stderr,"addrs buf %x line %x colval %x gotRow \n",
			buf, line, colval);
*/

	ncols = nl_get(NatProcs[hand].nlc, NL_COLUMN_COUNT);

/*
	fprintf(stderr,"Got a count of cols %d \n",ncols);
*/

        for (i=1; i<=ncols; i++) {

	fprintf(stderr,"addrs buf %x line %x colval %x iteration %d\n",
			buf, line, colval, i);
/*
*/

	if (nl_get(NatProcs[hand].nlc, NL_COLUMN_NULL, i) != 0) {
              sprintf (colval," ");
	      fprintf(stderr,"Got a null colval for col %d\n",i);
        } else {

		type = nl_get(NatProcs[hand].nlc,NL_COLUMN_TYPE, i);
		data = (char *)nl_get(NatProcs[hand].nlc,NL_COLUMN_DATA, i);
	
/*
		fprintf(stderr,"address of data ptr col %d is %x\n",i,data);

		fprintf(stderr,"Got type code %x and data pointer %x\n",
			type, data);
*/

		switch(type) {
                case NLCOL_INT:
                  sprintf(colval,"%d", *(int *)data);
                  break;
                case NLCOL_FLOAT:
                  sprintf(colval,"%g", *(double *)data);
                  break;
                case NLCOL_CHAR:
                  sprintf(colval,"%s", data);
                  break;
                case NLCOL_DATE:
      		  for (j = 0 ; j < 32 ; j++) {
		  	line[j] = 0;
		  }
/*
		  fprintf(stderr,"col %d is a date type line size is %d\n",
			i,sizeof(line));
*/
		  fprintf(stderr,"pre-datechar addrs buf %x line %x colval %x iteration %d\n",
			buf, line, colval, i);
/*
		  sprintf(line,"A Date String");
*/
                  nl_datechar((Nl_datetime *)data, line, sizeof(line), 0);
/*
		  fprintf(stderr,"line is %x\n",line);
		  fprintf(stderr,"We survived the the datechar call\n");
		  fprintf(stderr,"i addr is %x\n",&i);
*/
		  fprintf(stderr,"post-datechar addrs buf %x line %x colval %x iteration %d\n",
			buf, line, colval, i);
/*
		  for (j=0;j<32;j++) fprintf(stderr,"line[%d] = %d;%c\n",
			j,line[j],line[j]);
		  fprintf(stderr,"date string is %s\n",line);
*/
                  sprintf(colval,"%s", line);
  		  fprintf(stderr,"post-sprintf into colval\n");
		  fprintf(stderr,"i addr is %x\n",&i);
                  break;
                default:
                  fprintf(stderr,"Unknown datatype (%d)  ",type);
		  return TCL_ERROR;
		}

    }
    fprintf(stderr,"Got a colvalue %s for col %d\n",colval,i);
/*
*/
    sprintf(buf,"%s {%s}",buf,colval);

    }


    } else {

    sprintf(buf," ");

    }

    Tcl_SetResult(interp,buf,TCL_VOLATILE);
    return TCL_OK;

}
