157 lines
3.7 KiB
C++
157 lines
3.7 KiB
C++
|
/***
|
||
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
||
|
|
||
|
File: FS.Generic.cpp
|
||
|
Date: 2021-6-12
|
||
|
Author: Reece
|
||
|
***/
|
||
|
#include <RuntimeInternal.hpp>
|
||
|
#include "FS.hpp"
|
||
|
#include "FS.Generic.hpp"
|
||
|
#include <Time/Time.hpp>
|
||
|
|
||
|
#if defined(_AURUNTIME_GENERICFS)
|
||
|
#include <stdio.h>
|
||
|
// NOPE -> <sys/stat.h> -> stat
|
||
|
// NOPE -> <unistd.h> -> access
|
||
|
// if your platform isn't supported, let's not assume we have these dino headers
|
||
|
// HAS_STD_FS might exist, who knows, let's hope
|
||
|
|
||
|
namespace Aurora::IO::FS
|
||
|
{
|
||
|
AUKN_SYM bool FilesInDirectory(const AuString &string, AuList<AuString> &files)
|
||
|
{
|
||
|
return IterateDirEntriesSTL(string, true, files);
|
||
|
}
|
||
|
|
||
|
AUKN_SYM bool DirsInDirectory(const AuString &string, AuList<AuString> &dirs)
|
||
|
{
|
||
|
return IterateDirEntriesSTL(string, false, dirs);
|
||
|
}
|
||
|
|
||
|
AUKN_SYM bool WriteFile(const AuString &path, const void *data, size_t length)
|
||
|
{
|
||
|
auto file = fopen(NormalizePathRet(path).c_str(), "wb");
|
||
|
if (!file) return false;
|
||
|
auto bytesWritten = fwrite(data, 1, length, file);
|
||
|
fclose(file);
|
||
|
return bytesWritten == length;
|
||
|
}
|
||
|
|
||
|
AUKN_SYM bool WriteString(const AuString &path, const AuString &str)
|
||
|
{
|
||
|
return WriteFile(path, str.data(), str.size());
|
||
|
}
|
||
|
|
||
|
AUKN_SYM bool ReadFile(const AuString &path, AuList<uint8_t> &buffer)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
AUKN_SYM bool ReadString(const AuString &path, AuString &buffer)
|
||
|
{
|
||
|
AuList<uint8_t> buf;
|
||
|
|
||
|
if (!ReadFile(path, buf))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
buffer = AuString(buf.begin(), buf.end());
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
AUKN_SYM bool FileExists(const AuString &path)
|
||
|
{
|
||
|
auto file = fopen(NormalizePathRet(path).c_str(), "rb");
|
||
|
if (!file) return false;
|
||
|
fclose(file);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
AUKN_SYM bool DirExists(const AuString &path)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
AUKN_SYM bool DirMk(const AuString &path)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
AUKN_SYM bool Remove(const AuString &path)
|
||
|
{
|
||
|
#if defined(HAS_STD_FS)
|
||
|
try
|
||
|
{
|
||
|
std::filesystem::remove(std::filesystem::u8path(NormalizePathRet(path)));
|
||
|
return true;
|
||
|
}
|
||
|
catch (...)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
#else
|
||
|
// Do we have 'remove'?
|
||
|
return false;
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
AUKN_SYM bool Relink(const AuString &src, const AuString &dest)
|
||
|
{
|
||
|
#if defined(HAS_STD_FS)
|
||
|
try
|
||
|
{
|
||
|
std::filesystem::rename(std::filesystem::u8path(NormalizePathRet(src)), std::filesystem::u8path(NormalizePathRet(dest)));
|
||
|
return true;
|
||
|
}
|
||
|
catch (...)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
#else
|
||
|
return false;
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
AUKN_SYM bool Copy(const AuString &src, const AuString &dest)
|
||
|
{
|
||
|
#if defined(HAS_STD_FS)
|
||
|
try
|
||
|
{
|
||
|
std::filesystem::copy(std::filesystem::u8path(NormalizePathRet(src)), std::filesystem::u8path(NormalizePathRet(dest)));
|
||
|
return true;
|
||
|
}
|
||
|
catch (...)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
#else
|
||
|
// TODO: stream using stdio functions
|
||
|
return false;
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
AUKN_SYM bool StatFile(const AuString &path, Stat &stat)
|
||
|
{
|
||
|
stat = {};
|
||
|
stat.existsDirectory = DirExists(path);
|
||
|
stat.existsFile = FileExists(path);
|
||
|
stat.exists = stat.existsDirectory || stat.existsFile;
|
||
|
|
||
|
if (stat.exists)
|
||
|
{
|
||
|
auto time = Time::CurrentClockMS();
|
||
|
stat.created = time;
|
||
|
stat.modified = time;
|
||
|
stat.accessed = time;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
stat = {};
|
||
|
}
|
||
|
|
||
|
return stat.exists;
|
||
|
}
|
||
|
}
|
||
|
#endif
|