Skip to content

Commit

Permalink
FileSystem: Use File library for file read and write
Browse files Browse the repository at this point in the history
  • Loading branch information
Pagghiu committed Apr 21, 2024
1 parent d070d63 commit 20dddb7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 64 deletions.
55 changes: 17 additions & 38 deletions Libraries/FileSystem/FileSystem.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Stefano Cristiano
// SPDX-License-Identifier: MIT
#include "FileSystem.h"
#include "../File/FileDescriptor.h"
#include "../Strings/StringConverter.h"

#if SC_PLATFORM_WINDOWS
Expand Down Expand Up @@ -92,61 +93,39 @@ bool SC::FileSystem::convert(const StringView file, String& destination, StringV
} \
}
#endif

SC::Result SC::FileSystem::write(StringView path, Span<const char> data)
{
StringView encodedPath;
SC_TRY(convert(path, fileFormatBuffer1, &encodedPath));
FILE* handle = nullptr;
SC_TRY_FORMAT_ERRNO(path, Internal::openFileWrite(encodedPath.getNullTerminatedNative(), handle));
const size_t res = fwrite(data.data(), 1, data.sizeInBytes(), handle);
if (ferror(handle) != 0)
{
return formatError(errno, path, false);
}
fclose(handle);
return Result(res == data.sizeInBytes());
FileDescriptor file;
SC_TRY(file.open(encodedPath, FileDescriptor::WriteCreateTruncate));
return file.write(data);
}

#define SC_TRY_FORMAT_LIBC(func) \
{ \
if (func == -1) \
{ \
return formatError(errno, path, false); \
} \
}
SC::Result SC::FileSystem::read(StringView path, Vector<char>& data)
{
StringView encodedPath;
SC_TRY(convert(path, fileFormatBuffer1, &encodedPath));
FILE* handle;
SC_TRY_FORMAT_ERRNO(path, Internal::openFileRead(encodedPath.getNullTerminatedNative(), handle));
SC_TRY_FORMAT_LIBC(fseek(handle, 0, SEEK_END));
const auto fileSizeRes = ftell(handle);
SC_TRY_FORMAT_LIBC(fileSizeRes);
SC_TRY_FORMAT_LIBC(fseek(handle, 0, SEEK_SET));
const size_t fileSize = static_cast<size_t>(fileSizeRes);
SC_TRY(data.resizeWithoutInitializing(fileSize));

const auto readBytes = fread(data.data(), 1, fileSize, handle);
if (ferror(handle) != 0)
{
return formatError(errno, path, false);
}
fclose(handle);
return Result(readBytes == fileSize);
FileDescriptor file;
SC_TRY(file.open(encodedPath, FileDescriptor::ReadOnly));
return file.readUntilEOF(data);
}
#undef SC_TRY_FORMAT_LIBC

[[nodiscard]] SC::Result SC::FileSystem::writeString(StringView file, StringView text)
[[nodiscard]] SC::Result SC::FileSystem::writeString(StringView path, StringView text)
{
return write(file, text.toCharSpan());
return write(path, text.toCharSpan());
}

[[nodiscard]] SC::Result SC::FileSystem::read(StringView file, String& text, StringEncoding encoding)
[[nodiscard]] SC::Result SC::FileSystem::read(StringView path, String& text, StringEncoding encoding)
{
text.data.clearWithoutInitializing();
text.encoding = encoding;
SC_TRY(read(file, text.data));
return Result(StringConverter::pushNullTerm(text.data, encoding));
StringView encodedPath;
SC_TRY(convert(path, fileFormatBuffer1, &encodedPath));
FileDescriptor file;
SC_TRY(file.open(encodedPath, FileDescriptor::ReadOnly));
return file.readUntilEOF(text);
}

SC::Result SC::FileSystem::formatError(int errorNumber, StringView item, bool isWindowsNativeError)
Expand Down
4 changes: 0 additions & 4 deletions Libraries/FileSystem/Internal/FileSystemEmscripten.inl
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ struct SC::FileSystem::Internal

[[nodiscard]] static bool removeFile(const char* path) { return false; }

[[nodiscard]] static bool openFileRead(const char* path, FILE*& file) { return false; }

[[nodiscard]] static bool openFileWrite(const char* path, FILE*& file) { return false; }

[[nodiscard]] static bool formatError(int errorNumber, String& buffer) { return false; }

[[nodiscard]] static bool copyFile(const StringView& sourceFile, const StringView& destinationFile,
Expand Down
12 changes: 0 additions & 12 deletions Libraries/FileSystem/Internal/FileSystemPosix.inl
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,6 @@ struct SC::FileSystem::Internal

[[nodiscard]] static bool removeFile(const char* path) { return remove(path) == 0; }

[[nodiscard]] static bool openFileRead(const char* path, FILE*& file)
{
file = fopen(path, "rb");
return file != nullptr;
}

[[nodiscard]] static bool openFileWrite(const char* path, FILE*& file)
{
file = fopen(path, "wb");
return file != nullptr;
}

[[nodiscard]] static bool formatError(int errorNumber, String& buffer)
{
buffer.encoding = StringEncoding::Utf8;
Expand Down
10 changes: 0 additions & 10 deletions Libraries/FileSystem/Internal/FileSystemWindows.inl
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,6 @@ struct SC::FileSystem::Internal
return true;
}

[[nodiscard]] static bool openFileRead(const wchar_t* path, FILE*& file)
{
return _wfopen_s(&file, path, L"rb") == 0;
}

[[nodiscard]] static bool openFileWrite(const wchar_t* path, FILE*& file)
{
return _wfopen_s(&file, path, L"wb") == 0;
}

[[nodiscard]] static bool formatError(int errorNumber, String& buffer)
{
buffer.encoding = StringEncoding::Utf16;
Expand Down

0 comments on commit 20dddb7

Please sign in to comment.