-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminhtml.c
More file actions
156 lines (140 loc) · 3.35 KB
/
minhtml.c
File metadata and controls
156 lines (140 loc) · 3.35 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// HTML file minifier - removes newlines, tabs and comments.
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <fnmatch.h>
#include <stdlib.h>
#include <stdbool.h>
char *fslash(char *fpath);
char *ofname(char *path, char *infilename, bool *f);
int main(int argc, char *argv[])
{
// Check for correct amount of args
if (argc != 2 && argc != 3)
{
printf("Usage: minhtml <input> [<out dir>[/<out file>]]\n");
printf("Minfy html file into stdout or <out dir>\n");
return 1;
}
char *infilename;
FILE *outptr;
bool isheap = false; // Keeps track of weather malloc was used in ofname()
// work out input filename from file path
char *slash = fslash(argv[1]);
// if argv[1] contains '/'
if (slash != NULL)
{
infilename = slash + 1;
}
else
{
infilename = argv[1];
}
// Open input file
FILE *inptr = fopen(argv[1], "r");
if (inptr == NULL)
{
printf("Could not open %s\n", infilename);
return 2;
}
// define output file pointer
if (argc == 2)
{
outptr = stdout;
}
else
{
char *ofn = ofname(argv[2], infilename, &isheap);
// Open output file
outptr = fopen(ofn, "w");
// If malloc was used in ofname, free ofn
if (isheap) free(ofn);
if (outptr == NULL)
{
printf("Could not create output file: %s\n", ofn);
return 3;
}
}
char buff;
char bigbuff[3];
int csize = sizeof(char);
// Read input file
while (fread(&buff, csize, 1, inptr))
{
// If char is newline or tab, do nothing
if (buff == '\n' || buff == '\t') continue;
else if (buff == '<')
{
fread(&bigbuff[0], csize, 3, inptr);
if (bigbuff[0] == '!' && bigbuff[1] == '-' && bigbuff[2] == '-')
{
do
{
// Read three chars
fread(&bigbuff[0], csize, 3, inptr);
// Move pointer back two chars
fseek(inptr, -2 * csize, SEEK_CUR);
}
// While read chars not equal "-->"
while (!(bigbuff[0] == '-' && bigbuff[1] == '-' && bigbuff[2] == '>'));
// Return pointer end of closing comment
fseek(inptr, 2 * csize, SEEK_CUR);
}
else
{
// If those 4 chars != "<!--", write them.
fwrite(&buff, csize, 1, outptr);
fwrite(&bigbuff, csize, 3, outptr);
}
}
else
{
fwrite(&buff, csize, 1, outptr);
}
}
fclose(inptr);
fclose(outptr);
}
// Slash finder function - returns pointer to last slash on a string
char *fslash(char *fpath)
{
// Find a slash
char *sptr = memmem(fpath, strlen(fpath), "/", sizeof(char));
// If no slash, return null
if (sptr == NULL) return NULL;
// If no more slashes, return this slash
else if (memmem(sptr + 1, strlen(sptr + 1), "/", sizeof(char)) == NULL) return sptr;
// Else, keep looking recursively
else return fslash(sptr + 1);
}
// Out file name composer
char *ofname(char *path, char *infilename, bool *f)
{
// If slashes are found in string
char *outslash = fslash(path);
if (outslash != NULL)
{
// And there is a string after the last slash
if (*(outslash + 1) != '\0')
{
return path;
}
else
{
// Concat out file path and in file name
char *outfilename = malloc(strlen(path) + strlen(infilename) + 1);
*f = true;
strcat(outfilename, path);
return strcat(outfilename, infilename);
}
}
else
{
char *outfilename = malloc(strlen(path) + strlen(infilename) + 2);
*f = true;
// Add a slash into the mix
strcat(outfilename, path);
strcat(outfilename, "/");
return strcat(outfilename, infilename);
}
}