File list. Recursively searching folders.

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.
Sidar
Posts: 13
Joined: Sat May 12, 2012 11:12 pm

File list. Recursively searching folders.

Post by Sidar »

Hi there,

I'm not sure how IFileList works, but it looks like it can only find items one folder from the "current" folder.

The following code is the basic look up. It first searches the path I've added to my searchPaths ( which is basically just an array of irr::io::path ).
The first folder to search in is "asset/". If the image is in this folder, all goes well. t is set to the loaded texture.

If that failes the "findInPath" kicks in:

Code: Select all

 
        irr::video::ITexture * t = NULL;
 
        irr::io::IFileList * filelist = device->getFileSystem()->createFileList();
 
        for (unsigned int i = 0; i < pathCount ; i++)
        {
                //WORKS if image is in "asset/"
                if(filelist->findFile(searchPaths[i] + filename, false) != -1)
                {
                        t = driver->getTexture(searchPaths[i] + filename);
                        return t;
                }
                // Doesn't work
                else if((t = findInPath(searchPaths[i],filename,filelist))){
                        return t;
                }
        }
 
Here is the method

Code: Select all

irr::video::ITexture * TextureManager::findInPath(irr::io::path path,irr::io::path filename, irr::io::IFileList * filelist){
 
        irr::video::ITexture * t = NULL;
        irr::io::path dir(path);
        WIN32_FIND_DATA file;
        dir.append("*");
        HANDLE search_handle=FindFirstFile(LPCTSTR(dir.c_str()),&file);
 
        
        if (search_handle)
        {
 
                do
                {
                        if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                        {
                                
                                //Skip any reference to current or parent folder. Else inf-loop imminent
                                if(file.cFileName[0] == '.'){
                                        continue;
                                }
 
                                irr::io::path resultPath = path + file.cFileName + "/" + filename;
                                
                                std::cout  << resultPath.c_str() << "\n";
                                
                                if(filelist->findFile(resultPath, false) != -1)
                                {
                                        return t;
                                }
                                
                                else 
                                {
                                        t = findInPath(path+file.cFileName + "/",filename,filelist);
                                        if(t) return t;
                                }
                        }
 
                }while(FindNextFile(search_handle,&file));
 
                FindClose(search_handle);
 
        }
        return t;
};
In my test im looking for "Planet.png" in [assets/planets/]
I print out the "resultpath" with the following result:

Image

Now the the path seems to be ok.
It just fails at if(filelist->findFile(resultPath, false) != -1)

Been stuck on this for hours now.
Does anyone know why this goes wrong?

-Sidar
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: File list. Recursively searching folders.

Post by REDDemon »

cout<<resultpath ?? result on the console is?
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Sidar
Posts: 13
Joined: Sat May 12, 2012 11:12 pm

Re: File list. Recursively searching folders.

Post by Sidar »

Look at the image.
That's the result path.
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: File list. Recursively searching folders.

Post by REDDemon »

yes but link is broken or something else because i just see written "image" but i not see any image.
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Sidar
Posts: 13
Joined: Sat May 12, 2012 11:12 pm

Re: File list. Recursively searching folders.

Post by Sidar »

It works here...

http://i49.tinypic.com/nx1der.png

Here is a nother link
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: File list. Recursively searching folders.

Post by REDDemon »

edit: readed bad.
Last edited by REDDemon on Sun May 13, 2012 10:06 pm, edited 1 time in total.
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Sidar
Posts: 13
Joined: Sat May 12, 2012 11:12 pm

Re: File list. Recursively searching folders.

Post by Sidar »

I'm rather confused, the path name is still valid. it shows in the console output. I simply concatenate the paths and try to pass it in the findile method of irrlicht. I check if the file exists at the path by checking if the return int32 is not -1. If so I pass the concatenated path into the driver->getTexture method.

All I'm really doing is joining folder names.
The console shows that the folder name paths are correct.

Could you elaborate on it more please?
Sidar
Posts: 13
Joined: Sat May 12, 2012 11:12 pm

Re: File list. Recursively searching folders.

Post by Sidar »

Ok for some reason findfile isn't working at all anymore =(
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: File list. Recursively searching folders.

Post by REDDemon »

you are doind a series of little mistakes,

1) IVideoTexture* is a pointer to a ITexture (read about polymorphism in C++)

when you doing

Code: Select all

t = findInPath(path+file.cFileName + "/",filename,filelist);
you are searching for a file.. that file is not a Itexture, but is something else! (you cannot cast to Itexture)

2)

Code: Select all

 
if(filelist->findFile(resultPath, false) != -1);
                                {
                                        return t;
                                }
 
you are returning t without assigning any value to t. so t was null and you returned null. maybe you wanted to do

Code: Select all

 
t = filelist->findFile(resultPath, false)
if(t!=-1)
                                {
                                        return t; 
                                }
 
and finally

you should check if pointer is not 0 (not -1). because pointers does not works as "return -1" in main. When "main" is ok it returns 0. When pointers are NOT ok they return 0.

I assume you are moving to C++ from another language and you are experienced in programming, there are lots of good tutorials that can help you fit holes about C++ syntax and Object oriented programming. (is really worth and interesting C++ especially when you start diggin into OO design wich is really deep topic).
Last edited by REDDemon on Sun May 13, 2012 10:26 pm, edited 1 time in total.
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Sidar
Posts: 13
Joined: Sat May 12, 2012 11:12 pm

Re: File list. Recursively searching folders.

Post by Sidar »

1:
No I return an ITexture. I think you missed the point of the method. The method recursively looks in every folder. with findfile I check if the file is in the current folder. If so, I create the texture with driver->getTexture() passing the concatenated map names + file name and point my pointer t to it. I return it back to every findInPath method until It reaches the original caller trough TextureManager::getTexture().

Code: Select all

you are searching for a file.. that file is not a Itexture, but is something else! (you cannot cast to Itexture)
I was never attempting that.
2:

Code: Select all

you are returning t without assigning any value to t. so t was null and you returned null. maybe you wanted to do
That's the point. If nothing can be found I return null.

Code: Select all

you should check if pointer is not 0 (not -1). because pointers does not works as "return -1" in main. When "main" is ok it returns 0. When pointers are NOT ok they return 0.
Shouldn't if(t) suffice? It checks if it's pointing to something.
irr::video::ITexture * t = NULL; <--- it is set to NULL and can't be -1 in the end.
But that's not even the problem.

findfile seems to fail. Even if I manually give the path it always returns -1.

Edit:
I understand where your confusion came from
t = driver->getTexture(resultPath);

I forgot about this yes.

But it's not solving my problem with findfile which returns -1 on everything I pass it trough
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: File list. Recursively searching folders.

Post by REDDemon »

1. I not missed the point. You are basically returning ITexture. so you first need to get a pointer to Itexture, and you are not getting it (the path seems
ok so maybe you should just return a io::path?)

Code: Select all

 
if(filelist->findFile(resultPath, false) != -1)
                                {
                                        return t; // you are just retunin null here
                                }
                               
                                else
                                {
                                        t = findInPath(path+file.cFileName + "/",filename,filelist); // you are recurse here. to find t
                                        if(t) return t;
                                }
 
as you see there is no method assigning t a value. because the result is always. "recurse" or return t = null.

After you find the right path you don't need to search it in a filelist. Just open the texture with that path

(by the way you'll need a file list for every folder, are you creating a new filelist on each recursion? nope, and doing that will cause a memory leak so you'll need also to drop when exiting recursion).

if filelist is returning -1 its because that file is not in the list.

2. you are right (sorry it's very late here)
Last edited by REDDemon on Sun May 13, 2012 10:39 pm, edited 1 time in total.
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Sidar
Posts: 13
Joined: Sat May 12, 2012 11:12 pm

Re: File list. Recursively searching folders.

Post by Sidar »

Look at my edit.

I did miss the call to driver->getTexture().

But like I said, this is not the problem.

If i try:

Code: Select all

int ind = filelist->findFile("assets/test.png", false);
ind is -1.

And yes the map assets is at the base. And test.png is there in assets.
And this is tested before I even look for the texture I want.

Edit:

It's -1 with every path + filename I give...=[
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: File list. Recursively searching folders.

Post by REDDemon »

already replied to edit also.

you are changin folder wich recursion, filelist provides all files in a folder, if you change folder, filelist is no longer valid (it holds only a list of all files in old folder, it does not magically automatic update if you change folder). Anyway if the path you obtain is already the correct path you don't need a file list. When you have a path just create the texture with that path.
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Sidar
Posts: 13
Joined: Sat May 12, 2012 11:12 pm

Re: File list. Recursively searching folders.

Post by Sidar »

Actually the filelist is untouched, I use the windows api to look trough folders.

I was able to use the filelist base folder and look in it and all sub folders.
I don't exactly "change" folder. I just concatenate the folder names until findfile finds the file at the given path.
It used to work when I tried filelist->findfile("assets/" + Filename);

Now even that stopped working.
Anyway if the path you obtain is already the correct path you don't need a file list. When you have a path just create the texture with that path.
That's not the point of what I'm trying to achieve.
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: File list. Recursively searching folders.

Post by REDDemon »

yes but I already sad that filelist is not automagically updating. If you provide a filename wich is located at a different folder level, the file list (wich is as sad untouched) can't find the file. is that still obscure to you?
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Post Reply