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 "global.h"
#include "fileio.h"
#define MAX_ASM_LINE_LENGTH 30
#define MAX_ASM_LINE_LENGTH 300
int isValidFileFormat(char filename[], char expectedExtension[]){
char *pointLoc = strrchr(filename, '.');
@ -16,67 +15,76 @@ int isValidFileFormat(char filename[], char expectedExtension[]){
return(0);
}
int writeBinaryFile(word instrs[], char outputFile[], int numInstrs){
if (!isValidFileFormat(outputFile, "bin")){
return(-1);
void writeBinaryFile(word instrs[], char outputFile[], int numInstrs) {
FILE *fp = fopen(outputFile, "wb");
if (fp == NULL) {
fprintf(stderr, "Error: Could not open file %s\n", outputFile);
exit(EXIT_FAILURE);
}
FILE *fp;
fp = fopen(outputFile, "wb");
if(fp == NULL){
return(-1);
}
fwrite(instrs, 4, sizeof(word) * numInstrs, fp);
fwrite(instrs, sizeof(word), numInstrs, fp);
fclose(fp);
return(0);
}
char **readAssemblyFile(char inputFile[]) {
if (!isValidFileFormat(inputFile, "s")){
return(NULL);
int countLines(char *filename) {
FILE *file = fopen(filename, "r");
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){
return(NULL);
}
int lineCount = 0;
char ch;
while ((ch = fgetc(fp)) != EOF)
{
if (ch == '\n' || ch == '\0')
{
lineCount++;
while ((c = fgetc(file)) != EOF) {
if (c == '\n') {
count++;
}
}
char **heap = malloc(sizeof(char *) * lineCount);
rewind(fp);
for( int i=0; i<lineCount; i++) {
char tmp[512];
// read line into tmp
fgets(tmp, MAX_ASM_LINE_LENGTH-1, fp);
int size = strlen(tmp);
char *line = malloc(size+1); // allocate mem for text line
strcpy(line, tmp);
heap[i] = line; // store line pointer
return count;
}
char **readAssemblyFile(char filename[], int lineCount) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr, "Error: Could not read file %s\n", filename);
exit(EXIT_FAILURE);
}
return(heap);
char **lines = malloc(sizeof(char *) * lineCount + 1);
if (lines == NULL) {
fprintf(stderr, "Error: Could not allocate memory to store the assembly lines");
exit(EXIT_FAILURE);
}
rewind(fp); // Back to the beginning of the file.
char buffer[MAX_ASM_LINE_LENGTH];
int currentLine = 0;
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__
#define __FILEIO__
#include <stdio.h>
#include <stdlib.h>
#include "global.h"
#define EXIT_FAILURE 1
char **readAssemblyFile(char inputFile[]);
int writeBinaryFile(word instrs[], char outputFile[], int numInstrs);
char **readAssemblyFile(char filename[], int lineCount);
void writeBinaryFile(word instrs[], char outputFile[], int numInstrs);
int countLines(char *filename);
#endif