Repository navigation
Expand file tree
/
Copy pathFileVersionManager.hpp
More file actions
189 lines (165 loc) · 6.16 KB
/
Copy pathFileVersionManager.hpp
File metadata and controls
189 lines (165 loc) · 6.16 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#ifndef FILE_VERSION_MANAGER_HPP
#define FILE_VERSION_MANAGER_HPP
#include <string>
#include <fstream>
#include <stdexcept>
#include <filesystem>
#include "SkipList.hpp"
#include "Version.hpp"
class FileVersionManager {
private:
std::string filePath;
SkipList<Version> versions;
int currentVersion;
// Helper function to read file content
std::string readFileContent() const {
std::ifstream file(filePath);
if (!file.is_open()) {
throw std::runtime_error("Unable to open file: " + filePath);
}
std::stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
// Helper function to write content to file
void writeFileContent(const std::string& content) const {
std::ofstream file(filePath);
if (!file.is_open()) {
throw std::runtime_error("Unable to open file for writing: " + filePath);
}
file << content;
}
// Helper function to create backup of original file
void createBackup() const {
std::string backupPath = filePath + ".backup";
std::filesystem::copy_file(filePath, backupPath,
std::filesystem::copy_options::overwrite_existing);
}
public:
FileVersionManager(const std::string& path)
: filePath(path), currentVersion(0) {
if (!std::filesystem::exists(path)) {
throw std::runtime_error("File does not exist: " + path);
}
// Create a backup of the original file
createBackup();
}
// Create a new version of the file
void createVersion(const std::string& comment = "") {
try {
std::string content = readFileContent();
currentVersion++;
Version newVersion(currentVersion, content, comment);
versions.insert(newVersion);
} catch (const std::exception& e) {
throw std::runtime_error("Failed to create version: " + std::string(e.what()));
}
}
// List all versions
void listVersions(bool showPreview = false) const {
if (versions.isEmpty()) {
std::cout << "No versions available." << std::endl;
return;
}
std::cout << "\nVersion History for " << filePath << ":\n"
<< "----------------------------------------" << std::endl;
// Use proper type for Node
typename SkipList<Version>::Node* current = versions.getHead()->forward[0];
while (current != nullptr) {
if (showPreview) {
current->value.printWithPreview();
} else {
std::cout << current->value;
}
std::cout << "----------------------------------------" << std::endl;
current = current->forward[0];
}
}
// Restore a specific version
bool restoreVersion(int versionNumber) {
Version searchVersion(versionNumber, ""); // Dummy version for search
if (!versions.search(searchVersion)) {
std::cout << "Version " << versionNumber << " not found." << std::endl;
return false;
}
// Use proper type for Node
typename SkipList<Version>::Node* current = versions.getHead()->forward[0];
while (current != nullptr) {
if (current->value.getVersionNumber() == versionNumber) {
try {
writeFileContent(current->value.getContent());
std::cout << "Successfully restored version " << versionNumber << std::endl;
return true;
} catch (const std::exception& e) {
throw std::runtime_error("Failed to restore version: " + std::string(e.what()));
}
}
current = current->forward[0];
}
return false;
}
// Delete a specific version
bool deleteVersion(int versionNumber) {
if (versions.isEmpty()) {
std::cout << "No versions available to delete." << std::endl;
return false;
}
Version deleteVersion(versionNumber, ""); // Dummy version for deletion
return versions.remove(deleteVersion);
}
// Get the current version number
int getCurrentVersion() const {
return currentVersion;
}
// Get the total number of versions
int getVersionCount() const {
return versions.getSize();
}
// View a specific version's full content
bool viewVersion(int versionNumber) const {
Version searchVersion(versionNumber, "");
if (!versions.search(searchVersion)) {
std::cout << "Version " << versionNumber << " not found." << std::endl;
return false;
}
// Use proper type for Node
typename SkipList<Version>::Node* current = versions.getHead()->forward[0];
while (current != nullptr) {
if (current->value.getVersionNumber() == versionNumber) {
current->value.printFull();
return true;
}
current = current->forward[0];
}
return false;
}
// Restore to the latest version
bool restoreLatest() {
if (versions.isEmpty()) {
std::cout << "No versions available to restore." << std::endl;
return false;
}
return restoreVersion(currentVersion);
}
// Get the original backup file path
std::string getBackupPath() const {
return filePath + ".backup";
}
// Restore from original backup
bool restoreFromBackup() {
std::string backupPath = getBackupPath();
if (!std::filesystem::exists(backupPath)) {
std::cout << "Backup file does not exist." << std::endl;
return false;
}
try {
std::filesystem::copy_file(backupPath, filePath,
std::filesystem::copy_options::overwrite_existing);
std::cout << "Successfully restored from backup." << std::endl;
return true;
} catch (const std::exception& e) {
throw std::runtime_error("Failed to restore from backup: " + std::string(e.what()));
}
}
};
#endif // FILE_VERSION_MANAGER_HPP