-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
122 lines (97 loc) · 3.36 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
/*
Simple implementation of LZW compression algorithm
Copyright (C) 2019 Andreu Carminati
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <cstring>
#include <string>
#include <sstream>
#include <iostream>
#include "LZWDictionary.h"
#include "UncompressedFileReader.h"
#include "CompressedFileReader.h"
#include "LZWEncoder.h"
#include "CompressedFileWriter.h"
#include "LZWDecoder.h"
#include "UncompressedFileWriter.h"
using namespace std;
/*
*
*/
static bool exists(const char* name) {
if (FILE * file = fopen(name, "r")) {
fclose(file);
return true;
} else {
return false;
}
}
static void encoderDriver(const char* uncompressedFileName, const char* compressedFileName) {
cout << "Reading uncompressed file\n";
auto fr = std::make_unique<UncompressedFileReader>(uncompressedFileName);
auto fw = std::make_unique<CompressedFileWriter>(compressedFileName);
fw->writeFileName(uncompressedFileName);
cout << "Compressing data\n";
LZWEncoder encoder(std::move(fr), std::move(fw));
encoder.encode();
}
static void decoderDriver(const char* compressedFileName) {
cout << "Reading compressed file\n";
auto cfr = std::make_unique<CompressedFileReader>("saida.bin");
const char* uncompressedFilename = cfr->readUncompressedFileName();
if (exists(uncompressedFilename)) {
cout << "Aborting decompression, file already exists" << endl;
} else {
auto ufw = std::make_unique<UncompressedFileWriter>("saida.txt");
cout << "Decompressing data\n";
LZWDecoder decoder(std::move(cfr), std::move(ufw));
decoder.decode();
}
delete[] uncompressedFilename;
}
int main(int argc, char** argv) {
const char* input1 = NULL;
const char* input2 = NULL;
if (argc == 1) {
cout << "Usage:" << endl;
cout << "Compression: " << "-c <inputfile> <outputfile>" << endl;
cout << "Decompression: " << "-d <inputfile>" << endl;
return -1;
}
if (argc < 3) {
cout << "Insuffifient number of parameters" << endl;
return -1;
}
input1 = argv[2];
if (argc == 4) {
input2 = argv[3];
}
if (strcmp(argv[1], "-c") == 0) {
const char* uncompressedFileName = input1;
const char* compressedFileName = NULL;
if (!input2) {
cout << "Omitted compression parameter (file name): " << endl;
return -1;
}
compressedFileName = input2;
encoderDriver(uncompressedFileName, compressedFileName);
} else if (strcmp(argv[1], "-d") == 0) {
const char* compressedFileName = input1;
decoderDriver(compressedFileName);
} else {
cout << "Invalid parameter: " << argv[1] << endl;
return -1;
}
cout << "Done\n";
return 0;
}