Update fileio, skip new lines
This commit is contained in:
parent
b3a108d3a3
commit
fab4047d22
@ -35,11 +35,13 @@ int countLines(char *filename) {
|
|||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
char c;
|
char c;
|
||||||
|
char prevC = '\n';
|
||||||
|
|
||||||
while ((c = fgetc(file)) != EOF) {
|
while ((c = fgetc(file)) != EOF) {
|
||||||
if (c == '\n') {
|
if (c == '\n' && prevC != '\n') {
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
prevC = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
@ -71,6 +73,11 @@ char **readAssemblyFile(char filename[], int lineCount) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (*buffer == '\n') {
|
||||||
|
// Skip empty lines.
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
lines[currentLine] = malloc(strlen(buffer) + 1);
|
lines[currentLine] = malloc(strlen(buffer) + 1);
|
||||||
if (lines[currentLine] == NULL) {
|
if (lines[currentLine] == NULL) {
|
||||||
fprintf(stderr, "Error: Could not allocate memory to store the assembly line");
|
fprintf(stderr, "Error: Could not allocate memory to store the assembly line");
|
||||||
|
|||||||
@ -213,7 +213,8 @@ void parser_instruction(char asmLine[], a64inst_instruction *instr) {
|
|||||||
strcpy(stringptr, asmLine);
|
strcpy(stringptr, asmLine);
|
||||||
|
|
||||||
char *opcode = strtok(stringptr, " ");
|
char *opcode = strtok(stringptr, " ");
|
||||||
char *operands = strtok(NULL, "");
|
char *operands = strtok(stringptr, "");
|
||||||
|
|
||||||
|
|
||||||
if(strcmp(opcode, ".int") == 0){
|
if(strcmp(opcode, ".int") == 0){
|
||||||
//type is directive
|
//type is directive
|
||||||
@ -223,8 +224,8 @@ void parser_instruction(char asmLine[], a64inst_instruction *instr) {
|
|||||||
//type is label
|
//type is label
|
||||||
//add to symbol table
|
//add to symbol table
|
||||||
instr->type = a64inst_LABEL;
|
instr->type = a64inst_LABEL;
|
||||||
char *opcodeCpy = NULL;
|
char opcodeCpy[strlen(opcode)];
|
||||||
opcodeCpy = strcpy(opcodeCpy, opcode);
|
strcpy(opcodeCpy, opcode);
|
||||||
char *labelData = strtok(opcodeCpy, ":");
|
char *labelData = strtok(opcodeCpy, ":");
|
||||||
instr->data.LabelData.label = labelData;
|
instr->data.LabelData.label = labelData;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user