-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileModule.cpp
42 lines (35 loc) · 1.4 KB
/
fileModule.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
#include "fileModule.h"
void FileValidator::validateInput(const std::string& filename) {
std::ifstream inputFile(filename);
if (!inputFile) {
inputFile.close();
throw std::runtime_error("Cannot open file: " + filename +" \n");
}
inputFile.close();
}
void FileValidator::validateOutput(const std::string& filename) {
std::ofstream outputFile(filename);
if (!outputFile) {
outputFile.close();
throw std::runtime_error("Cannot open file: " + filename +" \n");
}
outputFile.close();
}
void CompressedFileValidator::validateCompressedFile(const std::string& filename, bool isInput){
if (isInput) validateInput(filename);
else validateOutput(filename);
// validate the extension
// Find the position of the dot (.) in the filename
std::size_t dotPosition = filename.find_last_of('.');
if (dotPosition == std::string::npos) {
throw std::invalid_argument("in filename " + filename +
" there is no extension .myzip\n");
}
// Extract the file extension
std::string fileExtension = filename.substr(dotPosition + 1);
// Compare the extracted extension with the provided extension
if (fileExtension != "myzip"){
throw std::invalid_argument("in filename " + filename +
" there is incorrect extension " + fileExtension +", (must be .myzip)\n");
}
}