Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void readFile(char *mem);
void checkDepends(char **mips, int lines);
void printOutput(int fLine, int sLine, char reg, int typeHaz);
char file_name[20];
int i = 0;
int main(int argc, char *argv[])
{
strcpy(file_name, argv[1]);
printf("%s", file_name);
readFile(file_name);
return 0;
}
void readFile(char *mem)
{
//declare a pointe of type File.
FILE *cfPtr;
//declare a character array to store the contents of the lines.
char **fileData;
fileData = malloc(20);
//declare a positioner to move about the memory array.
int linesRead = 0;
//point the pointer at the fopen function and test to see if it fails.
if((cfPtr = fopen(mem, "r")) == NULL)
{
//if it does, tell the user it couldn't open the file.
printf("Could not open file");
return;
}
//loop through the file, line by line, until the end of the file is reached.
while(!feof(cfPtr))
{
//scan each line through the pointer, store it in the character array.
fscanf(cfPtr, "%s", fileData[linesRead]);
//increment linesread to the next position in the array.
linesRead++;
}
//confirm the file was read.
printf("File read successfully\n");
printf("%d\n", linesRead);
for(i = 0; i < 40; i++)
printf("%c\n", fileData[i][0]);
//close the file the pointer is pointing at.
fclose(cfPtr);
linesRead--;
checkDepends(fileData, linesRead);
}
void checkDepends(char **mips, int lines)
{
int flag = 0;
char prevIns[3];
char currIns[3];
int counter = 1;
for(i = 0; i < 3; i++)
{
prevIns[i] = mips[i][0];
}
while(counter != lines)
{
for(i = 0; i < 3;i++)
{
currIns[i] = mips[counter][i];
}
if(strcmp(&prevIns[0], &currIns[1]) == 0 || strcmp(&prevIns[0], &currIns[2]) == 0)
{
if(strcmp(mips[counter], "lw") == 0)
{
printOutput((counter - 1 ), counter, prevIns[0], 2);
}
else if (strcmp(mips[counter], "sw") == 0)
{
printOutput((counter - 1 ), counter, prevIns[0], 2);
}
else if (strcmp(mips[counter], "bne") == 0)
{
printOutput((counter - 1 ), counter, prevIns[0], 2);
}
else
{
printOutput((counter - 1 ), counter, prevIns[0], 1);
}
counter++;
for(i = 0; i < 3; i++)
prevIns[i] = currIns[i];
}
}
}
void printOutput(int fLine, int sLine, char reg, int typeHaz)
{
switch(typeHaz)
{
case 1:
printf("%d:%d:%s:YES:NO", fLine, sLine, reg);
break;
case 2:
printf("%d:%d:%s:YES:YES", fLine, sLine, reg);
break;
case 3:
printf("%d:%d:%s:NO:NO", fLine, sLine, reg);
break;
}
}
Thanks all in advance for any help you can give!