mirror of
https://github.com/WinampDesktop/winamp.git
synced 2025-04-30 15:15:41 -04:00
Common/FileSystem: Add {Read,Write}BinaryFile helpers
This commit is contained in:
parent
0890164987
commit
bd164d2735
@ -401,6 +401,37 @@ std::FILE* OpenCFile(const char* filename, const char* mode)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<std::vector<u8>> ReadBinaryFile(const char* filename)
|
||||||
|
{
|
||||||
|
ManagedCFilePtr fp = OpenManagedCFile(filename, "rb");
|
||||||
|
if (!fp)
|
||||||
|
return std::nullopt;
|
||||||
|
|
||||||
|
std::fseek(fp.get(), 0, SEEK_END);
|
||||||
|
long size = std::ftell(fp.get());
|
||||||
|
std::fseek(fp.get(), 0, SEEK_SET);
|
||||||
|
if (size < 0)
|
||||||
|
return std::nullopt;
|
||||||
|
|
||||||
|
std::vector<u8> res(static_cast<size_t>(size));
|
||||||
|
if (size > 0 && std::fread(res.data(), 1u, static_cast<size_t>(size), fp.get()) != static_cast<size_t>(size))
|
||||||
|
return std::nullopt;
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WriteBinaryFile(const char* filename, const void* data, size_t data_length)
|
||||||
|
{
|
||||||
|
ManagedCFilePtr fp = OpenManagedCFile(filename, "wb");
|
||||||
|
if (!fp)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (data_length > 0 && std::fwrite(data, 1u, data_length, fp.get()) != data_length)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void BuildOSPath(char* Destination, u32 cbDestination, const char* Path)
|
void BuildOSPath(char* Destination, u32 cbDestination, const char* Path)
|
||||||
{
|
{
|
||||||
u32 i;
|
u32 i;
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -168,6 +169,9 @@ using ManagedCFilePtr = std::unique_ptr<std::FILE, void (*)(std::FILE*)>;
|
|||||||
ManagedCFilePtr OpenManagedCFile(const char* filename, const char* mode);
|
ManagedCFilePtr OpenManagedCFile(const char* filename, const char* mode);
|
||||||
std::FILE* OpenCFile(const char* filename, const char* mode);
|
std::FILE* OpenCFile(const char* filename, const char* mode);
|
||||||
|
|
||||||
|
std::optional<std::vector<u8>> ReadBinaryFile(const char* filename);
|
||||||
|
bool WriteBinaryFile(const char* filename, const void* data, size_t data_length);
|
||||||
|
|
||||||
// creates a directory in the local filesystem
|
// creates a directory in the local filesystem
|
||||||
// if the directory already exists, the return value will be true.
|
// if the directory already exists, the return value will be true.
|
||||||
// if Recursive is specified, all parent directories will be created
|
// if Recursive is specified, all parent directories will be created
|
||||||
|
Loading…
x
Reference in New Issue
Block a user