bool QFileStream::_OpenPlatformImplementation(const QPath &filePath, const EQFileOpenMode &eOpenMode, const bool bIsWritingAllowed, QFileStream::NativeHandle &handle, EQFileSystemError &eErrorInfo)
{
    bool bSuccess = true;

    using Kinesis::QuimeraEngine::Common::DataTypes::QArrayResult;
    using Kinesis::QuimeraEngine::Common::DataTypes::i8_q;
    using Kinesis::QuimeraEngine::Common::DataTypes::EQTextEncoding;

    QArrayResult<i8_q> szPath = filePath.ToString().ToBytes(EQTextEncoding::E_UTF8);
        
    int nAccess = bIsWritingAllowed ? O_RDWR: 
                                      O_RDONLY;
    int nPermissions = S_IRWXU; // Read, write and execution permissions for the owner
    int nOpenMode = 0;

    switch(eOpenMode)
    {
    case EQFileOpenMode::E_Append:
    case EQFileOpenMode::E_Open:
        nOpenMode = 0;
        break;
    case EQFileOpenMode::E_Create:
        nOpenMode = O_CREAT | O_EXCL;
        break;
    case EQFileOpenMode::E_CreateOrOverwrite:
        nOpenMode = O_CREAT | O_TRUNC;
        break;
    case EQFileOpenMode::E_OpenOrCreate:
        nOpenMode = O_CREAT;
        break;
    default:
        break;
    }
        
    handle = open(szPath.Get(), nAccess | nOpenMode, nPermissions);

    const bool FILE_OPENED_SUCCESSFULLY = handle >= 0;
        
    if(!FILE_OPENED_SUCCESSFULLY)
    {
        bSuccess = false;
        error_t lastError = errno;
        QE_ASSERT_ERROR(handle >= 0, string_q("An unexpected error occurred when opening the file \"") + filePath.GetAbsolutePath() + "\". The error code was: " + string_q::FromInteger(lastError) + ".");
            
        if(lastError == EACCES)
            eErrorInfo = EQFileSystemError::E_NoPermissions;
        else
            eErrorInfo = EQFileSystemError::E_Unknown;
    }

    return bSuccess;
}
bool QFileStream::_ClosePlarformImplementation(const QFileStream::NativeHandle &handle, const QPath &filePath)
{
    bool bSuccess = true;

    int nResult = close(handle);

    if(nResult < 0)
    {
        bSuccess = false;
        error_t lastError = errno;
        QE_ASSERT_ERROR(nResult >= 0, string_q("An unexpected error occurred when closing the file \"") + filePath.GetAbsolutePath() + "\". The error code was: " + string_q::FromInteger(lastError) + ".");
    }

    return bSuccess;
}
bool QFileStream::_WritePlatformImplementation(const void* pInputBuffer, const pointer_uint_q uNumberOfBytes, const pointer_uint_q uFileOffset, const QFileStream::NativeHandle &handle, const QPath &filePath)
{
    bool bSuccess = true;

    ssize_t nResult = pwrite(handle, pInputBuffer, uNumberOfBytes, uFileOffset);
        
    if(nResult < 0)
    {
        bSuccess = false;
        error_t lastError = errno;
        QE_ASSERT_ERROR(nResult >= 0, string_q("An unexpected error occurred when writing to the file \"") + filePath.GetAbsolutePath() + "\". The error code was: " + string_q::FromInteger(lastError) + ".");
    }

    return bSuccess;
}
bool QFileStream::_ClosePlarformImplementation(const QFileStream::NativeHandle &handle, const QPath &filePath)
{
    bool bSuccess = true;

    static const BOOL CLOSE_OPERATION_FAILED = 0;
    BOOL uCloseHandleResult = ::CloseHandle(handle);

    if(uCloseHandleResult == CLOSE_OPERATION_FAILED)
    {
        bSuccess = false;
        DWORD uCloseHandleLastError = ::GetLastError();
        QE_ASSERT_ERROR(uCloseHandleResult != 0, string_q("An unexpected error occurred when closing the file \"") + filePath.GetAbsolutePath() + "\". The error code was: " + string_q::FromInteger(uCloseHandleLastError) + ".");
    }

    return bSuccess;
}
bool QFileStream::_OpenPlatformImplementation(const QPath &filePath, const EQFileOpenMode &eOpenMode, const bool bIsWritingAllowed, QFileStream::NativeHandle &handle, EQFileSystemError &eErrorInfo)
{
    bool bSuccess = true;

    using Kinesis::QuimeraEngine::Common::DataTypes::QArrayResult;
    using Kinesis::QuimeraEngine::Common::DataTypes::i8_q;

    QArrayResult<i8_q> arPath = filePath.ToString().ToBytes(string_q::GetLocalEncodingUTF16());
    const wchar_t* szPath = rcast_q(arPath.Get(), wchar_t*);
    DWORD uAccess = bIsWritingAllowed ? GENERIC_READ | GENERIC_WRITE : 
                                        GENERIC_READ;
    DWORD uOpenMode = 0;

    switch(eOpenMode)
    {
    case EQFileOpenMode::E_Append:
    case EQFileOpenMode::E_Open:
        uOpenMode = OPEN_EXISTING;
        break;
    case EQFileOpenMode::E_Create:
        uOpenMode = CREATE_NEW;
        break;
    case EQFileOpenMode::E_CreateOrOverwrite:
        uOpenMode = CREATE_ALWAYS;
        break;
    case EQFileOpenMode::E_OpenOrCreate:
        uOpenMode = OPEN_ALWAYS;
        break;
    default:
        break;
    }

    handle = ::CreateFileW(szPath, uAccess, FILE_SHARE_READ, NULL, uOpenMode, FILE_FLAG_RANDOM_ACCESS, NULL);

    const bool FILE_OPENED_SUCCESSFULLY = handle != INVALID_HANDLE_VALUE;

    if(!FILE_OPENED_SUCCESSFULLY)
    {
        bSuccess = false;
        DWORD uCreateFileWLastError = ::GetLastError();
        QE_ASSERT_ERROR(handle != INVALID_HANDLE_VALUE, string_q("An unexpected error occurred when opening the file \"") + filePath.GetAbsolutePath() + "\". The error code was: " + string_q::FromInteger(uCreateFileWLastError) + ".");

        if(uCreateFileWLastError == ERROR_ACCESS_DENIED)
            eErrorInfo = EQFileSystemError::E_NoPermissions;
        else
            eErrorInfo = EQFileSystemError::E_Unknown;
    }

    return bSuccess;
}
bool QFileStream::_WritePlatformImplementation(const void* pInputBuffer, const pointer_uint_q uNumberOfBytes, const pointer_uint_q uFileOffset, const QFileStream::NativeHandle &handle, const QPath &filePath)
{
    bool bSuccess = true;

    OVERLAPPED offsets;
    memset(&offsets, 0, sizeof(OVERLAPPED)); // The structure MUST be zero-initialized

    // Sets the offset from where to start reading
#if QE_OS_WINDOWS == 32
        offsets.Offset = uFileOffset;
        offsets.OffsetHigh = 0;
#elif QE_OS_WINDOWS == 64
        const u32_q* pPosition = rcast_q(&uFileOffset, const u32_q*);
    #if QE_ENDIANNESS == QE_ENDIANNESS_LITTLEENDIAN
        offsets.Offset = pPosition[1];
        offsets.OffsetHigh = pPosition[0];
    #elif QE_ENDIANNESS == QE_ENDIANNESS_BIGENDIAN
        offsets.Offset = pPosition[0];
        offsets.OffsetHigh = pPosition[1];
    #endif
#endif

    static const BOOL WRITE_OPERATION_FAILED = 0;
    BOOL uWriteFileResult = ::WriteFile(handle, pInputBuffer, uNumberOfBytes, NULL, &offsets);

    if(uWriteFileResult == WRITE_OPERATION_FAILED)
    {
        bSuccess = false;
            
        DWORD uWriteFileLastError = ::GetLastError();
        QE_ASSERT_WARNING(uWriteFileResult != WRITE_OPERATION_FAILED, string_q("An unexpected error occurred when writing to the file \"") + filePath.GetAbsolutePath() + "\". The error code was: " + string_q::FromInteger(uWriteFileLastError) + ".");
    }

    return bSuccess;
}
EQFileSystemError QFileStream::Open(const QPath &filePath, const EQFileOpenMode &eOpenMode)
{
    QE_ASSERT_ERROR(m_bIsOpen != true, "The file stream is already open.");

    EQFileSystemError eErrorInfo = EQFileSystemError::E_Unknown;
    const bool FILE_EXISTS = SQFile::Exists(filePath, eErrorInfo);

    if((FILE_EXISTS && (eOpenMode == EQFileOpenMode::E_Append || eOpenMode == EQFileOpenMode::E_Open)) ||
       (!FILE_EXISTS && eOpenMode == EQFileOpenMode::E_Create)                                         ||
       eOpenMode == EQFileOpenMode::E_CreateOrOverwrite                                                || 
       eOpenMode == EQFileOpenMode::E_OpenOrCreate)
    {
        if(FILE_EXISTS)
        {
            QFileInfo fileInfo = SQFile::GetFileInfo(filePath, eErrorInfo);
            m_uFileSize = scast_q(fileInfo.GetSize(), pointer_uint_q);
            m_bWritingIsAllowed = !fileInfo.IsReadOnly();
            
            QE_ASSERT_ERROR(!(fileInfo.IsReadOnly() && eOpenMode == EQFileOpenMode::E_CreateOrOverwrite), string_q("The current user does not have permissions to overwrite the file \"") + filePath.GetAbsolutePath() + "\".");
        }
        else
        {
            m_uFileSize = 0;
            m_bWritingIsAllowed = true;
        }

        m_path = filePath;

        bool bOperationSuccessful = QFileStream::_OpenPlatformImplementation(m_path, eOpenMode, m_bWritingIsAllowed, m_nativeHandle, eErrorInfo);
        
        if(bOperationSuccessful)
        {
            m_bIsOpen = true;
            eErrorInfo = EQFileSystemError::E_Success;

            if(eOpenMode == EQFileOpenMode::E_Append)
                this->SetPosition(m_uFileSize);
            else
                this->SetPosition(0);

            QE_ASSERT_WARNING(m_uFileSize <= pointer_uint_q(-1), "The the file is too large, this class cannot access file offsets over 2^32 - 1 when it is compiled for x86 architecture.");

            if(m_uFileSize > pointer_uint_q(-1))
            {
                eErrorInfo = EQFileSystemError::E_FileIsTooLarge;
            }
        }
    }
    else if(FILE_EXISTS && eOpenMode == EQFileOpenMode::E_Create)
    {
        eErrorInfo = EQFileSystemError::E_AlreadyExists;
    }
    else
    {
        eErrorInfo = EQFileSystemError::E_DoesNotExist;
    }

    return eErrorInfo;
}