Read all directory files...

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
blu_eye4
Posts: 37
Joined: Sun Oct 26, 2008 8:41 pm

Read all directory files...

Post by blu_eye4 »

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!
sauron_le_noir
Posts: 203
Joined: Sat Jul 05, 2008 8:03 am

Post by sauron_le_noir »

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&#40;&#41;
   6. &#123;
   7.     struct dirent *lecture;
   8.     DIR *rep;
   9.     rep = opendir&#40;"." &#41;;
  10.     while &#40;&#40;lecture = readdir&#40;rep&#41;&#41;&#41; &#123;
  11.         printf&#40;"%s\n", lecture->d_name&#41;;
  12.     &#125;
  13.     closedir&#40;rep&#41;;
  14. &#125;
blu_eye4
Posts: 37
Joined: Sun Oct 26, 2008 8:41 pm

Post by blu_eye4 »

sauron_le_noir wrote: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&#40;&#41;
   6. &#123;
   7.     struct dirent *lecture;
   8.     DIR *rep;
   9.     rep = opendir&#40;"." &#41;;
  10.     while &#40;&#40;lecture = readdir&#40;rep&#41;&#41;&#41; &#123;
  11.         printf&#40;"%s\n", lecture->d_name&#41;;
  12.     &#125;
  13.     closedir&#40;rep&#41;;
  14. &#125;
great!!! Excuse me, if I want to select the file how do i do? thank a lot
sauron_le_noir
Posts: 203
Joined: Sat Jul 05, 2008 8:03 am

Post by sauron_le_noir »

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: Select all

#include "psp_pdf.h"

#define  DIRECTORY 1
#define  FILE      2

struct FILECHOOSER
&#123;
   char name&#91;32&#93;;
   char path&#91;256&#93;;
   int type;
   struct FILECHOOSER *droite;
   struct FILECHOOSER *gauche;
&#125; *tete = NULL;

void create_list&#40;char *path&#41;
&#123;

   struct FILECHOOSER *p,*q;
   
   DIR * dir_p;
   struct dirent * dir_entry_p;

   dir_p = opendir&#40; path &#41;;
   if &#40;dir_p == NULL&#41;
     &#123;
       triLogError&#40;"opendir error\n"&#41;;
       tete = NULL;
       return;
     &#125;
   while&#40; &#40;dir_entry_p = readdir&#40;dir_p&#41;&#41; != NULL&#41;
   &#123;
      if &#40;strcmp&#40;dir_entry_p->d_name,"."&#41; != 0&#41;
        &#123;
          p = &#40;struct FILECHOOSER *&#41; triMalloc&#40;sizeof&#40;struct FILECHOOSER&#41;&#41;;
          strcpy&#40;p->name,dir_entry_p->d_name&#41;;
          strcpy&#40;p->path,"path"&#41;;
          if &#40; FIO_SO_ISDIR&#40;dir_entry_p->d_stat.st_attr&#41; &#41;
                p->type = DIRECTORY;
          else  p->type = FILE;
          p->droite = NULL;
          q->gauche = NULL;
          if &#40;tete == NULL&#41;
               tete = p;
          else &#123;
                 q->droite = p;
                 p->gauche = q;
               &#125;
          q = p;
       &#125;
   &#125;
  closedir&#40; dir_p &#41;;
&#125;

void free_liste&#40;&#41;
&#123;

  struct FILECHOOSER *p,*q;
  p = tete;
  while &#40;p != NULL&#41;
  &#123;
     q = p;
     p = p -> droite;
     free &#40; q &#41;;
  &#125;
  tete = NULL;
&#125;

void file_chooser&#40;void&#41;
&#123;
  struct FILECHOOSER *q,*p;
  int i;
  int sel;

  triImage* ifolder = triImageLoad&#40; "./icon_folder.png", 0 &#41;;
  if &#40;ifolder == 0&#41;
   &#123;
     triLogError&#40;"Error loadingi icon_folder.png!\n"&#41;;
     isrunning = 0;
     sceKernelExitGame&#40;&#41;; 
   &#125; 
  triImage* iback = triImageLoad&#40; "./icon_back.png", 0 &#41;;
  if &#40;iback == 0&#41;
   &#123;
     triLogError&#40;"Error loadingi icon_back.png!\n"&#41;;
     isrunning = 0;
     sceKernelExitGame&#40;&#41;; 
   &#125;  
   create_list&#40;"./"&#41;;
   p = tete;
   sel = 0;
   while &#40;isrunning&#41;
   &#123;
      q = p;    
      triClear&#40; 0x000000 &#41;;
      i = 0;
      while&#40; p != NULL  && i < 6&#41;
      &#123;  
         triFontActivate&#40;impact16&#41;;
         if &#40;i == sel&#41;
                triFontPrint&#40;impact16, 60.0f, 87.0f+i*32, WHITE,p->name&#41;;  
           else triFontPrint&#40;impact16, 60.0f, 87.0f+i*32, BLUE,p->name&#41;;

         if &#40;p->type == DIRECTORY&#41;
           &#123;
             if &#40;strcmp&#40;p->name,".."&#41; == 0&#41;
                  triDrawImage2&#40;20.0f,75.0f+i*32,iback&#41;;
             else triDrawImage2&#40;20.0f,75.0f+i*32,ifolder&#41;; 
           &#125;
         p = p -> droite;
         i++;
      &#125;

      triSwapbuffers&#40;&#41;;
      triInputUpdate &#40;&#41;;
 
      if &#40;triInputHeld &#40;PSP_CTRL_UP&#41;&#41;
        if &#40;sel > 0&#41; sel--;
       
      if &#40;triInputHeld &#40;PSP_CTRL_DOWN&#41;&#41;
        &#123;
          sel++;
          if &#40;sel > 5&#41;
             &#123;
                sel = 4;
                q = q->droite;
             &#125;
        
        &#125;
      if &#40;triInputHeld &#40;PSP_CTRL_CROSS&#41;&#41;
        break;
      p = q;    
     
    &#125;
  triImageFree&#40; ifolder &#41;; 
  triImageFree&#40; iback &#41;; 
  free_liste&#40;&#41;;
  strcpy&#40;src_pdf,"test.pdf"&#41;;
&#125;
PosX100
Posts: 98
Joined: Wed Aug 15, 2007 1:02 am

Post by PosX100 »

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 &#40;part of Packer.c&#41; 
   Author &#58; 001xPOS
   Date    &#58; Sep 07
*/

#include <stdio.h>
#include <dirent.h>

struct TEntry
&#123;
    char* e_name;
    struct TEntry* next;
&#125; TEntry;

struct TEntry *lastEntry = NULL, *entries = NULL;

void pushEntry&#40;char* name&#41;
&#123;
    printf&#40;"Pushing entry %s \n",name&#41;;
    struct TEntry	* e = malloc&#40;sizeof&#40;TEntry&#41;&#41;;

    e->e_name = strdup&#40;name&#41;;
    e->next   = NULL;

    if&#40;lastEntry&#41;
        lastEntry->next = e;
    else
        entries  = e;

    lastEntry = e;
&#125;

void popEntries&#40;&#41;
&#123;
	while &#40;entries&#41;
	&#123;
	    printf&#40;"Removing entry %s \n",entries->e_name&#41;;
		lastEntry = entries->next;
		free&#40;entries->e_name&#41;;
		free&#40;entries&#41;;
		entries = lastEntry;
	&#125;
&#125;

void listFiles&#40;const char* head_directory&#41;
&#123;
    printf&#40;"Building file list \n"&#41;;
	struct dirent *dp = NULL;
	DIR*           dirp = opendir&#40;head_directory&#41;;

    while &#40;&#40;dp=readdir&#40;dirp&#41;&#41; != NULL&#41;
    &#123;
        if&#40;&#40;!strcmp&#40;dp->d_name,"."&#41;&#41;||&#40;!strcmp&#40;dp->d_name,".."&#41;&#41;&#41;continue;
        pushEntry&#40;dp->d_name&#41;;
    &#125;
    closedir&#40;dirp&#41;;
&#125;

//lets test our list...
void iterateEntries&#40;&#41;
&#123;
    struct TEntry* e = entries;
	while &#40;e&#41;
	&#123;
        printf&#40;"Iterating entry %s \n",e->e_name&#41;;
		e = e->next;
	&#125;
&#125;

int main&#40;&#41;
&#123;
    listFiles&#40;".."&#41;;
    iterateEntries&#40;&#41;;
    popEntries&#40;&#41;;
    return 0;
&#125;
Post Reply