-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_funcs1.c
111 lines (98 loc) · 2.34 KB
/
handler_funcs1.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "monty.h"
#include "lists.h"
/**
* pint_handler - handles the pint instruction
* @stack: double pointer to the stack to push to
* @line_number: number of the line in the file
*/
void pint_handler(stack_t **stack, unsigned int line_number)
{
stack_t *head = *stack;
if (!head)
{
dprintf(STDERR_FILENO, PINT_FAIL, line_number);
free_all(1);
exit(EXIT_FAILURE);
}
printf("%d\n", head->n);
}
/**
* pop_handler - handles the pop instruction
* @stack: double pointer to the stack to push to
* @line_number: number of the line in the file
*/
void pop_handler(stack_t **stack, unsigned int line_number)
{
stack_t *temp = *stack;
if (!temp)
{
dprintf(STDERR_FILENO, POP_FAIL, line_number);
free_all(1);
exit(EXIT_FAILURE);
}
delete_dnodeint_at_index(stack, 0);
}
/**
* swap_handler - handles the swap instruction
* @stack: double pointer to the stack to push to
* @line_number: number of the line in the file
*/
void swap_handler(stack_t **stack, unsigned int line_number)
{
stack_t *temp = *stack, *node = NULL;
int num;
if (dlistint_len(*stack) < 2)
{
dprintf(STDERR_FILENO, SWAP_FAIL, line_number);
free_all(1);
exit(EXIT_FAILURE);
}
temp = get_dnodeint_at_index(*stack, 0);
num = temp->n;
delete_dnodeint_at_index(stack, 0);
node = insert_dnodeint_at_index(stack, 1, num);
if (!node)
{
dprintf(STDERR_FILENO, MALLOC_FAIL);
free_all(1);
exit(EXIT_FAILURE);
}
}
/**
* add_handler - handles the add instruction
* @stack: double pointer to the stack to push to
* @line_number: number of the line in the file
*/
void add_handler(stack_t **stack, unsigned int line_number)
{
int sum = 0;
stack_t *node = NULL;
stack_t *node_0 = get_dnodeint_at_index(*stack, 0);
stack_t *node_1 = get_dnodeint_at_index(*stack, 1);
if (dlistint_len(*stack) < 2)
{
dprintf(STDERR_FILENO, ADD_FAIL, line_number);
free_all(1);
exit(EXIT_FAILURE);
}
sum = node_0->n + node_1->n;
delete_dnodeint_at_index(stack, 0);
delete_dnodeint_at_index(stack, 0);
node = add_dnodeint(stack, sum);
if (!node)
{
dprintf(STDERR_FILENO, MALLOC_FAIL);
free_all(1);
exit(EXIT_FAILURE);
}
}
/**
* nop_handler - handles the nop instruction
* @stack: double pointer to the stack to push to
* @line_number: number of the line in the file
*/
void nop_handler(stack_t **stack, unsigned int line_number)
{
(void)stack;
(void)line_number;
}