intial work on listing directories

This commit is contained in:
georgemoralis 2023-11-07 07:59:14 +02:00
parent 0f59f4a745
commit 461215a6f5
3 changed files with 51 additions and 50 deletions

View File

@ -1,16 +1,13 @@
#include "common/fs_file.h" #include "common/fs_file.h"
#include <filesystem>
namespace Common::FS { namespace Common::FS {
File::File() = default; File::File() = default;
File::File(const std::string& path, OpenMode mode) { File::File(const std::string& path, OpenMode mode) { open(path, mode); }
open(path, mode);
}
File::~File() { File::~File() { close(); }
close();
}
bool File::open(const std::string& path, OpenMode mode) { bool File::open(const std::string& path, OpenMode mode) {
close(); close();
@ -24,21 +21,17 @@ bool File::open(const std::string& path, OpenMode mode) {
bool File::close() { bool File::close() {
if (!isOpen() || std::fclose(m_file) != 0) [[unlikely]] { if (!isOpen() || std::fclose(m_file) != 0) [[unlikely]] {
m_file = nullptr; m_file = nullptr;
return false; return false;
} }
m_file = nullptr; m_file = nullptr;
return true; return true;
} }
bool File::write(std::span<const u08> data) { bool File::write(std::span<const u08> data) { return isOpen() && std::fwrite(data.data(), 1, data.size(), m_file) == data.size(); }
return isOpen() && std::fwrite(data.data(), 1, data.size(), m_file) == data.size();
}
bool File::read(void* data, u64 size) const { bool File::read(void* data, u64 size) const { return isOpen() && std::fread(data, 1, size, m_file) == size; }
return isOpen() && std::fread(data, 1, size, m_file) == size;
}
bool File::seek(s64 offset, SeekMode mode) { bool File::seek(s64 offset, SeekMode mode) {
#ifdef _WIN64 #ifdef _WIN64
@ -56,38 +49,51 @@ bool File::seek(s64 offset, SeekMode mode) {
u64 File::tell() const { u64 File::tell() const {
if (isOpen()) [[likely]] { if (isOpen()) [[likely]] {
#ifdef _WIN64 #ifdef _WIN64
return _ftelli64(m_file); return _ftelli64(m_file);
#else #else
return ftello64(m_file); return ftello64(m_file);
#endif #endif
} }
return -1; 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() { u64 File::getFileSize() {
#ifdef _WIN64 #ifdef _WIN64
const u64 pos = _ftelli64(m_file); const u64 pos = _ftelli64(m_file);
if (_fseeki64(m_file, 0, SEEK_END) != 0) { if (_fseeki64(m_file, 0, SEEK_END) != 0) {
return 0; return 0;
} }
const u64 size = _ftelli64(m_file); const u64 size = _ftelli64(m_file);
if (_fseeki64(m_file, pos, SEEK_SET) != 0) { if (_fseeki64(m_file, pos, SEEK_SET) != 0) {
return 0; return 0;
} }
#else #else
const u64 pos = ftello64(m_file); const u64 pos = ftello64(m_file);
if (fseeko64(m_file, 0, SEEK_END) != 0) { if (fseeko64(m_file, 0, SEEK_END) != 0) {
return 0; return 0;
} }
const u64 size = ftello64(m_file); const u64 size = ftello64(m_file);
if (fseeko64(m_file, pos, SEEK_SET) != 0) { if (fseeko64(m_file, pos, SEEK_SET) != 0) {
return 0; return 0;
} }
#endif #endif
return size; return size;
} }
} // namespace Common::FS } // namespace Common::FS

View File

@ -2,19 +2,15 @@
#include <bit> #include <bit>
#include <cstdio> #include <cstdio>
#include <string>
#include <span> #include <span>
#include <string>
#include <vector> #include <vector>
#include "common/types.h" #include "common/types.h"
namespace Common::FS { namespace Common::FS {
enum class OpenMode : u32 { enum class OpenMode : u32 { Read = 0x1, Write = 0x2, ReadWrite = Read | Write };
Read = 0x1,
Write = 0x2,
ReadWrite = Read | Write
};
enum class SeekMode : u32 { enum class SeekMode : u32 {
Set, Set,
@ -22,6 +18,11 @@ enum class SeekMode : u32 {
End, End,
}; };
struct DirEntry {
std::string name;
bool isFile;
};
class File { class File {
public: public:
File(); File();
@ -46,42 +47,31 @@ class File {
return read(data.data(), data.size() * sizeof(T)); return read(data.data(), data.size() * sizeof(T));
} }
bool isOpen() const { bool isOpen() const { return m_file != nullptr; }
return m_file != nullptr;
}
const char* getOpenMode(OpenMode mode) const { const char* getOpenMode(OpenMode mode) const {
switch (mode) { switch (mode) {
case OpenMode::Read: case OpenMode::Read: return "rb";
return "rb"; case OpenMode::Write: return "wb";
case OpenMode::Write: case OpenMode::ReadWrite: return "r+b";
return "wb"; default: return "r";
case OpenMode::ReadWrite:
return "r+b";
default:
return "r";
} }
} }
int getSeekMode(SeekMode mode) const { int getSeekMode(SeekMode mode) const {
switch (mode) { switch (mode) {
case SeekMode::Set: case SeekMode::Set: return SEEK_SET;
return SEEK_SET; case SeekMode::Cur: return SEEK_CUR;
case SeekMode::Cur: case SeekMode::End: return SEEK_END;
return SEEK_CUR; default: return SEEK_SET;
case SeekMode::End:
return SEEK_END;
default:
return SEEK_SET;
} }
} }
[[nodiscard]] std::FILE* fileDescr() const { [[nodiscard]] std::FILE* fileDescr() const { return m_file; }
return m_file; static std::vector<DirEntry> getDirectoryEntries(const std::string& path);
}
private: private:
std::FILE* m_file{}; std::FILE* m_file{};
}; };
} // namespace Common::FS } // namespace Common::FS

View File

@ -3,6 +3,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "common/fs_file.h"
namespace Core::FileSys { namespace Core::FileSys {
class MntPoints { class MntPoints {
@ -29,6 +31,9 @@ struct File {
std::atomic_bool isDirectory; std::atomic_bool isDirectory;
std::string m_host_name; std::string m_host_name;
std::string m_guest_name; std::string m_guest_name;
Common::FS::File f;
}; };
class HandleTable { class HandleTable {
public: public: