Пример #1
0
void HuffNode_destroy (HuffNode *tree)
{
 if (tree == NULL)
 {
  return;
 }
 
 HuffNode_destroy (tree -> left);
 HuffNode_destroy (tree -> right);
 free (tree);
}
Пример #2
0
void HuffNode_destroy ( HuffNode * array )
{
	//Destroys left, then right then the node itself
	if (array == NULL)
	{
		return;
	}
	HuffNode_destroy(array -> left);
	HuffNode_destroy(array -> right);
	free(array);

} 
Пример #3
0
static int process_file(const char * filename, int is_textmode)
{
    printf("Reading file: '%s', textmode = %s.\n",
           filename, (is_textmode ? "TRUE" : "FALSE"));

    FILE * fp = fopen(filename, "rb");
    if(fp == NULL) {
        fprintf(stderr, "Failed to open file '%s'\n", filename);
        return FALSE;
    }

    // Read in the Huffman Coding Tree
    HuffNode * codetree = NULL;
    if(is_textmode)
        codetree = HuffTree_readTextHeader(fp);
    else
        codetree = HuffTree_readBinaryHeader(fp);
    printf("Huffman coding tree is:\n");
    HuffNode_print(stdout, codetree);

    // Apply the tree
    size_t len;
    unsigned char * buffer = Huffman_applyTree(fp, codetree, &len);
    printf("\nMessage is:\n%s\n", buffer);

    // Cleanup
    fclose(fp);
    HuffNode_destroy(codetree);
    free(buffer);

    return TRUE;
}
Пример #4
0
int main (int argc, char ** argv)
{
  if (argc != 3)
    {
      printf("ERROR\n");
      printf("FORMAT: ./pa09 <input> <output>\n");
      return EXIT_FAILURE;
    }
  
  //OPEN FILE
  FILE * fh;
  fh = fopen(argv[1], "r");
  if (fh == NULL)
    {
      printf("ERROR\n");
      printf("INVALID INPUT FILE\n");
      return EXIT_FAILURE;
    }

  //CHECK METHOD
  const char * filename = argv[1];
  int size = strlen(filename);
  int method = 0; // DO BYTE
  if (filename[size - 1] == 't')
    {
      method = 1;
    }
  
  //CREATE TREE
  HuffNode * root = NULL;
  if (!method)
    {
      //do byte
      root = readbyByte(fh);
    }
  else
    {
      //do bit
      root = readbyBit(fh);
    }

  //PRINT TREE
  FILE * fptr;
  fptr = fopen(argv[2], "w");
  if (fptr == NULL)
    {
      HuffNode_destroy(root);
      fclose(fh);
      printf("ERROR\n");
      printf("INVALID OUTPUT FILE\n");
      return EXIT_FAILURE;
    }
  printtoFile(fptr, root);

  //FREE
  HuffNode_destroy(root);
  fclose(fh);
  fclose(fptr);

  return EXIT_SUCCESS;

}