[fixed] Efficiently iterate over FS using getFileSystem

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Cube_
Posts: 1010
Joined: Mon Oct 24, 2011 10:03 pm
Location: 0x45 61 72 74 68 2c 20 69 6e 20 74 68 65 20 73 6f 6c 20 73 79 73 74 65 6d

[fixed] Efficiently iterate over FS using getFileSystem

Post by Cube_ »

So I've been playing around with safeguarding my code (because if a file doesn't exist irrlicht crashes instead of handling it gracefully) and I figured; while I'm at it I might as well also figure out an efficient way to read files from FS; currently I just load a string list from a hardcoded config file and parse files one by one; this is obviously stupid but it works, eh?

anyway, since irrlicht has quite a few filesystem tools it presumably also has the ability to run the equivalent of ls and getting a file list, no?
Well, in any case - I tried my best at replicating such functionality in any meaningful way (I need to get a list of files so I can load them, and I don't know the file names at compile time)

If so, currently I just load strings from xml (this is not extensible) and then for each path I do this (minimal example with hardcoded path):

Code: Select all

 
std::string fpath = "data/models/levels/alphamap.obj";
io::IFileSystem* FileSystem = device->getFileSystem();
if (FileSystem->existFile(fpath.c_str()))
{
    IAnimatedMesh* levelMesh = smgr->getMesh(fpath.c_str());
    IMeshSceneNode* level = smgr->addMeshSceneNode(levelMesh);
    if (level)
    {
        level->setMaterialFlag(EMF_LIGHTING, false);
        level->setPosition(level->getPosition() + vector3df(0.0f, 0.0f, 50.0f));
    }
    else
    {
        level->drop();
        level = smgr->addCubeSceneNode(10.0f, 0, -1);
        level->setMaterialFlag(EMF_LIGHTING, false);
        level->setPosition(level->getPosition() + vector3df(0.0f, 0.0f, 50.0f));
    }
}
I tried using the following to iterate over the filesystem to find all .obj files (I have 10 of them for testing purposes)

Code: Select all

    io::IFileSystem* fs = Game->device->getFileSystem();
    fs->changeWorkingDirectoryTo("Data/");
    IFileList *fileList = fs->createFileList();
    fs->setFileListSystem(FILESYSTEM_NATIVE);
    for (int i = 0; i < fileList->getFileCount(); ++i)
    {
        s = fileList->getFullFileName(i);
        if (s.find(".obj") >= 0)
        {
            printf("path: %s\n", s);
        }
    }
Now; this sorta works - I get output.

but all lines are just "path: c" and I'm definitely certain none of the obj. files are named "c"; this implies that I misunderstand how to use that. s is defined as a core::stringw type, if that matters.
So the question is; what exactly am I doing wrong?
Worth noting is: I get exactly 10 lines out, so it is finding 10 obj files.
Last edited by Cube_ on Thu Jan 05, 2017 9:37 am, edited 1 time in total.
"this is not the bottleneck you are looking for"
Cube_
Posts: 1010
Joined: Mon Oct 24, 2011 10:03 pm
Location: 0x45 61 72 74 68 2c 20 69 6e 20 74 68 65 20 73 6f 6c 20 73 79 73 74 65 6d

Re: Efficiently iterate over filesystem using getFileSystem

Post by Cube_ »

Updated; managed to fix it - although it's technically an unsafe operation since it's an unsafe cast from core::stringw to wchar_t*

Code: Select all

    io::IFileSystem* fs = Game->device->getFileSystem();
    fs->changeWorkingDirectoryTo("Data/");
    IFileList *fileList = fs->createFileList();
    fs->setFileListSystem(FILESYSTEM_NATIVE);
    for (int i = 0; i < fileList->getFileCount(); ++i)
    {
        s = fileList->getFullFileName(i);
        if (s.find(".obj") >= 0)
        {
            wprintf(L"path: %s\n", s); 
        }
    }
"this is not the bottleneck you are looking for"
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [fixed] Efficiently iterate over FS using getFileSystem

Post by CuteAlien »

You can use .c_str() to get a wchar_t* from a core::stringw
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Cube_
Posts: 1010
Joined: Mon Oct 24, 2011 10:03 pm
Location: 0x45 61 72 74 68 2c 20 69 6e 20 74 68 65 20 73 6f 6c 20 73 79 73 74 65 6d

Re: [fixed] Efficiently iterate over FS using getFileSystem

Post by Cube_ »

CuteAlien wrote:You can use .c_str() to get a wchar_t* from a core::stringw
yeah, I didn't even think of that - I've been going for... 18 hours now? I'm mostly done implementing all my file serialization code (including correctly determining endianness and serializing to network order (big endian)), most of my rendering and loading code, some level paging code - on a roll; I would probably have rememberd c_str() eventually since I use it everywhere else.
"this is not the bottleneck you are looking for"
Post Reply