Traitement-signal-plantes/Code-C/queue.c
2022-06-10 15:50:27 +02:00

156 lines
3.4 KiB
C

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
/**
* @brief struct queue struct used for queueing string name
*
* @param charLen lenght of tabChar pointeur
* @param tabChar char array pointer
* @param pNextE point next element
*/
struct queue {
int charLen;
char * tabChar;
Pqueue pNextE;
};
typedef struct queue *Pqueue;
/**
* @brief Create Element of queue
*
* @param lenChar size of char array that will be created
* @return Pqueue new element created
*/
Pqueue createE(int lenChar){
Pqueue new = (Pqueue) malloc(sizeof(struct queue));
assert(new);
/* ???? tab char create or give pointer ???*/
new->charLen = lenChar;
new->tabChar = (char *) malloc(lenChar * sizeof(char));
new->pNextE = NULL;
return new;
}
/**
* @brief switch to the next element
*
* @param elem current element
* @return Pqueue next element
*/
Pqueue getNextE(Pqueue elem){
assert(elem);
return elem->pNextE;
}
int getCharLen(Pqueue elem){
assert(elem);
return elem->charLen;
}
char * gettabChar(Pqueue elem){
assert(elem);
return elem->tabChar;
}
void setCharLen(Pqueue elem , int _charLen){
assert(elem);
elem->charLen = _charLen;
}
void setTabChar(Pqueue elem , int _charLen , char *_tabChar){
assert(elem);
elem->charLen = _charLen;
elem->tabChar = (char *) malloc(_charLen * sizeof(char));
elem->tabChar = _tabChar;
}
void setNextE(Pqueue elem , Pqueue next){
assert(elem);
assert(next);
elem -> pNextE = next;
}
/**
* @brief remove and free the last element of queue
*
* @param elem current element
* @return Pqueue current element
*/
Pqueue rmLastE(Pqueue elem){
assert(elem);
Pqueue tmp = elem, previous = NULL;
while(elem->pNextE != NULL){
previous = tmp;
tmp = getNextE(tmp);
}
free(tmp->tabChar);
free(tmp);
previous->pNextE = NULL;
return elem;
}
/**
* @brief remove and free the first element of queue
*
* @param elem target to remove (the element need to be the first)
* @return Pqueue the second element after remove first
*/
Pqueue rmFrstE(Pqueue elem){
assert(elem);
Pqueue tmp = elem->pNextE;
free(elem->tabChar);
free(elem);
return tmp;
}
/**
* @brief delete the first value and return the next value
*
* @param elem
* @return Pqueue
*/
Pqueue nextDelFrst(Pqueue elem){
assert(elem);
Pqueue tmp = elem->pNextE;
rmFrstE(elem);
return tmp;
}
/**
* @brief add in the end of queue element with string parameter
*
* @param elem Pqueue will be added at the end of queue
* @param str the string will be bind at the element
* @param len the lenght of char array
* @return Pqueue pointer to the last element
*/
Pqueue addLastQ(Pqueue elem, const char* str, int len){
assert(elem);
assert(str);
Pqueue tmp = elem, previous = NULL;
while(elem->pNextE != NULL){
previous = tmp;
tmp = getNextE(tmp);
}
tmp -> pNextE = createE(len);
Pqueue next = getNextE(tmp);
next->tabChar = str;
next->charLen = len;
return next;
}
void printE(Pqueue elem){
Pqueue next = getNextE(elem);
printf("File Name : %s \n(size of the file name : %d)\n",elem -> tabChar , elem -> charLen );
if(next != NULL){
printf("Next File : %s\n", next ->tabChar);
}
else{
printf("No nextFile existing\n");
}
}