Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | ** EPITECH PROJECT, 2023 | ||
3 | ** my_showmem | ||
4 | ** File description: | ||
5 | ** Prints a memory dump of a string (str) with a defined size | ||
6 | */ | ||
7 | /** | ||
8 | * @file my_showmem.c | ||
9 | * @brief The file containing the my_showmem function | ||
10 | * @author Nicolas TORO | ||
11 | */ | ||
12 | |||
13 | #include "my.h" | ||
14 | |||
15 | 1 | static void print_address(int line) | |
16 | { | ||
17 | 1 | int len = 1 + line / 16; | |
18 | |||
19 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
|
7 | for (int i = 0; i < 8 - len - 1; i++) |
20 | 6 | my_putchar('0'); | |
21 | 1 | my_putnbr_base(line, "0123456789abcedf"); | |
22 | 1 | my_putstr("0: "); | |
23 | 1 | } | |
24 | |||
25 | 4 | static void check_zero_in_hexadecimal(char c) | |
26 | { | ||
27 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
|
4 | if (c < 16) |
28 | 1 | my_putchar('0'); | |
29 | 4 | } | |
30 | |||
31 | 11 | static void check_space(int space) | |
32 | { | ||
33 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | if (space != 2) |
34 | 11 | my_putstr(" "); | |
35 | 11 | } | |
36 | |||
37 | 13 | static void check_backslash(char c, int space) | |
38 | { | ||
39 |
4/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
|
13 | if (c == '\b' && space == 0) |
40 | 1 | my_putstr("08"); | |
41 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
12 | else if (c == '\b' && space == 2) |
42 | 1 | my_putchar('.'); | |
43 | else | ||
44 | 11 | check_space(space); | |
45 | 13 | } | |
46 | |||
47 | 1 | static void print_hexadecimal_content(char const *str, int size, int line) | |
48 | { | ||
49 | 1 | int end_of_line = 0; | |
50 | |||
51 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 times.
|
17 | for (int i = 16 * line; i < 16 * (line + 1); i++) { |
52 |
4/4✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 8 times.
|
16 | if (i != 16 * line && i % 2 == 0) |
53 | 7 | my_putchar(' '); | |
54 |
6/6✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 4 times.
|
16 | if (i == size || str[i] == '\b' || end_of_line == 1) { |
55 | 12 | check_backslash(str[i], end_of_line); | |
56 | 12 | end_of_line = 1; | |
57 | } else { | ||
58 | 4 | check_zero_in_hexadecimal(str[i]); | |
59 | 4 | my_putnbr_base(str[i], "0123456789abcdef"); | |
60 | } | ||
61 | } | ||
62 | 1 | my_putchar(' '); | |
63 | 1 | } | |
64 | |||
65 | 1 | static void print_string_content(char const *str, int size, | |
66 | int line, int *error) | ||
67 | { | ||
68 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | for (int i = 16 * line; i < 16 * (line + 1); i++) { |
69 |
3/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 4 times.
|
5 | if (i == size || str[i] == '\b') { |
70 | 1 | check_backslash(str[i], 2); | |
71 | 1 | *error = 1; | |
72 | 1 | break; | |
73 | } | ||
74 |
3/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
4 | if (str[i] >= 32 && str[i] <= 126) |
75 | 3 | my_putchar(str[i]); | |
76 | else | ||
77 | 1 | my_putchar('.'); | |
78 | } | ||
79 | 1 | } | |
80 | |||
81 | 1 | void my_showmem(char const *str, int size) | |
82 | { | ||
83 | 1 | int number_of_lines = size / 16; | |
84 | 1 | int error = 0; | |
85 | |||
86 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | for (int line = 0; line <= number_of_lines && error == 0; line++) { |
87 | 1 | print_address(line); | |
88 | 1 | print_hexadecimal_content(str, size, line); | |
89 | 1 | print_string_content(str, size, line, &error); | |
90 | 1 | my_putchar('\n'); | |
91 | } | ||
92 | 1 | } | |
93 |