-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilefunctions.c
More file actions
263 lines (232 loc) · 8.01 KB
/
Copy pathfilefunctions.c
File metadata and controls
263 lines (232 loc) · 8.01 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "filefunctions.h"
FILE *open_file(char *filename, char *mode, char *ext)
{
ErrorObject error = {0};
char *ext_filename = NULL;
FILE *file = NULL;
if (filename == NULL || mode == NULL || ext == NULL)
{
/*If any of the parameters are null return null*/
return NULL;
}
/*Allocates memory for a new file name that includes the extension*/
ext_filename = (char *)malloc(strlen(filename) + strlen(ext) + 1);
if (ext_filename == NULL)
{
/*If memory allocation failed, handle the error and return NULL*/
handle_system_error(ERROR_MEMORY_ALLOCATION_FAILED);
return NULL;
}
/*Initializes all chars to avoid garbage values*/
memset(ext_filename, 0, strlen(filename) + strlen(ext) + 1);
/*Copies the original file name to the new file name*/
strcpy(ext_filename, filename);
/*Adds the extension to the new file name*/
strcat(ext_filename, ext);
/*Opens the file with the new file name and the mode*/
file = fopen(ext_filename, mode);
free(ext_filename); /*ext_file no longer needed, free allocation*/
if (file == NULL)
{
if (strcmp(mode, "r") == 0)
{
/*Failed to open file for reading, handle error and return null*/
fill_error_object(ERROR_FILE_NOT_FOUND, 0, filename, &error);
}
else
{
/*Failed to open file for writing, handle error and return null*/
fill_error_object(ERROR_FILE_OPEN_FAILED, 0, filename, &error);
}
handle_error(&error);
return NULL;
}
return file;
}
void remove_file(char *filename, char *ext)
{
/*Allocates memory for a new file name that includes the extension*/
char *ext_filename = (char *)malloc(strlen(filename) + strlen(ext) + 1);
if (ext_filename == NULL)
{
/*If memory allocation failed, handle the error and return*/
handle_system_error(ERROR_MEMORY_ALLOCATION_FAILED);
return;
}
/*Initializes all chars to avoid garbage values*/
memset(ext_filename, 0, strlen(filename) + strlen(ext) + 1);
/*Copies the original file name to the new file name*/
strcpy(ext_filename, filename);
/*Adds the extension to the new file name*/
strcat(ext_filename, ext);
/*Removes the file with the new file name*/
remove(ext_filename);
free(ext_filename);
}
int build_output_files(char *filename, BinaryNode *code_image, BinaryNode *data_image, Label *label_list, ExternLabel *extern_list, int ICF, int DCF)
{
FILE *ob_file;
FILE *ent_file;
FILE *ext_file;
BinaryNode *current_node = code_image;
Label *current_label = label_list;
ExternLabel *current_extern_label = extern_list;
int is_ent_file = FALSE;
int is_ext_file = FALSE;
/*CREATE FILES*/
ob_file = create_ob_file(code_image, data_image, filename);
ent_file = create_ent_file(label_list, filename, &is_ent_file);
ext_file = create_ext_file(extern_list, filename, &is_ext_file);
if (!ob_file || (!ent_file && is_ent_file) || (!ext_file && is_ext_file)) /*If one of the files is supposed to be created but not, return*/
{
/*If any of the files failed to open, return error and stop building files*/
if (ob_file)
fclose(ob_file);
if (ent_file)
fclose(ent_file);
if (ext_file)
fclose(ext_file);
return ERROR;
}
/*WRITE TO OBJECT FILE*/
fprintf(ob_file, "%7d %-6d\n", ICF - 100, DCF); /*Write the header to the object file*/
/*Write the code image to the object file,
looping in reverse for order to be from first to last*/
current_node = code_image;
while (current_node->next != NULL)
{
current_node = current_node->next;
}
while (current_node != code_image)
{
/*Print the line in hex*/
print_line_ob(ob_file, current_node->line, 0);
current_node = current_node->prev;
}
/*Write the data image to the object file,
looping in reverse for order to be from first to last*/
current_node = data_image;
while (current_node->next != NULL)
{
current_node = current_node->next;
}
while (current_node != data_image)
{
/*Print the line in hex*/
print_line_ob(ob_file, current_node->line, ICF);
current_node = current_node->prev;
}
/*Done writing the object file*/
fclose(ob_file);
/*WRITE TO ENTRY FILE*/
current_label = label_list;
if (is_ent_file && current_label != NULL)
{
while (current_label->next != NULL)
{
current_label = current_label->next;
}
while (current_label != label_list)
{
if (current_label->type == ENTRY)
{
/*If label is entry, write it to the entry file with its address*/
fprintf(ent_file, "%s %07d\n", current_label->name, current_label->address);
}
current_label = current_label->prev;
}
fclose(ent_file);
}
current_extern_label = extern_list;
if (is_ext_file)
{
while (current_extern_label->next != NULL)
{
current_extern_label = current_extern_label->next;
}
while (current_extern_label != extern_list)
{
if (current_extern_label->usage_address != 0 && current_extern_label->label->name != NULL)
{
/*For every external label usage, write it to the file with the usage address and name*/
fprintf(ext_file, "%s %07d\n", current_extern_label->label->name, current_extern_label->usage_address);
}
current_extern_label = current_extern_label->prev;
}
fclose(ext_file);
}
/*Files built, exiting file building*/
printf("* No errors found in the assembly process, output files built. *\n");
return 0;
}
FILE *create_ob_file(BinaryNode *code_image, BinaryNode *data_image, char *filename)
{
FILE *ob_file = NULL;
/*If theres at least one code or data line in the memory images, create an object file*/
if (code_image->next != NULL || data_image->next != NULL)
{
ob_file = open_file(filename, "w", OB_EXT);
if (ob_file == NULL)
{
return NULL;
}
return ob_file;
}
return NULL; /*If no code or data lines, return null*/
}
FILE *create_ent_file(Label *label_list, char *filename, int *is_ent_file)
{
FILE *ent_file = NULL;
Label *current_label = label_list;
while (current_label != NULL)
{
if (current_label->type == ENTRY)
{
/*If there is at least one entry label, create an entry file*/
ent_file = open_file(filename, "w", ENT_EXT);
if (ent_file == NULL)
{
return NULL;
}
*is_ent_file = TRUE;
return ent_file;
}
current_label = current_label->next;
}
return NULL; /*If no entry labels, return null*/
}
FILE *create_ext_file(ExternLabel *extern_list, char *filename, int *is_ext_file)
{
FILE *ext_file = NULL;
ExternLabel *current_extern_label = extern_list;
while (current_extern_label != NULL)
{
if (current_extern_label->label != NULL)
{
/*If there is at least one external label usage, create an extern file*/
ext_file = open_file(filename, "w", EXT_EXT);
if (ext_file == NULL)
{
return NULL;
}
*is_ext_file = TRUE;
return ext_file;
}
current_extern_label = current_extern_label->next;
}
return NULL; /*If no external labels usage, return null*/
}
int print_line_ob(FILE *ob_file, BinaryLine *current_node, int starting_index)
{
int i = 0;
if (current_node == NULL)
{
return ERROR;
}
for (i = 0; i < current_node->num_of_lines; i++)
{
/*Print the line in hex, address in acending order*/
fprintf(ob_file, "%07d %06x\n", current_node->address + i + starting_index, current_node->code[i]);
}
return 0;
}