-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_handlers.c
85 lines (73 loc) · 1.82 KB
/
print_handlers.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "monty.h"
/**
* pall - print the contents of the stack
*
* @stack: the stack, represented as a pointer to a linked list
* @line_number: current line number of file being parsed
*/
void pall(stack_t **stack, unsigned int line_number __attribute__((unused)))
{
stack_t *head = *stack;
while (head)
{
printf("%d\n", head->n);
head = head->next;
}
}
/**
* pint - print the int value at the top of the stack, followed by a new line.
*
* @stack: the stack, represented as a pointer to a linked list
* @line_number: current line number of file being parsed
*/
void pint(stack_t **stack, unsigned int line_number)
{
stack_t *head = *stack;
if (!stack || !head)
{
fprintf(stderr, "L%d: can't pint, stack empty\n", line_number);
exit(EXIT_FAILURE);
}
printf("%d\n", head->n);
}
/**
* pchar - print the char value at the top of the stack,
* followed by a new line.
*
* @stack: the stack, represented as a pointer to a linked list
* @line_number: current line number of file being parsed
*/
void pchar(stack_t **stack, unsigned int line_number)
{
stack_t *head = *stack;
if (!stack || !head)
{
fprintf(stderr, "L%d: can't pchar, stack empty\n", line_number);
exit(EXIT_FAILURE);
}
if (head->n > 127 || head->n < 0)
{
fprintf(stderr, "L%d: can't pchar, value out of range\n", line_number);
exit(EXIT_FAILURE);
}
printf("%c\n", head->n);
}
/**
* pstr - print the char values in the stack as a string,
* followed by a new line.
*
* @stack: the stack, represented as a pointer to a linked list
* @line_number: current line number of file being parsed
*/
void pstr(stack_t **stack, unsigned int line_number __attribute__((unused)))
{
stack_t *head = *stack;
while (head)
{
if (head->n == 0 || head->n > 127 || head->n < 0)
break;
printf("%c", head->n);
head = head->next;
}
printf("\n");
}