Ejemplo n.º 1
0
int main(int argc,char **argv)
{
  FILE *fp;
  huffman_tree *huff;
  char prefix_arr[1000],*prefix;
  prefix=prefix_arr;
  /*start to statistics the word count*/
  fp=fopen("test.txt","r");
  word_table=hash_create(HASH_TABLE);

  while(get_word(fp)!=EOF){
    printf("-------%s--------\n",word);
    if(word[0]!='\0')
      word_count(word);
  }
  hash_print(word_table);
  /*start to generate the huffman code*/
  huff=huffman_generate_code(word_table);
  print_huffman_tree(huff);
  printf("print the huffman code\n");
  print_huffman_code(huff,"");
  /*start to write the huffman code into file*/
  
  return 0;
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
#if defined(_WIN32) // && defined(_DEBUG)
	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif


	if (argc != 4)
	{
		std::cerr << "Invalid number of arguments." << std::endl;
		std::cerr << "Usage: Huffman.exe -c/-u <infile> <outfile>" << std::endl;

		return 1;
	}

	std::ifstream in_file(argv[2], std::ios::in | std::ios::binary);
	if (!in_file)
	{
		std::cerr << "Couldn't open " << argv[2] << std::endl;
		return 2;
	}

	std::ofstream out_file(argv[3], std::ios::out | std::ios::binary | std::ios::trunc);
	if (!out_file)
	{
		std::cerr << "Couldn't open " << argv[3] << std::endl;
		return 3;
	}

	std::string arg(argv[1]);

	if (arg == "-c")
	{
		OFileBitstream stream(out_file);
		std::istreambuf_iterator<char> in_iter(in_file);

		std::cerr << "Construindo arvore..." << std::endl;
		Dictionary<unsigned char>* tree = build_huffman_tree(in_iter, std::istreambuf_iterator<char>());
		std::ifstream::pos_type size = in_file.tellg();
		in_file.seekg(0);
		std::cerr << "Comprimindo arquivo..." << std::endl;
		huffman_compress(tree, stream, in_iter, std::istreambuf_iterator<char>(), size);
		delete tree;
	} 
	else if (arg == "-u")
	{
		in_file.seekg(0, std::ios::end);
		unsigned long long size = in_file.tellg();
		in_file.seekg(0);

		IFileBitstream istream(in_file);
		std::cerr << "Lendo arvore..." << std::endl;
		Dictionary<unsigned char>* tree = readNode(istream);
		size -= in_file.tellg();
		std::cerr << "Descomprimindo arquivo..." << std::endl;
		huffman_uncompress(istream, out_file, tree, size);
		delete tree;
	}
	else if (arg == "--make-tree")
	{
		std::istreambuf_iterator<char> in_iter(in_file);

		Dictionary<unsigned char>* tree = build_huffman_tree(in_iter, std::istreambuf_iterator<char>());
		print_huffman_tree(tree, out_file);
		delete tree;
	}
	else if (arg == "--read-tree")
	{
		IFileBitstream istream(in_file);
		Dictionary<unsigned char>* tree = readNode(istream);
		print_huffman_tree(tree, out_file);
		delete tree;
	}

	out_file.close();
	in_file.close();


	return 0;
}