-
Notifications
You must be signed in to change notification settings - Fork 0
/
monty.c
98 lines (85 loc) · 1.93 KB
/
monty.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
#include "monty.h"
char **opcode_read = NULL;
/**
* trim - removes leading and trailing white spaces
* @len: number of characters read
* @trimmed_line: buffer to line trimmed
*/
void trim(char **trimmed_line, size_t *len)
{
/* Trim leading and trailing whitespace */
while (**trimmed_line == ' ' || **trimmed_line == '\t' ||
**trimmed_line == '\n')
{
(*trimmed_line)++;
}
*len = strlen(*trimmed_line);
while (*len > 0 && ((*trimmed_line)[*len - 1] == ' ' ||
(*trimmed_line)[*len - 1] == '\t' || (*trimmed_line)[*len - 1] == '\n'))
{
(*trimmed_line)[*len - 1] = '\0';
*len = *len - 1;
}
}
/**
* execute_op - reads and execute opcode
* @file: file stream
* @head: pointer to list head
*/
void execute_op(FILE *file, stack_t **head)
{
char line[1024];
char ***argv = &opcode_read, *trimmed_line;
unsigned int line_number = 0;
size_t len = 0;
while (fgets(line, sizeof(line), file) != NULL)
{
/* Trim leading and trailing whitespace */
trimmed_line = line;
trim(&trimmed_line, &len);
/* Check if the line is empty or starts with a '#' character */
if (len == 0 || trimmed_line[0] == '#')
continue;
*argv = tokenize(trimmed_line, DELIM);
if (*argv == NULL)
{
/* Cleanup before leaving */
fprintf(stderr, "Error: malloc failed\n");
free_grid(*argv);
free_list(*head);
fclose(file);
exit(EXIT_FAILURE);
}
line_number++;
get_opcode(head, line_number);
free_grid(*argv);
}
if (!*head)
return;
free_list(*head);
}
/**
* main - entry point
* @ac: number of command line argument
* @argv: arguments passed
* Return: Exit status
*/
int main(int ac, char **argv)
{
stack_t *head = NULL;
FILE *file;
if (ac != 2)
{
fprintf(stderr, "USAGE: monty file\n");
exit(EXIT_FAILURE);
}
file = fopen(argv[1], "r+");
if (file == NULL)
{
fprintf(stderr, "Error: Can't open file %s\n", argv[1]);
exit(EXIT_FAILURE);
}
execute_op(file, &head);
fclose(file);
return (EXIT_SUCCESS);
}