generated from ssciwr/cpp-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
93 lines (81 loc) · 2.46 KB
/
util.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
#include <boost/filesystem.hpp>
#include <boost/uuid/detail/sha1.hpp>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
namespace fs = boost::filesystem;
std::string read_file(const fs::path &filePath) {
try {
// Open the file
std::ifstream fileStream(filePath.string());
// Check if the file is successfully opened
if (fileStream.is_open()) {
// Read the content of the file
std::stringstream buffer;
buffer << fileStream.rdbuf();
return buffer.str();
} else {
std::cerr << "Error opening the file for reading: " << filePath
<< std::endl;
return "";
}
} catch (const std::exception &e) {
std::cerr << "Exception: " << e.what() << std::endl;
return "";
}
}
bool create_file(const fs::path &filePath, const std::string &content = "") {
try {
// Create the file
std::ofstream fileStream(filePath.string());
// Check if the file is successfully opened
if (fileStream.is_open()) {
// Write content to the file if provided
if (!content.empty()) {
fileStream << content;
}
std::cout << "File created successfully: " << filePath << std::endl;
return true;
} else {
std::cerr << "Error opening the file for writing: " << filePath
<< std::endl;
return false;
}
} catch (const std::exception &e) {
std::cerr << "Exception: " << e.what() << std::endl;
return false;
}
}
std::string sha1_hexdigest(const std::string &data) {
boost::uuids::detail::sha1 sha1;
sha1.process_bytes(data.data(), data.size());
unsigned int digest[5];
sha1.get_digest(digest);
std::stringstream ss;
ss << std::hex << std::setfill('0');
for (unsigned int i : digest) {
ss << std::setw(8) << i;
}
return ss.str();
}
std::string binaryToHex(const std::string &binaryData) {
std::ostringstream hexStream;
for (unsigned char byte : binaryData) {
hexStream << std::setw(2) << std::setfill('0') << std::hex
<< static_cast<int>(byte);
}
return hexStream.str();
}
std::string hexToBinary(const std::string &hexString) {
std::string binaryData;
for (size_t i = 0; i < hexString.size(); i += 2) {
// Take two hex characters at a time
std::string byteStr = hexString.substr(i, 2);
// Convert hex pair to a single byte (char)
char byte = static_cast<char>(std::stoi(byteStr, nullptr, 16));
binaryData.push_back(byte);
}
return binaryData;
}