Repository navigation
Expand file tree
/
Copy pathFileManager.cpp
More file actions
78 lines (70 loc) · 2.11 KB
/
Copy pathFileManager.cpp
File metadata and controls
78 lines (70 loc) · 2.11 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
//
// Created by Oliver Mrovcak on 08/01/2024.
//
#include "FileManager.h"
FileManager::FileManager() {
currentPath = fs::current_path();
}
void FileManager::printToScreen(const std::string& message) {
printw("%s\n", message.c_str()); // ncurses print function
refresh(); // Refresh the screen to show the update
}
void FileManager::listDirectoryContents(const std::string& path) {
try {
if (fs::exists(path) && fs::is_directory(path)) {
for (const auto& entry : fs::directory_iterator(path))
printToScreen(entry.path().filename());
getch();
} else {
printPathError(path);
}
} catch (const std::exception& e) {
printToScreen(e.what());
getch();
}
}
void FileManager::removeDirectory(const std::string& path) {
try {
if (fs::exists(path) && fs::is_directory(path)) {
fs::remove_all(path);
printToScreen("Directory removed: " + path);
getch();
} else {
printPathError(path);
}
} catch (const std::exception& e) {
printToScreen(e.what());
getch();
}
}
void FileManager::createDirectory(const std::string& path) {
try {
if (!fs::exists(path)) {
fs::create_directory(path);
printToScreen("Directory created: " + path);
getch();
} else {
std::cerr << "Directory already exists: " << path << std::endl;
}
} catch (const std::exception& e) {
printToScreen(e.what());
getch();
}
}
void FileManager::changeDirectory(const std::string& path) {
try {
if (fs::exists(path) && fs::is_directory(path)) {
currentPath = path;
fs::current_path(currentPath);
std::cout << "Changed directory to: " << currentPath << std::endl;
} else {
printPathError(path);
}
} catch (const std::exception& e) {
printToScreen(e.what());
getch();
}
}
void FileManager::printPathError(const std::string& path) {
std::cerr << "Invalid path: " << path << std::endl;
}