X-Git-Url: https://git.llucax.com/z.facultad/75.06/jacu.git/blobdiff_plain/69bddb46f46871d27bcf797cd6fae14430802380..1b23d92c000adeebfb1b68b9323b7df1c225614c:/src/statichuff/statichuff.c?ds=inline diff --git a/src/statichuff/statichuff.c b/src/statichuff/statichuff.c index e608bb2..2fed829 100644 --- a/src/statichuff/statichuff.c +++ b/src/statichuff/statichuff.c @@ -1,6 +1,5 @@ #include "statichuff.h" -#include void putbit(char bit, char restart, char flush, FILE *fp) { @@ -42,10 +41,10 @@ void shuff_cpynode(SHUFFNODE *node1, SHUFFNODE *node2) node1->rchild = node2->rchild; } -int shuff_compnode(SHUFFNODE *node1, SHUFFNODE *node2) +int shuff_compnode(const void *node1, const void *node2) { - if (node1->freq < node2->freq) return 1; - if (node1->freq > node2->freq) return -1; + if (((SHUFFNODE*)node1)->freq < ((SHUFFNODE*)node2)->freq) return 1; + if (((SHUFFNODE*)node1)->freq > ((SHUFFNODE*)node2)->freq) return -1; return 0; } @@ -121,7 +120,6 @@ SHUFFNODE *shuff_buildtree(SHUFFNODE *list, int listcount) SHUFFNODE *lastsymbol = list+(listcount-1); SHUFFNODE *node1,*node2; - /* Ordenamos inicialmente la inputlist para tomar las dos freqs min */ while (lastsymbol > list) { /* Ordeno la lista por frecuencia descendente */ qsort(list,listcount,sizeof(SHUFFNODE),shuff_compnode); @@ -256,6 +254,11 @@ int shuff_encode_file(char *inputfile, char *outputfile) /* Encodeo byte per byte */ shuff_encode_symbols(freqtable,codetable,inputfile,outputfile); + /* Free up memory baby yeah */ + free(freqtable); + free(inputlist); + free(codetable); + return 1; } @@ -317,50 +320,10 @@ int shuff_decode_file(char *inputfile, char *outputfile) fclose(fpsource); fclose(fpdest); + /* Free up memory baby yeah */ + free(ftable); + free(inputlist); + return 1; } -int main(int argc, char* argv[]) -{ - int cflag = 0; - int dflag = 0; - int tflag = 0; - long int volumesize = 0; - int ch; - - while ((ch = getopt(argc, argv, "cdt:")) != -1) { - - switch (ch) { - case 'c': cflag = 1; - break; - - case 'd': dflag = 1; - break; - - case 't': tflag = 1; - volumesize = atoi(optarg); - break; - - default: fprintf(stderr, "Usage: %s [-cdt] sourcefile targetfile\n", argv[0]); - return(2); - } - } - - if ( (argc == 1) || (cflag & dflag) || !(cflag | dflag) || ((argc - optind) < 2) ) { - fprintf(stderr, "Usage: %s [-cdt] sourcefile targetfile\n", argv[0]); - if ((tflag == 1) && (volumesize <= 0)) fprintf(stderr,"Error: The volume size must be a non-zero value\n"); - return (2); - } - - if (cflag == 1) { - /* Comprimo */ - return shuff_encode_file(argv[optind],argv[optind+1]); - } - - if (dflag == 1) { - /* Descomprimo */ - return shuff_decode_file(argv[optind],argv[optind+1]); - } - - return 0; -}