-
Notifications
You must be signed in to change notification settings - Fork 7
/
DirectoryManagement.hpp
114 lines (111 loc) · 3.36 KB
/
DirectoryManagement.hpp
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
#ifndef __DIRECTORYMANAGEMENT_HPP__
#define __DIRECTORYMANAGEMENT_HPP__
#include <exception>
#include <stdexcept>
#include <new>
#include <vector>
#include <string>
namespace Utils {
namespace DirectoryManagement {
class DirFileList {
public:
class entry {
private:
char* name;
int type; //0 folder, 1 file, 2 unknown or special types, -1 no entry.
public:
const char* GetName() const {return name;}
int GetType() const {return type;}
entry& operator=(const entry& other) {
if(this != &other) {
char* tmpname = (char*)calloc(strlen(other.name)+1, 1);
if(!tmpname) throw std::bad_alloc();
free(name);
name = tmpname;
type = other.type;
strcpy(name, other.name);
}
return *this;
}
entry& operator=(entry&& other) {
if(this != &other) {
name = other.name;
other.name = nullptr;
type = other.type;
other.type = -1;
}
return *this;
}
entry() : name(NULL), type(-1) {}
entry(const entry& other) : entry() {*this = other;}
entry(entry&& other) : entry() {*this = other;}
~entry() {free(name);}
friend class DirFileList;
friend int DirectoryListing(DirFileList& output, const char* path);
};
private:
char* original_path;
std::vector<entry*> entries;
void Clear() {
for(size_t i = 0; i < entries.size(); i++)
delete entries[i];
entries.clear();
free(original_path);
original_path = nullptr;
}
public:
const entry& operator[](int index) const {
return *(entries.at(index));
}
DirFileList& operator=(const DirFileList& other) {
if(this != &other) {
Clear();
char* new_path = (char*)malloc(strlen(other.original_path)+1);
if(!new_path) throw std::bad_alloc();
strcpy(new_path, other.original_path);
original_path = new_path;
for(size_t i = 0; i < other.entries.size(); i++) {
entry* foo = nullptr;
try {
foo = new entry(*other.entries[i]);
entries.push_back(nullptr);
entries[i] = foo;
} catch(...) {
Clear();
delete foo;
throw;
}
}
}
return *this;
}
DirFileList& operator=(DirFileList&& other) {
if(this != &other) {
free(original_path);
original_path = other.original_path;
other.original_path = nullptr;
entries = std::move(other.entries);
}
return *this;
}
std::string operator+(const entry& _entry) {
return std::string(std::string(original_path) + std::string(_entry.name));
}
size_t size() const {return entries.size();}
const char* GetPath() const {return original_path;};
DirFileList() : original_path(nullptr) {}
DirFileList(const char* path) : DirFileList() {DirectoryListing(*this, path);}
DirFileList(const std::string& path) : DirFileList() {DirectoryListing(*this, path.c_str());}
DirFileList(const DirFileList& other) : DirFileList() {*this = other;}
DirFileList(DirFileList&& other) : DirFileList() {*this = other;}
~DirFileList() {Clear();}
friend int DirectoryListing(DirFileList& output, const char* path);
};
int MakeDirectory(const char* path, bool recursive = true);
int RemoveDirectory(const char* path, bool recursive = true);
int DirectoryListing(DirFileList& output, const char* path);
int CheckIfDir(const char* path);
int FixUpPath(char*& out, const char* path, bool ending_delimitor = true);
}
}
#endif