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

233 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>
#include "queue.h"
2022-06-10 10:15:14 +02:00
/**
2022-06-10 10:47:17 +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
* @param pNextE point next element
*/
struct queue {
int charLen;
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-10 10:15:14 +02:00
*
* @return Pqueue
2022-06-10 10:15:14 +02:00
*/
2022-06-10 17:21:08 +02:00
<<<<<<< HEAD
Pqueue queueCreateEmpty(){
2022-06-10 17:21:08 +02:00
=======
2022-06-10 15:50:27 +02:00
Pqueue createE(int lenChar){
2022-06-10 17:21:08 +02:00
>>>>>>> 2e7253c43c8e65a98686582d853147831016103f
2022-06-10 10:47:17 +02:00
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;
}
/************ SETTER ************/
/**
* @brief set char size array
*
* @param elem targeted element
* @param _charLen size of char array
* @return Pqueue
*/
Pqueue 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
* @return Pqueue
*/
Pqueue queueSetTabChar(Pqueue elem, int _charLen, const char *_tabChar){
assert(elem);
assert(_tabChar);
elem->charLen = _charLen;
elem->tabChar = (char *) malloc(_charLen * sizeof(char));
for(int i = 0; i < _charLen; i++){
new->tabChar[i] = _tabChar[i];
}
}
/************ GETTER ************/
2022-06-10 10:15:14 +02:00
/**
* @brief switch to the next element
*
* @param elem current element
* @return Pqueue next element
*/
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
*
* @param elem targeted element
* @return int
*/
int queueGetCharLen(Pqueue elem){
2022-06-10 15:05:04 +02:00
assert(elem);
return elem->charLen;
}
/**
* @brief get the pointer of char array
*
* @param elem targeted elemnt
* @return char*
*/
char * queueGetTabChar(Pqueue elem){
2022-06-10 15:05:04 +02:00
assert(elem);
return elem->tabChar;
}
2022-06-10 17:21:08 +02:00
<<<<<<< HEAD
/************ ************/
2022-06-10 15:05:04 +02:00
/**
* @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;
2022-06-10 17:21:08 +02:00
=======
2022-06-10 15:50:27 +02:00
void setCharLen(Pqueue elem , int _charLen){
2022-06-10 15:05:04 +02:00
assert(elem);
elem->charLen = _charLen;
}
2022-06-10 15:50:27 +02:00
void setTabChar(Pqueue elem , int _charLen , char *_tabChar){
2022-06-10 15:05:04 +02:00
assert(elem);
elem->charLen = _charLen;
elem->tabChar = (char *) malloc(_charLen * sizeof(char));
elem->tabChar = _tabChar;
2022-06-10 17:21:08 +02:00
>>>>>>> 2e7253c43c8e65a98686582d853147831016103f
2022-06-10 15:05:04 +02:00
}
2022-06-10 15:50:27 +02:00
void setNextE(Pqueue elem , Pqueue next){
assert(elem);
assert(next);
elem -> pNextE = next;
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
*
* @param elem current element
* @return Pqueue current element
*/
Pqueue queueRmLastE(Pqueue elem){
2022-06-10 10:15:14 +02:00
assert(elem);
Pqueue tmp = elem, previous = NULL;
2022-06-10 10:47:17 +02:00
while(elem->pNextE != NULL){
2022-06-10 10:15:14 +02:00
previous = 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-10 10:47:17 +02:00
previous->pNextE = NULL;
2022-06-10 10:15:14 +02:00
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 queueRmFrstE(Pqueue elem){
2022-06-10 10:15:14 +02:00
assert(elem);
2022-06-10 10:47:17 +02:00
Pqueue tmp = elem->pNextE;
free(elem->tabChar);
2022-06-10 10:15:14 +02:00
free(elem);
return tmp;
}
/**
* @brief delete the first value and return the next value
*
* @param elem
* @return Pqueue
*/
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;
}
/**
* @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 queueAddLastQ(Pqueue elem, const char* str, int len){
2022-06-10 10:15:14 +02:00
assert(elem);
assert(str);
Pqueue tmp = elem, previous = NULL;
2022-06-10 10:47:17 +02:00
while(elem->pNextE != NULL){
2022-06-10 10:15:14 +02:00
previous = tmp;
tmp = queueGetNextE(tmp);
2022-06-10 10:15:14 +02:00
}
2022-06-10 17:21:08 +02:00
<<<<<<< HEAD
tmp = queueCreateE(len);
2022-06-10 10:47:17 +02:00
tmp->tabChar = str;
tmp->charLen = len;
2022-06-10 10:15:14 +02:00
return tmp;
2022-06-10 17:21:08 +02:00
=======
2022-06-10 15:50:27 +02:00
tmp -> pNextE = createE(len);
Pqueue next = getNextE(tmp);
next->tabChar = str;
next->charLen = len;
return next;
2022-06-10 17:21:08 +02:00
>>>>>>> 2e7253c43c8e65a98686582d853147831016103f
2022-06-10 10:15:14 +02:00
}
/**
* @brief Print targeted element
*
* @param elem targeted element
*/
void queuePrintE(Pqueue elem){
Pqueue next = queueGetNextE(elem);
2022-06-10 15:05:04 +02:00
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");
}
}