Browse Source

Use SDL for creating file during RW test

pull/5114/merge
Anders Jenbo 4 years ago
parent
commit
6f44b48f4e
  1. 8
      Source/restrict.cpp
  2. 23
      Source/utils/file_util.cpp
  3. 1
      Source/utils/file_util.h

8
Source/restrict.cpp

@ -8,17 +8,19 @@
#include "utils/file_util.h"
#include "utils/paths.h"
#include <SDL.h>
namespace devilution {
void ReadOnlyTest()
{
const std::string path = paths::PrefPath() + "Diablo1ReadOnlyTest.foo";
FILE *f = FOpen(path.c_str(), "w");
if (f == nullptr) {
SDL_RWops *file = SDL_RWFromFile(path.c_str(), "w");
if (file == nullptr) {
DirErrorDlg(paths::PrefPath());
}
fclose(f);
SDL_RWclose(file);
RemoveFile(path.c_str());
}

23
Source/utils/file_util.cpp

@ -76,7 +76,7 @@ bool FileExists(const char *path)
return ::access(path, F_OK) == 0;
#else
SDL_RWops *file = SDL_RWFromFile(path, "r+b");
if (file == NULL)
if (file == nullptr)
return false;
SDL_RWclose(file);
return true;
@ -115,7 +115,7 @@ bool FileExistsAndIsWriteable(const char *path)
if (!FileExists(path))
return false;
SDL_RWops *file = SDL_RWFromFile(path, "a+b");
if (file == NULL)
if (file == nullptr)
return false;
SDL_RWclose(file);
return true;
@ -225,23 +225,4 @@ std::optional<std::fstream> CreateFileStream(const char *path, std::ios::openmod
#endif
}
FILE *FOpen(const char *path, const char *mode)
{
#if (defined(_WIN64) || defined(_WIN32)) && !defined(NXDK)
const auto pathUtf16 = ToWideChar(path);
if (pathUtf16 == nullptr) {
LogError("UTF-8 -> UTF-16 conversion error code {}", ::GetLastError());
return nullptr;
}
const auto modeUtf16 = ToWideChar(mode);
if (modeUtf16 == nullptr) {
LogError("UTF-8 -> UTF-16 conversion error code {}", ::GetLastError());
return nullptr;
}
return ::_wfopen(&pathUtf16[0], &modeUtf16[0]);
#else
return std::fopen(path, mode);
#endif
}
} // namespace devilution

1
Source/utils/file_util.h

@ -17,7 +17,6 @@ bool GetFileSize(const char *path, std::uintmax_t *size);
bool ResizeFile(const char *path, std::uintmax_t size);
void RemoveFile(const char *path);
std::optional<std::fstream> CreateFileStream(const char *path, std::ios::openmode mode);
FILE *FOpen(const char *path, const char *mode);
#if (defined(_WIN64) || defined(_WIN32)) && !defined(NXDK)
std::unique_ptr<wchar_t[]> ToWideChar(string_view path);

Loading…
Cancel
Save