Fix open IOStream (required modes are: "wb", "w", "wt", "rb", "r", "rt") and improve read and write

This commit is contained in:
Transporter 2024-01-21 20:35:53 +01:00
parent f2350d9739
commit d6b17cdf87
1 changed files with 30 additions and 33 deletions

View File

@ -17,17 +17,22 @@ using namespace Qtk;
QtkIOStream::QtkIOStream(const char * pFile, const char * pMode) : QtkIOStream::QtkIOStream(const char * pFile, const char * pMode) :
mFile(pFile) { mFile(pFile) {
QString mode(pMode); QString mode(pMode);
bool read = mode.contains('r'); bool open = false;
bool write = mode.contains('w'); if (mode == "w" || mode == "wb") {
if(read && write) { open = mFile.open(QIODeviceBase::WriteOnly);
mFile.open(QIODevice::ReadWrite); } else if (mode == "r" || mode == "rb") {
} else if(read) { open = mFile.open(QIODeviceBase::ReadOnly);
mFile.open(QIODevice::ReadOnly); } else if (mode == "wt") {
} else if(write) { open = mFile.open(QIODeviceBase::WriteOnly | QIODeviceBase::Text);
mFile.open(QIODevice::WriteOnly); } else if (mode == "rt") {
open = mFile.open(QIODeviceBase::ReadOnly | QIODeviceBase::Text);
} else { } else {
open = false;
qDebug() << "[Qtk::QtkIOStream] Invalid file open mode: " << mode << "\n"; qDebug() << "[Qtk::QtkIOStream] Invalid file open mode: " << mode << "\n";
} }
if(!open) {
qDebug() << "[Qtk::QtkIOStream] Could not open file: " << QString(pFile) << "\n";
}
} }
/******************************************************************************* /*******************************************************************************
@ -35,24 +40,18 @@ QtkIOStream::QtkIOStream(const char * pFile, const char * pMode) :
******************************************************************************/ ******************************************************************************/
size_t QtkIOStream::Read(void * pvBuffer, size_t pSize, size_t pCount) { size_t QtkIOStream::Read(void * pvBuffer, size_t pSize, size_t pCount) {
size_t read = 0; qint64 readSize = mFile.read((char*)pvBuffer, pSize * pCount);
do {
auto readSize = mFile.read((char *)pvBuffer + read, pSize);
if(readSize < 0) { if(readSize < 0) {
qDebug() << "[Qtk::QtkIOStream] Failed to read (" << pSize qDebug() << "[Qtk::QtkIOStream] Failed to read (" << pSize
<< ") bytes from file at: " << mFile.filesystemFileName().c_str() << ") bytes from file at: " << mFile.filesystemFileName().c_str()
<< "\n"; << "\n";
return -1; return -1;
} }
read += readSize; return readSize;
} while(pCount--);
return read;
} }
size_t QtkIOStream::Write(const void * pvBuffer, size_t pSize, size_t pCount) { size_t QtkIOStream::Write(const void * pvBuffer, size_t pSize, size_t pCount) {
size_t wrote = 0; qint64 writeSize = mFile.write((char*)pvBuffer, pSize * pCount);
do {
auto writeSize = mFile.write((char *)pvBuffer + wrote, pSize);
if(writeSize < 0) { if(writeSize < 0) {
qDebug() << "[Qtk::QtkIOStream] Failed to write buffer with size (" qDebug() << "[Qtk::QtkIOStream] Failed to write buffer with size ("
<< pSize << pSize
@ -60,9 +59,7 @@ size_t QtkIOStream::Write(const void * pvBuffer, size_t pSize, size_t pCount) {
<< "\n"; << "\n";
return -1; return -1;
} }
wrote += writeSize; return writeSize;
} while(pCount--);
return wrote;
} }
aiReturn QtkIOStream::Seek(size_t pOffset, aiOrigin pOrigin) { aiReturn QtkIOStream::Seek(size_t pOffset, aiOrigin pOrigin) {