-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_tools.c
160 lines (138 loc) · 3.11 KB
/
file_tools.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "monty.h"
/**
* open_file - opens a file
* @file_name: the file namepath
* Return: void
*/
void open_file(char *file_name)
{
FILE *fd = fopen(file_name, "r");
if (file_name == NULL || fd == NULL)
err(2, file_name);
read_file(fd);
fclose(fd);
}
/**
* read_file - reads a file
* @fd: pointer to file descriptor
* Return: void
*/
void read_file(FILE *fd)
{
int line_number, format = 0;
char *buffer = NULL;
size_t len = 0;
for (line_number = 1; getline(&buffer, &len, fd) != -1; line_number++)
{
format = parse_line(buffer, line_number, format);
}
free(buffer);
}
/**
* parse_line - Separates each line into tokens to determine
* which function to call
* @buffer: line from the file
* @line_number: line number
* @format: storage format. If 0 Nodes will be entered as a stack.
* if 1 nodes will be entered as a queue.
* Return: Returns 0 if the opcode is stack. 1 if queue.
*/
int parse_line(char *buffer, int line_number, int format)
{
char *opcode, *value;
const char *delim = "\n ";
if (buffer == NULL)
err(4);
opcode = strtok(buffer, delim);
if (opcode == NULL)
return (format);
value = strtok(NULL, delim);
if (strcmp(opcode, "stack") == 0)
return (0);
if (strcmp(opcode, "queue") == 0)
return (1);
find_func(opcode, value, line_number, format);
return (format);
}
/**
* find_func - find the appropriate function for the opcode
* @opcode: opcode
* @value: argument of opcode
* @format: storage format. If 0 Nodes will be entered as a stack.
* @ln: line number
* if 1 nodes will be entered as a queue.
* Return: void
*/
void find_func(char *opcode, char *value, int ln, int format)
{
int i;
int flag;
instruction_t func_list[] = {
{"push", add_to_stack},
{"pall", print_stack},
{"pint", print_top},
{"pop", pop_top},
{"nop", nop},
{"swap", swap_nodes},
{"add", add_nodes},
{"sub", sub_nodes},
{"div", div_nodes},
{"mul", mul_nodes},
{"mod", mod_nodes},
{"pchar", print_char},
{"pstr", print_str},
{"rotl", rotl},
{"rotr", rotr},
{NULL, NULL}
};
if (opcode[0] == '#')
return;
for (flag = 1, i = 0; func_list[i].opcode != NULL; i++)
{
if (strcmp(opcode, func_list[i].opcode) == 0)
{
call_fun(func_list[i].f, opcode, value, ln, format);
flag = 0;
}
}
if (flag == 1)
err(3, ln, opcode);
}
/**
* call_fun - Calls the required function.
* @func: Pointer to the function that is about to be called.
* @op: string representing the opcode.
* @val: string representing a numeric value.
* @ln: line numeber for the instruction.
* @format: Format specifier. If 0 Nodes will be entered as a stack.
* if 1 nodes will be entered as a queue.
*/
void call_fun(op_func func, char *op, char *val, int ln, int format)
{
stack_t *node;
int flag;
int i;
flag = 1;
if (strcmp(op, "push") == 0)
{
if (val != NULL && val[0] == '-')
{
val = val + 1;
flag = -1;
}
if (val == NULL)
err(5, ln);
for (i = 0; val[i] != '\0'; i++)
{
if (isdigit(val[i]) == 0)
err(5, ln);
}
node = create_node(atoi(val) * flag);
if (format == 0)
func(&node, ln);
if (format == 1)
add_to_queue(&node, ln);
}
else
func(&head, ln);
}