-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_file.c
89 lines (81 loc) · 1.85 KB
/
parse_file.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
#include "monty.h"
/**
* handle_instruction - Handle file instructions
* @fp: Pointer to the file to be parsed.
* @stack: head node
* @instruct: instruction
* @line_number: line number
* @opcode: input buffer
* Return: Nothing
*/
void handle_instruction(stack_t **stack, unsigned int line_number,
char *instruct, char *opcode, FILE *fp)
{
char *argument;
void (*instruction_func)(stack_t **, unsigned int);
if (strcmp(instruct, "stack") == 0)
mode = STACK_MODE;
else if (strcmp(instruct, "queue") == 0)
mode = QUEUE_MODE;
else if (strcmp(instruct, "push") == 0)
{
argument = strtok(NULL, "\t\n ");
if (argument == NULL || !is_integer(argument))
{
fprintf(stderr, "L%d: usage: push integer\n", line_number);
free(opcode);
free_stack(*stack);
fclose(fp);
exit(EXIT_FAILURE);
}
if (mode == STACK_MODE)
add_dnodeint(stack, atoi(argument));
else if (mode == QUEUE_MODE)
add_dnodeint_end(stack, atoi(argument));
}
else
{
instruction_func = pick_func(instruct);
if (instruction_func)
instruction_func(stack, line_number);
else
handle_unknown_instruction(line_number, instruct, opcode, fp, stack);
}
}
/**
* parse_file - Parses the contents of the given file.
* @fp: Pointer to the file to be parsed.
* Return: Nothing
*/
void parse_file(FILE *fp)
{
char *opcode;
char *instruct = NULL;
int line_number = 1;
stack_t *stack = NULL;
opcode = malloc(BUFFER_SIZE * sizeof(char));
if (!opcode)
{
fprintf(stderr, "Error: malloc failed\n");
exit(EXIT_FAILURE);
}
while (fgets(opcode, BUFFER_SIZE, fp) != NULL)
{
if (opcode[0] == '\n')
{
line_number++;
continue;
}
instruct = strtok(opcode, "\t\n ");
if (instruct[0] == '#')
{
line_number++;
continue;
}
handle_instruction(&stack, line_number, instruct, opcode, fp);
line_number++;
}
free(opcode);
free_stack(stack);
fclose(fp);
}