-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
98 lines (80 loc) · 2.72 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
94
95
96
97
#include<stdio.h>
#include<stdlib.h>
#include"Command.h"
#include"Operand.h"
#include<stdbool.h>
#include<string.h>
/* main entrance of the program */
int main() {
/* Initiate the Instructions Database */
bool ret = CmdDbInit();
if(ret == false) {
printf("Initialisation failed\n");
return 0;
}
/* Open the input file for reading and converting */
FILE* ip = fopen("./sampleip.txt", "r");
if(ip == NULL) {
printf("Opening of file failed\n");
return 0;
}
/* Tokenise the input read line and send it for processing */
char line[80];
char* token;
char** toSend = (char**)malloc(sizeof(char*)*4); /* An array of character pointers to send as an arguement
to the Processing function */
if(toSend == NULL) {
printf("Malloc failed \n");
return 0;
}
char** it; /* Iterator to iterate over toSend character array */
int count; // holds information of the number of tokens from the line */
while(fgets(line, 80, ip) != NULL) {
it = toSend;
count =0;
line[strlen(line)-1] = '\0'; /* remove the new line character from the line, since we are using fgets */
token = strtok(line, " "); /* tokenise the line with space character as delimiter */
while(token != NULL) {
*it = (char*)malloc(strlen(token)* sizeof(char));
if(*it == NULL) {
printf("Arguments read failed \n");
return 0;
}
strcpy(*it, token);
it++;
count++;
token = strtok(NULL, " ");
}
int base = -1; /* Base value */
if(count != 0) { /*if the line is not an Empty line */
// printf("Called Process_ins with value %d \n", count);
base = Process_ins(toSend, count);
}
if(base == -1) { /* The returned base value is not a valid */
printf("%s\n", "Wrong Instruction format");
continue;
}
else if(base == -2) { /* Instruction not present in database */
printf("%s \n", "Not a valid Instruction");
continue;
}
else { /* Everything normal */
printf("0x%02x ", base);
}
int i ;
it = toSend;
// printf("Count is %d \n", count);
if(count > 1)
it++;
/* Print the codes for the operands only and not instruction */
for(i = 1; i < count; i++) {
if(strlen(*it) == 1)
printf("0x%02x ", **it - '0');
else
printf("0x%02x ", *((*it)+1) - '0');
it++;
}
printf("\n");
}
return 0;
}