-
Notifications
You must be signed in to change notification settings - Fork 0
/
froot_win.c
103 lines (76 loc) · 2.61 KB
/
froot_win.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
/**
* WINDOWS VERSION
*
* This program parses lines from stdio.
* It will stdout every line of a file.
* When it sees a
* >>>"relative/path/to/file.txt"
* it will output that file instead of the next line.
* Afterwards it will continue with the original input.
* This has been created as a simple solution for including snippets like
* headers and footers to HTML-files.
*
* USAGE: executable ./path/to/sourcefile
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void outputFile(char *filePath) {
char path_buffer[_MAX_PATH];
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
// open file with fopen(), read only
FILE *sourceFile;
sourceFile = fopen(filePath, "r");
if(sourceFile == NULL) {
printf("Couldn't open file %s\n", filePath);
return;
} else {
// placeholder for each line being iterated over
char lineBuffer[500] = "";
// for each line of the sourceFile, repeat
while( fgets(lineBuffer, sizeof lineBuffer, sourceFile) ) {
// Reset to original path
char pathToActiveFile[500];
_splitpath(filePath, NULL, pathToActiveFile, NULL, NULL, NULL);
// check if the current line is not empty
if( sscanf(lineBuffer, "%500[^\n]\n", lineBuffer) ) {
// if line contains the string ">>>", this is an include-line
if(strstr(lineBuffer, ">>>")) {
// catch having >>> but not " "
if(strstr(lineBuffer, "\"")) {} else {
printf("ERROR: There are >>> but no quotation-marks in %s\n", filePath);
return;
}
// placeholder for the file path, which will now be read from the line
char *relativePathToIncludedFile;
// parse the full path out of the quotation-marks on the include-line
relativePathToIncludedFile = strtok(lineBuffer, "\"");
relativePathToIncludedFile = strtok(NULL, "\"");
char fullPathToIncludedFile[500] = "";
strcat(fullPathToIncludedFile, pathToActiveFile);
strcat(fullPathToIncludedFile, "");
strcat(fullPathToIncludedFile, relativePathToIncludedFile);
//call self-function recursively and do same on the included file
outputFile(fullPathToIncludedFile);
} else {
// if it's not an include-line, just print out the line to stdout
printf("%s\n", lineBuffer);
}
}
}
//close file when done
fclose(sourceFile);
}
}
int main(int argc, char * argv[]) {
if(argc < 2) {
printf("Some arguments may be missing!");
return 0;
}
outputFile(argv[1]);
return 0;
}