#116822 - Arialia - Tue Jan 30, 2007 12:20 am
Hello,
I make a version of chism's LibFat with functions of directory (with source code of 13082006) because i couldn't wait for devkitARM r20
I only write a dirent.c and a dirent.h who uses directly Libfat ( so i include the source code of libfat in my project)
I want to know if a code like this will work under devkitARM r20 ?
Code: |
DIR* dir;
struct dirent * de;
struct stat statbuf;
mode_t mode;
char rep[255],ficname[255];
rep="/";
if ((dir = opendir(rep)))
{
int i=0,result;
while ((de = readdir( dir )))
{
sprintf(ficname,"%s%s",rep,de->d_name);
result=stat(ficname,&statbuf);
if (result!=-1)
{
mode=statbuf.st_mode;
if ( S_ISDIR(mode))
{
printf("%s", de->d_name );
}
}
else printf("pb stat:%s",de->d_name);
}
rewinddir(dir);
while ((de = readdir( dir )))
{
sprintf(ficname,"%s%s",rep,de->d_name);
result=stat(ficname,&statbuf);
if (result!=-1)
{
mode=statbuf.st_mode;
if ( !S_ISDIR(mode))
{
printf("%s", de->d_name );
}
}
}
closedir( dir );
}
|
find here my little prog with support of DLDI : http://www.playeradvance.org/forum/attachment.php?attachmentid=509&d=1169575189
and here my first homebrew made with this
DSPhoto
Thanks to Chism for LibFat and Wintermute for DevkitPro
Sorry for my bad english ....
#116828 - chishm - Tue Jan 30, 2007 2:09 am
DevkitARM r20 was released today. It doesn't contain <dirent.h> support, but uses a different method of getting file lists. Look in <dir.h> for details.
_________________
http://chishm.drunkencoders.com
http://dldi.drunkencoders.com
#116834 - tepples - Tue Jan 30, 2007 2:54 am
Does this mean the race is on to make a <dirent.h> compatible wrapper around <sys/dir.h>?
EDIT: Looking at the header files, I see what appears to be a nearly one-to-one mapping:
dirent.h->sys/dir.h
DIR->DIR_ITER
opendir->diropen
readdir->dirnext
closedir->dirclose
rewinddir->dirreset (?)
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#116838 - chishm - Tue Jan 30, 2007 3:58 am
The near one-to-one mapping is intended by design. There are a few differences, including forced re-entrancy support (dirnext is closer to readdir_r than readdir) and the optional returning of the file stat with the filename (reduces function calls, improving directory traversal performance).
WinterMute isn't keen on the <dirent.h> wrapper (the DS is not meant to be a POSIX system). I will write documentation and an example for <dir.h> when I get the chance, but it should be simple enough to use.
_________________
http://chishm.drunkencoders.com
http://dldi.drunkencoders.com
#116844 - tepples - Tue Jan 30, 2007 4:44 am
chishm wrote: |
WinterMute isn't keen on the <dirent.h> wrapper (the DS is not meant to be a POSIX system). |
Neither is Windows without Cygwin or SFU, but plenty of development environments on Windows (including parts of MinGW and MSYS) provide support for some common functions of a POSIX system.
Of course the DS isn't "meant" to be a POSIX system; it's not even intended by its manufacturer to run homebrew.
What is DSLinux other than an implementation of POSIX function on top of the DS hardware?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#116866 - wintermute - Tue Jan 30, 2007 1:34 pm
tepples wrote: |
What is DSLinux other than an implementation of POSIX function on top of the DS hardware? |
And devkitARM/libnds isn't nor is a FAT based file system.
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog
#116867 - wintermute - Tue Jan 30, 2007 1:36 pm
chishm wrote: |
WinterMute isn't keen on the <dirent.h> wrapper (the DS is not meant to be a POSIX system).
|
devkitARM/libnds is not meant to be a POSIX system, FAT is most definitely not POSIX and I'm concerned about providing functions that behave differently from the user's expectations.
The dilemma I'm faced with here is that I've put together devkitARM as a generic ARM toolchain allowing it to be used for many other systems rather than just a DS/GBA specific toolchain. dirent.h is normally provided by OS specific parts of the newlib source tree - cygwin and linux to name but two. If I then add GBA/DS specific dirent related code into the toolchain I'm liable to interfere with future upgrades so I'd prefer to avoid doing that.
It's probably best to put a dirent header in libfat which uses wrappers around the custom functions for the sake of porting code which uses dirent.
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog
#116952 - Arialia - Wed Jan 31, 2007 8:34 am
Ok you are right wintermute.
Thanks a lot again for your work
#117010 - GPFerror - Wed Jan 31, 2007 7:51 pm
Code: |
#include <nds.h>
#include <stdio.h>
#include <fat.h>
#include <sys/types.h>
#include <sys/dir.h>
#define VCOUNT (*((u16 volatile *) 0x04000006))
inline void WaitForVblank() {
while(VCOUNT>192); // wait for vblank
while(VCOUNT<192);
}
int main(int argc, char *argv[]) {
powerON(POWER_ALL);
videoSetMode(MODE_0_2D | DISPLAY_BG0_ACTIVE);
vramSetBankA(VRAM_A_MAIN_BG);
BG0_CR = BG_MAP_BASE(31);
BG_PALETTE[255] = RGB15(31,31,31);
consoleInitDefault((u16*)SCREEN_BASE_BLOCK(31), (u16*)CHAR_BASE_BLOCK(0), 16);
iprintf("Hello!\n");
// Map Game Cartridge memory to ARM9
sysSetCartOwner( BUS_OWNER_ARM9 );
if (!fatInitDefault())
{
iprintf("Unable to initialize media device!");
return -1;
}
WaitForVblank();
WaitForVblank();
DIR_ITER* di;
char * filename;
struct stat *fs;
int i;
filename= (char*)malloc(256);
if ( (di = diropen("/")) == NULL ) {
iprintf("Can't open directory: / \n");
return -1;
}
do{
i =dirnext(di,filename,fs);
if (i != -1) printf("%s\n",filename);
} while (!i);
dirclose(di);
free(filename);
iprintf("GoodBye!\n");
return 1;
}
|
This list the contents of the root directory / and then quits!
still would be nice to have a dirent.h for porting purposes :)
Troy(GPF)
http://gpf.dcemu.co.uk
#117033 - strager - Wed Jan 31, 2007 9:58 pm
GPFerror, you aren't initializing fs with anything. It could be a NULL pointer (which is what I think you're aiming for), or could be some random data from an older variable. Just thought I'd note.
Otherwise, that looks like a good reference for grabbing a directory listing. Thanks for posting!
#117043 - GPFerror - Wed Jan 31, 2007 10:33 pm
strager wrote: |
GPFerror, you aren't initializing fs with anything. It could be a NULL pointer (which is what I think you're aiming for), or could be some random data from an older variable. Just thought I'd note.
Otherwise, that looks like a good reference for grabbing a directory listing. Thanks for posting! |
Yeah I am not really familiar with stat, so i was just trying to ignore its existence :)
I googled for hours this morning and could not find a single example for using <sys/dir.h>, so it was a lot of trial an error to get this far based on the above comments that the functions were similiar to dirent.h api.
Troy(GPF)
#117046 - josath - Wed Jan 31, 2007 11:01 pm
I think you just need to do:
Code: |
...
struct stat fs;
...
i=dirnext(di,filename,&fs);
|
Or I believe you can pass NULL instead of fs, and it won't give you the file stats.
#117059 - strager - Thu Feb 01, 2007 12:09 am
josath wrote: |
I think you just need to do:
Code: | ...
struct stat fs;
...
i=dirnext(di,filename,&fs);
|
Or I believe you can pass NULL instead of fs, and it won't give you the file stats. |
That's the way I stat, and I have no problems with it. =]
#117062 - chishm - Thu Feb 01, 2007 12:25 am
GPFerror:
Is that code based on pre-DKARM r20 code I sent you? If so, you'll need to update it a bit. dirnext should now return 0 when it finds a valid dir entry, and -1 at all other times. Return -1 with errno==ENOENT indicates that it has reached the end of the directory.
So a simple example (untested, should work) would be:
Code: |
#include <sys/stat.h>
#include <dir.h>
struct stat st;
char filename[MAX_FILENAME_LEN]; // may need to be 256 instead of a constant
DIR_ITER* dir;
dir = diropen ("/"); // no error checking -- make sure you check for NULL!
while (dirnext(dir, filename, &st) == 0) {
// st.st_mode & S_IFDIR indicates a directory
printf ("%s: %s\n", (st.st_mode & S_IFDIR ? " DIR" : "FILE"), filename);
} |
_________________
http://chishm.drunkencoders.com
http://dldi.drunkencoders.com
#117142 - GPFerror - Thu Feb 01, 2007 9:46 pm
chishm wrote: |
GPFerror:
Is that code based on pre-DKARM r20 code I sent you? If so, you'll need to update it a bit. dirnext should now return 0 when it finds a valid dir entry, and -1 at all other times. Return -1 with errno==ENOENT indicates that it has reached the end of the directory.
So a simple example (untested, should work) would be:
Code: |
#include <sys/stat.h>
#include <dir.h>
struct stat st;
char filename[MAX_FILENAME_LEN]; // may need to be 256 instead of a constant
DIR_ITER* dir;
dir = diropen ("/"); // no error checking -- make sure you check for NULL!
while (dirnext(dir, filename, &st) == 0) {
// st.st_mode & S_IFDIR indicates a directory
printf ("%s: %s\n", (st.st_mode & S_IFDIR ? " DIR" : "FILE"), filename);
} |
|
Nope just based on trial and error, wish I had your code, even if it was pre :)
yeah my code does that checks if return = -1 and doesn't print the filename and exits the loop on if return is !0 .
I originally was trying to do it very similiar to how you did it(with out the stat stuff though) , but was running into some problem and by the time I got it working it, the above code is what I got :)
Troy(GPF)
#117160 - chishm - Fri Feb 02, 2007 12:41 am
I guess this means I need to write some documentation and examples soon. Only problem is, it's 2nd on my list of projects (gotta get this SC Lite HB compability fixed).
_________________
http://chishm.drunkencoders.com
http://dldi.drunkencoders.com
#117171 - SukkoPera - Fri Feb 02, 2007 1:26 am
I can write the examples, I guess. One that lists all files on the card, for instance. Any other ideas?
_________________
Nintendo DS Lite (White) + Supercard Lite + R4 + Sandisk 1 GB MicroSD
Sony PSP + Firmware 3.03 OE-A2
#117183 - chishm - Fri Feb 02, 2007 3:07 am
SukkoPera wrote: |
I can write the examples, I guess. One that lists all files on the card, for instance. Any other ideas? |
That's probably the most important example. Maybe in both a depth-first and breadth-first traversal. Using chdir in combination with dir* shouldn't be too hard, or you could have a recursive function call itself with a new DIR_ITER for each directory found. This is probably the better way, provided you have enough stack space.
_________________
http://chishm.drunkencoders.com
http://dldi.drunkencoders.com
#117187 - SukkoPera - Fri Feb 02, 2007 4:05 am
I have that code in a program of mine already, so it won't be too hard. I'll work on it in the weekend and I'll let you know.
_________________
Nintendo DS Lite (White) + Supercard Lite + R4 + Sandisk 1 GB MicroSD
Sony PSP + Firmware 3.03 OE-A2
#117673 - SukkoPera - Tue Feb 06, 2007 2:53 pm
Sorry for the delay, but finally here is my little demo:
http://www.sukkology.net/software/nds/fatsample.zip
It's not the best example that could be made (output sucks!), but it should show basic usage of directory access functions. I also added some code for flashcart type detection.
Feel free to do whatever you want with the code. Perhaps improve it and share it ;).
PS: For some reason I can't understand, the code will loop indefinitely on desmume.
_________________
Nintendo DS Lite (White) + Supercard Lite + R4 + Sandisk 1 GB MicroSD
Sony PSP + Firmware 3.03 OE-A2
#117684 - GPFerror - Tue Feb 06, 2007 5:01 pm
great example SukkoPera thanks.
I tested it with an appended FCSR image to my .ds.gba in desmume 6.0 and latest no$gba and it works great, except for scrolling everything off the screen :)
FAT support in Desmume has been broken since Normatt's 3.4 release for everything I have tried in it, maybe other have had better luck with it?
Troy(GPF)
#117685 - SukkoPera - Tue Feb 06, 2007 5:18 pm
GPFerror wrote: |
I tested it with an appended FCSR image to my .ds.gba in desmume 6.0 and latest no$gba and it works great, except for scrolling everything off the screen :) |
That's why I said output sucks :).
_________________
Nintendo DS Lite (White) + Supercard Lite + R4 + Sandisk 1 GB MicroSD
Sony PSP + Firmware 3.03 OE-A2