-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles_handler.c
More file actions
111 lines (101 loc) · 4.33 KB
/
files_handler.c
File metadata and controls
111 lines (101 loc) · 4.33 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
/* Created by Omri Kaisari on 14/08/2020.*/
#include <string.h>
#include "files_handler.h"
#include "printer.h"
/************************************************** Internal functions ************************************************/
/**
* @brief gets a stings array as given to main and converts it to appropriate file-name strings array
* @param args the given array to main
* @param size the size of args
* @param stringArrayPtr pointer to which the functions returns the file name array
* @return
*/
Error file_generator_make_as_array(const char **args, int size, char ***stringArrayPtr);
/**
* @brief creates all the needed output files and checks if ext. and ent. are needed
* @param args the file name array of the input files
* @param error an error pointer to which error code might be returned
* @param reader a reader type with all the needed information for producing the files
* @param currentFileIndex the index of the current input file in args
*/
void write_output_files(const char **args, Error *error, Reader reader, int currentFileIndex);
/**
* @brief checks the file naming correctness and runs the first and the second pass of the assembly process.
* it also sends the relevant parameters to write_output_files in order to produce the needed output files
* @param args the file name array of the input files
* @param reader a reader type with all the needed information for producing the files
*/
void analyze_files(const char **args, Reader reader);
void analyze_files(const char **args, Reader reader) {
Error error;
int i = FIRST_FILE_INDEX;
while ((error = reader_load_next_file(reader)) != NoMoreFiles) {
if (error == FileTypeWrong || error == FileNotExist) {
++i;
continue;
}
error = reader_run_first_pass(reader);
if (reader_is_error_occurred(reader)) {
++i;
continue;
}
error = reader_run_second_pass(reader);
if (reader_is_error_occurred(reader)) {
++i;
continue;
}
write_output_files(args, &error, reader, i);
++i;
}
}
void write_output_files(const char **args, Error *error, Reader reader, int currentFileIndex) {
char fileName[MAX_LENGTH];
FILE *outputObject, *outputExtern, *outputEntry;
sprintf(fileName, "%s%s", args[currentFileIndex], OBJECT_FILE_EXTENSION);
outputObject = fopen(fileName, "w");
sprintf(fileName, "%s%s", args[currentFileIndex], EXTERN_FILE_EXTENSION);
outputExtern = fopen(fileName, "w");
print_object_and_extern_files(builder_get_instructions_list(reader_get_builder(reader)),
builder_get_data_items_list(reader_get_builder(reader)), outputObject,
outputExtern);
fclose(outputObject);
if (ftell(outputExtern) == 0)
remove(fileName);
else
fclose(outputExtern);
sprintf(fileName, "%s%s", args[currentFileIndex], ENTRY_FILE_EXTENSION);
outputEntry = fopen(fileName, "w");
(*error) = print_entry_file(builder_get_symbols_list(reader_get_builder(reader)), outputEntry);
fclose(outputEntry);
if ((*error) == NoEntries) {
remove(fileName);
}
}
Error file_generator_make_as_array(const char **args, int size, char ***stringArrayPtr) {
char currentFileName[MAX_LENGTH];
int i;
if (args == NULL)
return NoFiles;
*stringArrayPtr = malloc(sizeof(char *) * (size - 1));
for (i = FIRST_FILE_INDEX; i < size; ++i) {
strcpy(currentFileName, args[i]);
strcat(currentFileName, ASSEMBLER_FILE_EXTENSION);
(*stringArrayPtr)[i - 1] = malloc(sizeof(char) * (strlen(currentFileName) + 1));
strcpy((*stringArrayPtr)[i - 1], currentFileName);
}
return NoErrorsFound;
}
/************************************************** Functions implementations *****************************************/
void assemble(const char **args, int size) {
char **fileNames;
Reader reader;
if (size == 1) {
error_print(NoInputFiles, NO_FILE, NULL);
return;
}
file_generator_make_as_array(args, size, &fileNames);
reader = reader_create((const char **) fileNames, size - 1);
analyze_files(args, reader);
reader_destroy(reader);
free_string_array(fileNames, size - 1);
}