-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilesOps.c
More file actions
160 lines (138 loc) · 3.01 KB
/
Copy pathFilesOps.c
File metadata and controls
160 lines (138 loc) · 3.01 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
#include "monty.h"
/**
* open - opens file
* @FileName: file namepath
* Return: none since void function
*/
void open(char *FileName)
{
FILE *fd = fopen(FileName, "r");
if (FileName == NULL || fd == NULL)
Error(2, FileName);
Read(fd);
fclose(fd);
}
/**
* Read - Reads file
* @fd: points to file descriptor
* Return: none since void function
*/
void Read(FILE *fd)
{
int LineNumber, format = 0;
char *buffer = NULL;
size_t len = 0;
for (LineNumber = 1; (getline(&buffer, &len, fd) != -1); LineNumber++)
{
format = ParseLine(buffer, LineNumber, format);
}
free(buffer);
}
/**
* FindFunct - find the matching function for the opcode
* @opcode: opcode
* @value: argument of opcode
* @format: storage format. If 0 Nodes will be entered as a stack.
* @LineNumber: line number
* if 1 nodes will be entered as a queue.
* Return: void
*/
void FindFunct(char *opcode, char *value, int LineNumber, int format)
{
int i;
int flag;
instruction_t func_list[] = {
{"push", Add2Stack},
{"pall", PrintStack},
{"pint", PrintTop},
{"pop", PopTop},
{"nop", nop},
{"swap", SwapNodes},
{"add", AddNodes},
{"sub", SubNodes},
{"div", DivNodes},
{"mul", MulNodes},
{"mod", ModNodes},
{"pchar", PrintChar},
{"pstr", PrintStr},
{"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)
{
CallFunc(func_list[i].f, opcode, value, LineNumber, format);
flag = 0;
}
}
if (flag == 1)
Error(3, LineNumber, opcode);
}
/**
* CallFunc - 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.
* @LineNumber: 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 CallFunc(op_funct func, char *op, char *val, int LineNumber, 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)
Error(5, LineNumber);
for (i = 0; val[i] != '\0'; i++)
{
if (isdigit(val[i]) == 0)
Error(5, LineNumber);
}
node = CreateNode(atoi(val) * flag);
if (format == 0)
func(&node, LineNumber);
if (format == 1)
Add2Queue(&node, LineNumber);
}
else
func(&head, LineNumber);
}
/**
* ParseLine - Separates each line into tokens to determine
* which function to call
* @buffer: line from the file
* @LineNumber: 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 ParseLine(char *buffer, int LineNumber, int format)
{
char *opcode, *value;
const char *delim = "\n ";
if (buffer == NULL)
Error(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);
FindFunct(opcode, value, LineNumber, format);
return (format);
}