-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileSystem.cpp
executable file
·117 lines (98 loc) · 2.06 KB
/
FileSystem.cpp
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
#include "StdAfx.h"
#include "FileSystem.h"
namespace fs = boost::filesystem;
CFileSystem::CFileSystem(void)
{
}
CFileSystem::~CFileSystem(void)
{
}
bool CFileSystem::file_exists(string filename)
{
fs::path p(filename);
return fs::exists(p);
}
string CFileSystem::currentFolder()
{
fs::path fp = fs::current_path();
return fp.generic_string();
}
void CFileSystem::createFolder(string folder)
{
try
{
fs::path someDir(folder);
if ( !fs::exists(someDir) )
{
fs::create_directories(someDir);
}
}
catch(...)
{
CApp::Engine()->debug_(string("Failed creating folder : ") + folder);
}
}
CFileSystem::FileList CFileSystem::getFolderFiles(string folder, string filterExt)
{
fs::path someDir(folder);
fs::directory_iterator end_iter;
FileList vs;
if ( fs::exists(someDir) && fs::is_directory(someDir))
{
for( fs::directory_iterator dir_iter(someDir) ; dir_iter != end_iter ; ++dir_iter)
{
if (fs::is_regular_file(dir_iter->status()) )
{
string fPath = dir_iter->path().generic_string();
if(filterExt != "")
{
if(fPath.find(filterExt) != fPath.npos)
{
vs.push_back(fPath);
}
}
else
{
vs.push_back(fPath);
}
}
}
}
return vs;
}
CFileSystem::FileContent CFileSystem::readFile(string fname)
{
ifstream ifs(fname, ios::binary);
if(ifs.is_open())
{
vector<unsigned char> content( (std::istreambuf_iterator<char>(ifs) ),
(std::istreambuf_iterator<char>() ) );
ifs.close();
return content;
}
return vector<unsigned char>();
}
bool CFileSystem::writeFile(string fname, string data)
{ /*
ofstream file(fname);
if(file.is_open())
{
file << data;
file.close();
return true;
}
return false;
*/
FILE *f = fopen(fname.c_str(), "wb");
if (f == NULL)
{
return false;
}
char* cstr = new char[data.size()+1];
memcpy ( cstr, data.data(), data.size() );
fwrite ((char *)data.c_str() , sizeof(char), data.size(), f);
delete(cstr);
cstr = NULL;
fclose(f);
return true;
}