-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
166 lines (138 loc) · 4.2 KB
/
main.cpp
File metadata and controls
166 lines (138 loc) · 4.2 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
#include <iostream>
#include <string>
#include <vector>
#include <filesystem>
#include <stdexcept>
#include <algorithm>
#include "huffman.hpp"
#include "path-manager.h"
#include "messages.h"
namespace fs = std::filesystem;
// Constants
static const std::string ZIP_CMD = "zip";
static const std::string UNZIP_CMD = "unzip";
static const std::string ZIPPED_EXT = ".hzip";
enum Operation { ZIP = 1, UNZIP = 2 };
// Function prototypes
bool isValidCommandLineArgs(int argc, const std::string& command);
int processCommandLineArgs(int argc, char** argv);
int promptUserForOperation();
int compressFile();
int decompressFile();
int main(int argc, char** argv) {
if (argc > 1) {
// If there is command line arguments, process them
try {
return processCommandLineArgs(argc, argv);
}
catch (std::exception& e) {
std::cout << e.what() << std::endl;
return 1;
}
}
else {
// If there is no command line arguments, prompt the user to
// provide the desired operation and the input/output file paths
return promptUserForOperation();
}
}
// It supposes that the command line arguments are correct
int processCommandLineArgs(int argc, char** argv) {
std::string command(argv[1]);
std::transform(command.begin(), command.end(), command.begin(), ::tolower);
if (!isValidCommandLineArgs(argc, command)) {
throw std::invalid_argument(Messages::INVALID_ARGUMENTS);
}
std::string inputFilePath(argv[2]);
if (!fs::exists(inputFilePath)) {
std::cout << "Error: The specified input file does not exist." << std::endl;
return 1;
}
std::string outputFilePath;
if (argc == 4) {
// Get provided output file path if the user provided one
outputFilePath = argv[3];
// Put a default extension (".hzip") if the user didn't provide one
if (!hasExtension(outputFilePath)) {
outputFilePath += ZIPPED_EXT;
}
}
else {
// If the user didn't provided an output file path, the compressed file will
// be created with the same path as the input file
outputFilePath = stripExtension(inputFilePath) + ZIPPED_EXT;
}
if (command == ZIP_CMD) {
try {
Compressor::zip(inputFilePath, outputFilePath);
}
catch (std::exception& e) {
throw;
}
std::cout << "File compressed successfully!" << std::endl;
}
else {
try {
Decompressor::unzip(inputFilePath, outputFilePath);
}
catch (std::exception& e) {
throw;
}
std::cout << "File decompressed successfully!" << std::endl;
}
return 0;
}
bool isValidCommandLineArgs(int argc, const std::string& command) {
return !(argc > 4 || argc < 3 ||
!(command == ZIP_CMD || command == UNZIP_CMD) ||
(command == UNZIP_CMD && argc != 4));
}
int promptUserForOperation() {
std::cout << "Please choose the desired operation:" << std::endl;
std::cout << ZIP << ". Compress file" << std::endl;
std::cout << UNZIP << ". Decompress file" << std::endl;
int choice;
std::cin >> choice;
switch (choice) {
case ZIP:
return compressFile();
case UNZIP:
return decompressFile();
default:
std::cout << "Error: Invalid operation selected." << std::endl;
return 1;
}
}
int compressFile() {
std::cout << "Please provide the path to the file you want to compress: ";
std::string filePath;
std::cin >> filePath;
if (!fs::exists(filePath)) {
std::cout << "Error: The specified file does not exist." << std::endl;
return 1;
}
std::cout << "Please provide the path where the compressed file should be created: ";
std::string compressedFilePath;
std::cin >> compressedFilePath;
if (!hasExtension(compressedFilePath)) {
compressedFilePath += ZIPPED_EXT;
}
Compressor::zip(filePath, compressedFilePath);
std::cout << "File compressed successfully!" << std::endl;
return 0;
}
int decompressFile() {
std::cout << "Please provide the path to the file you want to decompress: ";
std::string compressedFilePath;
std::cin >> compressedFilePath;
if (!fs::exists(compressedFilePath)) {
std::cout << "Error: The specified file does not exist." << std::endl;
return 1;
}
std::cout << "Please provide the path where the decompressed file should be created: ";
std::string decompressedFilePath;
std::cin >> decompressedFilePath;
Decompressor::unzip(compressedFilePath, decompressedFilePath);
std::cout << "File decompressed successfully!" << std::endl;
return 0;
}