From 14006f54dc161db79b411ad3b3cfb1d0b55fc92e Mon Sep 17 00:00:00 2001 From: Celvin Cambloid Date: Sat, 16 Mar 2024 14:37:22 +0100 Subject: [PATCH 1/2] Use Qt for directory functions --- Makefile | 10 ++++++++-- libs/os/dir.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ libs/os/dir.h | 40 ++++++++++++++++++++++++++++------------ 3 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 libs/os/dir.cpp diff --git a/Makefile b/Makefile index 3bd218b8e..4ca26633d 100644 --- a/Makefile +++ b/Makefile @@ -921,8 +921,13 @@ $(INSTALLDIR)/radiant.$(EXE): \ libprofile.$(A) \ libquickhull.$(A) \ libxmllib.$(A) \ + libos.$(A) \ $(if $(findstring Win32,$(OS)),icons/radiant.o,) \ +libos.$(A): CPPFLAGS_EXTRA := $(CPPFLAGS_QTCORE) -Ilibs -Iinclude +libos.$(A): \ + libs/os/dir.o \ + libfilematch.$(A): CPPFLAGS_EXTRA := -Ilibs libfilematch.$(A): \ libs/filematch.o \ @@ -1072,13 +1077,14 @@ $(INSTALLDIR)/modules/shaders.$(DLL): \ plugins/shaders/shaders.o \ libcommandlib.$(A) \ -$(INSTALLDIR)/modules/vfspk3.$(DLL): LIBS_EXTRA := $(LIBS_GLIB) -$(INSTALLDIR)/modules/vfspk3.$(DLL): CPPFLAGS_EXTRA := $(CPPFLAGS_GLIB) -Ilibs -Iinclude +$(INSTALLDIR)/modules/vfspk3.$(DLL): LIBS_EXTRA := $(LIBS_GLIB) $(LIBS_QTCORE) +$(INSTALLDIR)/modules/vfspk3.$(DLL): CPPFLAGS_EXTRA := $(CPPFLAGS_GLIB) $(CPPFLAGS_QTCORE) -Ilibs -Iinclude $(INSTALLDIR)/modules/vfspk3.$(DLL): \ plugins/vfspk3/archive.o \ plugins/vfspk3/vfs.o \ plugins/vfspk3/vfspk3.o \ libfilematch.$(A) \ + libos.$(A) \ $(INSTALLDIR)/plugins/bobtoolz.$(DLL): LIBS_EXTRA := $(LIBS_GLIB) $(LIBS_QTCORE) $(LIBS_QTGUI) $(LIBS_QTWIDGETS) $(INSTALLDIR)/plugins/bobtoolz.$(DLL): CPPFLAGS_EXTRA := $(CPPFLAGS_GLIB) $(CPPFLAGS_QTCORE) $(CPPFLAGS_QTGUI) $(CPPFLAGS_QTWIDGETS) -Ilibs -Iinclude diff --git a/libs/os/dir.cpp b/libs/os/dir.cpp new file mode 100644 index 000000000..5086ac61b --- /dev/null +++ b/libs/os/dir.cpp @@ -0,0 +1,40 @@ +#include "dir.h" + +#include +#include +#include +#include + +Directory::Directory(const char* name) { + this->dir = new QDir(name); +} + +bool Directory::good() { + if(this->dir) { + QDir* dir_instance = reinterpret_cast(this->dir); + return dir_instance->exists(); + } + return false; +} + +void Directory::close() { + if(this->dir) { + QDir* dir_instance = reinterpret_cast(this->dir); + delete dir_instance; + this->dir = nullptr; + } +} + +const char* Directory::read_and_increment(){ + if(this->dir) { + QDir* dir_instance = reinterpret_cast(this->dir); + QStringList entryList = dir_instance->entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); + if(this->entry_idx < entryList.size()) { + QString file_path = entryList.at(this->entry_idx); + QString file_name = QFileInfo(file_path).fileName(); + this->entry_idx++; + return file_name.toStdString().c_str(); + } + } + return nullptr; +} \ No newline at end of file diff --git a/libs/os/dir.h b/libs/os/dir.h index 3d5c774f1..79eb97090 100644 --- a/libs/os/dir.h +++ b/libs/os/dir.h @@ -24,24 +24,40 @@ /// \file /// \brief OS directory-listing object. -#include - -typedef GDir Directory; - -inline bool directory_good( Directory* directory ){ - return directory != 0; +class Directory { + private: + int entry_idx = 0; + void* dir; + + public: + Directory(const char* name); + bool good(); + void close(); + const char* read_and_increment(); +}; + +inline bool directory_good(Directory* directory) { + if(directory) { + return directory->good(); + } + return false; } -inline Directory* directory_open( const char* name ){ - return g_dir_open( name, 0, 0 ); +inline Directory* directory_open(const char* name){ + return new Directory(name); } -inline void directory_close( Directory* directory ){ - g_dir_close( directory ); +inline void directory_close(Directory* directory){ + if(directory) { + directory->close(); + } } -inline const char* directory_read_and_increment( Directory* directory ){ - return g_dir_read_name( directory ); +inline const char* directory_read_and_increment(Directory* directory) { + if(directory) { + return directory->read_and_increment(); + } + return nullptr; } template From fa71fdf195c7038c9ecbd2dad1dafa701e388303 Mon Sep 17 00:00:00 2001 From: Celvin Cambloid Date: Mon, 18 Mar 2024 23:50:08 +0100 Subject: [PATCH 2/2] Changes --- libs/os/dir.cpp | 34 ++++++++++++++-------------------- libs/os/dir.h | 10 ++++++++-- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/libs/os/dir.cpp b/libs/os/dir.cpp index 5086ac61b..fcaa4921a 100644 --- a/libs/os/dir.cpp +++ b/libs/os/dir.cpp @@ -7,34 +7,28 @@ Directory::Directory(const char* name) { this->dir = new QDir(name); + QStringList entryList = this->dir->entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); + for(int i = 0; i < entryList.size(); i++) { + QString file_path = entryList.at(i); + QString file_name = QFileInfo(file_path).fileName(); + this->entries.push_back(file_name.toLatin1().data()); + } } bool Directory::good() { - if(this->dir) { - QDir* dir_instance = reinterpret_cast(this->dir); - return dir_instance->exists(); - } - return false; + return this->dir->exists(); } void Directory::close() { - if(this->dir) { - QDir* dir_instance = reinterpret_cast(this->dir); - delete dir_instance; - this->dir = nullptr; - } + this->entries.clear(); + delete this->dir; } -const char* Directory::read_and_increment(){ - if(this->dir) { - QDir* dir_instance = reinterpret_cast(this->dir); - QStringList entryList = dir_instance->entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); - if(this->entry_idx < entryList.size()) { - QString file_path = entryList.at(this->entry_idx); - QString file_name = QFileInfo(file_path).fileName(); - this->entry_idx++; - return file_name.toStdString().c_str(); - } +const char* Directory::read_and_increment() { + if(this->entry_idx < this->entries.size()) { + const char* entry = this->entries.at(this->entry_idx); + this->entry_idx++; + return entry; } return nullptr; } \ No newline at end of file diff --git a/libs/os/dir.h b/libs/os/dir.h index 79eb97090..d119d3055 100644 --- a/libs/os/dir.h +++ b/libs/os/dir.h @@ -24,10 +24,16 @@ /// \file /// \brief OS directory-listing object. +#include + +class QDir; + class Directory { + private: - int entry_idx = 0; - void* dir; + QDir* dir; + size_t entry_idx = 0; + std::vector entries; public: Directory(const char* name);