-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadline.c
45 lines (38 loc) · 904 Bytes
/
readline.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
#include "monty.h"
#define SIZE 1024
/**
* readline - function that reads a line of a instruction
* from a file
*
* Return: 0 on success, 1 on newline, 2 on error
*/
int readline(void)
{
char *line;
char buff[SIZE];
/*Read a line from stream*/
line = fgets(buff, SIZE, gv->stream);
/*Exit program if EOF is reached*/
if (line == NULL)
{
if (feof(gv->stream))
return (2);
kill("Error: Failed to read line from file");
}
/*Return 1 if line is newline character*/
if (strcmp(line, "\n") == 0 || line[0] == '#')
return (1);
/*VALIDATE INSTRUCTION*/
/*Clean and tokenize instruction*/
gv->instruction = tokenize(line, " \t\n");
/*Return 1 if instruction is blank spaces or is comment*/
if (gv->instruction == NULL || gv->instruction[0] == NULL ||
gv->instruction[0][0] == '#')
{
free(gv->instruction);
return (1);
}
/*Validate instructions*/
validate();
return (0);
}