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 1 commit
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
34 changes: 14 additions & 20 deletions libs/os/dir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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() {
if(this->dir) {
QDir* dir_instance = reinterpret_cast<QDir*>(this->dir);
return dir_instance->exists();
}
return false;
return this->dir->exists();
}

void Directory::close() {
if(this->dir) {
QDir* dir_instance = reinterpret_cast<QDir*>(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<QDir*>(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;
}
10 changes: 8 additions & 2 deletions libs/os/dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@
/// \file
/// \brief OS directory-listing object.

#include <vector>

class QDir;

class Directory {

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

public:
Directory(const char* name);
Expand Down