-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
92 lines (70 loc) · 2.29 KB
/
Copy pathutils.cpp
File metadata and controls
92 lines (70 loc) · 2.29 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
#include "utils.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;
bool extract_content(const string& path, string& xml) {
// 1. Open the file for reading
ifstream inputFile(path);
// 2. Check if the file was successfully opened
if (!inputFile.is_open()) {
cerr << "Error: Could not open input file at path: " << path << endl;
xml = ""; // Ensure the output string is empty on failure
return false;
}
// 3. Read the entire file content into a stringstream
stringstream buffer;
buffer << inputFile.rdbuf();
// 4. Store the stringstream's content into the reference string
xml = buffer.str();
// 5. Close the file (optional, as ifstream destructor handles it, but good practice)
inputFile.close();
return true;
}
bool extract_binary_content(const string& path, string& content) {
// Open the file in binary mode
ifstream inputFile(path, ios::binary | ios::ate);
// Check if the file was successfully opened
if (!inputFile.is_open()) {
cerr << "Error: Could not open input file at path: " << path << endl;
content = "";
return false;
}
// Get file size
streamsize size = inputFile.tellg();
inputFile.seekg(0, ios::beg);
// Read file into a vector
vector<char> buffer(size);
if (!inputFile.read(buffer.data(), size)) {
cerr << "Error: Failed to read binary file" << endl;
content = "";
return false;
}
// Convert to string
content = string(buffer.begin(), buffer.end());
inputFile.close();
return true;
}
bool writeToFile(const string& outputPath, const string& content) {
ofstream file(outputPath);
if (!file.is_open()) {
cerr << "Error: Could not open output file: " << outputPath << endl;
return false;
}
// Write content
file << content;
file.close();
return true;
}
bool writeBinaryToFile(const string& outputPath, const string& content) {
ofstream file(outputPath, ios::binary);
if (!file.is_open()) {
cerr << "Error: Could not open output file: " << outputPath << endl;
return false;
}
// Write binary content
file.write(content.c_str(), content.length());
file.close();
return true;
}