Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
** EPITECH PROJECT, 2023 |
3 |
|
|
** my_show_list |
4 |
|
|
** File description: |
5 |
|
|
** Shows a linked list |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#include "mylist.h" |
9 |
|
|
|
10 |
|
✗ |
static int print_int(void *data, type_t type) |
11 |
|
|
{ |
12 |
|
✗ |
if (type == SHORT_SHORT) |
13 |
|
✗ |
return my_printf("%hhd", *((char *)data)); |
14 |
|
✗ |
if (type == SHORT) |
15 |
|
✗ |
return my_printf("%hd", *((short *)data)); |
16 |
|
✗ |
if (type == INT) |
17 |
|
✗ |
return my_printf("%d", *((int *)data)); |
18 |
|
✗ |
if (type == LONG) |
19 |
|
✗ |
return my_printf("%ld", *((long *)data)); |
20 |
|
✗ |
if (type == LONG_LONG) |
21 |
|
✗ |
return my_printf("%lld", *((long long *)data)); |
22 |
|
✗ |
return 0; |
23 |
|
|
} |
24 |
|
|
|
25 |
|
✗ |
static int print_unsigned_int(void *data, type_t type) |
26 |
|
|
{ |
27 |
|
✗ |
if (type == UNSIGNED_SHORT_SHORT) |
28 |
|
✗ |
return my_printf("%hhu", *((unsigned char *)data)); |
29 |
|
✗ |
if (type == UNSIGNED_SHORT) |
30 |
|
✗ |
return my_printf("%hu", *((unsigned short *)data)); |
31 |
|
✗ |
if (type == UNSIGNED_INT) |
32 |
|
✗ |
return my_printf("%u", *((unsigned int *)data)); |
33 |
|
✗ |
if (type == UNSIGNED_LONG) |
34 |
|
✗ |
return my_printf("%lu", *((unsigned long *)data)); |
35 |
|
✗ |
if (type == UNSIGNED_LONG_LONG) |
36 |
|
✗ |
return my_printf("%llu", *((unsigned long long *)data)); |
37 |
|
✗ |
if (type == SIZE_T) |
38 |
|
✗ |
return my_printf("%zu", *((size_t *)data)); |
39 |
|
✗ |
return 0; |
40 |
|
|
} |
41 |
|
|
|
42 |
|
✗ |
static int print_other(void *data, type_t type) |
43 |
|
|
{ |
44 |
|
✗ |
if (type == FLOAT) |
45 |
|
✗ |
return my_printf("%f", *((float *)data)); |
46 |
|
✗ |
if (type == DOUBLE) |
47 |
|
✗ |
return my_printf("%f", *((double *)data)); |
48 |
|
✗ |
if (type == CHAR) |
49 |
|
✗ |
return my_printf("%c", *((char *)data)); |
50 |
|
✗ |
if (type == STRING) |
51 |
|
✗ |
return my_printf("%s", (char *)data); |
52 |
|
✗ |
if (type == ARRAY_OF_STRING) |
53 |
|
✗ |
return my_printf("%S", ((char **)data)); |
54 |
|
✗ |
if (type == VOID) |
55 |
|
✗ |
return my_printf("%p", data); |
56 |
|
✗ |
return 0; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
✗ |
static int select_print(void *data, type_t type) |
60 |
|
|
{ |
61 |
|
✗ |
int (*PRINT_FUNCTIONS[])(void *, type_t) = { |
62 |
|
|
&print_int, &print_unsigned_int, &print_other}; |
63 |
|
|
|
64 |
|
✗ |
if (type <= 4) |
65 |
|
✗ |
return PRINT_FUNCTIONS[0](data, type); |
66 |
|
✗ |
if (type <= 10) |
67 |
|
✗ |
return PRINT_FUNCTIONS[1](data, type); |
68 |
|
|
else |
69 |
|
✗ |
return PRINT_FUNCTIONS[2](data, type); |
70 |
|
|
} |
71 |
|
|
|
72 |
|
✗ |
void my_show_list(linked_list_t *list) |
73 |
|
|
{ |
74 |
|
✗ |
my_printf("List:\nPrevious | Current | Next\n"); |
75 |
|
✗ |
for (linked_list_t *tmp = list; tmp != NULL; tmp = tmp->next) { |
76 |
|
✗ |
if (tmp->prev == NULL) |
77 |
|
✗ |
my_printf("NULL"); |
78 |
|
|
else |
79 |
|
✗ |
select_print(tmp->prev->data, tmp->prev->type); |
80 |
|
✗ |
my_printf(" ---> "); |
81 |
|
✗ |
select_print(tmp->data, tmp->type); |
82 |
|
✗ |
my_printf(" ---> "); |
83 |
|
✗ |
if (tmp->next == NULL) |
84 |
|
✗ |
my_printf("NULL"); |
85 |
|
|
else |
86 |
|
✗ |
select_print(tmp->next->data, tmp->next->type); |
87 |
|
✗ |
write(1, "\n", 1); |
88 |
|
|
} |
89 |
|
✗ |
} |
90 |
|
|
|