-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDesktopFS.cpp
More file actions
93 lines (77 loc) · 2.37 KB
/
Copy pathDesktopFS.cpp
File metadata and controls
93 lines (77 loc) · 2.37 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
// SVE (Simple Vulkan Engine) Library
// Copyright (c) 2018-2019, Igor Barinov
// Licensed under the MIT License
#include "DesktopFS.h"
#include <cppfs/fs.h>
#include <cppfs/FileHandle.h>
#include <cppfs/FileIterator.h>
#include <cppfs/FilePath.h>
#include <SDL2/SDL_filesystem.h>
#include <SVE/VulkanException.h>
#include <ios>
#include <iterator>
namespace SVE
{
DesktopFSEntity::DesktopFSEntity(cppfs::FileHandle handle)
: Handle(std::move(handle))
{
}
bool DesktopFSEntity::isDirectory() const
{
return Handle.isDirectory();
}
std::string DesktopFSEntity::getPath() const
{
return Handle.path();
}
std::string DesktopFSEntity::resolveFilePath(const std::string& file) const
{
return cppfs::FilePath(Handle.path()).resolve(file).fullPath();
}
bool DesktopFSEntity::exist() const
{
return Handle.exists();
}
std::string DesktopFS::getExtension(std::shared_ptr<FileSystemEntity> file) const
{
cppfs::FilePath fp(file->getPath());
return fp.extension();
}
FSEntityPtr DesktopFS::getContainingDirectory(std::shared_ptr<FileSystemEntity> file) const
{
cppfs::FilePath fp(file->getPath());
return getEntity(fp.directoryPath());
}
FSEntityList DesktopFS::getFileList(std::shared_ptr<FileSystemEntity> dir) const
{
FSEntityList fileList;
if (!dir->isDirectory())
return fileList;
auto dirHandle = std::static_pointer_cast<DesktopFSEntity>(dir)->Handle;
cppfs::FilePath fp(dirHandle.path());
for (cppfs::FileIterator it = dirHandle.begin(); it != dirHandle.end(); ++it)
{
fileList.push_back(std::make_shared<DesktopFSEntity>(cppfs::fs::open(fp.resolve(*it).fullPath())));
}
return fileList;
}
std::string DesktopFS::getFileContent(std::shared_ptr<FileSystemEntity> file) const
{
auto stream = std::static_pointer_cast<DesktopFSEntity>(file)->Handle.createInputStream(std::ios::in | std::ios::binary);
std::string s(std::istreambuf_iterator<char>(*stream), {});
return s;
}
std::shared_ptr<FileSystemEntity> DesktopFS::getEntity(const std::string& localPath, bool /*isDirectory*/) const
{
return std::make_shared<DesktopFSEntity>(cppfs::fs::open(localPath));
}
std::string DesktopFS::getSavePath() const
{
char *path = SDL_GetPrefPath("TurbulentSoftware", "Chewman");
if (!path)
{
throw VulkanException("Can't get folder for saving data");
}
return std::string(path);
}
} // namespace SVE