mirror of
https://github.com/akuker/RASCSI.git
synced 2024-06-28 05:29:33 +00:00
05db0e4688
* Fixing SonarCloud issues, first round * Fixing SonarCLoud issues, next round * Fixing SonarQube issues, next round * Fixed warning * Replaced empty constructors/destructors with = default; * Fixed warning * Replaced new * Use constants instead of macros * Use structured binding declarations * Use init statements for if * Use string views * Use enum class, use using instead of typedef * Fixed more SonarCloud warnings * Replaced redundant/duplicate types with auto * Devlared methods const * Memory management update * Fixed warning * Added missing const * Improved RaScsiResponse memory management * Improved memory management * Improved memory management * Replaced macros by constants, removed unused constants * Made member private * Fixed warning * Added comment * Fixed shadowing warnings * Cleanup * Cleanup * Cleanup * Fixed shadowing warning * Removed unused code * Fixed more warnings * Removed obsolete casts * Fixed warnings * Removed unused field * Removed library not needed by rasctl * Include cleanup * Updated platform check for better compatibility * Improved check for invalid option. This prevents rasctl to break on macos. * Updated option check * Fixed typo * Added TODO * Removed macro * Scope update * Replaced macro * Added TODO, update memory management * Fixed typo * Replaced NULL by nullptr * Use more structured bindings * Added TODO * Use calloc instead of mallco to not need memset * Fixed warnings * Fixed SonarQube initialization issues * Fixed warning * Cleaned up override/virtual/final * Fixed warnings * Constructor update * Fixed tests * Improved memory management * Added missing const * Added const * Fixed two bugs reported by SonarCloud * Fix SonarCloud hotspot * Fixed memory management * Memory management update * Addressing hotspot by using strncpy * Fixed SonarCloud issues * Fixed SonarQube issues * Added missing const * Added const * Added const * Suppress false positive * Added SonarQube suppressions for false positives * Added suppresoin * Fixed code smells * Reverted changes that is a SonarQube issue, but caused problems with -O3 * Removed TODO based on review
236 lines
3.6 KiB
C++
236 lines
3.6 KiB
C++
//---------------------------------------------------------------------------
|
||
//
|
||
// X68000 EMULATOR "XM6"
|
||
//
|
||
// Copyright (C) 2001-2006 PI.(ytanaka@ipc-tokai.or.jp)
|
||
// Copyright (C) 2010-2020 GIMONS
|
||
// [ File I/O (Subset for RaSCSI) ]
|
||
//
|
||
//---------------------------------------------------------------------------
|
||
|
||
#include "os.h"
|
||
#include "filepath.h"
|
||
#include "fileio.h"
|
||
|
||
#include "config.h"
|
||
|
||
//===========================================================================
|
||
//
|
||
// File I/O
|
||
//
|
||
//===========================================================================
|
||
|
||
Fileio::~Fileio()
|
||
{
|
||
ASSERT(handle == -1);
|
||
|
||
// Safety measure for Release
|
||
Close();
|
||
}
|
||
|
||
BOOL Fileio::Load(const Filepath& path, void *buffer, int size)
|
||
{
|
||
ASSERT(buffer);
|
||
ASSERT(size > 0);
|
||
ASSERT(handle < 0);
|
||
|
||
if (!Open(path, ReadOnly)) {
|
||
return FALSE;
|
||
}
|
||
|
||
if (!Read(buffer, size)) {
|
||
Close();
|
||
return FALSE;
|
||
}
|
||
|
||
Close();
|
||
|
||
return TRUE;
|
||
}
|
||
|
||
BOOL Fileio::Save(const Filepath& path, const void *buffer, int size)
|
||
{
|
||
ASSERT(buffer);
|
||
ASSERT(size > 0);
|
||
ASSERT(handle < 0);
|
||
|
||
if (!Open(path, WriteOnly)) {
|
||
return FALSE;
|
||
}
|
||
|
||
if (!Write(buffer, size)) {
|
||
Close();
|
||
return FALSE;
|
||
}
|
||
|
||
Close();
|
||
|
||
return TRUE;
|
||
}
|
||
|
||
BOOL Fileio::Open(const char *fname, OpenMode mode, BOOL directIO)
|
||
{
|
||
mode_t omode;
|
||
|
||
ASSERT(fname);
|
||
ASSERT(handle < 0);
|
||
|
||
// Always fail a read from a null array
|
||
if (fname[0] == _T('\0')) {
|
||
handle = -1;
|
||
return FALSE;
|
||
}
|
||
|
||
// Default mode
|
||
omode = directIO ? O_DIRECT : 0;
|
||
|
||
switch (mode) {
|
||
case ReadOnly:
|
||
handle = open(fname, O_RDONLY | omode);
|
||
break;
|
||
|
||
case WriteOnly:
|
||
handle = open(fname, O_CREAT | O_WRONLY | O_TRUNC | omode, 0666);
|
||
break;
|
||
|
||
case ReadWrite:
|
||
// Make sure RW does not succeed when reading from CD-ROM
|
||
if (access(fname, 0x06) != 0) {
|
||
return FALSE;
|
||
}
|
||
handle = open(fname, O_RDWR | omode);
|
||
break;
|
||
|
||
default:
|
||
ASSERT(FALSE);
|
||
break;
|
||
}
|
||
|
||
// Evaluate results
|
||
if (handle == -1) {
|
||
return FALSE;
|
||
}
|
||
|
||
ASSERT(handle >= 0);
|
||
return TRUE;
|
||
}
|
||
|
||
BOOL Fileio::Open(const char *fname, OpenMode mode)
|
||
{
|
||
|
||
return Open(fname, mode, FALSE);
|
||
}
|
||
|
||
BOOL Fileio::Open(const Filepath& path, OpenMode mode)
|
||
{
|
||
|
||
return Open(path.GetPath(), mode);
|
||
}
|
||
|
||
BOOL Fileio::OpenDIO(const char *fname, OpenMode mode)
|
||
{
|
||
|
||
// Open with included O_DIRECT
|
||
if (!Open(fname, mode, TRUE)) {
|
||
// Normal mode retry (tmpfs etc.)
|
||
return Open(fname, mode, FALSE);
|
||
}
|
||
|
||
return TRUE;
|
||
}
|
||
|
||
BOOL Fileio::OpenDIO(const Filepath& path, OpenMode mode)
|
||
{
|
||
|
||
return OpenDIO(path.GetPath(), mode);
|
||
}
|
||
|
||
BOOL Fileio::Read(void *buffer, int size) const
|
||
{
|
||
long count;
|
||
|
||
ASSERT(buffer);
|
||
ASSERT(size > 0);
|
||
ASSERT(handle >= 0);
|
||
|
||
count = read(handle, buffer, size);
|
||
if (count != size) {
|
||
return FALSE;
|
||
}
|
||
|
||
return TRUE;
|
||
}
|
||
|
||
BOOL Fileio::Write(const void *buffer, int size) const
|
||
{
|
||
long count;
|
||
|
||
ASSERT(buffer);
|
||
ASSERT(size > 0);
|
||
ASSERT(handle >= 0);
|
||
|
||
count = write(handle, buffer, size);
|
||
if (count != size) {
|
||
return FALSE;
|
||
}
|
||
|
||
return TRUE;
|
||
}
|
||
|
||
BOOL Fileio::Seek(off_t offset, BOOL relative) const
|
||
{
|
||
ASSERT(handle >= 0);
|
||
ASSERT(offset >= 0);
|
||
|
||
// Add current position in case of relative seek
|
||
if (relative) {
|
||
offset += GetFilePos();
|
||
}
|
||
|
||
if (lseek(handle, offset, SEEK_SET) != offset) {
|
||
return FALSE;
|
||
}
|
||
|
||
return TRUE;
|
||
}
|
||
|
||
off_t Fileio::GetFileSize() const
|
||
{
|
||
off_t cur;
|
||
off_t end;
|
||
|
||
ASSERT(handle >= 0);
|
||
|
||
// Get file position in 64bit
|
||
cur = GetFilePos();
|
||
|
||
// Get file size in64bitで
|
||
end = lseek(handle, 0, SEEK_END);
|
||
|
||
// Return to start position
|
||
Seek(cur);
|
||
|
||
return end;
|
||
}
|
||
|
||
off_t Fileio::GetFilePos() const
|
||
{
|
||
off_t pos;
|
||
|
||
ASSERT(handle >= 0);
|
||
|
||
// Get file position in 64bit
|
||
pos = lseek(handle, 0, SEEK_CUR);
|
||
return pos;
|
||
|
||
}
|
||
|
||
void Fileio::Close()
|
||
{
|
||
|
||
if (handle != -1) {
|
||
close(handle);
|
||
handle = -1;
|
||
}
|
||
}
|