-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_stream.c
64 lines (62 loc) · 1.48 KB
/
file_stream.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
#include "monty.h"
int num_store = 0;
/**
* file_stream - get tokens from monty bytecode file
* @av : arguments array
*
* Return: 0 if exited successfully, otherwise 1
*/
int file_stream(char **av)
{
FILE *instream = NULL;
ssize_t numchar = 0;
size_t len = MAXLEN;
int line = 1, opDONE = 0, numberDONE = 0;
stack_t **top = malloc(sizeof(stack_t) * STACK_SIZE);
char *op_instruction = NULL, *delim = " \r\f\v\t\n";
char **lineptr = malloc(sizeof(char) * MAXLEN);
char **token_str = malloc(sizeof(char) * MAXLEN);
if (lineptr == NULL || token_str == NULL)
{
fprintf(stderr, "Error: malloc failed");
return (EXIT_FAILURE);
}
instream = fopen(av[1], "r");
if (instream == NULL)
{
fprintf(stderr, "Error: Can't open file %s", av[1]);
return (EXIT_FAILURE);
}
while ((numchar = getline(lineptr, &len, instream)) != -1)
{
while ((*token_str = strtok(*lineptr++, delim)) != NULL)
{
if (!is_alpha(token_str[0]))
{
fprintf(stdout, "OP Instruction:=> %s\n",
token_str[0]);
op_instruction = token_str[0];
opDONE = 1;
}
if (!is_digit(token_str[0]))
{
fprintf(stdout, "Processed Int:=> %s\n",
token_str[0]);
num_store = atoi(token_str[0]);
fprintf(stdout, "File_stream Int:=> %d\n",
num_store);
numberDONE = 1;
}
if (opDONE && numberDONE)
break;
}
get_opcode(op_instruction)(top, line);
opDONE = numberDONE = 0;
line++;
}
free_stack_t(top);
free(lineptr);
free(token_str);
fclose(instream);
return (EXIT_SUCCESS);
}