/*************************************************************************** htmlcompress.c ------------------- begin : Fri Mar 21 01:05:09 CST 2003 copyright : (C) 2003 by Andy Ruder email : aeruder@yahoo.com ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ /***************************************************************************/ /** File : HTMLCOMP.H -- Main include file **/ /** Program : HTML Compressor **/ /** Developer : Andy Ruder **/ /** Contact : Email - aeruder@yahoo.com **/ /** Copyright (C) 2003 Andy Ruder **/ /***************************************************************************/ /*********************************DEFINES***********************************/ #define VERSION_MAJOR 1 #define VERSION_MINOR 1 /********************************VARIABLES**********************************/ extern char error_message[500]; /**************************FUNCTION DECLARATIONS****************************/ void compress(int number, char *htmlfile); void get_name(int number, char *name); /**********************************INCLUDES*********************************/ #include #include #include #include "htmlcompress.h" /******************************GLOBAL VARIABLES*****************************/ char error_message[500] = ""; /************************************MAIN***********************************/ int main(int argc, char *argv[]) { int main_loop; printf("HTML Compressor by Andy Ruder.\n\r"); for (main_loop = 1; main_loop < argc; main_loop++) { compress(main_loop, argv[main_loop]); if (strlen(error_message) != 0) { printf("\n\r"); printf("%s\n\r", error_message); return -1; } } return 0; } /*****************************START COMPRESSING*****************************/ void compress(int number, char *htmlfile) { FILE *infile; FILE *outfile; char *name, *buffer; int write_flag, bytes_read, current_byte, abort_flag; char this_byte, last_byte; buffer = calloc(64000, 1); name = calloc(100, 1); infile = fopen(htmlfile, "rb"); if (infile == NULL) { sprintf(error_message, "Could not open input file : %s", htmlfile); return; } get_name(number, name); remove(name); outfile = fopen(name, "wb"); if (outfile == NULL) { sprintf(error_message, "Could not open output file : %s", name); return; } last_byte = 255; abort_flag = 0; while(!abort_flag) { current_byte = 0; bytes_read = fread(buffer, 1, 64000, infile); while(current_byte < bytes_read) { write_flag = 1; this_byte = buffer[current_byte]; if ((this_byte == ' ') && (last_byte == ' ')) { write_flag = 0; } /* if ((this_byte == ' ') && (last_byte = '>')) { write_flag = 0; } */ if (this_byte == 9) { this_byte = 32; } if (this_byte == 13) { write_flag = 0; } if (this_byte == 10) { write_flag = 0; } if (write_flag) { fputc(this_byte, outfile); last_byte = this_byte; } current_byte++; } if (!bytes_read) { abort_flag = 1; } } free(buffer); free(name); fclose(infile); fclose(outfile); return; } /*********************************GET NAME**********************************/ void get_name(int number, char *name) { char *temp_buffer; temp_buffer = calloc(20, 1); strcpy(name, "compressed"); sprintf(name + 10, "%03i", number); strcpy(name + 13, ".html"); printf("%s\n\r", name); free(temp_buffer); }