Play-/Source/iop/DirectoryDevice.cpp

77 lines
1.8 KiB
C++
Raw Normal View History

2019-08-17 13:51:31 -04:00
#include <cassert>
#include "DirectoryDevice.h"
#include "StdStream.h"
#include "string_cast.h"
#include "../AppConfig.h"
using namespace Iop::Ioman;
CDirectoryDevice::CDirectoryDevice(const char* basePathPreferenceName)
2018-04-30 21:01:23 +01:00
: m_basePathPreferenceName(basePathPreferenceName)
{
}
template <typename StringType>
Framework::CStdStream* CreateStdStream(const StringType&, const char*);
template <>
Framework::CStdStream* CreateStdStream(const std::string& path, const char* mode)
{
return new Framework::CStdStream(path.c_str(), mode);
}
template <>
Framework::CStdStream* CreateStdStream(const std::wstring& path, const char* mode)
{
auto cvtMode = string_cast<std::wstring>(mode);
return new Framework::CStdStream(path.c_str(), cvtMode.c_str());
}
Framework::CStream* CDirectoryDevice::GetFile(uint32 accessType, const char* devicePath)
{
auto basePath = CAppConfig::GetInstance().GetPreferencePath(m_basePathPreferenceName.c_str());
auto path = basePath / devicePath;
const char* mode = nullptr;
switch(accessType)
{
case 0:
case OPEN_FLAG_RDONLY:
mode = "rb";
break;
2018-05-09 16:11:56 -04:00
case(OPEN_FLAG_WRONLY | OPEN_FLAG_CREAT):
2018-05-10 19:19:01 -04:00
case(OPEN_FLAG_WRONLY | OPEN_FLAG_CREAT | OPEN_FLAG_TRUNC):
2018-05-09 16:11:56 -04:00
mode = "wb";
break;
2018-09-18 12:29:59 -04:00
case OPEN_FLAG_RDWR:
mode = "r+";
break;
2018-04-30 21:01:23 +01:00
case(OPEN_FLAG_RDWR | OPEN_FLAG_CREAT):
mode = "w+";
break;
default:
assert(0);
break;
}
try
{
return CreateStdStream(path.native(), mode);
}
catch(...)
{
return nullptr;
}
}
Directory CDirectoryDevice::GetDirectory(const char* devicePath)
{
auto basePath = CAppConfig::GetInstance().GetPreferencePath(m_basePathPreferenceName.c_str());
auto path = basePath / devicePath;
2019-10-16 20:51:11 -04:00
if(!fs::is_directory(path))
{
throw std::runtime_error("Not a directory.");
}
2019-10-16 20:51:11 -04:00
return fs::directory_iterator(path);
}