-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
93 lines (86 loc) · 1.6 KB
/
main.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
#include "monty.h"
global_t dataStruct;
/**
* change_last_character - change the last character('\n') in a line per ('\0')
* @line: Line to modify
* Return: returnless
*/
void change_last_character(char *line)
{
int i = 0;
while (line[i] != '\n')
{
i++;
}
line[i] = '\0';
}
/**
* validate_line - validate if a line is diferent to ' ' or null
* @line: Line to modify
* Return: 1 to next or 0 to entry
*/
unsigned int validate_line(char *line)
{
int i = 0;
if (!line[0])
{
return (1);
}
while (line[i])
{
if (line[i] != ' ')
{
return (0);
}
i++;
}
return (1);
}
/**
* main - Entry point of project
* @argc: acount of arguments
* @argv: value of any argument
* Return: 0 is sucessfull
*/
int main(int argc, char *argv[])
{
FILE *fp = NULL;
char *c = NULL;
unsigned int line = 1, valLine = 0;
size_t sizeC = 0;
stack_t *stackMonty = NULL;
void (*cp_get_op_code)(stack_t **stack, unsigned int line);
if (argc != 2)
{
fprintf(stderr, "USAGE: monty file\n");
exit(1);
}
fp = fopen(argv[1], "r");
if (!fp)
{
fprintf(stderr, "Error: Can't open file %s\n", argv[1]);
exit(1);
}
while (getline(&c, &sizeC, fp) != -1)
{
valLine = 0, change_last_character(c), dataStruct.lineTokenized = c;
valLine = validate_line(dataStruct.lineTokenized);
if (!valLine)
{
cp_get_op_code = (*get_op_code)();
if (cp_get_op_code)
{
(*cp_get_op_code)(&stackMonty, line);
}
else
{
fprintf(stderr, "L%d: unknown instruction %s\n",
line, dataStruct.opCodeNoExist);
exit(1);
}
}
line++;
}
free(c), fclose(fp), free_dlistint(stackMonty);
return (0);
}