rewrite fileio to load file into memory
This commit is contained in:
parent
173bdf08ec
commit
647f47e39d
46
src/fileio.c
46
src/fileio.c
@ -1,7 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define MAX_ASM_LINE_LENGTH 100
|
#define MAX_ASM_LINE_LENGTH 30
|
||||||
|
|
||||||
int isValidFileFormat(char filename[], char expectedExtension[]){
|
int isValidFileFormat(char filename[], char expectedExtension[]){
|
||||||
int *pointLoc = strrchr(filename, '.');
|
int *pointLoc = strrchr(filename, '.');
|
||||||
@ -34,23 +34,47 @@ int writeBinaryFile(word instrs[], char outputFile[]){
|
|||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int readAssemblyFile(char inputFile[]) {
|
char **readAssemblyFile(char inputFile[]) {
|
||||||
if (!isValidFileFormat(filename, "s")){
|
if (!isValidFileFormat(filename, "s")){
|
||||||
return(1);
|
return(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *fp;
|
FILE *fp = fopen(inputFile, "r");
|
||||||
char savedLine[MAX_ASM_LINE_LENGTH];
|
|
||||||
|
|
||||||
fp = fopen(inputFile, "r");
|
|
||||||
|
|
||||||
if (fp == NULL){
|
if (fp == NULL){
|
||||||
return(-1);
|
return(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (fgets(savedLine, MAX_ASM_LINE_LENGTH-1, fp) != NULL) {
|
int lineCount = 0;
|
||||||
//pass line to parser
|
char ch;
|
||||||
|
while ((ch = fgetc(fp)) != EOF)
|
||||||
|
{
|
||||||
|
if (ch == '\n' || ch == '\0')
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return(0);
|
char **heap = malloc(sizeof(char *) * count);
|
||||||
|
|
||||||
|
rewind(fp);
|
||||||
|
|
||||||
|
for( int i=0; i<count; i++) {
|
||||||
|
|
||||||
|
char tmp[512];
|
||||||
|
|
||||||
|
// read line into tmp
|
||||||
|
fgets(tmp, MAX_ASM_LINE_LENGTH-1, fp);
|
||||||
|
|
||||||
|
size = strlen(tmp)
|
||||||
|
|
||||||
|
char *line = malloc(size+1) // allocate mem for text line
|
||||||
|
|
||||||
|
strcpy(line, tmp);
|
||||||
|
|
||||||
|
heap[i] = line; // store line pointer
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return(heap);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user