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

276 lines
5.1 KiB
C
Raw Normal View History

2022-06-10 10:15:14 +02:00
#include <assert.h>
2022-06-10 15:05:04 +02:00
#include <stdio.h>
2022-06-10 10:47:17 +02:00
#include <stdlib.h>
2022-06-13 15:03:51 +02:00
#include <string.h>
2022-06-23 16:24:47 +02:00
#include <stdbool.h>
2022-12-01 17:29:47 +01:00
#include "include/queue.h"
2022-06-10 10:15:14 +02:00
/**
2022-06-21 15:13:10 +02:00
* @brief struct queue struct used for queueing string name
*
2022-06-10 10:15:14 +02:00
* @param charLen lenght of tabChar pointeur
* @param tabChar char array pointer
2022-06-21 15:13:10 +02:00
* @param pNextE point next element
2022-06-10 10:15:14 +02:00
*/
2022-06-21 15:13:10 +02:00
struct queue
{
2022-06-10 10:15:14 +02:00
int charLen;
2022-06-21 15:13:10 +02:00
char *tabChar;
Pqueue pNextE;
2022-06-10 10:47:17 +02:00
};
2022-06-10 10:15:14 +02:00
2022-06-10 10:47:17 +02:00
typedef struct queue *Pqueue;
2022-06-10 10:15:14 +02:00
/**
* @brief create an empty element of queue
2022-06-21 15:13:10 +02:00
*
* @return Pqueue
2022-06-10 10:15:14 +02:00
*/
2022-06-21 15:13:10 +02:00
Pqueue queueCreateEmpty()
{
Pqueue new = (Pqueue)malloc(sizeof(struct queue));
2022-06-10 10:15:14 +02:00
assert(new);
new->charLen = 0;
new->tabChar = NULL;
2022-06-10 10:47:17 +02:00
new->pNextE = NULL;
2022-06-10 10:15:14 +02:00
return new;
}
2022-06-21 15:13:10 +02:00
/************ SETTER ************/
/**
* @brief set char size array
2022-06-21 15:13:10 +02:00
*
* @param elem targeted element
* @param _charLen size of char array
*/
2022-06-21 15:13:10 +02:00
void queueSetCharLen(Pqueue elem, int _charLen)
{
assert(elem);
elem->charLen = _charLen;
}
/**
* @brief set the char array in the element
2022-06-21 15:13:10 +02:00
*
* @param elem targeted element
* @param _charLen char array size
* @param _tabChar pointer to static char array
*/
2022-06-21 15:13:10 +02:00
void queueSetTabChar(Pqueue elem, int _charLen, const char *_tabChar)
{
assert(elem);
assert(_tabChar);
elem->charLen = _charLen;
2022-06-21 15:13:10 +02:00
elem->tabChar = calloc(_charLen, sizeof(char));
for (int i = 0; i < _charLen; i++)
{
2022-06-13 15:03:51 +02:00
elem->tabChar[i] = _tabChar[i];
}
}
2022-06-13 15:03:51 +02:00
/**
* @brief set next pqueue element
2022-06-21 15:13:10 +02:00
*
2022-06-13 15:03:51 +02:00
* @param elem target element
* @param next next element
*/
2022-06-21 15:13:10 +02:00
void queueSetNextE(Pqueue elem, Pqueue next)
{
2022-06-13 15:03:51 +02:00
assert(elem);
assert(next);
2022-06-21 15:13:10 +02:00
elem->pNextE = next;
2022-06-13 15:03:51 +02:00
}
2022-06-21 15:13:10 +02:00
/************ GETTER ************/
2022-06-10 10:15:14 +02:00
/**
* @brief switch to the next element
2022-06-21 15:13:10 +02:00
*
2022-06-10 10:15:14 +02:00
* @param elem current element
* @return Pqueue next element
*/
2022-06-21 15:13:10 +02:00
Pqueue queueGetNextE(Pqueue elem)
{
2022-06-10 10:15:14 +02:00
assert(elem);
2022-06-10 10:47:17 +02:00
return elem->pNextE;
2022-06-10 10:15:14 +02:00
}
/**
* @brief get the size char array
2022-06-21 15:13:10 +02:00
*
* @param elem targeted element
2022-06-21 15:13:10 +02:00
* @return int
*/
2022-06-21 15:13:10 +02:00
int queueGetCharLen(Pqueue elem)
{
2022-06-10 15:05:04 +02:00
assert(elem);
return elem->charLen;
}
/**
* @brief get the pointer of char array
2022-06-21 15:13:10 +02:00
*
* @param elem targeted elemnt
2022-06-21 15:13:10 +02:00
* @return char*
*/
2022-06-21 15:13:10 +02:00
char *queueGetTabChar(Pqueue elem)
{
2022-06-10 15:05:04 +02:00
assert(elem);
return elem->tabChar;
}
2022-06-21 15:13:10 +02:00
/************ ************/
2022-06-10 15:05:04 +02:00
/**
* @brief Create Element of queue
2022-06-21 15:13:10 +02:00
*
* @param lenChar size of char array that will be created
* @return Pqueue new element created
*/
2022-06-21 15:13:10 +02:00
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;
2022-06-10 15:05:04 +02:00
}
2022-06-10 10:15:14 +02:00
/**
* @brief remove and free the last element of queue
2022-06-21 15:13:10 +02:00
*
2022-06-10 10:15:14 +02:00
* @param elem current element
* @return Pqueue current element
*/
2022-06-21 15:13:10 +02:00
Pqueue queueRmLastE(Pqueue elem)
{
2022-06-10 10:15:14 +02:00
assert(elem);
2022-06-23 16:24:47 +02:00
Pqueue tmp = elem, current = NULL;
while (tmp->pNextE != NULL)
2022-06-21 15:13:10 +02:00
{
2022-06-23 16:24:47 +02:00
current = tmp;
tmp = queueGetNextE(tmp);
2022-06-10 10:15:14 +02:00
}
2022-06-10 10:47:17 +02:00
free(tmp->tabChar);
2022-06-10 10:15:14 +02:00
free(tmp);
2022-06-23 16:24:47 +02:00
current->pNextE = NULL;
2022-06-10 10:15:14 +02:00
return elem;
}
/**
* @brief remove and free the first element of queue
2022-06-21 15:13:10 +02:00
*
2022-06-10 10:15:14 +02:00
* @param elem target to remove (the element need to be the first)
2022-06-23 16:24:47 +02:00
* @return next element of the queue
2022-06-10 10:15:14 +02:00
*/
2022-06-23 16:24:47 +02:00
Pqueue queueRmFrstE(Pqueue elem)
2022-06-21 15:13:10 +02:00
{
2022-06-10 10:15:14 +02:00
assert(elem);
2022-06-23 16:24:47 +02:00
Pqueue next = queueGetNextE(elem);
2022-06-21 15:13:10 +02:00
if (elem->tabChar != NULL)
{
free(elem->tabChar);
}
2022-06-10 10:15:14 +02:00
free(elem);
2022-06-23 16:24:47 +02:00
return next;
2022-06-10 10:15:14 +02:00
}
/**
* @brief delete the first value and return the next value
2022-06-21 15:13:10 +02:00
*
* @param elem
* @return Pqueue
2022-06-10 10:15:14 +02:00
*/
2022-06-21 15:13:10 +02:00
Pqueue queueNextDelFrst(Pqueue elem)
{
2022-06-10 10:15:14 +02:00
assert(elem);
2022-06-10 10:47:17 +02:00
Pqueue tmp = elem->pNextE;
queueRmFrstE(elem);
2022-06-10 10:15:14 +02:00
return tmp;
}
/**
2022-06-21 15:13:10 +02:00
* @brief add in the end of queue element with string parameter
*
2022-06-10 10:15:14 +02:00
* @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
*/
2022-06-21 15:13:10 +02:00
void queueAddLastQ(Pqueue elem, const char *str, int len)
{
2022-06-10 10:15:14 +02:00
assert(elem);
2022-06-23 16:24:47 +02:00
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
2022-06-21 15:13:10 +02:00
{
2022-06-23 16:24:47 +02:00
next = queueCreateEmpty();
2022-06-10 10:15:14 +02:00
}
2022-06-23 16:24:47 +02:00
queueSetNextE(current, next);
2022-06-10 10:15:14 +02:00
}
/**
* @brief Print targeted element
2022-06-21 15:13:10 +02:00
*
* @param elem targeted element
*/
2022-06-21 15:13:10 +02:00
void queuePrintE(Pqueue elem)
{
Pqueue next = queueGetNextE(elem);
2022-06-21 15:13:10 +02:00
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);
2022-06-10 15:05:04 +02:00
}
2022-06-21 15:13:10 +02:00
else
{
2022-06-13 15:03:51 +02:00
printf("No nextFile existing (null)\n");
}
}
2022-06-14 10:46:12 +02:00
/**
* @brief Print all the element starting from @param firstE till the end of the linked list
2022-06-21 15:13:10 +02:00
*
* @param firstE
2022-06-14 10:46:12 +02:00
*/
2022-06-21 15:13:10 +02:00
void queuePrintWholeQ(Pqueue firstE)
{
2022-06-13 15:03:51 +02:00
queuePrintE(firstE);
Pqueue elem = firstE;
2022-06-21 15:13:10 +02:00
while (queueGetNextE(elem) != NULL)
{
2022-06-13 15:03:51 +02:00
elem = queueGetNextE(elem);
queuePrintE(elem);
2022-06-10 15:05:04 +02:00
}
}
2022-06-21 15:13:10 +02:00
void queueDelAll(Pqueue elem)
{
assert(elem);
Pqueue condemned = elem, tmp;
while (condemned != NULL)
{
tmp = queueGetNextE(condemned);
queueRmFrstE(condemned);
condemned = tmp;
}
}