-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreAssembler.c
83 lines (75 loc) · 2.17 KB
/
preAssembler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "constants.h"
#include "preAssembler.h"
/**
* @file preAssembler.c
* @author roei daniel
* @brief preAssembler replace all macros in file, create a new file with .am extension
* @copyright Copyright (c) 2022
*/
int replaceAllMacro(FILE *fp, char *fileName){
char lineBuffer[MAX_LINE], *cp, macroName[MAX_MACRO_NAME];
int counter;
MacroNode *replaceMacro, *macro = NULL;
MacroNode *temp = newMacroNodeList();
FILE *secondfp;
secondfp = fopen(fileName, "w");
writeNewFileName(secondfp, fileName);
while(fgets(lineBuffer, MAX_LINE, fp) != NULL){/*read each line*/
strcpy(macroName, "");
if((cp = isMacro(lineBuffer)) != NULL){
createNewNode(fp, cp, temp);
insertMacro(¯o, temp);
}
else{
sscanf(lineBuffer, " %s", macroName);
if((replaceMacro = findMacro(macroName, macro)) != NULL){
fprintMacro(replaceMacro, secondfp);
}
else
fprintf(secondfp, "%s", lineBuffer);
}
counter++;
}
fclose(secondfp);
free(temp);
return 0;
}
MacroNode *findMacro(char *macroName, MacroNode *macro){
MacroNode *temp = macro;
while(temp != NULL){
if(!strcmp( getMacroNodeName(temp), macroName))
return temp;
temp = getNext(temp);
}
return NULL;
}
char *isMacro(char *inputLine){
return strstr(inputLine, "macro");
}
void insertMacro(MacroNode **macro, MacroNode *newNode){
MacroNode *temp = *macro;
if(*macro == NULL)
*macro = newNode;
else{
while( getNext(temp) != NULL){
temp = getNext(temp);
}
setNextNode(temp, newNode);
}
}
void fprintMacro(MacroNode *macro, FILE *fp){
int i;
for(i = 0 ; i < getMacroLines(macro) ; i++)
fprintf(fp, "%s",getMacroNodeContentAtIndex(macro, i));
}
void writeNewFileName(FILE *fp, char *fileName){
char *tempFileName;
tempFileName = strstr(fileName, "/");
if(tempFileName != NULL)
fprintf(fp, "; file %s\n", tempFileName+1);
else
fprintf(fp, "; file %s\n", fileName);
}