intial work on listing directories
This commit is contained in:
parent
0f59f4a745
commit
461215a6f5
|
@ -1,16 +1,13 @@
|
|||
#include "common/fs_file.h"
|
||||
#include <filesystem>
|
||||
|
||||
namespace Common::FS {
|
||||
|
||||
File::File() = default;
|
||||
|
||||
File::File(const std::string& path, OpenMode mode) {
|
||||
open(path, mode);
|
||||
}
|
||||
File::File(const std::string& path, OpenMode mode) { open(path, mode); }
|
||||
|
||||
File::~File() {
|
||||
close();
|
||||
}
|
||||
File::~File() { close(); }
|
||||
|
||||
bool File::open(const std::string& path, OpenMode mode) {
|
||||
close();
|
||||
|
@ -24,21 +21,17 @@ bool File::open(const std::string& path, OpenMode mode) {
|
|||
|
||||
bool File::close() {
|
||||
if (!isOpen() || std::fclose(m_file) != 0) [[unlikely]] {
|
||||
m_file = nullptr;
|
||||
return false;
|
||||
m_file = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
m_file = nullptr;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool File::write(std::span<const u08> data) {
|
||||
return isOpen() && std::fwrite(data.data(), 1, data.size(), m_file) == data.size();
|
||||
}
|
||||
bool File::write(std::span<const u08> data) { return isOpen() && std::fwrite(data.data(), 1, data.size(), m_file) == data.size(); }
|
||||
|
||||
bool File::read(void* data, u64 size) const {
|
||||
return isOpen() && std::fread(data, 1, size, m_file) == size;
|
||||
}
|
||||
bool File::read(void* data, u64 size) const { return isOpen() && std::fread(data, 1, size, m_file) == size; }
|
||||
|
||||
bool File::seek(s64 offset, SeekMode mode) {
|
||||
#ifdef _WIN64
|
||||
|
@ -56,38 +49,51 @@ bool File::seek(s64 offset, SeekMode mode) {
|
|||
u64 File::tell() const {
|
||||
if (isOpen()) [[likely]] {
|
||||
#ifdef _WIN64
|
||||
return _ftelli64(m_file);
|
||||
return _ftelli64(m_file);
|
||||
#else
|
||||
return ftello64(m_file);
|
||||
return ftello64(m_file);
|
||||
#endif
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::vector<DirEntry> File::getDirectoryEntries(const std::string& path) {
|
||||
std::vector < DirEntry> files;
|
||||
|
||||
for (const auto& entry : std::filesystem::directory_iterator(path)) {
|
||||
if (std::filesystem::is_regular_file(path)) {
|
||||
|
||||
}
|
||||
//entry.path()
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
u64 File::getFileSize() {
|
||||
#ifdef _WIN64
|
||||
const u64 pos = _ftelli64(m_file);
|
||||
if (_fseeki64(m_file, 0, SEEK_END) != 0) {
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const u64 size = _ftelli64(m_file);
|
||||
if (_fseeki64(m_file, pos, SEEK_SET) != 0) {
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
const u64 pos = ftello64(m_file);
|
||||
if (fseeko64(m_file, 0, SEEK_END) != 0) {
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const u64 size = ftello64(m_file);
|
||||
if (fseeko64(m_file, pos, SEEK_SET) != 0) {
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
return size;
|
||||
}
|
||||
|
||||
} // namespace Common::FS
|
||||
} // namespace Common::FS
|
||||
|
|
|
@ -2,19 +2,15 @@
|
|||
|
||||
#include <bit>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "common/types.h"
|
||||
|
||||
namespace Common::FS {
|
||||
|
||||
enum class OpenMode : u32 {
|
||||
Read = 0x1,
|
||||
Write = 0x2,
|
||||
ReadWrite = Read | Write
|
||||
};
|
||||
enum class OpenMode : u32 { Read = 0x1, Write = 0x2, ReadWrite = Read | Write };
|
||||
|
||||
enum class SeekMode : u32 {
|
||||
Set,
|
||||
|
@ -22,6 +18,11 @@ enum class SeekMode : u32 {
|
|||
End,
|
||||
};
|
||||
|
||||
struct DirEntry {
|
||||
std::string name;
|
||||
bool isFile;
|
||||
};
|
||||
|
||||
class File {
|
||||
public:
|
||||
File();
|
||||
|
@ -46,42 +47,31 @@ class File {
|
|||
return read(data.data(), data.size() * sizeof(T));
|
||||
}
|
||||
|
||||
bool isOpen() const {
|
||||
return m_file != nullptr;
|
||||
}
|
||||
bool isOpen() const { return m_file != nullptr; }
|
||||
|
||||
const char* getOpenMode(OpenMode mode) const {
|
||||
switch (mode) {
|
||||
case OpenMode::Read:
|
||||
return "rb";
|
||||
case OpenMode::Write:
|
||||
return "wb";
|
||||
case OpenMode::ReadWrite:
|
||||
return "r+b";
|
||||
default:
|
||||
return "r";
|
||||
case OpenMode::Read: return "rb";
|
||||
case OpenMode::Write: return "wb";
|
||||
case OpenMode::ReadWrite: return "r+b";
|
||||
default: return "r";
|
||||
}
|
||||
}
|
||||
|
||||
int getSeekMode(SeekMode mode) const {
|
||||
switch (mode) {
|
||||
case SeekMode::Set:
|
||||
return SEEK_SET;
|
||||
case SeekMode::Cur:
|
||||
return SEEK_CUR;
|
||||
case SeekMode::End:
|
||||
return SEEK_END;
|
||||
default:
|
||||
return SEEK_SET;
|
||||
case SeekMode::Set: return SEEK_SET;
|
||||
case SeekMode::Cur: return SEEK_CUR;
|
||||
case SeekMode::End: return SEEK_END;
|
||||
default: return SEEK_SET;
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] std::FILE* fileDescr() const {
|
||||
return m_file;
|
||||
}
|
||||
[[nodiscard]] std::FILE* fileDescr() const { return m_file; }
|
||||
static std::vector<DirEntry> getDirectoryEntries(const std::string& path);
|
||||
|
||||
private:
|
||||
std::FILE* m_file{};
|
||||
};
|
||||
|
||||
} // namespace Common::FS
|
||||
} // namespace Common::FS
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "common/fs_file.h"
|
||||
|
||||
namespace Core::FileSys {
|
||||
|
||||
class MntPoints {
|
||||
|
@ -29,6 +31,9 @@ struct File {
|
|||
std::atomic_bool isDirectory;
|
||||
std::string m_host_name;
|
||||
std::string m_guest_name;
|
||||
Common::FS::File f;
|
||||
|
||||
|
||||
};
|
||||
class HandleTable {
|
||||
public:
|
||||
|
|
Loading…
Reference in New Issue