-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrlencode.cpp
128 lines (102 loc) · 2.78 KB
/
rlencode.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
#include <iostream>
#include <fstream>
#include "rlencode.h"
#include "common.h"
using namespace std;
int main (int argc, char **argv) {
// command line arguments
if (argc < 2 || argc > 3) {
usage(argv);
exit(1);
}
// set operation mode
int mode = STDOUT_MODE;
if (argc == 3) mode = OUTFILE_MODE;
// open files
ifstream infile;
infile.open(argv[1], ios::in | ios::binary);
if (!infile.is_open()) {
cerr << "Unable to open input file " << argv[1] << endl;
exit(1);
}
ofstream outfile;
if (mode == OUTFILE_MODE) {
outfile.open(argv[2], ios::out | ios::binary);
if (!outfile.is_open()) {
cerr << "Unable to open output file " << argv[2] << endl;
exit(1);
}
}
// perform encoding
encode(infile, outfile, mode);
// close files
infile.close();
if (outfile.is_open()) outfile.close();
return 0;
}
void encode(ifstream &infile, ofstream &outfile, int mode) {
// read the first character
char inbyte = 0;
if (!infile.read(&inbyte,1)) {
// failed reading first character
if (infile.eof()) return; // empty file, do nothing
cerr << "Failed reading first character of input file" << endl;
infile.close();
outfile.close();
exit(1);
}
char oldbyte = START_FLAG;
unsigned int count = 1;
// read input stream one byte at a time
while(infile) {
// check that input is standard ASCII
if (inbyte > 127) {
cerr << "Input file contains non-standard ASCII" << endl;
infile.close();
if (outfile) outfile.close();
exit(1);
}
// increment count if same as before
if (inbyte == oldbyte) count++;
// otherwise output code for previous character and reset count
else if (oldbyte != START_FLAG) {
output_code(outfile, oldbyte, count, mode);
count = 1;
}
// get a new character
oldbyte = inbyte;
infile.read(&inbyte,1);
}
// output the last character
output_code(outfile, oldbyte, count, mode);
if (mode == STDOUT_MODE) cout << flush;
return;
}
void output_code(ofstream &outfile, char byte, unsigned int count, int mode) {
// output the characters only if there are 2 or less
if (count <= 2) {
while (count-- > 0) {
if (mode == OUTFILE_MODE) outfile << byte;
else cout << byte;
}
return;
}
// otherwise output byte and count minus three
count -= 3;
if (mode == OUTFILE_MODE) outfile << byte;
else cout << byte;
if (mode == OUTFILE_MODE) {
while (count > 0x7F) {
// won't fit into seven bits, need to split
char least_sig = count & 0x7F;
// prepend a flag bit and output
least_sig |= 0x80;
outfile << least_sig;
count >>= 7;
}
// output last 7 bits
count |= 0x80;
outfile << (char)count;
}
else cout << "[" << count << "]";
}