forked from ankush0259/CS50-pset6-Huff-n-Puff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.c
106 lines (92 loc) · 2.17 KB
/
dump.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/****************************************************************************
* dump.c
*
* Computer Science 50
* Problem Set 6
*
* Dumps contents of a huff-compressed file in human-readable form.
***************************************************************************/
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "forest.h"
#include "huffile.h"
#include "tree.h"
// first and last symbols to dump
#define FIRST '!'
#define LAST '~'
// number of columns to use
#define COLS 8
int main(int argc, char* argv[])
{
// ensure proper usage
if (argc != 2)
{
printf("Usage: %s input\n", argv[0]);
return 1;
}
// open input
Huffile* input = hfopen(argv[1], "r");
if (input == NULL)
{
printf("Could not open %s for reading.\n", argv[1]);
return 1;
}
// read in header
Huffeader header;
if (hread(&header, input) == false)
{
hfclose(input);
printf("Could not read header.\n");
return 1;
}
// check for magic number
if (header.magic != MAGIC)
{
hfclose(input);
printf("File was not huffed.\n");
return 1;
}
// check checksum
int checksum = header.checksum;
for (int i = 0; i < SYMBOLS; i++)
{
checksum -= header.frequencies[i];
}
if (checksum != 0)
{
hfclose(input);
printf("File was not huffed.\n");
return 1;
}
// determine number of rows to use
const int rows = (int) ceil((LAST - FIRST) / (double) COLS);
// dump frequencies in a nice table
printf("\n");
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < COLS; col++)
{
int index = FIRST + row + rows * col;
if (index > LAST)
{
break;
}
printf("%c %-6d ", index, header.frequencies[index]);
}
printf("\n");
}
printf("\n");
// dump bits contiguously
int bit;
while ((bit = bread(input)) != EOF)
{
printf("%d", bit);
}
printf("\n\n");
// close input
hfclose(input);
// that's all folks!
return 0;
}