π
<-
Chat plein-écran
[^]

Loosing my nerves on the folder/file listing function

C, C++, ASM...
Online

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
Some works in progress :
The GUI Toolkit NF for nSpireMyShmup for fxCG-50Magic Light for Casio Graph 90+E
and
Magic Light for nSpire CX/CX-II
Simple Text Editor for nSpireOutRun for Casio Graph 90+E
95%
50%
100%
75%
100%
And more to come ... stay tuned
User avatar
SlyVTTPremium
Niveau 12: CP (Calculatrice sur Pattes)
Niveau 12: CP (Calculatrice sur Pattes)
Level up: 43.1%
 
Posts: 484
Images: 31
Joined: 19 Jan 2021, 09:41
Location: France
Gender: Male
Calculator(s):
MyCalcs profile
GitHub: SlyVTT

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 :)
Image

MyCalcs: Help the community's calculator documentations by filling out your calculators info!
MyCalcs: Aidez la communauté à documenter les calculatrices en donnant des infos sur vos calculatrices !
Inspired-Lua.org: All about TI-Nspire Lua programming (tutorials, wiki/docs...)
User avatar
AdriwebAdmin
Niveau 16: CC2 (Commandeur des Calculatrices)
Niveau 16: CC2 (Commandeur des Calculatrices)
Level up: 80.2%
 
Posts: 14616
Images: 1218
Joined: 01 Jun 2007, 00:00
Location: France
Gender: Male
Calculator(s):
MyCalcs profile
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 10: GR (Guide de Référence)
Niveau 10: GR (Guide de Référence)
Level up: 4.5%
 
Posts: 374
Joined: 21 Dec 2015, 22:22
Gender: Not specified
Calculator(s):
MyCalcs profile
GitHub: boricj

Online

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
Some works in progress :
The GUI Toolkit NF for nSpireMyShmup for fxCG-50Magic Light for Casio Graph 90+E
and
Magic Light for nSpire CX/CX-II
Simple Text Editor for nSpireOutRun for Casio Graph 90+E
95%
50%
100%
75%
100%
And more to come ... stay tuned
User avatar
SlyVTTPremium
Niveau 12: CP (Calculatrice sur Pattes)
Niveau 12: CP (Calculatrice sur Pattes)
Level up: 43.1%
 
Posts: 484
Images: 31
Joined: 19 Jan 2021, 09:41
Location: France
Gender: Male
Calculator(s):
MyCalcs profile
GitHub: SlyVTT

Online

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
Some works in progress :
The GUI Toolkit NF for nSpireMyShmup for fxCG-50Magic Light for Casio Graph 90+E
and
Magic Light for nSpire CX/CX-II
Simple Text Editor for nSpireOutRun for Casio Graph 90+E
95%
50%
100%
75%
100%
And more to come ... stay tuned
User avatar
SlyVTTPremium
Niveau 12: CP (Calculatrice sur Pattes)
Niveau 12: CP (Calculatrice sur Pattes)
Level up: 43.1%
 
Posts: 484
Images: 31
Joined: 19 Jan 2021, 09:41
Location: France
Gender: Male
Calculator(s):
MyCalcs profile
GitHub: SlyVTT

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: 69.6%
 
Posts: 5665
Joined: 11 Mar 2008, 00:00
Location: Lyon
Gender: Male
Calculator(s):
MyCalcs profile

Online

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
Some works in progress :
The GUI Toolkit NF for nSpireMyShmup for fxCG-50Magic Light for Casio Graph 90+E
and
Magic Light for nSpire CX/CX-II
Simple Text Editor for nSpireOutRun for Casio Graph 90+E
95%
50%
100%
75%
100%
And more to come ... stay tuned
User avatar
SlyVTTPremium
Niveau 12: CP (Calculatrice sur Pattes)
Niveau 12: CP (Calculatrice sur Pattes)
Level up: 43.1%
 
Posts: 484
Images: 31
Joined: 19 Jan 2021, 09:41
Location: France
Gender: Male
Calculator(s):
MyCalcs profile
GitHub: SlyVTT

Online

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.
Some works in progress :
The GUI Toolkit NF for nSpireMyShmup for fxCG-50Magic Light for Casio Graph 90+E
and
Magic Light for nSpire CX/CX-II
Simple Text Editor for nSpireOutRun for Casio Graph 90+E
95%
50%
100%
75%
100%
And more to come ... stay tuned
User avatar
SlyVTTPremium
Niveau 12: CP (Calculatrice sur Pattes)
Niveau 12: CP (Calculatrice sur Pattes)
Level up: 43.1%
 
Posts: 484
Images: 31
Joined: 19 Jan 2021, 09:41
Location: France
Gender: Male
Calculator(s):
MyCalcs profile
GitHub: SlyVTT

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: 69.6%
 
Posts: 5665
Joined: 11 Mar 2008, 00:00
Location: Lyon
Gender: Male
Calculator(s):
MyCalcs profile

Online

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
Some works in progress :
The GUI Toolkit NF for nSpireMyShmup for fxCG-50Magic Light for Casio Graph 90+E
and
Magic Light for nSpire CX/CX-II
Simple Text Editor for nSpireOutRun for Casio Graph 90+E
95%
50%
100%
75%
100%
And more to come ... stay tuned
User avatar
SlyVTTPremium
Niveau 12: CP (Calculatrice sur Pattes)
Niveau 12: CP (Calculatrice sur Pattes)
Level up: 43.1%
 
Posts: 484
Images: 31
Joined: 19 Jan 2021, 09:41
Location: France
Gender: Male
Calculator(s):
MyCalcs profile
GitHub: SlyVTT


Return to Native: Ndless, Linux, ...

Who is online

Users browsing this forum: No registered users and 24 guests

-
Search
-
Social TI-Planet
-
Featured topics
Comparaisons des meilleurs prix pour acheter sa calculatrice !
Aidez la communauté à documenter les révisions matérielles en listant vos calculatrices graphiques !
Phi NumWorks jailbreak
123
-
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.
1443 utilisateurs:
>1409 invités
>29 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)