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 <string.h>
|
||||
|
||||
#define MAX_ASM_LINE_LENGTH 100
|
||||
#define MAX_ASM_LINE_LENGTH 30
|
||||
|
||||
int isValidFileFormat(char filename[], char expectedExtension[]){
|
||||
int *pointLoc = strrchr(filename, '.');
|
||||
@ -34,23 +34,47 @@ int writeBinaryFile(word instrs[], char outputFile[]){
|
||||
return(0);
|
||||
}
|
||||
|
||||
int readAssemblyFile(char inputFile[]) {
|
||||
char **readAssemblyFile(char inputFile[]) {
|
||||
if (!isValidFileFormat(filename, "s")){
|
||||
return(1);
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
FILE *fp;
|
||||
char savedLine[MAX_ASM_LINE_LENGTH];
|
||||
FILE *fp = fopen(inputFile, "r");
|
||||
|
||||
fp = fopen(inputFile, "r");
|
||||
if (fp == NULL){
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
if(fp == NULL){
|
||||
return(-1);
|
||||
int lineCount = 0;
|
||||
char ch;
|
||||
while ((ch = fgetc(fp)) != EOF)
|
||||
{
|
||||
if (ch == '\n' || ch == '\0')
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
while (fgets(savedLine, MAX_ASM_LINE_LENGTH-1, fp) != NULL) {
|
||||
//pass line to parser
|
||||
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(0);
|
||||
return(heap);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user