-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileprocess.c
146 lines (120 loc) · 2.86 KB
/
fileprocess.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
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
#include "fileprocess.h"
#include "lineprocess.h"
int _courrent_line_number = 0;
bool _error_flag = false;
extern int get_instructions_memory_amount();
extern int get_data_memory_amount();
extern void reset_memory_counters();
extern void reset_symbol_list();
extern void reset_output_list();
extern void reset_external_list();
extern void create_output_files(char * filename);
bool is_empty(char line[]);
void reset_all();
char * get_filename_prefix(char *);
void process_file(char *filename)
{
FILE *file;
char line[MAX_LINE_INPUT];
int ic_mem_cells, dc_mem_cells;
char * filenameprefix;
/* is valid file */
if((file = fopen(filename, "r"))) {
/* read each line */
while(fgets(line, MAX_LINE_INPUT, file)) {
/* line counter */
_courrent_line_number++;
/* skips comment and empty lines */
if(!is_empty(line)) {
first_loop_process(line);
}
}
/* reset line counter */
_courrent_line_number = 0;
/* save memory cells */
ic_mem_cells = get_instructions_memory_amount();
dc_mem_cells = get_data_memory_amount();
set_saved_memory_cells(ic_mem_cells, dc_mem_cells);
/* update data symbols adresses after instructions */
update_data_symbols_addresses();
/* reset ic and dc counters */
reset_counters();
rewind(file);
/* read each line */
while(fgets(line, MAX_LINE_INPUT, file)) {
/* line counter */
_courrent_line_number++;
/* skips comment and empty lines */
if(!is_empty(line)) {
second_loop_process(line);
}
}
/* stops action on error */
if (_error_flag) {
printf("found some erros, cannot create files, please fix and run agian. \n");
return;
}
filenameprefix = get_filename_prefix(filename);
create_output_files(filenameprefix);
/* reset all global variables fro the next file */
reset_all();
/* free allocated space */
filenameprefix = NULL;
filename = NULL;
free(filenameprefix);
free(filename);
} else {
printf("%s does not exist...\n", filename);
}
}
bool is_empty(char line[])
{
unsigned i = 0;
bool isEmpty = true;
while(line[i]) {
/* checks if char is empty (whitespace, tab, new line...)*/
if(!isspace(line[i])) {
/* if first character is a comment sign */
if(line[i] == COMMENT_SIGN) {
return true;
}
isEmpty = false;
break;
}
i++;
}
return isEmpty;
}
int get_file_line()
{
return _courrent_line_number;
}
void set_error_mode()
{
_error_flag = true;
}
void reset_all()
{
_courrent_line_number = 0;
_error_flag = false;
reset_counters();
reset_memory_counters();
reset_symbol_list();
reset_output_list();
reset_external_list();
}
char * get_filename_prefix(char * filename)
{
int i = 0;
char * newFileName;
while(filename[i] && filename[i] != '.') {
i++;
}
i = 0;
newFileName = (char *) malloc(i+1 * sizeof(char));
while(filename[i] && filename[i] != '.') {
newFileName[i] = filename[i];
i++;
}
return newFileName;
}