Update fileio, add countLine, rewrite read and write file funcs

This commit is contained in:
sBubshait 2024-06-12 00:50:00 +01:00
parent 269a150926
commit 53ab6a2bf6
2 changed files with 63 additions and 53 deletions

View File

@ -1,9 +1,8 @@
#include <stdio.h>
#include <string.h> #include <string.h>
#include "global.h" #include "global.h"
#include "fileio.h" #include "fileio.h"
#define MAX_ASM_LINE_LENGTH 30 #define MAX_ASM_LINE_LENGTH 300
int isValidFileFormat(char filename[], char expectedExtension[]){ int isValidFileFormat(char filename[], char expectedExtension[]){
char *pointLoc = strrchr(filename, '.'); char *pointLoc = strrchr(filename, '.');
@ -16,67 +15,76 @@ int isValidFileFormat(char filename[], char expectedExtension[]){
return(0); return(0);
} }
int writeBinaryFile(word instrs[], char outputFile[], int numInstrs){ void writeBinaryFile(word instrs[], char outputFile[], int numInstrs) {
FILE *fp = fopen(outputFile, "wb");
if (!isValidFileFormat(outputFile, "bin")){ if (fp == NULL) {
return(-1); fprintf(stderr, "Error: Could not open file %s\n", outputFile);
exit(EXIT_FAILURE);
} }
FILE *fp; fwrite(instrs, sizeof(word), numInstrs, fp);
fp = fopen(outputFile, "wb");
if(fp == NULL){
return(-1);
}
fwrite(instrs, 4, sizeof(word) * numInstrs, fp);
fclose(fp); fclose(fp);
return(0);
} }
char **readAssemblyFile(char inputFile[]) { int countLines(char *filename) {
if (!isValidFileFormat(inputFile, "s")){ FILE *file = fopen(filename, "r");
return(NULL); if (file == NULL) {
fprintf(stderr, "Error: Could not read file %s\n", filename);
exit(EXIT_FAILURE);
} }
FILE *fp = fopen(inputFile, "r"); int count = 0;
char c;
if (fp == NULL){ while ((c = fgetc(file)) != EOF) {
return(NULL); if (c == '\n') {
} count++;
int lineCount = 0;
char ch;
while ((ch = fgetc(fp)) != EOF)
{
if (ch == '\n' || ch == '\0')
{
lineCount++;
} }
} }
char **heap = malloc(sizeof(char *) * lineCount); return count;
}
rewind(fp);
char **readAssemblyFile(char filename[], int lineCount) {
for( int i=0; i<lineCount; i++) { FILE *fp = fopen(filename, "r");
if (fp == NULL) {
char tmp[512]; fprintf(stderr, "Error: Could not read file %s\n", filename);
exit(EXIT_FAILURE);
// read line into tmp }
fgets(tmp, MAX_ASM_LINE_LENGTH-1, fp);
char **lines = malloc(sizeof(char *) * lineCount + 1);
int size = strlen(tmp); if (lines == NULL) {
fprintf(stderr, "Error: Could not allocate memory to store the assembly lines");
char *line = malloc(size+1); // allocate mem for text line exit(EXIT_FAILURE);
}
strcpy(line, tmp);
rewind(fp); // Back to the beginning of the file.
heap[i] = line; // store line pointer
char buffer[MAX_ASM_LINE_LENGTH];
} int currentLine = 0;
return(heap); while (fgets(buffer, MAX_ASM_LINE_LENGTH, fp) != NULL) {
if (buffer[strlen(buffer) - 1] != '\n') {
// It was actually longer than the maximum.
// NOTE: I believe this must mean that this is a malformed line, so throw an error.
fprintf(stderr, "Error: Line %d in the file %s is too long\n", currentLine, filename);
exit(EXIT_FAILURE);
}
lines[currentLine] = malloc(strlen(buffer) + 1);
if (lines[currentLine] == NULL) {
fprintf(stderr, "Error: Could not allocate memory to store the assembly line");
exit(EXIT_FAILURE);
}
strcpy(lines[currentLine], buffer);
currentLine++;
}
if (ferror(fp)) {
fprintf(stderr, "Error: Could not read file %s", filename);
exit(EXIT_FAILURE);
}
return lines;
} }

View File

@ -1,11 +1,13 @@
#ifndef __FILEIO__ #ifndef __FILEIO__
#define __FILEIO__ #define __FILEIO__
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "global.h" #include "global.h"
#define EXIT_FAILURE 1 #define EXIT_FAILURE 1
char **readAssemblyFile(char inputFile[]); char **readAssemblyFile(char filename[], int lineCount);
int writeBinaryFile(word instrs[], char outputFile[], int numInstrs); void writeBinaryFile(word instrs[], char outputFile[], int numInstrs);
int countLines(char *filename);
#endif #endif