Listes en c comme en delphi

Contenu du snippet

Voilà bonjour, c'est mon premier code, il s'agit de gestion dynamique de listes. Je l'ai fait quand je suis passé du Delphi plein temps au mi-Delphi mi-C pour avoir des fonctions en C qui permettent de gérer une liste d'entiers ou de chaînes de caractères avec les mêms fonctions (add, extract...). La liste est une structure contenant un pointeur vers la base en mémoire et les fonctions gèrent tout. N'oubliez pas d'appeler Initialize avant d'utiliser une liste.

Source / Exemple :


#include <stdlib.h>
#include <string.h>

typedef struct TList {
         int Capacity;
         int Count;
         int *List;
         int Heapsize;
         struct TList *Tailles; // tailles des chunks
         };

#define OUT_OF_MEM 1
#define OUT_OF_LIMITS 2
#define SUCCESS 3
#define FINISHED 4

         // These are functions for Lists in general

         int Add(struct TList *List,int value);
         int Clear(struct TList *List);
         int Exchange(struct TList *List,int index,int index2);
         int SetCapacity(struct TList *List,int Capacity);
         int Getelement(struct TList *List,int index,int *result);
         int Extract(struct TList *List,int index,int *result);
         int Indexof(struct TList *List,int value,int *result,int start);
         int Setelement(struct TList *List,int index,int value);
         int Insert(struct TList *List,int index,int value);
         int Move(struct TList *List,int firstplace,int newplace);
         int Remove(struct TList *List,int index);
         int Pack(struct TList *List);
         int addsize(struct TList *List,int size);
         void Initializelist(struct TList *List);

         /* These are functions for stringLists in particular.
         Some of the functions above can also be used with stringlists
         because they just work with the integer value, so they don't
         have "string version". for exemple, Pack just deletes null Pointers*/

         int Addstr(struct TList *List,char *str);
         int Clearstr(struct TList *List);
         int Getelementstr(struct TList *List,int index,char *result);
         int Extractstr(struct TList *List,int index,char *result);
         int Indexofstr(struct TList *List,char *value,int *result,int start);
         int Setelementstr(struct TList *List,int index,char *value);
         int Insertstr(struct TList *List,int index,char *value);
         int Deletestr(struct TList *List,int index);

         /* Fonctions chunk, elles utilisent les fonctions chaînes de façon détournée pour 
         gérer des bouts de mémoire non typés. */
         
         int Addchunk(struct TList *List,int size,void **pointeur);
         int Getchunkpointer(struct TList *List,int index,int *result);
         int Removechunk(struct TList *List,int index);
         int Getchunksize( struct TList *List, int index, int *size );
         int Insertchunk( struct TList *List, int index, int size );
         char *Prepare_Initialization( int size );

void Initializelist(struct TList *List)
{
List->List=malloc(sizeof(int));
List->Capacity=1;
List->Count=0;
List->Heapsize=0;
}

int Add(struct TList *List,int value)
{ int backup;

if ( !( List->Capacity > List->Count ) ) {
         backup=List->List;
         List->List=realloc(List->List,(List->Count+1)*sizeof(int));
         if ( List->List==NULL ) {
            List->List=backup;
            return OUT_OF_MEM; }
            List->Capacity=List->Count+1;
         }
List->List[List->Count]=value;
List->Count++;
return SUCCESS;
}

int Clear(struct TList *List)
{
free(List->List);
Initializelist(List);
return SUCCESS;
}

int Exchange(struct TList *List,int index,int index2)
{    int temp;

    if ( ( ( index < 0 ) || ( index > List->Count-1 ) )
    || ( ( index2 < 0 ) || ( index2 > List->Count-1 ) ) ) return OUT_OF_LIMITS ;
    temp=List->List[index];
    List->List[index]=List->List[index2];
    List->List[index2]=temp;
    return SUCCESS;
}

int SetCapacity(struct TList *List,int Capacity)
{ int backup;

backup=List->List;
List->List=realloc(List->List,Capacity*sizeof(int));
         if ( List->List==NULL ) {
            List->List=backup;
            return OUT_OF_MEM; }
else List->Capacity=Capacity;
return SUCCESS;
}

int Getelement(struct TList *List,int index,int *result)
{
    if ( ( index < 0 ) || ( index > List->Count-1 ) ) return OUT_OF_LIMITS;

  • result=List->List[index];
return SUCCESS; } int Extract(struct TList *List,int index,int *result) { Getelement(List,index,result); Remove(List,index); } int Indexof(struct TList *List,int value,int *result,int start) { int element; if ( start>=List->Count ) return FINISHED; for ( start==start ; start<List->Count ; start++ ) { Getelement(List,start,&element); if ( element==value ) {
  • result=start;
return SUCCESS; } } return FINISHED; } int Setelement(struct TList *List,int index,int value) { if ( ( index < 0 ) || ( index > List->Count-1 ) ) return OUT_OF_LIMITS; List->List[index]=value; return SUCCESS; } int Insert(struct TList *List,int index,int value) { int pos; if ( Add(List,0)==OUT_OF_MEM ) return OUT_OF_MEM; pos=List->Count-1; for ( pos==List->Count-1 ; pos>index ; pos-- ) { Move(List,pos-1,pos); } List->List[index]=value; return SUCCESS; } int Move(struct TList *List,int firstplace,int newplace) { if ( ( firstplace < 0 ) || ( firstplace > List->Count-1 ) || ( newplace < 0 ) || ( newplace > List->Count-1 ) ) return OUT_OF_LIMITS ; List->List[newplace]=List->List[firstplace]; return SUCCESS; } int Remove(struct TList *List,int index) { for ( index==index ; index<List->Count+1 ; index++ ) { Move(List,index+1,index); } List->Count--; return SUCCESS; } int Pack(struct TList *List) { int result; while ( Indexof(List,0,&result,0)!=FINISHED ) Remove(List,result); return SUCCESS; } int addsize(struct TList *List,int size) { void *pointer; pointer=malloc(size); if ( pointer==NULL ) return OUT_OF_MEM; if ( Add(List,pointer)==OUT_OF_MEM ) { free(pointer); return OUT_OF_MEM ; } else return SUCCESS; } // ----------------------------- String functions ------------------------------// int Addstr(struct TList *List,char *str) { char *pointer; char *firststring; char *newstring; int compteur=0; signed int Delta; int temp; if ( !List->Heapsize ) { pointer=malloc(strlen(str)+1); if ( pointer==NULL ) return OUT_OF_MEM; } else { Getelement(List,0,&firststring); pointer=(int)firststring+List->Heapsize; newstring=realloc(firststring,List->Heapsize+strlen(str)+1); Delta=newstring-firststring; pointer+=Delta; for ( compteur==0 ; compteur<List->Count ; compteur++ ) { Getelement(List,compteur,&temp); temp+=Delta; Setelement(List,compteur,temp); } } if ( Add(List,pointer)==OUT_OF_MEM ) { newstring=realloc(firststring,List->Heapsize); return OUT_OF_MEM ; } else strcpy(pointer,str); List->Heapsize=List->Heapsize+strlen(str)+1; return SUCCESS; } // Peut être utilisé pour les listes de chunks int Clearstr(struct TList *List) { char *firststring; Getelement(List,0,&firststring); free(firststring); Clear(List); return SUCCESS; } int Getelementstr(struct TList *List,int index,char *result) { char *pointer; Getelement(List,index,&pointer); strcpy(result,pointer); return SUCCESS; } int Extractstr(struct TList *List,int index,char *result) { Getelementstr(List,index,result); Deletestr(List,index); } int Indexofstr(struct TList *List,char *value,int *result,int start) { char element[256]; if ( start>=List->Count ) return FINISHED; for ( start==start ; start<List->Count ; start++ ) { Getelementstr(List,start,&element[0]); if ( !strcmp(value,&element[0]) ) {
  • result=start;
return SUCCESS; } } return FINISHED; } int Setelementstr(struct TList *List,int index,char *value) { char *pointer; if ( Deletestr(List,index)!=3 ) return OUT_OF_LIMITS; if ( Insertstr(List,index,value)!=3 ) return OUT_OF_MEM; return SUCCESS; } int Insertstr(struct TList *List,int index,char *value) { char *pointer; char *firststring; char *newstring; int compteur=0; signed int Delta; int temp; char tempstring[256]; char tempbuffer; if ( !List->Heapsize ) return OUT_OF_LIMITS; Getelement(List,0,&firststring); pointer=(int)firststring; for ( compteur==0 ; compteur<List->Count ; compteur++ ) { Getelementstr(List,compteur,&tempstring); pointer+=strlen(tempstring)+1; } newstring=realloc(firststring,List->Heapsize+strlen(value)+1); if ( newstring==NULL ) return OUT_OF_MEM; Delta=newstring-firststring; pointer+=Delta; for ( compteur==0 ; compteur<List->Count ; compteur++ ) { Getelement(List,compteur,&temp); temp+=Delta; Setelement(List,compteur,temp); } for ( compteur==List->Heapsize ; compteur>=pointer ; compteur-- ) { tempbuffer=*(char *)compteur;
  • (char *)(compteur+strlen(value)+1)=tempbuffer;
} if ( Insert(List,index,pointer)==OUT_OF_MEM ) { free(pointer); return OUT_OF_MEM; } else strcpy(pointer,value); return SUCCESS; } int Deletestr(struct TList *List,int index) { int compteur,adressechaine,temp; char stringtodelete[256]; char tempbuffer; char *firststring; if ( Getelement(List,index,&adressechaine)!=3 ) return OUT_OF_LIMITS; Getelementstr(List,index,stringtodelete); adressechaine+=strlen(stringtodelete)+1; for ( compteur==index+1 ; compteur<List->Count ; compteur++ ) { Getelement(List,compteur,&temp); temp-=strlen(stringtodelete)+1; Setelement(List,compteur,temp); } for ( compteur==adressechaine ; compteur<=List->Heapsize ; compteur-- ) { tempbuffer=*(char *)compteur;
  • (char *)(compteur-(strlen(stringtodelete)+1))=tempbuffer;
} Getelement(List,0,&firststring); realloc(firststring,List->Heapsize-(strlen(stringtodelete)+1)); Remove(List,index); } // ----------------------------- Chunk functions ------------------------------// int Addchunk(struct TList *List,int size,void **pointeur) { char *str; int counter; str = Prepare_Initialization(size); Addstr(List,str); Add(List->Tailles,size); free(str); Getelement(List,List->Count-1,pointeur); return SUCCESS; } int Getchunkpointer(struct TList *List,int index,int *result) { if ( ( index < 0 ) || ( index > List->Count-1 ) ) return OUT_OF_LIMITS; return Getelement(List,index,result); } int Removechunk(struct TList *List,int index) { char *str,*towrite; int counter,size; Getchunksize(List,index,&size); Getchunkpointer(List,index,&towrite); str = Prepare_Initialization(size); strcpy(towrite,str); free(str); Deletestr(List,index); Remove(List->Tailles,index); return SUCCESS; } int Getchunksize( struct TList *List, int index, int *size ) { if ( ( index < 0 ) || ( index > List->Count-1 ) ) return OUT_OF_LIMITS; return Getelement(List->Tailles,index,size); } int Insertchunk( struct TList *List, int index, int size ) { char *str; int result; str = Prepare_Initialization(size); result=Insertstr(List,index,str); if ( result != SUCCESS ) return result; result=Insert(List->Tailles,index,size); if ( result != SUCCESS ) return result; free(str); return SUCCESS; } char *Prepare_Initialization( int size ) { char *str; int counter; str = malloc(size); if ( !str ) return str; counter=0; for ( counter==0 ; counter<size-1 ; counter++ ) { str[counter]='a'; } str[size-1]=0x00; return str; }

Conclusion :


Le code est simple et court, on peut trouver des améliorations éventuellement mais les personnes qui utilisent déjà les listes chaînées ( par exemple celle de GTK+ ) n'y trouveront rien de particulier. Par contre j'ai testé tous les bugs possibles là c'est blindé.
salut!

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.