1
- #include < iostream>
2
- #include < iomanip>
3
- #include < string>
4
- #include < fstream>
5
- #include < sstream>
6
1
#include < boost/filesystem.hpp>
7
2
#include < boost/uuid/detail/sha1.hpp>
3
+ #include < fstream>
4
+ #include < iomanip>
5
+ #include < iostream>
6
+ #include < sstream>
7
+ #include < string>
8
8
9
9
namespace fs = boost::filesystem;
10
10
11
11
std::string read_file (const fs::path &filePath) {
12
- try {
13
- // Open the file
14
- std::ifstream fileStream (filePath.string ());
12
+ try {
13
+ // Open the file
14
+ std::ifstream fileStream (filePath.string ());
15
15
16
- // Check if the file is successfully opened
17
- if (fileStream.is_open ()) {
18
- // Read the content of the file
19
- std::stringstream buffer;
20
- buffer << fileStream.rdbuf ();
21
- return buffer.str ();
22
- } else {
23
- std::cerr << " Error opening the file for reading: " << filePath << std::endl;
24
- return " " ;
25
- }
26
- } catch (const std::exception & e) {
27
- std::cerr << " Exception: " << e.what () << std::endl;
28
- return " " ;
16
+ // Check if the file is successfully opened
17
+ if (fileStream.is_open ()) {
18
+ // Read the content of the file
19
+ std::stringstream buffer;
20
+ buffer << fileStream.rdbuf ();
21
+ return buffer.str ();
22
+ } else {
23
+ std::cerr << " Error opening the file for reading: " << filePath
24
+ << std::endl;
25
+ return " " ;
29
26
}
27
+ } catch (const std::exception &e) {
28
+ std::cerr << " Exception: " << e.what () << std::endl;
29
+ return " " ;
30
+ }
30
31
}
31
32
32
- bool create_file (const fs::path &filePath, const std::string& content = " " ) {
33
- try {
34
- // Create the file
35
- std::ofstream fileStream (filePath.string ());
33
+ bool create_file (const fs::path &filePath, const std::string & content = " " ) {
34
+ try {
35
+ // Create the file
36
+ std::ofstream fileStream (filePath.string ());
36
37
37
- // Check if the file is successfully opened
38
- if (fileStream.is_open ()) {
39
- // Write content to the file if provided
40
- if (!content.empty ()) {
41
- fileStream << content;
42
- }
38
+ // Check if the file is successfully opened
39
+ if (fileStream.is_open ()) {
40
+ // Write content to the file if provided
41
+ if (!content.empty ()) {
42
+ fileStream << content;
43
+ }
43
44
44
- std::cout << " File created successfully: " << filePath << std::endl;
45
- return true ;
46
- } else {
47
- std::cerr << " Error opening the file for writing: " << filePath << std::endl;
48
- return false ;
49
- }
50
- } catch (const std::exception & e) {
51
- std::cerr << " Exception: " << e.what () << std::endl;
52
- return false ;
45
+ std::cout << " File created successfully: " << filePath << std::endl;
46
+ return true ;
47
+ } else {
48
+ std::cerr << " Error opening the file for writing: " << filePath
49
+ << std::endl;
50
+ return false ;
53
51
}
52
+ } catch (const std::exception &e) {
53
+ std::cerr << " Exception: " << e.what () << std::endl;
54
+ return false ;
55
+ }
54
56
}
55
57
56
- std::string sha1_hexdigest (const std::string& data) {
57
- boost::uuids::detail::sha1 sha1;
58
- sha1.process_bytes (data.data (), data.size ());
58
+ std::string sha1_hexdigest (const std::string & data) {
59
+ boost::uuids::detail::sha1 sha1;
60
+ sha1.process_bytes (data.data (), data.size ());
59
61
60
- unsigned int digest[5 ];
61
- sha1.get_digest (digest);
62
+ unsigned int digest[5 ];
63
+ sha1.get_digest (digest);
62
64
63
- std::stringstream ss;
64
- ss << std::hex << std::setfill (' 0' );
65
- for (unsigned int i : digest) {
66
- ss << std::setw (8 ) << i;
67
- }
68
- return ss.str ();
65
+ std::stringstream ss;
66
+ ss << std::hex << std::setfill (' 0' );
67
+ for (unsigned int i : digest) {
68
+ ss << std::setw (8 ) << i;
69
+ }
70
+ return ss.str ();
69
71
}
72
+
73
+ std::string binaryToHex (const std::string &binaryData) {
74
+ std::ostringstream hexStream;
75
+ for (unsigned char byte : binaryData) {
76
+ hexStream << std::setw (2 ) << std::setfill (' 0' ) << std::hex
77
+ << static_cast <int >(byte);
78
+ }
79
+ return hexStream.str ();
80
+ }
81
+
82
+ std::string hexToBinary (const std::string &hexString) {
83
+ std::string binaryData;
84
+ for (size_t i = 0 ; i < hexString.size (); i += 2 ) {
85
+ // Take two hex characters at a time
86
+ std::string byteStr = hexString.substr (i, 2 );
87
+
88
+ // Convert hex pair to a single byte (char)
89
+ char byte = static_cast <char >(std::stoi (byteStr, nullptr , 16 ));
90
+ binaryData.push_back (byte);
91
+ }
92
+ return binaryData;
93
+ }
0 commit comments