mirror of
https://github.com/RPCSX/rpcsx.git
synced 2025-12-06 07:12:14 +01:00
rx: add file lock utility
This commit is contained in:
parent
2b9232e4aa
commit
b33e2662b6
|
|
@ -9,6 +9,7 @@ add_library(${PROJECT_NAME} OBJECT
|
||||||
src/hexdump.cpp
|
src/hexdump.cpp
|
||||||
src/mem.cpp
|
src/mem.cpp
|
||||||
src/Version.cpp
|
src/Version.cpp
|
||||||
|
src/FileLock.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(${PROJECT_NAME}
|
target_include_directories(${PROJECT_NAME}
|
||||||
|
|
|
||||||
37
rx/include/rx/FileLock.hpp
Normal file
37
rx/include/rx/FileLock.hpp
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace rx {
|
||||||
|
void fileLock(std::FILE *file);
|
||||||
|
void fileUnlock(std::FILE *file);
|
||||||
|
|
||||||
|
class ScopedFileLock {
|
||||||
|
std::FILE *mFile = nullptr;
|
||||||
|
|
||||||
|
public:
|
||||||
|
ScopedFileLock() = default;
|
||||||
|
ScopedFileLock(const ScopedFileLock &) = delete;
|
||||||
|
ScopedFileLock &operator=(const ScopedFileLock &) = delete;
|
||||||
|
|
||||||
|
ScopedFileLock(ScopedFileLock &&other) noexcept
|
||||||
|
: mFile(std::exchange(other.mFile, nullptr)) {}
|
||||||
|
ScopedFileLock &operator=(ScopedFileLock &&other) noexcept {
|
||||||
|
std::swap(mFile, other.mFile);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
ScopedFileLock(std::FILE *file) : mFile(file) {
|
||||||
|
if (file) {
|
||||||
|
fileLock(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~ScopedFileLock() {
|
||||||
|
if (mFile) {
|
||||||
|
fileUnlock(mFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace rx
|
||||||
|
|
@ -4,5 +4,8 @@
|
||||||
#include <span>
|
#include <span>
|
||||||
|
|
||||||
namespace rx {
|
namespace rx {
|
||||||
void hexdump(std::span<std::byte> bytes);
|
void hexdump(std::span<const std::byte> bytes);
|
||||||
|
inline void hexdump(const void *data, std::size_t size) {
|
||||||
|
hexdump({reinterpret_cast<const std::byte *>(data), size});
|
||||||
|
}
|
||||||
} // namespace rx
|
} // namespace rx
|
||||||
|
|
|
||||||
18
rx/src/FileLock.cpp
Normal file
18
rx/src/FileLock.cpp
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
#include "FileLock.hpp"
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
void rx::fileLock(std::FILE *file) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
_lock_file(file);
|
||||||
|
#else
|
||||||
|
flockfile(file);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void rx::fileUnlock(std::FILE *file) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
_unlock_file(file);
|
||||||
|
#else
|
||||||
|
funlockfile(file);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
#include "hexdump.hpp"
|
#include "hexdump.hpp"
|
||||||
|
#include "FileLock.hpp"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <print>
|
#include <print>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
void rx::hexdump(std::span<std::byte> bytes) {
|
void rx::hexdump(std::span<const std::byte> bytes) {
|
||||||
unsigned sizeWidth = 1;
|
unsigned sizeWidth = 1;
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
@ -16,7 +17,7 @@ void rx::hexdump(std::span<std::byte> bytes) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
flockfile(stderr);
|
rx::ScopedFileLock stderrLock(stderr);
|
||||||
std::print(stderr, "{} ", std::string(sizeWidth, ' '));
|
std::print(stderr, "{} ", std::string(sizeWidth, ' '));
|
||||||
for (unsigned i = 0; i < 16; ++i) {
|
for (unsigned i = 0; i < 16; ++i) {
|
||||||
std::print(stderr, " {:02x}", i);
|
std::print(stderr, " {:02x}", i);
|
||||||
|
|
@ -82,5 +83,4 @@ void rx::hexdump(std::span<std::byte> bytes) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::println(stderr, "");
|
std::println(stderr, "");
|
||||||
funlockfile(stderr);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue