-
Notifications
You must be signed in to change notification settings - Fork 0
/
fifo_context.c
70 lines (62 loc) · 1.63 KB
/
fifo_context.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* fifo_context.c
*
* Created on: 16 dec. 2012
* Author: Hugo
*/
#include <stdio.h>
#include <stdlib.h>
#include "fifo_context.h"
void fifo_savenextline(Fifo_linecontext **p_file, FILE * file,unsigned int max_size)
{
Fifo_linecontext *p_nouveau = malloc(sizeof *p_nouveau);
if (p_nouveau != NULL)
{
p_nouveau->next = NULL;
p_nouveau->data = malloc(sizeof (fpos_t));
fgetpos(file,p_nouveau->data);
if (*p_file == NULL)
{
*p_file = p_nouveau;
}
else
{
unsigned int size=1;
Fifo_linecontext *p_tmp = *p_file;
while (p_tmp->next != NULL)
{
size++;
p_tmp = p_tmp->next;
}
p_tmp->next = p_nouveau;
if(size > max_size){
fifo_deletefirst(p_file);
}
}
}
}
Fifo_linecontext* fifo_deletefirst(Fifo_linecontext **p_file)
{
/* On teste si la file n'est pas vide. */
if (*p_file != NULL)
{
/* Creation d'un element temporaire pointant vers le deuxieme element de la file. */
Fifo_linecontext *p_tmp = (*p_file)->next;
/* Free the data */
free((*p_file)->data),(*p_file)->data = NULL;
/* Effacement du premier element. */
free(*p_file), *p_file = NULL;
/* On fait pointer la file vers le deuxieme element. */
*p_file = p_tmp;
}
return *p_file;
}
void fifo_clear(Fifo_linecontext **p_file)
{
/* Tant que la file n'est pas vide. */
while (*p_file != NULL)
{
/* On enleve l'element courant. */
fifo_deletefirst(p_file);
}
}