 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
blu_eye4
Joined: 26 Oct 2008 Posts: 37
|
Posted: Mon Nov 17, 2008 7:46 am Post subject: Read all directory files... |
|
|
Hi boys :)
Is there any function whick allows me to read the files that there are in a directory? Have you understand me? I have a directory, here there are some files, I select them and play them... thank a lot for your help! |
|
| Back to top |
|
 |
sauron_le_noir
Joined: 05 Jul 2008 Posts: 229
|
Posted: Mon Nov 17, 2008 7:53 am Post subject: |
|
|
LIBC have the opendir,,readtdir,closedir function that perform this
| Code: |
1. #include <stdio.h>
2. #include <sys/types.h>
3. #include <dirent.h>
4.
5. int main()
6. {
7. struct dirent *lecture;
8. DIR *rep;
9. rep = opendir("." );
10. while ((lecture = readdir(rep))) {
11. printf("%s\n", lecture->d_name);
12. }
13. closedir(rep);
14. } |
|
|
| Back to top |
|
 |
blu_eye4
Joined: 26 Oct 2008 Posts: 37
|
Posted: Mon Nov 17, 2008 8:05 am Post subject: |
|
|
| sauron_le_noir wrote: | LIBC have the opendir,,readtdir,closedir function that perform this
| Code: |
1. #include <stdio.h>
2. #include <sys/types.h>
3. #include <dirent.h>
4.
5. int main()
6. {
7. struct dirent *lecture;
8. DIR *rep;
9. rep = opendir("." );
10. while ((lecture = readdir(rep))) {
11. printf("%s\n", lecture->d_name);
12. }
13. closedir(rep);
14. } |
|
great!!! Excuse me, if I want to select the file how do i do? thank a lot |
|
| Back to top |
|
 |
sauron_le_noir
Joined: 05 Jul 2008 Posts: 229
|
Posted: Mon Nov 17, 2008 8:37 am Post subject: |
|
|
Put the result of the readdir in a table or a linked list
and implement a GU that display it after this uset for example the up & down key
to navigate and the button X to selection.
See below a sample that perform this.
| Code: |
#include "psp_pdf.h"
#define DIRECTORY 1
#define FILE 2
struct FILECHOOSER
{
char name[32];
char path[256];
int type;
struct FILECHOOSER *droite;
struct FILECHOOSER *gauche;
} *tete = NULL;
void create_list(char *path)
{
struct FILECHOOSER *p,*q;
DIR * dir_p;
struct dirent * dir_entry_p;
dir_p = opendir( path );
if (dir_p == NULL)
{
triLogError("opendir error\n");
tete = NULL;
return;
}
while( (dir_entry_p = readdir(dir_p)) != NULL)
{
if (strcmp(dir_entry_p->d_name,".") != 0)
{
p = (struct FILECHOOSER *) triMalloc(sizeof(struct FILECHOOSER));
strcpy(p->name,dir_entry_p->d_name);
strcpy(p->path,"path");
if ( FIO_SO_ISDIR(dir_entry_p->d_stat.st_attr) )
p->type = DIRECTORY;
else p->type = FILE;
p->droite = NULL;
q->gauche = NULL;
if (tete == NULL)
tete = p;
else {
q->droite = p;
p->gauche = q;
}
q = p;
}
}
closedir( dir_p );
}
void free_liste()
{
struct FILECHOOSER *p,*q;
p = tete;
while (p != NULL)
{
q = p;
p = p -> droite;
free ( q );
}
tete = NULL;
}
void file_chooser(void)
{
struct FILECHOOSER *q,*p;
int i;
int sel;
triImage* ifolder = triImageLoad( "./icon_folder.png", 0 );
if (ifolder == 0)
{
triLogError("Error loadingi icon_folder.png!\n");
isrunning = 0;
sceKernelExitGame();
}
triImage* iback = triImageLoad( "./icon_back.png", 0 );
if (iback == 0)
{
triLogError("Error loadingi icon_back.png!\n");
isrunning = 0;
sceKernelExitGame();
}
create_list("./");
p = tete;
sel = 0;
while (isrunning)
{
q = p;
triClear( 0x000000 );
i = 0;
while( p != NULL && i < 6)
{
triFontActivate(impact16);
if (i == sel)
triFontPrint(impact16, 60.0f, 87.0f+i*32, WHITE,p->name);
else triFontPrint(impact16, 60.0f, 87.0f+i*32, BLUE,p->name);
if (p->type == DIRECTORY)
{
if (strcmp(p->name,"..") == 0)
triDrawImage2(20.0f,75.0f+i*32,iback);
else triDrawImage2(20.0f,75.0f+i*32,ifolder);
}
p = p -> droite;
i++;
}
triSwapbuffers();
triInputUpdate ();
if (triInputHeld (PSP_CTRL_UP))
if (sel > 0) sel--;
if (triInputHeld (PSP_CTRL_DOWN))
{
sel++;
if (sel > 5)
{
sel = 4;
q = q->droite;
}
}
if (triInputHeld (PSP_CTRL_CROSS))
break;
p = q;
}
triImageFree( ifolder );
triImageFree( iback );
free_liste();
strcpy(src_pdf,"test.pdf");
} |
|
|
| Back to top |
|
 |
PosX100
Joined: 15 Aug 2007 Posts: 98
|
Posted: Tue Nov 18, 2008 12:43 am Post subject: |
|
|
Take this code that i've written for a packing tool , add the artistic part/event handling , also make the function "listFiles" recursive if you want , and you're ready to go...:
| Code: |
/*Listing.c (part of Packer.c)
Author : 001xPOS
Date : Sep 07
*/
#include <stdio.h>
#include <dirent.h>
struct TEntry
{
char* e_name;
struct TEntry* next;
} TEntry;
struct TEntry *lastEntry = NULL, *entries = NULL;
void pushEntry(char* name)
{
printf("Pushing entry %s \n",name);
struct TEntry * e = malloc(sizeof(TEntry));
e->e_name = strdup(name);
e->next = NULL;
if(lastEntry)
lastEntry->next = e;
else
entries = e;
lastEntry = e;
}
void popEntries()
{
while (entries)
{
printf("Removing entry %s \n",entries->e_name);
lastEntry = entries->next;
free(entries->e_name);
free(entries);
entries = lastEntry;
}
}
void listFiles(const char* head_directory)
{
printf("Building file list \n");
struct dirent *dp = NULL;
DIR* dirp = opendir(head_directory);
while ((dp=readdir(dirp)) != NULL)
{
if((!strcmp(dp->d_name,"."))||(!strcmp(dp->d_name,"..")))continue;
pushEntry(dp->d_name);
}
closedir(dirp);
}
//lets test our list...
void iterateEntries()
{
struct TEntry* e = entries;
while (e)
{
printf("Iterating entry %s \n",e->e_name);
e = e->next;
}
}
int main()
{
listFiles("..");
iterateEntries();
popEntries();
return 0;
}
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|