Repository navigation
Expand file tree
/
Copy pathfileSystem.cpp
More file actions
156 lines (126 loc) · 4.42 KB
/
Copy pathfileSystem.cpp
File metadata and controls
156 lines (126 loc) · 4.42 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
#include "fileSystem.h"
namespace fs = std::filesystem;
bool fileSys::isValidFile(std::string filePath)
{
if (fs::exists(filePath)) {
return true;
}
else {
return false;
}
}
bufferData fileSys::readFile(const std::string& filePath) {
bufferData result;
uint8_t* buffer = nullptr;
std::ifstream file(filePath, std::ios::binary | std::ios::ate);
if (!file.is_open()) {
console::print(upk_PackagerFileSysHeaderTitle + upk_error, " Error at opening file(0x0):" + filePath);
return result;
}
// Dosyanýn boyutunu al
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
buffer = (uint8_t*)malloc(size + 1); // Ekstra byte, eksik olan '\n' eklemek için
result.pointer = buffer;
result.size = size;
if (buffer == nullptr) {
console::exitPrint(upk_PackagerFileSysHeaderTitle + upk_error, " Bad allocation!");
return result;
}
// Dosyayý buffer'a oku
if (!file.read((char*)buffer, size)) {
console::print(upk_PackagerFileSysHeaderTitle + upk_error, " Error at reading file(0x1):" + filePath);
return result;
}
// Unix tarzý satýr sonu (\n) eklemek için son karakteri kontrol et
if (buffer[size - 1] != '\n') {
buffer[size] = '\n'; // Sonuna LF ekle
result.size += 1; // Boyutu artýr
}
#ifdef debugFileReaderOutput
console::print(upk_PackagerDebugFileSysHeaderTitle + upk_info, "File read successfully:" + filePath);
#endif // debugFileReaderOutput
return result;
}
size_t fileSys::getFileSize(const std::string& filePath)
{
std::ifstream file(filePath, std::ios::binary | std::ios::ate); // dosyanýn sonuna git
if (file.is_open()) {
std::streampos fileSize = file.tellg(); // tellg() ile dosyanýn boyutunu al
file.close();
//std::cout << filePath << " -[Size]-> " << fileSize << std::endl;
return fileSize;
}
else {
console::exitPrint(upk_PackagerFileSysHeaderTitle + upk_error, "An error occured while reading file size!");
return -1;
}
}
std::vector<fs::path> fileSys::getAllFiles(const fs::path& path) {
std::vector<fs::path> result;
for (const auto& entry : fs::directory_iterator(path)) {
if (entry.is_regular_file()) {
result.push_back(entry.path());
}
}
return result;
}
std::string fileSys::getFileName(std::string path)
{
fs::path _file(path);
return _file.stem().string();
}
std::string fileSys::getFileExtension(std::string path)
{
fs::path filePath(path);
return filePath.extension().string();
}
std::string fileSys::getFileRelativePath(std::string path)
{
fs::path _file(path);
return _file.relative_path().string();
}
std::string fileSys::getFileRootPath(std::string path)
{
fs::path _file(path);
return _file.root_directory().string();
}
bool fileSys::createFile(std::string path, bufferData toWrite) {
std::ofstream file(path, std::ios::binary); // Dosyayý ikili modda aç
if (!file) {
console::exitPrint(upk_PackagerFileSysHeaderTitle + upk_error, "An error occurred while creating file!");
return false;
}
// Satýr sonlarýný LF yapacak þekilde yazma iþlemi
for (std::size_t i = 0; i < toWrite.size; ++i) {
if (toWrite.pointer[i] == '\r') {
// Eðer \r karakterini bulursan, onu atla
continue;
}
file.put(toWrite.pointer[i]); // Karakteri yaz
}
file.close();
#ifdef debugFileWriterOutput
console::print(upk_PackagerDebugFileSysHeaderTitle + upk_info, "File created! Path:" + path);
#endif // debugFileWriterOutput
return true;
}
std::vector<std::string> fileSys::getAllDirectories(const std::string path) {
std::vector<std::string> result;
std::vector<std::string> stack; // Dizinleri geçici olarak saklamak için bir yýðýn kullanýyoruz
stack.push_back(path);
while (!stack.empty()) {
std::string currentPath = stack.back();
stack.pop_back();
for (const auto& entry : fs::directory_iterator(currentPath)) {
if (entry.is_directory()) {
result.push_back(entry.path().string());
#ifdef debugFileSearchOutput
console::print(upk_PackagerDebugFileSysHeaderTitle + upk_info, " Found folder:" + entry.path().string());
#endif // debugFileInputOutput
stack.push_back(entry.path().string());
}
}
}
return result;
}