-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathread_file.c
More file actions
179 lines (159 loc) · 3.84 KB
/
Copy pathread_file.c
File metadata and controls
179 lines (159 loc) · 3.84 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "monty.h"
/**
* open_file - Opens a file.
* @file_name: String with the name of the file.
*/
void open_file(char *file_name)
{
int file_check;
FILE *fd;
if (file_name == NULL)
err(2, file_name);
/*Checks if the file exists*/
file_check = access(file_name, R_OK);
if (file_check == -1)
err(2, file_name);
fd = fopen(file_name, "r");
if (fd == NULL)
err(2, file_name);
/*errors should be handled inside this function*/
read_file(fd);
/*might need to check for errors on closing a file.*/
fclose(fd);
}
/**
* read_file - Reads the content of a file line by line.
* @fd: Pointer to a file descriptor of an open file
*/
void read_file(FILE *fd)
{
int line_n;
int format;
char *lineprt;
size_t n;
lineprt = NULL;
n = 0;
format = 0;
if (fd == NULL)
err(2, "file_name");
/*Getting each line in the file*/
for (line_n = 1; getline(&lineprt, &n, fd) != EOF; line_n++)
{
format = interpret_line(lineprt, line_n, format);
}
free(lineprt);
}
/**
* interpret_line - Separates each line into tokens to determine
* which function to call.
* @lineptr: String representing a line in a file.
* @line_number: Line number for the opcode.
* @format: Format specifier. 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 interpret_line(char *lineptr, int line_number, int format)
{
const char *delim;
char *opcode;
char *value;
if (lineptr == NULL)
err(4);
delim = "\n ";
opcode = strtok(lineptr, delim);
/*hanlding blank lines*/
if (opcode == NULL)
return (format);
value = strtok(NULL, delim);
if (strcmp(opcode, "queue") == 0)
return (1);
else if (strcmp(opcode, "stack") == 0)
return (0);
find_func(opcode, value, line_number, format);
return (format);
}
/**
* find_func - Finds the appropite function to run the opcode instructions.
* @opcode: The operation code, It could be push, pall, ...
* @value: The possible value for the operation.
* @ln: Line number for the opcode.
* @format: Format specifier. If 0 Nodes will be entered as a stack.
* if 1 nodes will be entered as a queue.
*/
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;
/*Iterates through list to find the right function*/
for (flag = 1, i = 0; func_list[i].opcode != NULL; i++)
{
/*When 0 found the right opcode*/
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.
* @f: 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 f, char *op, char *val, int ln, int format)
{
stack_t *node;
int flag;
int i;
flag = 1;
if (strcmp(op, "push") == 0)
{
/*Checks if the number is negative and moves the val ptr*/
if (val != NULL && val[0] == '-')
{
val = val + 1;
flag = -1;
}
/*val is not a digit is the return value is 0*/
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)
f(&node, ln);
if (format == 1)
add_to_queue(&node, ln);
}
else
f(&head, ln);
}