Page 1 sur 1

Loosing my nerves on the folder/file listing function

Message non luPosté: 05 Mai 2021, 22:13
de SlyVTT
Hello everybody,

I am experiencing a bug in my GUI Tollkit when trying to list files and folders in a given folder.

My code is the following :

Code: Tout sélectionner
int FileDialogBoxWidget::listdir(const char *path)
{
    char name[255];
    struct dirent *ent;
    DIR *dir = opendir(path);
    while((ent = readdir(dir)))
    {
        strcpy(name, path);
        strcat(name,"/");
        strcat(name, ent->d_name);
        DIR *test = opendir( name );
        if(test)
        {
            closedir(test);
            folderlist->additem( (char *) ent->d_name );
        }
        else
        {
            filelist->additem( (char *) ent->d_name );
        }
    }
    closedir(dir);
  return 0;
}



It loops the correct number of times but ent->d_name always return the name of the first item (in my case the name of the first folder) in the considered folder to scrutinize.

I made the same routine on my linux distribution and it works perfectly well. The code is as follows :

Code: Tout sélectionner
int listdir(const char *path)
{
    char name[255];
    struct dirent *ent;
    DIR *dir = opendir(path);
    while((ent = readdir(dir)))
    {
        //Test whether it's a directory
        strcpy(name, path);
        strcat(name,"/");
        strcat(name, ent->d_name);
        DIR *test = opendir( name );
        if(test)    // This is a directory and we add to the folder list widget
        {
            closedir(test);
            printf( "[ %s ] \n", ent->d_name );
        }
        else    // this is a file and we add to the folder list widget
        {
                printf( " %s  \n", ent->d_name );
        }
    }
    closedir(dir);
  return 0;
}


No need to say that I checked many thing to try to identify if the folderlist and filelist works fine, and they do.
I do not understand what's happening, does anyone also met that bug/issue ?
The bug is in the following file : https://github.com/SlyVTT/Widget-for-TI-NSpire/blob/main/Toolkit/FileDialogBoxWidget.cpp

Ciao

Sly

Re: Loosing my nerves on the folder/file listing function

Message non luPosté: 05 Mai 2021, 22:24
de Adriweb
I suppose that if it's an ndless-related issue, it's good that you opened a github issue, Vogtinator will likely reply soon :)

Re: Loosing my nerves on the folder/file listing function

Message non luPosté: 05 Mai 2021, 22:25
de jean-baptiste boric
Looking at your code, you try to open a directory entry to see if that's a directory or a file. You should probably instead use lstat() if that's available on your system.

Re: Loosing my nerves on the folder/file listing function

Message non luPosté: 05 Mai 2021, 22:33
de SlyVTT
Adriweb a écrit:I suppose that if it's an ndless-related issue, it's good that you opened a github issue, Vogtinator will likely reply soon :)


I did it concurrently. I don't know where Vogtinator will navigate first :-)

Thanks Adriweb

Re: Loosing my nerves on the folder/file listing function

Message non luPosté: 05 Mai 2021, 22:39
de SlyVTT
jean-baptiste boric a écrit:Looking at your code, you try to open a directory entry to see if that's a directory or a file. You should probably instead use lstat() if that's available on your system.


I tried to stay 'as basic as possible' to keep the size of the toolkit as low as possible.
For sure this is not the best way.

I don't know if the stat include is available. The dirent structure is hence very limited and does not offer all the info. In particular the d_type is missing. So this is why a need a trick to check if it is a file or a folder.

Thanks

Sly

Re: Loosing my nerves on the folder/file listing function

Message non luPosté: 06 Mai 2021, 08:22
de Bisam
I really don't know anything about it but it SEEMS that the struct dirent is badly implemented...

Re: Loosing my nerves on the folder/file listing function

Message non luPosté: 06 Mai 2021, 09:27
de SlyVTT
Bisam,

Also looks like this to me.
Right now it is a copy of the "nucleus" struct nuc_dirent, which only contains a "char d_name[1];" member.
Looks a bit strange.

And I cannot find direct useage of nuc_dirent in other programs sources.
Vogtinator's pyWrite uses the dirent.h functions and it seems to work earlier, but I was not able to compile the sources with current revision of Ndless to check if it is still working or is now broken.
Hope he (or another skilled guy of the Ndless team) will be able to give feedbacks on that issue.

Ciao

Sly

Re: Loosing my nerves on the folder/file listing function

Message non luPosté: 06 Mai 2021, 11:51
de SlyVTT
Ok Guys,

Spending a lot of time on this, we can conclude I am a bit brainless ... :bang:

Now this is working. The problem was coming from the use of the ent->d_name as an allocated (char *) while it is just a pointer. Now I allocate memory on the fly for each iteration of the loop and copy the ent->d_name to the right place.
This avoids pointing (and adding in the lists) always the first item.

below is the working code, note the malloc part that saves my life ;-)

Code: Tout sélectionner
int FileDialogBoxWidget::listdir(const char *path)
{
char name[255];
    struct dirent *ent;
    DIR *dir = opendir(path);
    while((ent = readdir(dir)))
    {
        //Test whether it's a directory
        strcpy(name, path);
        strcat(name,"/");
        strcat(name, ent->d_name);
        DIR *test = opendir( name );
        if(test)    // This is a directory and we add to the folder list widget
        {
            closedir(test);
            char *temp = (char*) malloc( strlen(ent->d_name) +1 );
            strcpy(temp, ent->d_name );
            folderlist->additem( (char *) temp );
        }
        else    // this is a file and we add to the file list widget
        {
            char *temp = (char*) malloc( strlen(ent->d_name) +1 );
            strcpy(temp, ent->d_name );
            filelist->additem( (char *) temp );
        }
    }
    closedir(dir);
  return 0;
}


and to celebrate that important moment :p , this is the picture of the working file picking dialog box (still needing some works of mine to make it fully functional).

Image

Ciao

Sly


PS: the Ndless repository PR has been closed with the working code posted

Re: Loosing my nerves on the folder/file listing function

Message non luPosté: 06 Mai 2021, 13:30
de Bisam
Nice work !
Hopefully, you'll change the color of the selected files... I really dislike this pink.

Re: Loosing my nerves on the folder/file listing function

Message non luPosté: 06 Mai 2021, 13:58
de SlyVTT
Bisam,

this is to highlight the "Girly" part of mine :#langue#: . Don't worry, the application is fully themable.

but for visibility reason, I keep this "bright" theme, cause it helps a lot the development (I know very well the color scheme and can see in one single eyeshot is something is wrong or not).

-- Advertisement mode : ON --

I am looking for contributors to create THEMES and LOGO ... Still :#gni#:

-- Advertisement mode : OFF --

Ciao

Sly