[gcc] librairie c - tableaux de chaines de caracteres

Description

Librairie de fonctions permettant de gérer plus facilement les tableaux de chaines de caractères.

Source / Exemple :


/* --- STRARRAY.H --- */

#ifndef STRARRAY_H
#define STRARRAY_H 1
#define XOOSTR char * 
#include <string.h>

/* --- STRUCTURE STRARRAY --- */
typedef struct
{
	int dim;
	XOOSTR *string;
} STRARRAY;
/* --- FIN: STRUCTURE STRARRAY --- */

int strarray_init(STRARRAY *array);
int strarray_redim(STRARRAY *array, int size);
int strarray_concat(STRARRAY *array1, STRARRAY *array2);
int strarray_push(STRARRAY *array, XOOSTR string);
int strarray_pushfirst(STRARRAY *array, XOOSTR string);
int strarray_change(STRARRAY *array, int number, XOOSTR string);
int strarray_copy(STRARRAY *array1, STRARRAY *array2);
int strarray_erase(STRARRAY *array, int number);
#endif

/* --- STRARRAY.C --- */
#include "strarray.h"

/* --- STRARRAY_INIT 
type:                     fonction
description:              initialise un tableau de chaines de caractères
paramètres:               @array             structure STRARRAY
retour:                   entier */
int strarray_init(STRARRAY *array)
{
	array->dim = -1;
	return 0;
}
/* --- FIN: STRARRAY_INIT */

/* --- STRARRAY_REDIM 
type:                     fonction
description:              redimensionne un tableau de chaines de caractères
paramètres:               @array             structure STRARRAY
                          @size              integer
retour:                   entier */
int strarray_redim(STRARRAY *array, int size)
{
	XOOSTR *temp;
	int i;

	if (size < 0) return 1;

	temp = (XOOSTR*)calloc(array->dim, sizeof(XOOSTR));
	for (i=0; i<=array->dim; i++)
	{
		temp[i] = (XOOSTR)malloc(strlen(array->string[i]));
		strcpy(temp[i], array->string[i]);
	}

	array->string = (XOOSTR*)calloc(size, sizeof(XOOSTR));
	for (i=0; i<=array->dim; i++)
	{
		array->string[i] = (XOOSTR)malloc(strlen(temp[i]));
		strcpy(array->string[i], temp[i]);
	}
	array->dim = size;
	return 0;
}
/* --- FIN: STRARRAY_REDIM */

/* --- STRARRAY_CONCAT 
type:                     fonction
description:              ajoute le tableau @array2 à la fin du tableau @array1
paramètres:               @array1            structure STRARRAY
                          @array2            structure STRARRAY
retour:                   entier */
int strarray_concat(STRARRAY *array1, STRARRAY *array2)
{
	int i;
	STRARRAY temp;
	STRARRAY *pTemp;
	pTemp = &temp;

	strarray_init(pTemp);
	strarray_copy(pTemp, array1);
	for (i=0; i<=array2->dim; i++)
		strarray_push(pTemp, array2->string[i]);
	strarray_copy(array1, pTemp);
	return 0;
}
/* --- FIN: STRARRAY_CONCAT */

/* --- STRARRAY_PUSH 
type:                     fonction
description:              ajoute une chaine en fin de tableau
paramètres:               @array1            structure STRARRAY
                          @string            char *
retour:                   entier */
int strarray_push(STRARRAY *array, XOOSTR string)
{
	if (array->dim == -1)
	{
		array->string = (XOOSTR*)calloc(0, sizeof(XOOSTR));
		array->dim = 0;
	}
	else
	{	
		strarray_redim(array, (array->dim + 1));
	}
	array->string[array->dim] = (XOOSTR)malloc(strlen(string));
	strcpy(array->string[array->dim], string);
	return 0;
}
/* --- FIN: STRARRAY_PUSH */

/* --- STRARRAY_PUSHFIRST
type:                     fonction
description:              ajoute une chaine comme premier élément du tableau
paramètres:               @array1            structure STRARRAY
                          @string            char *
retour:                   entier */
int strarray_pushfirst(STRARRAY *array, XOOSTR string)
{
	STRARRAY temp;
	STRARRAY *pTemp;
	strarray_init(pTemp);
	strarray_push(pTemp, string);
	strarray_concat(pTemp, array);
	strarray_copy(array, pTemp);
	return 0;
}
/* --- FIN: STRARRAY_PUSHFIRST --- */

/* --- STRARRAY_CHANGE ---
type:                     fonction
description:              modifie l'élément d'indice @number du tableau de chaines
paramètres:               @array1            structure STRARRAY
                          @number            integer
                          @string            char *
retour:                   entier */
int strarray_change(STRARRAY *array, int number, XOOSTR string)
{
	if (number > array->dim)
	{
		printf("error: index '%d' out of array limits!\n", number);	
		return 1;
	}
	array->string[number] = (XOOSTR)malloc(strlen(string));
	strcpy(array->string[number], string);
	return 0;
}
/* --- FIN: STRARRAY_CHANGE --- */

/* --- STRARRAY_COPY ---
type:                     fonction
description:              copie à l'identique @array2 au sein de @array1
paramètres:               @array1            structure STRARRAY
                          @array2            structure STRARRAY
retour:                   entier */
int strarray_copy(STRARRAY *array1, STRARRAY *array2)
{
	int i;

	strarray_redim(array1, array2->dim);
	for (i=0; i<=array2->dim; i++)
	{
		strarray_change(array1, i, array2->string[i]);
	}
	return 0;
}
/* --- FIN: STRARRAY_COPY --- */

/* --- STRARRAY_ERASE ---
type:                     fonction
description:              Supprime l'élément d'indice @number du tableau de chaines
paramètres:               @array1            structure STRARRAY
                          @number            integer
retour:                   entier */
int strarray_erase(STRARRAY *array, int number)
{
	int olddim, newdim, i;
	STRARRAY temp;
        STRARRAY *pTemp;
        pTemp = &temp;
        strarray_init(pTemp);

	olddim = array->dim;
	newdim = olddim - 1;

	for (i=0; i<number; i++)
	{
		strarray_push(pTemp, array->string[i]);
	}
	for (i=number+1; i<=olddim; i++)
		strarray_push(pTemp, array->string[i]);

	strarray_copy(array, pTemp);

	return 0;
}
/* --- FIN: STRARRAY_ERASE --- */

/* --- SAMPLE.C --- */
#include "strarray.h"

int main(void)
{
	int i;
	STRARRAY tab, tab2;
	STRARRAY *montableau, *mytab;

	montableau = &tab;
	mytab = &tab2;
	
	strarray_init(montableau);
	strarray_init(mytab);

	strarray_push(montableau, "premier element");
	strarray_push(montableau, "deuxième élément");
	strarray_push(montableau, "salut !!");
	strarray_push(montableau, "quatrieme élément");
	
	strarray_erase(montableau, 3);
	strarray_concat(montableau, mytab);
	strarray_pushfirst(montableau, "... devient le premier élément");	
	printf("taille du tableau = %d (%d éléments)\n", montableau->dim, (montableau->dim+1));
	for (i=0; i<=montableau->dim; i++) printf("valeur élément %d = %s\n", i, montableau->string[i]);
}

Conclusion :


compilation de l'exemple :
gcc -c strarray.c
gcc -c sample.c
gcc -o sample sample.o strarray.o

voilà...

Codes Sources

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.