#define       GLC_SOURCE

     /*****************************************************************/
     /* IDENTIFICATION       | GLC.C                                  */
     /*****************************************************************/
     /* DEPARTEMENT          | Amiga Source Editeur Developpement     */
     /* AUTHOR               | FONTANIN A.                            */
     /*****************************************************************/
     /* PACKAGE TYPE         | Gestionnaire de Listes chainées        */
     /*****************************************************************/


#include <exec/types.h>
#include <exec/memory.h>
#include <stdio.h>
#include <string.h>

#include <DEV/GLC.H>



/*                               STRUCTURE:                                  */
/*                               ----------                                  */

/* Fonction     InsereListe .......................................PART 1    */
/* Fonction     ConcateneListe ....................................PART 2    */
/* Fonction     RetireListe .......................................PART 3    */
/* Fonction     EffaceListe .......................................PART 4    */
/* Fonction     InitialiseListe ...................................PART 5    */
/* Fonction     AllerAElement .....................................PART 6    */
/* Fonction     Deplacement .......................................PART 7    */












/* Fonction      InsereListe .......................................PART 1
*/

struct SL * InsereListe(
/*---------------------*/
struct SL * Lap,
struct SL *Lins,
struct SL *Lancre)
    {
    register struct SL *Lif;
    
    Lif = AllerAElement(AA_FIN, Lins);
    
    if(Lap != Lancre) /* Est-ce la téte de la liste */
        /* NON */
        Lap->PtrPrec->PtrSuiv = Lins;
    else
        /* OUI */
        Lancre    = Lins;
    Lins->PtrPrec = Lap->PtrPrec;
    Lap->PtrPrec  = Lins;
    Lif->PtrSuiv  = Lap;
    return(Lancre);
    }



/* Fonction      ConcateneListe ....................................PART 2
*/

void ConcateneListe(
/*-----------------*/
struct SL *Ldest,
struct SL *Lsource)
    {
    register struct SL * LdestF;
    
    LdestF = AllerAElement(AA_FIN, Ldest);
    
    LdestF->PtrSuiv  = Lsource;
    Lsource->PtrPrec = Ldest;
    }


/* Fonction      RetireListe .......................................PART 3
*/

struct SL * RetireListe(
/*---------------------*/
struct SL *Ld,
struct SL *Lf,
struct SL *Lancre)
    {
    
    if(Lancre == Ld) /* est-ce la téte de la liste */
        {
        if(Lf->PtrSuiv)
            Lf->PtrSuiv->PtrPrec = NULL;
        Lancre = Lf->PtrSuiv;
        }
    else
        {
        if(Lf->PtrSuiv)
            Lf->PtrSuiv->PtrPrec = Ld->PtrPrec;
        Ld->PtrPrec->PtrSuiv = Lf->PtrSuiv;
        }
    
    /* Transformer la sous-liste en liste. */
    
    Ld->PtrPrec = NULL;
    Lf->PtrSuiv = NULL;
    
    return(Lancre);
    }


/* Fonction      EffaceListe .......................................PART 4
*/

struct SL * EffaceListe(
/*---------------------*/
struct SL *Ld1,
BOOL      Efface)
    {
    register struct SL *Ld2;
    
    while(Ld1)
        {
        Ld2 = Ld1->PtrSuiv;
        if(Efface == EFF_DATA)
            {
            if(Ld1->PtrData)
                FreeMem(Ld1->PtrData, strlen(Ld1->PtrData) + 1);
            }
        FreeMem(Ld1, sizeof(struct SL));
        Ld1 = Ld2;
        }
    return(NULL);
    }


/* Fonction      InitialiseListe ...................................PART 5
*/

struct SL * InitialiseListe(
/*-------------------------*/
short int NombreElement)
    {
    register struct SL *Lancre, *L1, *L2 = NULL;
    register short int iter;
    
    for(iter=1;iter<=NombreElement;iter++)
        {
        L1 = (struct SL *) AllocMem(sizeof(struct SL), MEMF_PUBLIC | MEMF_CLEAR);
        if(L1) /* Allocation mémoire réussit */
            {
            if(L2)  /* est-ce la premiére Alloc ? */
                /* NON */
                L2->PtrSuiv = L1;
            else
                Lancre = L1;       /* OUI */
            L1->PtrPrec = L2;
            L2 = L1;
            L1->PtrSuiv = NULL;
            L1->PtrData = NULL;
            }
        else
            { /* Pb d'Allocation Mémoire */
            Lancre = EffaceListe(Lancre, EFF_RIEN);
            break;
            }
        }
    return(Lancre);
    }


/* Fonction      AllerAElement .....................................PART 6
*/

struct SL * AllerAElement(
/*-----------------------*/
short int Numero,
struct SL *Lancre)
    {
    register short int Pos=1;
    register struct SL *La = NULL;
    
    while(Lancre)
        {
        if(Pos == Numero)
            break;
        La = Lancre;
        Lancre = Lancre->PtrSuiv;
        Pos++;
        }
    if(Numero != AA_FIN)
        return(Lancre);
    else
        return(La);
    }

/* Fonction      Deplacement .......................................PART 7
*/

struct SL  *Deplacement(
/*---------------------*/
short int NbDep,
short int Direction,
struct SL *Lancre)
    {
    register short int DepFait = 0;

    while(DepFait < NbDep)
        {
        if(Direction == AV_BAS)
            Lancre = Lancre->PtrSuiv;
        else
            Lancre = Lancre->PtrPrec;
        if(Lancre)
            DepFait++;
        else
            break;
        }
    return(Lancre);
    }
