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!
Read all directory files...
-
- Posts: 203
- Joined: Sat Jul 05, 2008 8:03 am
LIBC have the opendir,,readtdir,closedir function that perform this
Code: Select all
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 lotsauron_le_noir wrote:LIBC have the opendir,,readtdir,closedir function that perform thisCode: Select all
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. }
-
- Posts: 203
- Joined: Sat Jul 05, 2008 8:03 am
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.
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: Select all
#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");
}
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: Select all
/*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;
}