-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreads_manage.c
94 lines (88 loc) · 1.43 KB
/
reads_manage.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
#include "monty.h"
/**
*montystruct_init - init struct
*/
void montystruct_init(void)
{
monty.file = NULL;
monty.line = NULL;
monty.stack = NULL;
monty.line_number = 1;
monty.is_queue = false;
}
/**
* openfile - input validation
* @argc: args count
* @filename: pointer to fiile
*/
void openfile(int argc, char *filename)
{
if (argc != 2)
{
fprintf(stderr, "USAGE: monty file\n");
exit(EXIT_FAILURE);
}
monty.file = fopen(filename, "r");
if (!monty.file)
{
fprintf(stderr, "Error: Can't open file %s\n", filename);
exit(EXIT_FAILURE);
}
}
/**
* read_line - reads and executes each line
*/
void read_line(void)
{
size_t len = 0;
ssize_t read;
char *opcode, *data;
while ((read = getline(&monty.line, &len, monty.file) != -1))
{
opcode = strtok(monty.line, " ");
if (*opcode == '#' || *opcode == '\n')
{
monty.line_number++;
continue;
}
else if (strcmp(opcode, "push") == 0)
{
data = strtok(NULL, " \n");
if (monty.is_queue)
{
push_queue(data);
}
else
push(data);
}
else
op_choose(&monty.stack, opcode);
monty.line_number++;
}
}
/**
* free_all - frees files
*/
void free_all(void)
{
fclose(monty.file);
free(monty.line);
free_stack(monty.stack);
}
/**
* free_stack - frees the stack
* @h: the head of stack
*
*
*/
void free_stack(stack_t *h)
{
stack_t *temp;
stack_t *location = h;
while (location)
{
temp = location;
location = location->next;
free(temp);
}
}