Play-/Source/iop/ioman/DirectoryDevice.cpp

97 lines
2.4 KiB
C++
Raw Permalink Normal View History

2019-08-17 13:51:31 -04:00
#include <cassert>
#include "DirectoryDevice.h"
2021-01-05 17:25:38 -05:00
#include "../Iop_PathUtils.h"
#include "PathDirectoryIterator.h"
#include "StdStream.h"
#include "string_cast.h"
using namespace Iop::Ioman;
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 = GetBasePath();
auto path = Iop::PathUtils::MakeHostPath(basePath, devicePath);
2021-03-09 17:42:50 -05:00
//Get rid of unwanted flags
//Used by Midnight Club 3
accessType &= ~OPEN_FLAG_NOWAIT;
const char* mode = nullptr;
switch(accessType)
{
default:
assert(0);
[[fallthrough]];
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;
case OPEN_FLAG_WRONLY:
2018-09-18 12:29:59 -04:00
case OPEN_FLAG_RDWR:
2020-07-06 12:11:17 -04:00
mode = "r+b";
2018-09-18 12:29:59 -04:00
break;
2018-04-30 21:01:23 +01:00
case(OPEN_FLAG_RDWR | OPEN_FLAG_CREAT):
2021-01-03 20:08:28 -05:00
case(OPEN_FLAG_RDWR | OPEN_FLAG_CREAT | OPEN_FLAG_TRUNC):
2020-07-06 12:11:17 -04:00
mode = "w+b";
break;
}
try
{
return CreateStdStream(path.native(), mode);
}
catch(...)
{
return nullptr;
}
}
DirectoryIteratorPtr CDirectoryDevice::GetDirectory(const char* devicePath)
{
auto basePath = GetBasePath();
auto path = Iop::PathUtils::MakeHostPath(basePath, devicePath);
2019-10-16 20:51:11 -04:00
if(!fs::is_directory(path))
{
throw std::runtime_error("Not a directory.");
}
return std::make_unique<CPathDirectoryIterator>(path);
}
void CDirectoryDevice::MakeDirectory(const char* devicePath)
{
auto basePath = GetBasePath();
auto path = Iop::PathUtils::MakeHostPath(basePath, devicePath);
if(!fs::create_directory(path))
{
throw std::runtime_error("Failed to create directory.");
}
}
2024-08-12 18:10:33 -04:00
void CDirectoryDevice::Rename(const char* srcDevicePath, const char* dstDevicePath)
{
auto basePath = GetBasePath();
auto srcPath = Iop::PathUtils::MakeHostPath(basePath, srcDevicePath);
auto dstPath = Iop::PathUtils::MakeHostPath(basePath, dstDevicePath);
fs::rename(srcPath, dstPath);
}