N
N
Nikita Savinykh2018-07-10 18:44:55
Qt
Nikita Savinykh, 2018-07-10 18:44:55

What is the right way to get rid of nested loops?

Good day to all!
In a good effort to leave neat code behind, I rewrite the fragment of reading the contents of files in the subdirectories of the directory:

>Католог текстур
    |
    |--> Тип текстуры (Diffuse)
    |                    |
    |                    |-->Текстура_0.dds
    |                    |-->Текстура_1.dds
    |                    |-->Текстура_N.dds
    |
    |--> Тип текстуры (Normal)
    |                    |
    |                    |-->Текстура_0.dds
    |                    |-->Текстура_1.dds
    |                    |-->Текстура_N.dds
    |
    |--> Тип текстуры (Typename)
                         |
                         |-->Текстура_0.dds
                         |-->Текстура_1.dds
                         |-->Текстура_N.dds

Wrote a simple code that reads this and does something with it:
ResourcesManager::ResourcesManager(QObject *parent) : QObject(parent)
{
    QDir root(":/graphic/");
    QStringList subdirs = root.entryList(QDir::NoDotAndDotDot|QDir::AllDirs);
    qDebug() << subdirs ;
    parceAllRes(root);
}

void ResourcesManager::parceAllRes(QDir root)
{
    QDir textureSubdir(root.path()+"/textures/");
    QStringList texturesTypes = textureSubdir.entryList(QDir::NoDotAndDotDot|QDir::AllDirs);
    qDebug() << texturesTypes;

    foreach (QString textureType, texturesTypes) {
        QDir textureTypeSubdir(root.path()+"/textures/"+textureType);
        QStringList texturesType = textureTypeSubdir.entryList();
        foreach (QString textureFile, texturesType) {
            qDebug() << "Type - " << textureType << " filename - " << textureFile;
        }
    }
}

And, to be honest, I don’t understand how you can get rid of this not very nice construction:
foreach (QString textureType, texturesTypes) {
        QDir textureTypeSubdir(root.path()+"/textures/"+textureType);
        QStringList texturesType = textureTypeSubdir.entryList();
        foreach (QString textureFile, texturesType) {
            qDebug() << "Type - " << textureType << " filename - " << textureFile;
        }
    }

Tell me how to unravel such noodles?
Split by function? So you can get even more confused.
Maybe a book to read, what pattern to use here?
I strongly apologize if the question is too banal for discussion, and I am extremely grateful to everyone who can suggest a vector for thought.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor Yanyshev, 2018-07-10
@villiwalla

Traversing directories, even with reading files in these directories, is best done through recursion.

I
ittakir, 2018-07-11
@ittakir

I don't see much problem in your code.
The format of the file paths is strictly defined, so no recursion is needed.
All work is divided into 2 steps:
1. Determine what types of textures are available.
2. Download all textures for each type.
I would break this logic into 2 separate functions. The first one returns a list of available types, where the type is given by enum, and the function inside matches the text name of the folder with enum.
The second function takes a texture type as input and loads all available files of that type.
Well, the main load function:

QMap<TextureType, QList<Texture>> loadTextures()
{
  QMap<TextureType, QList<Texture>> result;
  for (auto type : getAvailableTextureTypes()) {
    result.insert(type, loadTexturesForType(type));
  }
  return result;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question