-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComp.c
More file actions
289 lines (269 loc) · 5.22 KB
/
Comp.c
File metadata and controls
289 lines (269 loc) · 5.22 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<string.h>
int min_bitsize = 8;
/*unsigned char compstr[100];*/
int count;
struct list
{
unsigned char ch;
unsigned int count;
struct list *l;
struct list *r;
struct list *next;
};
struct table
{
unsigned char ch;
long int count;
int nbits;
unsigned int bvalue;
};
struct File_Info
{
int Min_BitSize;
char *Fname;
long FSize;
}Fhead;
struct table tab[255];
typedef struct list node;
node* newnode(unsigned char,unsigned int);
node* insertlist(node*);
void displist(void);
void huffmantree(node*,int);
node* inserttree(void);
void disptable(void);
void compress();
void do_compress(char,int,int);
node *head=NULL;
FILE *Fp, *Fp1;
char Gstr;
void main()
{
node *temp,*h;
long int an[255];
char ch,c1;
int i;
if((Fp=fopen("Dcomp.txt","r"))==NULL)
{
printf("Error in file open");
exit(0);
}
clrscr();
for(i = 0;i<255;i++)
an[i] = 0;
/* p=str;*/
while ((ch=fgetc(Fp))!=EOF)
{
count++;
an[ch]++;
}
fclose(Fp);
for(i=0;i<255;i++)
{
if(an[i] != 0)
{
c1 =(char)i;
printf("%c",c1);
printf("->");
printf("%d",an[i]);
temp = newnode(c1,an[i]);
h = insertlist(temp);
}
}
printf("List contents\n");
displist();
printf("Tree constructioxn\n");
while(head->next != NULL)
{
h = inserttree();
head = head->next->next;
h = insertlist(h);
displist();
printf("\n");
}
printf("\ndisplay from tree\n");
huffmantree(head,0);
/* while(head!=NULL)
{
head=head->next;
free(head);
}
*/ printf("\ndisplay from table\n");
disptable();
compress();
}
node *newnode(unsigned char data,unsigned int occurance)
{
node *temp;
temp = (node*)malloc(sizeof(node));
temp->ch = data;
temp->count = occurance;
temp->l=NULL;
temp->r=NULL;
return(temp);
}
node *insertlist(node *temp)
{
node *duphead;
duphead = head;
if(head == NULL || head->count> temp->count)
{
head = temp;
head->next = duphead;
return head;
}
else
{
while(duphead != NULL)
{
if(duphead->count <= temp->count && (duphead->next == NULL || duphead->next->count > temp->count))
{
temp->next = duphead->next;
duphead->next = temp;
return head;
}
duphead = duphead->next;
}
duphead->next = temp;
}
return head;
}
void displist()
{
node *duphead;
duphead = head;
if(duphead==NULL)
{
printf("no elements");
}
else
{
while(duphead != NULL)
{
printf("\nch=%c\tcount=%d",duphead->ch,duphead->count);
duphead = duphead->next;
}
}
}
node *inserttree()
{
node *temp,*duphead;
duphead = head;
temp = (node*)malloc(sizeof(node));
temp->ch = '~';
temp->count = duphead->count + duphead->next->count;
temp->l = (node*)malloc(sizeof(node));
temp->l = duphead;
temp->r = (node*)malloc(sizeof(node));
temp->r = duphead->next;
return temp;
}
void huffmantree (node *T,int Direction)
{
static int nbits=1; /* To count no of bits*/
static int Val=0;
int shiftVal=0; /* To get the unique value*/
if (T !=NULL)
{
Val |= Direction;
if (T->l ==NULL && T->r ==NULL) /* We need only leaf*/
{
shiftVal=Val<<(8-nbits);
printf("\n%c\t%d\t%d\t%d\t%d\n",T->ch,T->count,nbits,Val,shiftVal);
if(nbits < min_bitsize)
min_bitsize=nbits; /* To get Minimum bit size*/
tab[T->ch].ch = T->ch;
tab[T->ch].count =T->count ;
tab[T->ch].nbits =nbits;
tab[T->ch].bvalue =shiftVal;
/*printf("\t%c\t%ld\t%d\t%d\n",tab[T->ch].ch,tab[T->ch].count,tab[T->ch].nbits,tab[T->ch].bvalue);*/
}
nbits++; /* Increase the no of bit count when traversing left side */
Val<<=1;
huffmantree(T->l,0);
nbits++; /* Increase the no of bit count when traversing right side*/
Val<<=1;
huffmantree(T->r,1);
}
nbits--; /* Decrease the no of bit count when back track*/
Val>>=1;
}
void disptable()
{
int i=0;
for(i=0;i<255;i++)
if(tab[i].count > 0)
printf("\t%c\t%ld\t%d\t%d\n",tab[i].ch,tab[i].count,tab[i].nbits,tab[i].bvalue);
}
void compress()
{
char i,c;
if((Fp= fopen("Dcomp.txt","r"))==NULL)
{
printf("Error in file open");
exit(0);
}
if((Fp1=fopen("Write.txt","w"))==NULL)
{
printf("Error in file open");
exit(0);
}
Fhead.Min_BitSize=min_bitsize;
strcpy(Fhead.Fname,"File.txt");
Fhead.FSize=count;
/*fputc(min_bitsize,Fp1);*/
fwrite(&Fhead,sizeof(Fhead),1,Fp1);
fwrite(&tab,sizeof(tab),1,Fp1);
while((c=fgetc(Fp))!=EOF)
{
do_compress(c,tab[c].nbits,tab[c].bvalue);
}
if (c==EOF){Gstr=c; do_compress(c,1,0);}
fcloseall();
}
void do_compress(char str,int nbits,int shiftval)
{
static int k;
int compval=128;
int j,flag;
static unsigned char wchar='\0',wchar1='\0';
for(;k<8;)
{
for(j=0;j<nbits;j++)
{
if(Gstr == EOF)
{
wchar=wchar1;
while(k < 8)
{
wchar<<=1;
compval>>=1;
k++;
}
/* printf("%c",wchar);*/
fputc(wchar,Fp1);
}
else
{
wchar |=(shiftval & compval)?1:0;
compval>>=1;
k++;
if(k==8)
{
/* printf("%c",wchar);*/
fputc(wchar,Fp1);
wchar = '\0';
k=0;
/*compval = 128;*/
wchar<<=1;
}
wchar1=wchar;
wchar<<=1;
}
}
break;
}
}