Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Qt for directory functions #175

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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) \
Garux marked this conversation as resolved.
Show resolved Hide resolved

$(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
Expand Down
34 changes: 34 additions & 0 deletions libs/os/dir.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "dir.h"

#include <QDir>
#include <QString>
#include <QStringList>
#include <QFileInfo>

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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary copy. QString is using hashed pool, but it's not free still. Normal practice is taking reference.

QString file_name = QFileInfo(file_path).fileName();
this->entries.push_back(file_name.toLatin1().data());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You store pointer to temp string here. Do you even test? This is straightforward crash.

}
}

bool Directory::good() {
return this->dir->exists();
}

void Directory::close() {
this->entries.clear();
delete this->dir;
}

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;
}
42 changes: 32 additions & 10 deletions libs/os/dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,46 @@
/// \file
/// \brief OS directory-listing object.

#include <glib.h>
#include <vector>

typedef GDir Directory;
class QDir;

inline bool directory_good( Directory* directory ){
return directory != 0;
class Directory {

private:
QDir* dir;
size_t entry_idx = 0;
std::vector<const char*> entries;

public:
Directory(const char* name);
bool good();
void close();
const char* read_and_increment();
};

inline bool directory_good(Directory* directory) {
if(directory) {
Garux marked this conversation as resolved.
Show resolved Hide resolved
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<typename Functor>
Expand Down