#include <stdlib.h>
#include "stack-c.h"
#include "api_scilab.h"
#include "Scierror.h"
#include "sciprint.h"
#include "string.h"

#include "Strahler.h"
#include "Erreur.h"

/***************************************
 * SCILAB function : StrahlerOrdering  *
 ***************************************/

int gtw_StrahlerOrdering ( char * fname )
{
    int m,n,l;
	int HandleRaster;
    int HandleTree;
	float threshold;
	int nsrc;
	int * SRC;
    int un=1;

    CheckRhs(3,3);
    CheckLhs(1,1);
    
    /* Variable HandleTree */

    GetRhsVar(1,"i",&m,&n,&l);
    CheckScalar(1,m,n);
    HandleTree = *istk(l);
	
    /* Variable threshold */

    GetRhsVar(2,"r",&m,&n,&l);
    CheckScalar(2,m,n);
    threshold = *sstk(l);
	
    /* Variable HandleRaster */

    GetRhsVar(3,"i",&m,&n,&l);
    CheckScalar(3,m,n);
    HandleRaster = *istk(l);
	
	/* Appel à l'API */

	if ( StrahlerOrdering ( HandleTree, threshold , HandleRaster , & nsrc , & SRC ) )
		 Scierror (999, GetErreur());
		 
	CreateVarFromPtr ( Rhs + 1 ,  "i", & nsrc , & un , & SRC );	 	 
	free(SRC);
	
	LhsVar(1) = Rhs + 1;
	
	return(0);
}

/*****************************************
 * SCILAB function : MakeStrahlerStreams *
 *****************************************/

int gtw_MakeStrahlerStreams ( char * fname )
{
    int m,n,l,l_mlist,i;
    int HandleTree;
    float threshold;
    int ** StreamCells ;
	int * StreamConnect , * StreamOrder , * StreamLength ;
	int nstreams;
    int un=1;
	char ** Labels;

    CheckRhs(2,2);
    CheckLhs(3,3);
		
    /* Variable HandleTree */

    GetRhsVar(1,"i",&m,&n,&l);
    CheckScalar(1,m,n);
    HandleTree = *istk(l);

    /* Variable threshold */

    GetRhsVar(2,"r",&m,&n,&l);
    CheckScalar(2,m,n);
    threshold = *sstk(l);

    /* Appel a l'API */
	
    if ( MakeStrahlerStreams ( HandleTree , & threshold , & nstreams , & StreamLength , & StreamOrder , & StreamConnect , & StreamCells ) )
        Scierror (999, GetErreur());
	
	// Création des labels
		
	m = nstreams + 1;
	
	if ( !(Labels = (char **) malloc ( m * sizeof(char *) ) ) )
		Scierror (999, "gtw_MakeStrahlerStreams : echec de l'allocation des chaines de caracteres\n");
	
	Labels[0] = (char *) malloc ( 4 * sizeof(char) );
	strncpy (Labels[0],"Type",4);
	
	for(i=0;i<nstreams;i++)
	{
		Labels[i+1] = (char *) malloc ( 6 * sizeof(char) );
		strncpy (Labels[i+1],"Stream",6);
	}
	
	// Création d'un object de type mlist
	
	CreateVar ( Rhs + 1 , "m", & m, & un , & l );	

	// Affectation des labels
	
	CreateListVarFromPtr ( Rhs + 1 ,  1 , "S" , & m , & un , Labels );
	
	// Création des composantes de la liste
	
	for (i=0;i<nstreams;i++)
	{
		CreateListVarFromPtr ( Rhs + 1 ,  i + 2 , "i" , & StreamLength[i] , & un , & StreamCells[i] );
	}

	/* Création du tableau contenant l'ordre de chaque drain */
	
	CreateVarFromPtr ( Rhs + 2 ,  "i", & nstreams , & un , & StreamOrder );

	/* Création du tableau contenant la cellule aval de chaque drain */
	
	CreateVarFromPtr ( Rhs + 3 ,  "i", & nstreams , & un , & StreamConnect );
	
	/* Affectation des sorties */
	
    LhsVar(1) = Rhs + 1;
	LhsVar(2) = Rhs + 2;
	LhsVar(3) = Rhs + 3;

	/* Désaffectation des pointeurs */
	
	for (i=0;i<nstreams;i++)
	{
			free(StreamCells[i]);
	}				
	
	free(StreamCells);
	free(StreamLength);
	free(StreamOrder);
	free(StreamConnect);

    return(0);
}
