fix(linux): more shit

This commit is contained in:
Mohamed Ashraf authored and Anaël committed 2026-11-28 01:58:00 +01:00
1 parent 20278c2856
commit a0a35f3c0b
18 files changed
+267 -80

No files matched your search

+50 -7
View File
@@ -1,6 +1,16 @@
#include "stdafx.h"
#include "File.h"
#include "FileOutputStream.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <cwchar>
#include <fcntl.h>
#include <sys/types.h>
#include <cstring>
#if defined(__linux__)
#include <unistd.h>
#endif
//Creates a file output stream to write to the file represented by the specified File object. A new FileDescriptor object is
//created to represent this file connection.
@@ -29,6 +39,13 @@ FileOutputStream::FileOutputStream(const File &file) : m_fileHandle( INVALID_HAN
FILE_ATTRIBUTE_NORMAL , // file attributes
nullptr // Unsupported
);
#elif defined(__linux__)
std::wstring path = file.getPath();
char* convertedPath = new char[path.size() + 1];
std::wcstombs(convertedPath, path.c_str(), path.size() + 1);
m_fileHandle = open(convertedPath, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
delete[] convertedPath;
#else
m_fileHandle = CreateFile(
wstringtofilename(file.getPath()) , // file name
@@ -43,26 +60,33 @@ FileOutputStream::FileOutputStream(const File &file) : m_fileHandle( INVALID_HAN
if( m_fileHandle == INVALID_HANDLE_VALUE )
{
DWORD error = GetLastError();
// TODO 4J Stu - Any form of error/exception handling
#ifndef __linux__
DWORD error = GetLastError();
#endif // __linux__
}
}
FileOutputStream::~FileOutputStream()
{
#ifdef _WIN32
if( m_fileHandle != INVALID_HANDLE_VALUE )
CloseHandle( m_fileHandle );
#else
if( m_fileHandle != INVALID_HANDLE_VALUE )
::close( m_fileHandle );
#endif
}
//Writes the specified byte to this file output stream. Implements the write method of OutputStream.
//Parameters:
//b - the byte to be written.
void FileOutputStream::write(unsigned int b)
{
DWORD numberOfBytesWritten;
{
byte value = static_cast<byte>(b);
#if defined(_WIN32)
DWORD numberOfBytesWritten;
BOOL result = WriteFile(
m_fileHandle, // handle to file
&value, // data buffer
@@ -70,6 +94,11 @@ void FileOutputStream::write(unsigned int b)
&numberOfBytesWritten, // number of bytes written
nullptr // overlapped buffer
);
#else // LINUX
int fileDescriptor = reinterpret_cast<int>(m_fileHandle);
ssize_t numberOfBytesWritten = ::write(fileDescriptor, NULL, 1);
int result = static_cast<int>(numberOfBytesWritten);
#endif // _WIN32
if( result == 0 )
{
@@ -86,8 +115,8 @@ void FileOutputStream::write(unsigned int b)
//b - the data.
void FileOutputStream::write(byteArray b)
{
DWORD numberOfBytesWritten;
#if defined(_WIN32)
DWORD numberOfBytesWritten;
BOOL result = WriteFile(
m_fileHandle, // handle to file
&b.data, // data buffer
@@ -95,6 +124,11 @@ void FileOutputStream::write(byteArray b)
&numberOfBytesWritten, // number of bytes written
nullptr // overlapped buffer
);
#else // Linux
int fileDescriptor = reinterpret_cast<int>(m_fileHandle);
ssize_t numberOfBytesWritten = ::write(fileDescriptor, NULL, 1);
int result = static_cast<int>(numberOfBytesWritten);
#endif // _WIN32
if( result == 0 )
{
@@ -116,6 +150,7 @@ void FileOutputStream::write(byteArray b, unsigned int offset, unsigned int leng
// 4J Stu - We don't want to write any more than the array buffer holds
assert( length <= ( b.length - offset ) );
#if defined(_WIN32)
DWORD numberOfBytesWritten;
BOOL result = WriteFile(
@@ -125,6 +160,11 @@ void FileOutputStream::write(byteArray b, unsigned int offset, unsigned int leng
&numberOfBytesWritten, // number of bytes written
nullptr // overlapped buffer
);
#else
int fileDescriptor = reinterpret_cast<int>(m_fileHandle);
ssize_t numberOfBytesWritten = ::write(fileDescriptor, static_cast<const void*>(&b[offset]), length);
int result = static_cast<int>(numberOfBytesWritten);
#endif // _WIN32
if( result == 0 )
{
@@ -141,8 +181,11 @@ void FileOutputStream::write(byteArray b, unsigned int offset, unsigned int leng
//If this stream has an associated channel then the channel is closed as well.
void FileOutputStream::close()
{
#ifdef _WIN32
BOOL result = CloseHandle( m_fileHandle );
#else // __linux__
int result = ::close( m_fileHandle );
#endif // _WIN32
if( result == 0 )
{
// TODO 4J Stu - Some kind of error handling