/*
 * Recursive function that get the huffman code of data and put into code.
 * Runs the huffman tree, add (strcat) "0" for left branch and "1" to right branch.
 */
void huffman_code (node_t* root, char* data, char* code) {
    if (root == NULL || code == NULL) {
    	return;
    }
    if (strstr(root->sample, data) == NULL) {
    	return;
    }
    if (count_ocurrencies(root->sample, '/') == 2) {
    	return;
    } else {
        if (strstr(root->left->sample, data)) {
            strncat(code, "0", strlen("0"));
            huffman_code(root->left, data, code);
        } else if (strstr(root->right->sample, data)) {
            strncat(code, "1", strlen("1"));
            huffman_code(root->right, data, code);
        }
    }
}
/*
 * Obtains the Huffman code of a sample
 */
char* get_code (node_t* root, uint8_t data) {
	char* code = (char*) malloc(sizeof(char) * MAX_HUFF_CODE);
	char* string_data = (char*) malloc(sizeof(char) * MAX_LENGHT_SAMPLE);

	memset(code, '\0', sizeof(char) * MAX_HUFF_CODE);
	memset(string_data, '\0', sizeof(char) * MAX_LENGHT_SAMPLE);
	sprintf(string_data, "/%d/", data);

	huffman_code(root, string_data, code);

	return code;
}
Beispiel #3
0
int             main (int argc, char *argv[])
{
    FILE           *instream;
    struct stat     fstat_input;
    long            bytes_in = 0L;
    int             mychar;
    time_t          now, start_stamp;

    setlocale (LC_ALL, "");
    dtsearch_catd = catopen (FNAME_DTSRCAT, 0);
    printf (catgets(dtsearch_catd, MS_huff, 40,
	"HUFFCODE Version %s\n"), AUSAPI_VERSION);

    /* validate user's command line arguments */
    user_args_processor (argc, argv);

    /* initialize tree table, using the table file if it exists */
    init_treebase ();
    if (total_count == 0L)
	printf ("%s", catgets(dtsearch_catd, MS_huff, 41,
	    "Huffman Code Tables will be newly created.\n"));
    else
	printf (catgets(dtsearch_catd, MS_huff, 42,
	    "Table '%s' already contains %ld Kbytes from previous runs.\n"),
	    filename_huf, total_count / 1000L);

    if (!input_file_specified && no_huffcode_file) {
	fprintf (stderr, catgets(dtsearch_catd, MS_huff, 43,
	    "645 Input file not specified and '%s' table file\n"
	    "   doesn't exist--nothing to do!\n"),
	    filename_huf);
	print_usage ();
	exit (2);
    }

    /* read the input file and count its bytes */
    if (input_file_specified) {
	if ((instream = fopen (filename_input, "rb")) == NULL) {
    BAD_INPUT_FILE:
	    fprintf (stderr, catgets(dtsearch_catd, MS_huff, 44,
		"Could not open input file '%s' or access status: %s\n"),
		filename_input, strerror (errno));
	    exit (2);
	}
	if (fstat (fileno (instream), &fstat_input) == -1)
	    goto BAD_INPUT_FILE;
	printf (catgets(dtsearch_catd, MS_huff, 45,
	    "Input file '%s' contains about %ld Kbytes.\n"),
	    filename_input, fstat_input.st_size / 1000L);

	time (&start_stamp);
	while ((mychar = getc (instream)) != EOF) {
	    hctree1[mychar].count++;
	    total_count++;

	    /* echo progress to user every so often */
	    if (!(++bytes_in % 10000L))
		printf (catgets(dtsearch_catd, MS_huff, 46,
		    "\r%ld%% done. %2ld Kbytes read.  "
		    "Estimate %3ld seconds to completion.   "),
		    (bytes_in * 100L) / fstat_input.st_size,
		    bytes_in / 1000L,
		    (fstat_input.st_size - bytes_in) *
		    (time (NULL) - start_stamp) / bytes_in);
	}	/* end read loop for each char in input file */

	putchar ('\n');
	fclose (instream);
    }	/* endif that processes input file */

    /* build huffman code tree, write out files */
    time (&now);	/* this will be the official tree id time
			 * stamp */
    printf (catgets(dtsearch_catd, MS_huff, 47,
	"Identifying timestamp will be '%ld'.\n"
	"%s Huffman Code Tables in '%s' and '%s'..."),
	now,
	(no_huffcode_file) ?
	    catgets(dtsearch_catd, MS_huff, 48, "Creating") :
	    catgets(dtsearch_catd, MS_huff, 49, "Rebuilding"),
	filename_huf,
	filename_huc);
    huffman_code (now);
    putchar ('\n');
    return 0;
}  /* end of function main */