-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFireSharing.cpp
48 lines (39 loc) · 1.44 KB
/
FireSharing.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
#include "FileSharing.h"
namespace fs = std::filesystem;
namespace FileSharing {
std::vector<std::string> listFiles(const std::string& userDirectory) {
std::vector<std::string> fileList;
for (const auto& entry : fs::directory_iterator(userDirectory)) {
fileList.push_back(entry.path().string());
}
return fileList;
}
std::string readFile(const std::string& filePath) {
std::ifstream file(filePath);
if (!file.is_open()) {
throw std::runtime_error("Could not open file: " + filePath);
}
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
file.close();
return content;
}
bool writeFile(const std::string& filePath, const std::string& content) {
std::ofstream file(filePath);
if (!file.is_open()) {
std::cerr << "Could not open file for writing: " + filePath << std::endl;
return false;
}
file << content;
file.close();
return true;
}
bool shareFile(const std::string& sourceFilePath, const std::string& destinationFilePath) {
try {
std::string content = readFile(sourceFilePath);
return writeFile(destinationFilePath, content);
} catch (const std::exception& e) {
std::cerr << "Error sharing file: " << e.what() << std::endl;
return false;
}
}
}