-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
106 lines (71 loc) · 2.36 KB
/
main.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
FILE *finalSrcFile;
char includedFileNames[50][50];
int countIncludedFiles = 0;
char* includeStatement = "include ";
int lengthOfIncludeStatement;
void processFile(char *inputFileName){
char libPath[100];
strcpy(libPath, "C:\\cmpl\\lib\\");
FILE* file = fopen(inputFileName, "r");
if (!file){ // Check file existance
strcat(libPath, inputFileName);
file = fopen(libPath, "r");
if (!file){
printf("File '%s' not found.\n", inputFileName);
exit(1);
}
}
// Check if file already included
int i;
for (i=0; i<countIncludedFiles; i++){
if (strcmp(includedFileNames[i], inputFileName) == 0){
printf("File '%s' already included.\n", inputFileName);
exit(1);
}
}
strcpy(includedFileNames[countIncludedFiles++], inputFileName);
int countSpaces;
char line[256];
char firstNLetters[lengthOfIncludeStatement+2];
char includedFileName[50];
char *pos;
while (fgets(line, sizeof(line), file)) {
countSpaces = 0;
while (isspace(line[countSpaces])){
countSpaces++;
}
strncpy(firstNLetters, line+countSpaces, lengthOfIncludeStatement);
firstNLetters[lengthOfIncludeStatement] = 0;
if (strcmp(firstNLetters, includeStatement) == 0){
// found include
strcpy(includedFileName, line + lengthOfIncludeStatement + countSpaces); // get the name of the file to be included
if ((pos=strchr(includedFileName, '\n')) != NULL)
*pos = '\0'; // replace \n with \0
//printf("!%s!\n", includedFileName);
processFile(includedFileName);
} else {
fprintf(finalSrcFile, "%s", line);
}
}
/* may check feof here to make a difference between eof and io failure -- network
timeout for instance */
fprintf(finalSrcFile, "\n");
fclose(file);
}
int main(int argc, char *argv[])
{
if (argc>1){
lengthOfIncludeStatement = strlen(includeStatement);
char* srcFileName = argv[1];
finalSrcFile = fopen("C:\\cmpl\\Final.code", "w+");
processFile(srcFileName);
fclose(finalSrcFile);
} else {
printf("No source file specified.\n");
}
return 0;
}