/
main.cpp
367 lines (325 loc) · 8.58 KB
/
main.cpp
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//
// main.cpp
// cip
//
// Created by cms on 15/10/26.
// Copyright © 2015年 Maosen Chen. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <ctype.h>
#include <queue>
#include <cstring>
#include <sstream>
#include <string.h>
#define Maxn 330
#define NodeNum 1100
#define CompressedFlag "###!!!CIP!!!COMPRESSED!!!FILE\n"
using namespace std;
struct Info
{
int num;
int value;
int position;
bool operator < (const Info & a) const
{
return a.num < num;
}
} charInfo[Maxn];
int child[NodeNum][2]; //left is 0, right is 1
int parent[NodeNum][2]; //parent node is 0, from left or right decided by 1
priority_queue<struct Info> charPrioriytQueue;
int nodeNum, rootNodeNum, sumCharNum;
int positionToValue[Maxn], lastOneDigit;
void init()
{
for(int i=0; i < Maxn; i++)
{
charInfo[i].num = 0;
charInfo[i].value = 0;
charInfo[i].position = 0; //The char in which node
}
memset(child, -1, sizeof(child));
memset(parent, -1, sizeof(parent));
nodeNum = 0;
}
void writeCompressInfo(FILE * out)
{
//FILE * out = fopen(targetFilename, "a+");
fputs(CompressedFlag, out);
fprintf(out, " %d %d ",rootNodeNum, sumCharNum);
int j = 0;
for(int i=0; i<Maxn; i++) //Decompress only need leaf position and value
{
if(charInfo[i].num)
{
j++;
fprintf(out, "%d %d", charInfo[i].position, charInfo[i].value);
if(j != sumCharNum)
{
fprintf(out, " ");
}
else
{
fprintf(out, "\n");
}
}
}
fprintf(out, "%d ", nodeNum);
for(int i=sumCharNum; i<nodeNum; i++) //Decompress only need child array
{
fprintf(out, "%d %d", child[i][0], child[i][1]);
if(i != (nodeNum-1))
{
fprintf(out, " ");
}
else
{
fprintf(out, "\n");
}
}
//fclose(out);
}
void compresss(FILE * in, const char * targetFilename)
{
init();
unsigned char c;
while(fscanf(in,"%c",&c) != EOF)
{
charInfo[c].num++;
charInfo[c].value = int(c);
//printf("%c ",c);
}
for(int i=0; i < Maxn; i++)
{
if(charInfo[i].num)
{
//printf(":::%c ",charInfo[i].value);
charInfo[i].position = nodeNum++;
charPrioriytQueue.push(charInfo[i]);
}
}
sumCharNum = nodeNum;
while(charPrioriytQueue.size() > 1)
{
struct Info first, second;
first = charPrioriytQueue.top();
charPrioriytQueue.pop();
second = charPrioriytQueue.top();
charPrioriytQueue.pop();
struct Info next;
next.num = first.num + second.num;
next.position = nodeNum++;
child[next.position][0] = first.position;
child[next.position][1] = second.position;
parent[first.position][0] = next.position;
parent[second.position][0] = next.position;
parent[first.position][1] = 0; //from left
parent[second.position][1] = 1; //from right
charPrioriytQueue.push(next);
}
rootNodeNum = nodeNum-1;
FILE * out = fopen(targetFilename, "w+");
writeCompressInfo(out);
fseek(in, 0, SEEK_SET);
int current = 0, digitNum = 0;
//string compressString = "";
while(fscanf(in, "%c", &c) != EOF)
{
//printf("%c",c);
int leafPosition = charInfo[c].position;
string temp = "";
while(leafPosition != rootNodeNum)
{
if(parent[leafPosition][1])
{
temp += "1";
}
else
{
temp += "0";
}
leafPosition = parent[leafPosition][0];
}
for(int i=temp.length()-1; i>=0; i--)
{
digitNum++;
if(temp[i]=='0')
current = current<<1;
else
current = (current<<1)+1;
if(digitNum == 8)
{
digitNum = 0;
//printf("%d:%d",current,compressString.length());
fputc(current, out);
//stringstream ss;
//ss<<(unsigned char)current;
//string tt = ss.str();
//compressString = compressString + tt;
current = 0;
}
}
}
if(digitNum)
{
//printf("%d\n",current);
//stringstream ss;
//ss<<(unsigned char)current;
//string tt = ss.str();
//compressString = compressString + tt;
//printf("digit::%d\n", digitNum);
fputc(current, out);
//fprintf(out, "%s", compressString.c_str());
string temp = CompressedFlag;
fseek(out, temp.length(), SEEK_SET);
fprintf(out, "%d", digitNum);
}
else
{
string temp = CompressedFlag;
fseek(out, temp.length(), SEEK_SET);
fprintf(out, "8");
}
printf("Compress successful done.\n");
fclose(out);
fclose(in);
return ;
}
int inverse(int a, bool last)
{
int b = 0;
int end = 8;
if(last)
end = lastOneDigit;
for(int i=0; i<end; i++)
{
if(a&1)
b = (b<<1)+1;
else
b = b<<1;
a = a>>1;
}
return b;
}
void decompress(FILE * in, const char * targetFilename)
{
init();
fscanf(in, "%d %d %d",&lastOneDigit, &rootNodeNum, &sumCharNum);
printf("r and s %d %d\n",rootNodeNum, sumCharNum);
for(int i=0; i<sumCharNum; i++)
{
int a,b;
fscanf(in, "%d%d", &a, &b);
positionToValue[a] = b;
}
fscanf(in, "%d", &nodeNum);
for(int i=sumCharNum; i<nodeNum; i++)
{
fscanf(in, "%d%d", &child[i][0], &child[i][1]);
}
fgetc(in); //get the Enter key
//fscanf(in, "%d\n", &lastOneDigit);
//printf("last:%d\n", lastOneDigit);
unsigned char c;
unsigned char nextc;
FILE * out = fopen(targetFilename, "w+");
int current = rootNodeNum;
bool lastOne = false;
fscanf(in, "%c", &c);
while(true)
{
if(fscanf(in, "%c", &nextc) == EOF)
{
lastOne = true;
}
//printf("c:%d nextc:%d \n", c, nextc);
int value = inverse(c, lastOne), i = 1;
//printf("value:%d\n", value);
while(i <= 8)
{
i++;
if(value&1)
{
current = child[current][1];
}
else
{
current = child[current][0];
}
if((child[current][0] == -1) && (child[current][1] == -1))
{
fputc(positionToValue[current], out);
//printf("->->%d i:%d ", positionToValue[current], i);
//printf("%c", positionToValue[current]);
if(lastOne && i > lastOneDigit)
{
printf("Decompress successful.\n");
return;
}
current = rootNodeNum;
}
value = value>>1;
}
c = nextc;
}
}
void handle(const char * filename, const char * targetFilename)
{
FILE * in;
in = fopen(filename, "r");
if(in == NULL)
{
printf("File %s open failed.",filename);
return ;
}
char isComOrDecom[Maxn];
fgets(isComOrDecom, Maxn, in);
if(!strcmp(isComOrDecom, CompressedFlag))
{
printf("Decompressing file %s to file %s begins ...\n", filename, targetFilename);
decompress(in, targetFilename);
}
else
{
fseek(in, 0, SEEK_SET);
printf("Compress file %s to file %s begins ...\n",filename, targetFilename);
compresss(in, targetFilename);
}
return ;
}
void testReadAChar()
{
FILE * in = fopen("/Users/cms/UCAS/code/cip/a","r");
unsigned char c;
while(fscanf(in, "%c", &c) != EOF)
{
//fscanf(in, "%c", &c);
//c = fgetc(in);
printf("%d\n", c);
}
return ;
}
void testWriteSize()
{
FILE * out = fopen("/Users/cms/UCAS/code/cip/testsize","a+");
fprintf(out, " 4");
fseek(out, 0, SEEK_SET);
fprintf(out, "456");
fclose(out);
}
int main(int argc, const char * argv[]) {
//cout << argc << " " << argv[0];
//cout << argv[1];
//testReadAChar();
//testWriteSize();
if(argc != 3)
{
printf("Argument must be 3.Eg ./main.cpp sourceFile targetFile\n");
return 0;
}
handle(argv[1], argv[2]);
return 0;
}