-
Notifications
You must be signed in to change notification settings - Fork 0
/
macro.c
92 lines (81 loc) · 1.67 KB
/
macro.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
84
85
86
87
88
89
90
91
#include "data.h"
void spread(char * file)
{
char *line, *macro , *macroname, *token;
char name[MAX];
FILE * fpf;
FILE * fp = fopen(file,"r");
strcpy(name, file);
strcat(name,".temp");
fpf = fopen(name,"w+");
line = malloc(sizeof(int)*MAX);
macro = malloc(sizeof(int)*MAX);
while (fgets(line,MAX,fp)!= NULL)
{
token = getword(line);
if (strcmp(token,"macro")==0)
{
while (strcmp(token,"endm")!=0)
{
fgets(line,MAX,fp);
token = getword(line);
}
fgets(line,MAX,fp);
}
fputs(line,fpf);
}
rewind(fp);
rewind(fpf);
while (fgets(line,MAX,fp)!= NULL)
{
token = getword(line);
if (strcmp(token,"macro")==0)
{
line = line+strlen(token);
line = skipspace(line);
macroname = getword(line);
fgets(line,MAX,fp);
macro = malloc(sizeof(int)*MAX);
strcpy(macro, "");
while (strstr(line,"endm") == NULL)
{
strcat(macro,line);
fgets(line,MAX,fp);
}
replace(file,macroname, macro);
}
}
remove(name);
}
void replace(char * file, char *old, char *new)
{
char name[MAX],name1[MAX];
FILE * fp;
FILE * fpf;
char *pos, *line, *temp;
int index = 0;
int olen;
strcpy(name,file);
strcpy(name1,file);
strcat(name, ".am");
strcat(name1,".temp");
temp = malloc(sizeof(int)*MAX);
line = malloc(sizeof(int)*MAX);
olen = strlen(old);
fp = fopen(name1,"r+");
fpf = fopen(name,"w+");
while (fgets(line,MAX,fp)!= NULL)
{
while ((pos = strstr(line, old)) != NULL)
{
strcpy(temp, line);
index = pos - line;
line[index] = '\0';
strcat(line, new);
strcat(line, temp + index + olen);
}
fputs(line,fpf);
}
fclose(fp);
fclose(fpf);
}