-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFileManager.cpp
More file actions
161 lines (122 loc) · 4.52 KB
/
FileManager.cpp
File metadata and controls
161 lines (122 loc) · 4.52 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
//=============================================================================
#include "FileManager.h"
#include "Log.h"
#include "Stringify.h"
#ifndef _WIN32
#include <sys/param.h>
#include <dirent.h>
#else
#include <Windows.h>
#endif
#include <stdio.h>
//=============================================================================
bool FileManager::SetContents(std::string Filename, std::string Content, bool Relative) {
if(Filename == "") return false;
if(Relative) Filename = GetCWD() + DIR_SEPARATOR + Filename;
std::ofstream FileHandle;
FileHandle.open(Filename.c_str());
if(!FileHandle.is_open()) return false;
FileHandle << Content;
FileHandle.close();
return true;
}
//-----------------------------------------------------------------------------
std::string FileManager::GetContents(std::string Filename, bool Relative) {
if(Filename == "") return "";
if(Relative) Filename = GetCWD() + DIR_SEPARATOR + Filename;
std::string Content;
std::ifstream FileHandle;
FileHandle.open(Filename.c_str());
if(FileHandle.is_open()) {
while(FileHandle.good()) {
std::string Buffer;
getline(FileHandle, Buffer);
if(Buffer == "") continue;
Content += Buffer + "\n";
}
FileHandle.close();
}
return Content;
}
//-----------------------------------------------------------------------------
std::vector<std::string> FileManager::GetFilesInFolder(std::string Folder) {
std::vector<std::string> List;
std::string CWD = GetCWD();
std::string Path = CWD;
if(Folder != "") Path += DIR_SEPARATOR + Folder;
#ifdef __APPLE__
NSError* Error;
NSString* PathNS = [NSString stringWithUTF8String:Path.c_str()];
NSArray* DirectoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:PathNS error:&Error];
for(id File in DirectoryContents) {
std::string Filename = Path + DIR_SEPARATOR + [File cStringUsingEncoding:1];
List.push_back(Filename);
}
#elif _WIN32
HANDLE dirHandle = NULL;
WIN32_FIND_DATA FileHandle;
std::string winPath(Path + DIR_SEPARATOR + std::string("*"));
if ((dirHandle = FindFirstFile(winPath.c_str(), &FileHandle)) != INVALID_HANDLE_VALUE)
{
do
{
if (std::string(FileHandle.cFileName) == ".") continue;
if (std::string(FileHandle.cFileName) == "..") continue;
std::string Filename = Path + DIR_SEPARATOR + FileHandle.cFileName;
List.push_back(Filename);
} while (FindNextFile(dirHandle, &FileHandle) != false);
FindClose(dirHandle);
}
else
{
Log("Unable to open directory: %s", Path.c_str());
}
#else
DIR* DirHandle = NULL;
dirent* FileHandle = NULL;
// Needs improved
if((DirHandle = opendir(Folder.c_str())) != NULL) {
while((FileHandle = readdir(DirHandle)) != NULL) {
if(std::string(FileHandle->d_name) == ".") continue;
if(std::string(FileHandle->d_name) == "..") continue;
std::string Filename = Path + DIR_SEPARATOR + FileHandle->d_name;
//Log("Found File: %s", Filename.c_str());
List.push_back(Filename);
}
closedir(DirHandle);
}else{
Log("Unable to open directory : %s", Path.c_str());
}
#endif
return List;
}
//-----------------------------------------------------------------------------
std::string FileManager::GetCWD() {
std::string CWD;
#ifdef __APPLE__
NSString* ResourcePath = [[NSBundle mainBundle] resourcePath];
CWD = [ResourcePath cStringUsingEncoding:1];
#elif defined(_WIN32)
char buffer[MAX_PATH];
CWD = ((GetCurrentDirectory(MAX_PATH, buffer) > 0) ? std::string(buffer) : std::string(""));
#else
char Buffer[MAXPATHLEN];
CWD = (getcwd(Buffer, MAXPATHLEN) ? std::string(Buffer) : std::string(""));
#endif
return CWD;
}
//-----------------------------------------------------------------------------
std::string FileManager::GetFilenameWithoutExt(std::string Filename) {
std::vector<std::string> Parts = Stringify::Explode(Filename, DIR_SEPARATOR);
std::string NewFilename = Parts[Parts.size() - 1];
// To Do: Filename could potentially have one or more dots
Parts = Stringify::Explode(NewFilename, ".");
NewFilename = Parts[0];
return NewFilename;
}
//-----------------------------------------------------------------------------
std::string FileManager::GetFilenameExt(std::string Filename) {
std::vector<std::string> Parts = Stringify::Explode(Filename, ".");
return (Parts.size() <= 1 ? "" : Parts[Parts.size() - 1]);
}
//=============================================================================