forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Read all directory files...

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
blu_eye4



Joined: 26 Oct 2008
Posts: 37

PostPosted: Mon Nov 17, 2008 7:46 am    Post subject: Read all directory files... Reply with quote

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
View user's profile Send private message
sauron_le_noir



Joined: 05 Jul 2008
Posts: 229

PostPosted: Mon Nov 17, 2008 7:53 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail MSN Messenger
blu_eye4



Joined: 26 Oct 2008
Posts: 37

PostPosted: Mon Nov 17, 2008 8:05 am    Post subject: Reply with quote

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
View user's profile Send private message
sauron_le_noir



Joined: 05 Jul 2008
Posts: 229

PostPosted: Mon Nov 17, 2008 8:37 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail MSN Messenger
PosX100



Joined: 15 Aug 2007
Posts: 98

PostPosted: Tue Nov 18, 2008 12:43 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
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