Traitement-signal-plantes/Code-C/queue.c

276 lines
5.1 KiB
C

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "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 an empty element of queue
*
* @return Pqueue
*/
Pqueue queueCreateEmpty()
{
Pqueue new = (Pqueue)malloc(sizeof(struct queue));
assert(new);
new->charLen = 0;
new->tabChar = NULL;
new->pNextE = NULL;
return new;
}
/************ SETTER ************/
/**
* @brief set char size array
*
* @param elem targeted element
* @param _charLen size of char array
*/
void queueSetCharLen(Pqueue elem, int _charLen)
{
assert(elem);
elem->charLen = _charLen;
}
/**
* @brief set the char array in the element
*
* @param elem targeted element
* @param _charLen char array size
* @param _tabChar pointer to static char array
*/
void queueSetTabChar(Pqueue elem, int _charLen, const char *_tabChar)
{
assert(elem);
assert(_tabChar);
elem->charLen = _charLen;
elem->tabChar = calloc(_charLen, sizeof(char));
for (int i = 0; i < _charLen; i++)
{
elem->tabChar[i] = _tabChar[i];
}
}
/**
* @brief set next pqueue element
*
* @param elem target element
* @param next next element
*/
void queueSetNextE(Pqueue elem, Pqueue next)
{
assert(elem);
assert(next);
elem->pNextE = next;
}
/************ GETTER ************/
/**
* @brief switch to the next element
*
* @param elem current element
* @return Pqueue next element
*/
Pqueue queueGetNextE(Pqueue elem)
{
assert(elem);
return elem->pNextE;
}
/**
* @brief get the size char array
*
* @param elem targeted element
* @return int
*/
int queueGetCharLen(Pqueue elem)
{
assert(elem);
return elem->charLen;
}
/**
* @brief get the pointer of char array
*
* @param elem targeted elemnt
* @return char*
*/
char *queueGetTabChar(Pqueue elem)
{
assert(elem);
return elem->tabChar;
}
/************ ************/
/**
* @brief Create Element of queue
*
* @param lenChar size of char array that will be created
* @return Pqueue new element created
*/
Pqueue queueCreateE(int lenChar, const char *_tabChar)
{
Pqueue new = (Pqueue)malloc(sizeof(struct queue));
assert(new);
queueSetTabChar(new, lenChar, _tabChar);
new->pNextE = NULL;
return new;
}
/**
* @brief remove and free the last element of queue
*
* @param elem current element
* @return Pqueue current element
*/
Pqueue queueRmLastE(Pqueue elem)
{
assert(elem);
Pqueue tmp = elem, current = NULL;
while (tmp->pNextE != NULL)
{
current = tmp;
tmp = queueGetNextE(tmp);
}
free(tmp->tabChar);
free(tmp);
current->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 next element of the queue
*/
Pqueue queueRmFrstE(Pqueue elem)
{
assert(elem);
Pqueue next = queueGetNextE(elem);
if (elem->tabChar != NULL)
{
free(elem->tabChar);
}
free(elem);
return next;
}
/**
* @brief delete the first value and return the next value
*
* @param elem
* @return Pqueue
*/
Pqueue queueNextDelFrst(Pqueue elem)
{
assert(elem);
Pqueue tmp = elem->pNextE;
queueRmFrstE(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
*/
void queueAddLastQ(Pqueue elem, const char *str, int len)
{
assert(elem);
bool str1 = true;
if (len == 0)
{
str = false;
}
else
{
assert(str);
}
Pqueue current = elem, next = NULL;
while (current->pNextE != NULL)
{
current = queueGetNextE(current);
}
if (str1 == true)
{
next = queueCreateE(len, str);
}
else
{
next = queueCreateEmpty();
}
queueSetNextE(current, next);
}
/**
* @brief Print targeted element
*
* @param elem targeted element
*/
void queuePrintE(Pqueue elem)
{
Pqueue next = queueGetNextE(elem);
printf("\nFile 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 (null)\n");
}
}
/**
* @brief Print all the element starting from @param firstE till the end of the linked list
*
* @param firstE
*/
void queuePrintWholeQ(Pqueue firstE)
{
queuePrintE(firstE);
Pqueue elem = firstE;
while (queueGetNextE(elem) != NULL)
{
elem = queueGetNextE(elem);
queuePrintE(elem);
}
}
void queueDelAll(Pqueue elem)
{
assert(elem);
Pqueue condemned = elem, tmp;
while (condemned != NULL)
{
tmp = queueGetNextE(condemned);
queueRmFrstE(condemned);
condemned = tmp;
}
}