Skip to content

Instantly share code, notes, and snippets.

@DreamVB
Last active July 30, 2016 09:38
Show Gist options
  • Save DreamVB/58c0141c848871cb29d64479b28d091f to your computer and use it in GitHub Desktop.
Save DreamVB/58c0141c848871cb29d64479b28d091f to your computer and use it in GitHub Desktop.
Compress htm and html sourcecode by reduceing not needed chars, allowing pages to load faster
/*
Compress HTML By DreamVB
Version 1.0
A small tool to compress htm and html files makeing them smaller
in size to allow your web pages to load faster.
Please feel free to to use as you like or if you want add your own features and let me see the changes.
Please let me know if you found this code usfull e. dreamvb@outlook.com
*/
#include <stdio.h>
#include <string>
int main(int argc, char* argv[]){
FILE *fin = NULL;
FILE *fout = NULL;
char c = '\0';
if(argc < 3){
printf("Usage: <InFile> <OutFile>\n");
return 0;
}
//Open inputfile.
fin = fopen(argv[1],"r");
//Try and see if inputfile opened.
if(fin == NULL){
printf("IO/Error cannot open file\n");
return 0;
}
//Open output file
fout = fopen(argv[2],"w");
//See if output file opened.
if(!fout){
printf("IO/Error cannot write file\n");
return 0;
}
while(!feof(fin)){
//Read char
c = getc(fin);
//Remove \n
switch(c){
case '\n':
c = fgetc(fin);
//Exit if at end of file
if(c == EOF){
break;
}
//Eat spaces
while((c == ' ') || (c=='\t'))
{
c = fgetc(fin);
}
//Eat line breaks
if(c == '\n'){continue;}
if(c == '\r'){continue;}
fputc(c,fout);
case '\t':
//Eat tab
continue;
}
//else{
if(!feof(fin)){
//Put char into output file.
fputc(c,fout);
}
}
//Close files
fclose(fin);
fclose(fout);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment