Repository navigation
Expand file tree
/
Copy pathcreateOutput.c
More file actions
160 lines (131 loc) · 4.67 KB
/
Copy pathcreateOutput.c
File metadata and controls
160 lines (131 loc) · 4.67 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 "assembler.h"
/*triggers the various output creation functions based on necessity. returns 0 upon error, 1 if succeeded.*/
int createOutput(char path[], labelNode *labelTableHead, short binary[], metadata *instructionLength, metadata *dataLength, externCallList *externCallListHead){
/*create ob file*/
if(!printBase64(path, binary, instructionLength->value + dataLength->value, instructionLength, dataLength)){/*check for errors*/
printf(ERROR"Can't create ob file\n");
return 0;
}
/*create ent file if necessary*/
if(existsEntry(labelTableHead)){
if(!printEnt(path, labelTableHead)){/*check for errors*/
printf(ERROR"Can't create ent file\n");
return 0;
}
}
else{/*notify user that no entry file was saved*/
printf("No entry locations detected and therefore entry location file not saved.\n");
}
/*create ext file if necessary*/
if(existsExtern(labelTableHead)){
if(!printExt(path, externCallListHead)){/*check for errors*/
printf(ERROR"Can't create ext file\n");
return 0;
}
}
else{/*notify user that no extern file was saved*/
printf("No external references detected and therefore external reference file not saved.\n");
}
/*no errors*/
return 1;
}
/*creates the ob file in base 64. returns 0 upon error, 1 if succeeded.*/
int printBase64(char path[], short binary[], int length, metadata *instructionLength, metadata *dataLength){
int index = 0;
short left;
short right;
FILE *fp;
char *ob;
/*allocate memory for path to be stored*/
ob = calloc((strlen(path)+3)/sizeof(char), sizeof(char));
if(allocationCheck(1, ob)){
printf(ERROR"memory allocation failed during processing of %s, skipping file\n", path);
return 0;
}
/*copy path*/
strcpy(ob, path);
/*add ent to name*/
strcat(ob, ".ob");
if(!(fp = fopen(ob, "w"))){/*return error if fopen fails*/
return 0;
}
/*print lengths*/
fprintf(fp, "%d %d\n", instructionLength->value, dataLength->value);
/*Convert binary to base64 and print*/
while(index < length){
left = 0;
right = 0;
left |= getBits(6, 6, binary[index]);
right |= getBits(0, 6, binary[index++]);
fprintf(fp, "%c%c\n", getBase64(left), getBase64(right));
}
/*notify user that file was saved*/
printf("Assembled data stored in %s\n", ob);
/*close and free*/
fclose(fp);
free(ob);
/*no errors*/
return 1;
}
/*creates the ent file. returns 0 upon error, 1 if succeeded.*/
int printEnt(char path[], labelNode *labelTableHead){
char *ent;
FILE *fp;
labelNode *currentNode;
/*allocate memory for path to be stored*/
ent = calloc((strlen(path)+4)/sizeof(char), sizeof(char));
if(allocationCheck(1, ent)){
printf(ERROR"memory allocation failed during processing of %s, skipping file\n", path);
return 0;
}
/*copy path*/
strcpy(ent, path);
/*add ent to name*/
strcat(ent, ".ent");
if(!(fp = fopen(ent, "w"))){/*return error if fopen fails*/
return 0;
}
/*loop entire label list and print entry labels and addresses to file*/
for(currentNode = labelTableHead; currentNode != NULL; currentNode = currentNode->next){
if(currentNode->isEntry){
fprintf(fp, "%s\t%d\n", currentNode->label, currentNode->address);
}
}
/*notify user that file was saved*/
printf("Entry locations stored in %s\n", ent);
/*close and free*/
fclose(fp);
free(ent);
/*no errors*/
return 1;
}
/*creates the ext file. returns 0 upon error, 1 if succeeded.*/
int printExt(char path[], externCallList *externCallListHead){
char *ext;
FILE *fp;
externCallList *currentNode;
/*allocate memory for path to be stored*/
ext = calloc((strlen(path)+4)/sizeof(char), sizeof(char));
if(allocationCheck(1, ext)){
printf(ERROR"memory allocation failed during processing of %s, skipping file\n", path);
return 0;
}
/*copy path*/
strcpy(ext, path);
/*add ext to name*/
strcat(ext, ".ext");
if(!(fp = fopen(ext, "w"))){/*return error if fopen fails*/
return 0;
}
/*loop entire external call list and print labels and addresses to file*/
for(currentNode = externCallListHead; currentNode != NULL; currentNode = currentNode->next){
fprintf(fp, "%s\t%d\n", currentNode->label, currentNode->address);
}
/*notify user that file was saved*/
printf("Locations referencing externs stored in %s\n", ext);
/*close and free*/
fclose(fp);
free(ext);
/*no errors*/
return 1;
}