π
<-
Chat plein-écran
[^]

Loosing my nerves on the folder/file listing function

C, C++, ASM...

Loosing my nerves on the folder/file listing function

Unread postby SlyVTT » 05 May 2021, 22:13

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: Select all
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: Select all
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
Developing the GUI Toolkit for nSpire
see current revision here : https://github.com/SlyVTT/Widget-for-TI-NSpire
Image Image Image Image
User avatar
SlyVTTProgrammeur
Niveau 8: ER (Espèce Rare: nerd)
Niveau 8: ER (Espèce Rare: nerd)
Level up: 46.5%
 
Posts: 105
Images: 0
Joined: 19 Jan 2021, 09:41
Gender: Male
Calculator(s):

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

Unread postby Adriweb » 05 May 2021, 22:24

I suppose that if it's an ndless-related issue, it's good that you opened a github issue, Vogtinator will likely reply soon :)
User avatar
AdriwebAdmin
Niveau 16: CC2 (Commandeur des Calculatrices)
Niveau 16: CC2 (Commandeur des Calculatrices)
Level up: 57.5%
 
Posts: 13413
Images: 1094
Joined: 01 Jun 2007, 00:00
Location: France
Gender: Male
Calculator(s):
Class: (ingénieur)
Twitter: adriweb
GitHub: adriweb

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

Unread postby jean-baptiste boric » 05 May 2021, 22:25

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.
User avatar
jean-baptiste boricPremium
Niveau 9: IC (Compteur Infatigable)
Niveau 9: IC (Compteur Infatigable)
Level up: 94.9%
 
Posts: 347
Joined: 21 Dec 2015, 22:22
Gender: Not specified
GitHub: boricj

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

Unread postby SlyVTT » 05 May 2021, 22:33

Adriweb wrote: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
Developing the GUI Toolkit for nSpire
see current revision here : https://github.com/SlyVTT/Widget-for-TI-NSpire
Image Image Image Image
User avatar
SlyVTTProgrammeur
Niveau 8: ER (Espèce Rare: nerd)
Niveau 8: ER (Espèce Rare: nerd)
Level up: 46.5%
 
Posts: 105
Images: 0
Joined: 19 Jan 2021, 09:41
Gender: Male
Calculator(s):

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

Unread postby SlyVTT » 05 May 2021, 22:39

jean-baptiste boric wrote: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
Developing the GUI Toolkit for nSpire
see current revision here : https://github.com/SlyVTT/Widget-for-TI-NSpire
Image Image Image Image
User avatar
SlyVTTProgrammeur
Niveau 8: ER (Espèce Rare: nerd)
Niveau 8: ER (Espèce Rare: nerd)
Level up: 46.5%
 
Posts: 105
Images: 0
Joined: 19 Jan 2021, 09:41
Gender: Male
Calculator(s):

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

Unread postby Bisam » 06 May 2021, 08:22

I really don't know anything about it but it SEEMS that the struct dirent is badly implemented...
User avatar
BisamAdmin
Niveau 15: CC (Chevalier des Calculatrices)
Niveau 15: CC (Chevalier des Calculatrices)
Level up: 53.8%
 
Posts: 5577
Joined: 11 Mar 2008, 00:00
Location: Lyon
Gender: Male
Calculator(s):

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

Unread postby SlyVTT » 06 May 2021, 09:27

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
Developing the GUI Toolkit for nSpire
see current revision here : https://github.com/SlyVTT/Widget-for-TI-NSpire
Image Image Image Image
User avatar
SlyVTTProgrammeur
Niveau 8: ER (Espèce Rare: nerd)
Niveau 8: ER (Espèce Rare: nerd)
Level up: 46.5%
 
Posts: 105
Images: 0
Joined: 19 Jan 2021, 09:41
Gender: Male
Calculator(s):

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

Unread postby SlyVTT » 06 May 2021, 11:51

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: Select all
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
Last edited by SlyVTT on 07 May 2021, 13:24, edited 1 time in total.
Developing the GUI Toolkit for nSpire
see current revision here : https://github.com/SlyVTT/Widget-for-TI-NSpire
Image Image Image Image
User avatar
SlyVTTProgrammeur
Niveau 8: ER (Espèce Rare: nerd)
Niveau 8: ER (Espèce Rare: nerd)
Level up: 46.5%
 
Posts: 105
Images: 0
Joined: 19 Jan 2021, 09:41
Gender: Male
Calculator(s):

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

Unread postby Bisam » 06 May 2021, 13:30

Nice work !
Hopefully, you'll change the color of the selected files... I really dislike this pink.
User avatar
BisamAdmin
Niveau 15: CC (Chevalier des Calculatrices)
Niveau 15: CC (Chevalier des Calculatrices)
Level up: 53.8%
 
Posts: 5577
Joined: 11 Mar 2008, 00:00
Location: Lyon
Gender: Male
Calculator(s):

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

Unread postby SlyVTT » 06 May 2021, 13:58

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
Developing the GUI Toolkit for nSpire
see current revision here : https://github.com/SlyVTT/Widget-for-TI-NSpire
Image Image Image Image
User avatar
SlyVTTProgrammeur
Niveau 8: ER (Espèce Rare: nerd)
Niveau 8: ER (Espèce Rare: nerd)
Level up: 46.5%
 
Posts: 105
Images: 0
Joined: 19 Jan 2021, 09:41
Gender: Male
Calculator(s):


Return to Native: Ndless, Linux, ...

Who is online

Users browsing this forum: No registered users and 2 guests

-
Search
-
Social
-
Featured topics
Comparaisons des meilleurs prix pour acheter sa calculatrice !
Découvre les nouvelles fonctionnalités en Python de l'OS 5.2 pour les Nspire CX II
Découvre les nouvelles fonctionnalités en Python de l'OS 5.5 pour la 83PCE/84+C-T Python Edition
Omega, le fork étendant les capacités de ta NumWorks, même en mode examen !
1234
-
Donations / Premium
For more contests, prizes, reviews, helping us pay the server and domains...
Donate
Discover the the advantages of a donor account !
JoinRejoignez the donors and/or premium!les donateurs et/ou premium !


Partner and ad
Notre partenaire Jarrety Calculatrices à acheter chez Calcuso
-
Stats.
640 utilisateurs:
>630 invités
>5 membres
>5 robots
Record simultané (sur 6 mois):
6892 utilisateurs (le 07/06/2017)

-
Other interesting websites
Texas Instruments Education
Global | France
 (English / Français)
Banque de programmes TI
ticalc.org
 (English)
La communauté TI-82
tout82.free.fr
 (Français)