sys_fs_open() O_APPEND support

Added vm::static_ptr_cast, vm::const_ptr_cast, vm::reinterpret_ptr_cast
Added pointer comparison with vm::null
This commit is contained in:
Nekotekina 2015-06-18 14:55:11 +03:00
parent e338c3938b
commit 63c5b2ab32
3 changed files with 127 additions and 44 deletions

View File

@ -9,9 +9,9 @@
static_assert(fs::file::null == intptr_t(INVALID_HANDLE_VALUE) && fs::dir::null == fs::file::null, "Check fs::file::null definition");
std::unique_ptr<wchar_t[]> ConvertUTF8ToWChar(const std::string& source)
std::unique_ptr<wchar_t[]> to_wchar(const std::string& source)
{
const size_t length = source.size() + 1; // size + null terminator
const auto length = source.size() + 1; // size + null terminator
const int size = source.size() < INT_MAX ? static_cast<int>(length) : throw std::length_error(__FUNCTION__);
@ -26,15 +26,13 @@ std::unique_ptr<wchar_t[]> ConvertUTF8ToWChar(const std::string& source)
return buffer;
}
std::string ConvertWCharToUTF8(const wchar_t* source)
void to_utf8(std::string& result, const wchar_t* source)
{
const int length = lstrlenW(source); // source length
std::string result;
if (length == 0)
{
return result;
return result.clear();
}
const int size = WideCharToMultiByte(CP_UTF8, 0, source, length, NULL, 0, NULL, NULL); // output size
@ -51,8 +49,6 @@ std::string ConvertWCharToUTF8(const wchar_t* source)
{
throw __FUNCTION__;
}
return result;
}
time_t to_time_t(const ULARGE_INTEGER& ft)
@ -81,7 +77,7 @@ time_t to_time_t(const FILETIME& ft)
bool truncate_file(const std::string& file, u64 length)
{
// open the file
const auto handle = CreateFileW(ConvertUTF8ToWChar(file).get(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
const auto handle = CreateFileW(to_wchar(file).get(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (handle == INVALID_HANDLE_VALUE)
{
@ -124,7 +120,7 @@ bool fs::stat(const std::string& path, stat_t& info)
{
#ifdef _WIN32
WIN32_FILE_ATTRIBUTE_DATA attrs;
if (!GetFileAttributesExW(ConvertUTF8ToWChar(path).get(), GetFileExInfoStandard, &attrs))
if (!GetFileAttributesExW(to_wchar(path).get(), GetFileExInfoStandard, &attrs))
{
return false;
}
@ -156,7 +152,7 @@ bool fs::stat(const std::string& path, stat_t& info)
bool fs::exists(const std::string& path)
{
#ifdef _WIN32
return GetFileAttributesW(ConvertUTF8ToWChar(path).get()) != 0xFFFFFFFF;
return GetFileAttributesW(to_wchar(path).get()) != 0xFFFFFFFF;
#else
struct stat buffer;
return stat(path.c_str(), &buffer) == 0;
@ -167,7 +163,7 @@ bool fs::is_file(const std::string& file)
{
#ifdef _WIN32
DWORD attrs;
if ((attrs = GetFileAttributesW(ConvertUTF8ToWChar(file).get())) == INVALID_FILE_ATTRIBUTES)
if ((attrs = GetFileAttributesW(to_wchar(file).get())) == INVALID_FILE_ATTRIBUTES)
{
return false;
}
@ -188,7 +184,7 @@ bool fs::is_dir(const std::string& dir)
{
#ifdef _WIN32
DWORD attrs;
if ((attrs = GetFileAttributesW(ConvertUTF8ToWChar(dir).get())) == INVALID_FILE_ATTRIBUTES)
if ((attrs = GetFileAttributesW(to_wchar(dir).get())) == INVALID_FILE_ATTRIBUTES)
{
return false;
}
@ -208,7 +204,7 @@ bool fs::is_dir(const std::string& dir)
bool fs::create_dir(const std::string& dir)
{
#ifdef _WIN32
if (!CreateDirectoryW(ConvertUTF8ToWChar(dir).get(), NULL))
if (!CreateDirectoryW(to_wchar(dir).get(), NULL))
#else
if (mkdir(dir.c_str(), 0777))
#endif
@ -265,7 +261,7 @@ bool fs::create_path(const std::string& path)
bool fs::remove_dir(const std::string& dir)
{
#ifdef _WIN32
if (!RemoveDirectoryW(ConvertUTF8ToWChar(dir).get()))
if (!RemoveDirectoryW(to_wchar(dir).get()))
#else
if (rmdir(dir.c_str()))
#endif
@ -279,9 +275,8 @@ bool fs::remove_dir(const std::string& dir)
bool fs::rename(const std::string& from, const std::string& to)
{
// TODO: Deal with case-sensitivity
#ifdef _WIN32
if (!MoveFileW(ConvertUTF8ToWChar(from).get(), ConvertUTF8ToWChar(to).get()))
if (!MoveFileW(to_wchar(from).get(), to_wchar(to).get()))
#else
if (rename(from.c_str(), to.c_str()))
#endif
@ -332,7 +327,7 @@ int OSCopyFile(const char* source, const char* destination, bool overwrite)
bool fs::copy_file(const std::string& from, const std::string& to, bool overwrite)
{
#ifdef _WIN32
if (!CopyFileW(ConvertUTF8ToWChar(from).get(), ConvertUTF8ToWChar(to).get(), !overwrite))
if (!CopyFileW(to_wchar(from).get(), to_wchar(to).get(), !overwrite))
#else
if (OSCopyFile(from.c_str(), to.c_str(), overwrite))
#endif
@ -347,7 +342,7 @@ bool fs::copy_file(const std::string& from, const std::string& to, bool overwrit
bool fs::remove_file(const std::string& file)
{
#ifdef _WIN32
if (!DeleteFileW(ConvertUTF8ToWChar(file).get()))
if (!DeleteFileW(to_wchar(file).get()))
#else
if (unlink(file.c_str()))
#endif
@ -388,7 +383,7 @@ fs::file::~file()
bool fs::file::open(const std::string& filename, u32 mode)
{
this->~file();
this->close();
#ifdef _WIN32
DWORD access = 0;
@ -424,7 +419,7 @@ bool fs::file::open(const std::string& filename, u32 mode)
return false;
}
m_fd = (intptr_t)CreateFileW(ConvertUTF8ToWChar(filename).get(), access, FILE_SHARE_READ, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
m_fd = (intptr_t)CreateFileW(to_wchar(filename).get(), access, FILE_SHARE_READ, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
#else
int flags = 0;
@ -537,31 +532,35 @@ bool fs::file::close()
u64 fs::file::read(void* buffer, u64 count) const
{
const int size = count <= INT_MAX ? static_cast<int>(count) : throw std::length_error(__FUNCTION__);
#ifdef _WIN32
DWORD nread;
if (!ReadFile((HANDLE)m_fd, buffer, count, &nread, NULL))
if (!ReadFile((HANDLE)m_fd, buffer, size, &nread, NULL))
{
return -1;
}
return nread;
#else
return ::read(m_fd, buffer, count);
return ::read(m_fd, buffer, size);
#endif
}
u64 fs::file::write(const void* buffer, u64 count) const
{
const int size = count <= INT_MAX ? static_cast<int>(count) : throw std::length_error(__FUNCTION__);
#ifdef _WIN32
DWORD nwritten;
if (!WriteFile((HANDLE)m_fd, buffer, count, &nwritten, NULL))
if (!WriteFile((HANDLE)m_fd, buffer, size, &nwritten, NULL))
{
return -1;
}
return nwritten;
#else
return ::write(m_fd, buffer, count);
return ::write(m_fd, buffer, size);
#endif
}
@ -631,7 +630,7 @@ void fs::dir::import(handle_type dd, const std::string& path)
m_dd = dd;
#ifdef _WIN32
m_path = ConvertUTF8ToWChar(path);
m_path = to_wchar(path);
#else
m_path.reset(new char[path.size() + 1]);
memcpy(m_path.get(), path.c_str(), path.size() + 1);
@ -659,7 +658,7 @@ bool fs::dir::open(const std::string& dirname)
}
#ifdef _WIN32
m_path = ConvertUTF8ToWChar(dirname + "/*");
m_path = to_wchar(dirname + "/*");
#else
m_path.reset(new char[dirname.size() + 1]);
memcpy(m_path.get(), dirname.c_str(), dirname.size() + 1);
@ -723,7 +722,7 @@ bool fs::dir::get_first(std::string& name, stat_t& info)
return false;
}
name = ConvertWCharToUTF8(found.cFileName);
to_utf8(name, found.cFileName);
info.is_directory = (found.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
info.is_writable = (found.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0;
@ -755,7 +754,7 @@ bool fs::dir::get_next(std::string& name, stat_t& info)
return false;
}
name = ConvertWCharToUTF8(found.cFileName);
to_utf8(name, found.cFileName);
info.is_directory = (found.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
info.is_writable = (found.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0;

View File

@ -53,12 +53,12 @@ namespace vm
}
// enable only the conversions which are originally possible between pointer types
template<typename T2, typename AT2, typename dummy = std::enable_if_t<std::is_convertible<T*, T2*>::value>> operator _ptr_base<T2, AT2>() const
template<typename T2, typename AT2, typename = std::enable_if_t<std::is_convertible<T*, T2*>::value>> operator _ptr_base<T2, AT2>() const
{
return{ convert_le_be<AT2>(vm::cast(m_addr)) };
}
template<typename T2, typename dummy = std::enable_if_t<std::is_convertible<T*, T2*>::value>> explicit operator T2*() const
template<typename T2, typename = std::enable_if_t<std::is_convertible<T*, T2*>::value>> explicit operator T2*() const
{
return get_ptr();
}
@ -205,9 +205,28 @@ namespace vm
RT>;
// helper SFINAE type for vm::_ptr_base pointer arithmetic operators and indirection (disabled for void and function pointers)
template<typename T, typename RT> using if_arithmetical_t = std::enable_if_t<
!std::is_void<T>::value && !std::is_function<T>::value,
template<typename T, typename RT> using if_arithmetical_ptr_t = std::enable_if_t<
!std::is_void<T>::value &&
!std::is_function<T>::value,
RT>;
// perform static_cast (for example, vm::ptr<void> to vm::ptr<char>)
template<typename CT, typename T, typename AT, typename = decltype(static_cast<CT*>(std::declval<T*>()))> inline _ptr_base<CT, AT> static_ptr_cast(const _ptr_base<T, AT>& other)
{
return{ other.m_addr };
}
// perform const_cast (for example, vm::ptr<const char> to vm::ptr<char>)
template<typename CT, typename T, typename AT, typename = decltype(const_cast<CT*>(std::declval<T*>()))> inline _ptr_base<CT, AT> const_ptr_cast(const _ptr_base<T, AT>& other)
{
return{ other.m_addr };
}
// perform reinterpret_cast (for example, vm::ptr<char> to vm::ptr<u32>)
template<typename CT, typename T, typename AT, typename = decltype(reinterpret_cast<CT*>(std::declval<T*>()))> inline _ptr_base<CT, AT> reinterpret_ptr_cast(const _ptr_base<T, AT>& other)
{
return{ other.m_addr };
}
}
// unary plus operator for vm::_ptr_base (always available)
@ -217,13 +236,13 @@ template<typename T, typename AT> inline vm::_ptr_base<T, AT> operator +(const v
}
// indirection operator for vm::_ptr_base
template<typename T, typename AT> inline vm::if_arithmetical_t<T, T&> operator *(const vm::_ptr_base<T, AT>& ptr)
template<typename T, typename AT> inline vm::if_arithmetical_ptr_t<T, T&> operator *(const vm::_ptr_base<T, AT>& ptr)
{
return vm::get_ref<T>(vm::cast(ptr.m_addr));
}
// postfix increment operator for vm::_ptr_base
template<typename T, typename AT> inline vm::if_arithmetical_t<T, vm::_ptr_base<T, AT>> operator ++(vm::_ptr_base<T, AT>& ptr, int)
template<typename T, typename AT> inline vm::if_arithmetical_ptr_t<T, vm::_ptr_base<T, AT>> operator ++(vm::_ptr_base<T, AT>& ptr, int)
{
const AT result = ptr.m_addr;
ptr.m_addr += sizeof32(T);
@ -231,14 +250,14 @@ template<typename T, typename AT> inline vm::if_arithmetical_t<T, vm::_ptr_base<
}
// prefix increment operator for vm::_ptr_base
template<typename T, typename AT> inline vm::if_arithmetical_t<T, vm::_ptr_base<T, AT>&> operator ++(vm::_ptr_base<T, AT>& ptr)
template<typename T, typename AT> inline vm::if_arithmetical_ptr_t<T, vm::_ptr_base<T, AT>&> operator ++(vm::_ptr_base<T, AT>& ptr)
{
ptr.m_addr += sizeof32(T);
return ptr;
}
// postfix decrement operator for vm::_ptr_base
template<typename T, typename AT> inline vm::if_arithmetical_t<T, vm::_ptr_base<T, AT>> operator --(vm::_ptr_base<T, AT>& ptr, int)
template<typename T, typename AT> inline vm::if_arithmetical_ptr_t<T, vm::_ptr_base<T, AT>> operator --(vm::_ptr_base<T, AT>& ptr, int)
{
const AT result = ptr.m_addr;
ptr.m_addr -= sizeof32(T);
@ -246,40 +265,40 @@ template<typename T, typename AT> inline vm::if_arithmetical_t<T, vm::_ptr_base<
}
// prefix decrement operator for vm::_ptr_base
template<typename T, typename AT> inline vm::if_arithmetical_t<T, vm::_ptr_base<T, AT>&> operator --(vm::_ptr_base<T, AT>& ptr)
template<typename T, typename AT> inline vm::if_arithmetical_ptr_t<T, vm::_ptr_base<T, AT>&> operator --(vm::_ptr_base<T, AT>& ptr)
{
ptr.m_addr -= sizeof32(T);
return ptr;
}
// addition assignment operator for vm::_ptr_base (pointer += integer)
template<typename T, typename AT> inline vm::if_arithmetical_t<T, vm::_ptr_base<T, AT>&> operator +=(vm::_ptr_base<T, AT>& ptr, to_ne_t<AT> count)
template<typename T, typename AT> inline vm::if_arithmetical_ptr_t<T, vm::_ptr_base<T, AT>&> operator +=(vm::_ptr_base<T, AT>& ptr, to_ne_t<AT> count)
{
ptr.m_addr += count * sizeof32(T);
return ptr;
}
// subtraction assignment operator for vm::_ptr_base (pointer -= integer)
template<typename T, typename AT> inline vm::if_arithmetical_t<T, vm::_ptr_base<T, AT>&> operator -=(vm::_ptr_base<T, AT>& ptr, to_ne_t<AT> count)
template<typename T, typename AT> inline vm::if_arithmetical_ptr_t<T, vm::_ptr_base<T, AT>&> operator -=(vm::_ptr_base<T, AT>& ptr, to_ne_t<AT> count)
{
ptr.m_addr -= count * sizeof32(T);
return ptr;
}
// addition operator for vm::_ptr_base (pointer + integer)
template<typename T, typename AT> inline vm::if_arithmetical_t<T, vm::_ptr_base<T, AT>> operator +(const vm::_ptr_base<T, AT>& ptr, to_ne_t<AT> count)
template<typename T, typename AT> inline vm::if_arithmetical_ptr_t<T, vm::_ptr_base<T, AT>> operator +(const vm::_ptr_base<T, AT>& ptr, to_ne_t<AT> count)
{
return{ convert_le_be<AT>(ptr.m_addr + count * sizeof32(T)) };
}
// addition operator for vm::_ptr_base (integer + pointer)
template<typename T, typename AT> inline vm::if_arithmetical_t<T, vm::_ptr_base<T, AT>> operator +(to_ne_t<AT> count, const vm::_ptr_base<T, AT>& ptr)
template<typename T, typename AT> inline vm::if_arithmetical_ptr_t<T, vm::_ptr_base<T, AT>> operator +(to_ne_t<AT> count, const vm::_ptr_base<T, AT>& ptr)
{
return{ convert_le_be<AT>(ptr.m_addr + count * sizeof32(T)) };
}
// subtraction operator for vm::_ptr_base (pointer - integer)
template<typename T, typename AT> inline vm::if_arithmetical_t<T, vm::_ptr_base<T, AT>> operator -(const vm::_ptr_base<T, AT>& ptr, to_ne_t<AT> count)
template<typename T, typename AT> inline vm::if_arithmetical_ptr_t<T, vm::_ptr_base<T, AT>> operator -(const vm::_ptr_base<T, AT>& ptr, to_ne_t<AT> count)
{
return{ convert_le_be<AT>(ptr.m_addr - count * sizeof32(T)) };
}
@ -302,36 +321,96 @@ template<typename T1, typename AT1, typename T2, typename AT2> vm::if_comparable
return left.m_addr == right.m_addr;
}
template<typename T, typename AT> bool operator ==(const vm::null_t&, const vm::_ptr_base<T, AT>& ptr)
{
return ptr.m_addr == 0;
}
template<typename T, typename AT> bool operator ==(const vm::_ptr_base<T, AT>& ptr, const vm::null_t&)
{
return ptr.m_addr == 0;
}
// comparison operator for vm::_ptr_base (pointer1 != pointer2)
template<typename T1, typename AT1, typename T2, typename AT2> vm::if_comparable_t<T1, T2, bool> operator !=(const vm::_ptr_base<T1, AT1>& left, const vm::_ptr_base<T2, AT2>& right)
{
return left.m_addr != right.m_addr;
}
template<typename T, typename AT> bool operator !=(const vm::null_t&, const vm::_ptr_base<T, AT>& ptr)
{
return ptr.m_addr != 0;
}
template<typename T, typename AT> bool operator !=(const vm::_ptr_base<T, AT>& ptr, const vm::null_t&)
{
return ptr.m_addr != 0;
}
// comparison operator for vm::_ptr_base (pointer1 < pointer2)
template<typename T1, typename AT1, typename T2, typename AT2> vm::if_comparable_t<T1, T2, bool> operator <(const vm::_ptr_base<T1, AT1>& left, const vm::_ptr_base<T2, AT2>& right)
{
return left.m_addr < right.m_addr;
}
template<typename T, typename AT> bool operator <(const vm::null_t&, const vm::_ptr_base<T, AT>& ptr)
{
return ptr.m_addr != 0;
}
template<typename T, typename AT> bool operator <(const vm::_ptr_base<T, AT>&, const vm::null_t&)
{
return false;
}
// comparison operator for vm::_ptr_base (pointer1 <= pointer2)
template<typename T1, typename AT1, typename T2, typename AT2> vm::if_comparable_t<T1, T2, bool> operator <=(const vm::_ptr_base<T1, AT1>& left, const vm::_ptr_base<T2, AT2>& right)
{
return left.m_addr <= right.m_addr;
}
template<typename T, typename AT> bool operator <=(const vm::null_t&, const vm::_ptr_base<T, AT>&)
{
return true;
}
template<typename T, typename AT> bool operator <=(const vm::_ptr_base<T, AT>& ptr, const vm::null_t&)
{
return ptr.m_addr == 0;
}
// comparison operator for vm::_ptr_base (pointer1 > pointer2)
template<typename T1, typename AT1, typename T2, typename AT2> vm::if_comparable_t<T1, T2, bool> operator >(const vm::_ptr_base<T1, AT1>& left, const vm::_ptr_base<T2, AT2>& right)
{
return left.m_addr > right.m_addr;
}
template<typename T, typename AT> bool operator >(const vm::null_t&, const vm::_ptr_base<T, AT>&)
{
return false;
}
template<typename T, typename AT> bool operator >(const vm::_ptr_base<T, AT>& ptr, const vm::null_t&)
{
return ptr.m_addr != 0;
}
// comparison operator for vm::_ptr_base (pointer1 >= pointer2)
template<typename T1, typename AT1, typename T2, typename AT2> vm::if_comparable_t<T1, T2, bool> operator >=(const vm::_ptr_base<T1, AT1>& left, const vm::_ptr_base<T2, AT2>& right)
{
return left.m_addr >= right.m_addr;
}
template<typename T, typename AT> bool operator >=(const vm::null_t&, const vm::_ptr_base<T, AT>& ptr)
{
return ptr.m_addr == 0;
}
template<typename T, typename AT> bool operator >=(const vm::_ptr_base<T, AT>&, const vm::null_t&)
{
return true;
}
// external specialization for is_be_t<> (true if AT is be_t<>)
template<typename T, typename AT>

View File

@ -68,6 +68,11 @@ s32 sys_fs_open(vm::ptr<const char> path, s32 flags, vm::ptr<u32> fd, s32 mode,
open_mode |= o_trunc;
}
if (flags & CELL_FS_O_APPEND)
{
open_mode |= o_append;
}
if (flags & CELL_FS_O_EXCL)
{
if (flags & CELL_FS_O_CREAT)
@ -80,7 +85,7 @@ s32 sys_fs_open(vm::ptr<const char> path, s32 flags, vm::ptr<u32> fd, s32 mode,
}
}
if (flags & ~(CELL_FS_O_ACCMODE | CELL_FS_O_CREAT | CELL_FS_O_TRUNC | CELL_FS_O_EXCL))
if (flags & ~(CELL_FS_O_ACCMODE | CELL_FS_O_CREAT | CELL_FS_O_TRUNC | CELL_FS_O_APPEND | CELL_FS_O_EXCL))
{
open_mode = 0; // error
}