2019-08-17 13:51:31 -04:00
|
|
|
#include <cassert>
|
2008-01-15 20:27:44 +00:00
|
|
|
#include "DirectoryDevice.h"
|
|
|
|
#include "StdStream.h"
|
2017-12-31 13:38:32 -05:00
|
|
|
#include "string_cast.h"
|
2008-08-24 21:28:42 +00:00
|
|
|
#include "../AppConfig.h"
|
2008-01-15 20:27:44 +00:00
|
|
|
|
2008-01-19 03:36:27 +00:00
|
|
|
using namespace Iop::Ioman;
|
2008-01-15 20:27:44 +00:00
|
|
|
|
2012-09-05 20:40:23 +00:00
|
|
|
CDirectoryDevice::CDirectoryDevice(const char* basePathPreferenceName)
|
2018-04-30 21:01:23 +01:00
|
|
|
: m_basePathPreferenceName(basePathPreferenceName)
|
2008-01-15 20:27:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-12-31 13:38:32 -05:00
|
|
|
template <typename StringType>
|
|
|
|
Framework::CStdStream* CreateStdStream(const StringType&, const char*);
|
|
|
|
|
|
|
|
template <>
|
|
|
|
Framework::CStdStream* CreateStdStream(const std::string& path, const char* mode)
|
2008-01-15 20:27:44 +00:00
|
|
|
{
|
2017-12-31 13:38:32 -05:00
|
|
|
return new Framework::CStdStream(path.c_str(), mode);
|
|
|
|
}
|
2008-01-15 20:27:44 +00:00
|
|
|
|
2017-12-31 13:38:32 -05:00
|
|
|
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());
|
|
|
|
}
|
2008-01-15 20:27:44 +00:00
|
|
|
|
2017-12-31 13:38:32 -05:00
|
|
|
Framework::CStream* CDirectoryDevice::GetFile(uint32 accessType, const char* devicePath)
|
|
|
|
{
|
|
|
|
auto basePath = CAppConfig::GetInstance().GetPreferencePath(m_basePathPreferenceName.c_str());
|
|
|
|
auto path = basePath / devicePath;
|
2008-01-15 20:27:44 +00:00
|
|
|
|
2017-12-31 13:38:32 -05:00
|
|
|
const char* mode = nullptr;
|
2008-01-15 20:27:44 +00:00
|
|
|
switch(accessType)
|
|
|
|
{
|
|
|
|
case 0:
|
2012-09-05 20:40:23 +00:00
|
|
|
case OPEN_FLAG_RDONLY:
|
2008-01-15 20:27:44 +00:00
|
|
|
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):
|
2008-01-15 20:27:44 +00:00
|
|
|
mode = "w+";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-12-31 13:38:32 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return CreateStdStream(path.native(), mode);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2008-01-15 20:27:44 +00:00
|
|
|
}
|
2018-09-20 19:05:24 -04:00
|
|
|
|
|
|
|
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))
|
2018-09-20 19:05:24 -04:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Not a directory.");
|
|
|
|
}
|
2019-10-16 20:51:11 -04:00
|
|
|
return fs::directory_iterator(path);
|
2018-09-20 19:05:24 -04:00
|
|
|
}
|