gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Problem with PAlib and libfat...

#152985 - SnakerDLK - Sun Mar 23, 2008 7:07 pm

Hi,
Im using PAlib Community Release 080203 and...
the following code has and error...
the line with the error is
Code:
/*THIS IS THE LINE !*/while(!dirnext(dir, item, &fileStat))

(line 129...)

when the line is commented, the program keeps prefectly in loop, in the main function...

but when uncommented, the program freezes on the line
Code:
   debug("exiting");
   return OK;
}


where OK is defined as 0.

here is the 'full' code:

Code:
//INCLUDES

// Include for PA_Lib
#include <PA9.h>

//Includes for fat lib
#include <fat.h>
#include <sys/dir.h>

//DEFINES
#define MENU_END 0
#define MENU_SELECTABLE 1
#define MENU_NOT_SELECTABLE 2
#define MENU_MAX_LINES 18
#define MENU_SYMBOL "*"
#define MENU_MAX_NAME 31

#define BROWSETYPE_FOLDER 0
#define BROWSETYPE_FILE 1
#define BROWSE_NAME_SIZE 100
#define BROWSE_CURRENT_DIR "."
#define BROWSE_SOURCE_DIR ".."

#define OK 0
#define ERROR_OPEN_DIR 1
#define INPUT_ERROR 2
#define ERROR_ALLOCATING_VAR 3
#define LIMIT_ERROR 4
#define CANCEL 5
#define BUG 666;

//TYPEDEFS
//file list struct...prev could be removed...
typedef struct browseTypeStruct{
   char name[MENU_MAX_NAME+1];
   unsigned char type;
   struct browseTypeStruct *next;
   struct browseTypeStruct *prev;
}browseType;

//PROTOS
void debug(char *input);
browseType *getBrowseItem(browseType *first, unsigned num);
int browseFile(char *home, char *path, unsigned char browseType);

//FUNCTIONS
void debug(char *input)
{
   unsigned char counter;
   
   //check if 'input' fits on the screen...
   if(strlen(input) > 31)
   {
      debug("DEBUG SIZE EXCEEDED");
   }else{
      counter = 31 - strlen(input);
      PA_Print(1, "%s", input);
      
      //this is for another bug... the text was being garbled if I dont specifically set it
      while(counter)
      {
         PA_Print(1, " ");
         counter--;
      }
      PA_Print(1, "\n");
   }
   
   //there was a time where the upper screen just went black..so this is a timer to stop the program for a few seconds
   //for the user to be able to read the log... just for debugging purpouses...
   counter = 20;
   while(counter)
   {
      PA_WaitForVBL();
      counter--;
   }
}

browseType *getBrowseItem(browseType *first, unsigned num)
{
   browseType *current;
   unsigned counter;
   
   current = first;
   for(counter = 0; ((counter < num)&&(current != NULL)); counter ++)
   {
      current = current->next;
   }
   return current;
}

int browseFile(char *home, char *pathOut, unsigned char browseForType)
{
   struct stat fileStat;                //to check wether the item is a file or folder.
   browseType *first = NULL, *current = NULL, *new;   //create the file list.
   char item[BROWSE_NAME_SIZE+1];            //not really needed..but without this I would
                        //have to allocate the first list item before checking the
                        //first item...whatever...
   
   unsigned offset, choice, counter;    //offset and choice are explained later... counter is a loop counter...
   unsigned lastItem = 0;         //used to limit the offset+choice...see later.
   unsigned char waiting;         //status... if the menu needs to be reput or the file list updated...
   char path[256];            //I was using the 'pathOut' var but the program was resetting it...see later.
   
   //check for possible programming errors...who knows...
   if((home == NULL)||(path == NULL))
      return INPUT_ERROR;
      
   //to open the home dir
   //well...this does not limit the user to home dir and subdirs... just to put a place to start browsing.
   strcpy(path, home);
   
   
   waiting = 1;
   while(waiting)
   {   
      
      //get file list for path directory
      DIR_ITER* dir = diropen(path);
      if(dir == NULL)
      {
         debug("Error opening dir...");
         return ERROR_OPEN_DIR;
      }
      
      debug("Directory opened:");
      debug(path);
      
      //create dir list

/*THIS IS THE LINE !*/while(!dirnext(dir, item, &fileStat))

      {
      }
      
      debug("Created list of directories");
      
      //close dir
      dirclose (dir);
      debug("Directory closed");
      
      {
      }
      
      debug("Going to free list");
      
      //free dir list
      current = first;
      while(current != NULL)
      {
         first = current->next;
         free(current);
         current = first;
      }
      
      first = current = new = NULL;
      
      debug("File list freed");
      
      waiting = 0;
   }
   
   debug(path);
   strcpy(pathOut, path);
   debug(pathOut);
   debug("exiting");
   return OK;
}

// Function: main()
int main(void)
{
   int option;
   char path[256], home[256];
   PA_Init();    // Initializes PA_Lib
   PA_InitVBL(); // Initializes a standard VBL
   
   PA_InitText(0, 0);
   PA_InitText(1, 1);
   
   debug("Palib init");
   
   //from the example...did not try to remove the following lines yet...
   PA_WaitForVBL(); 
   PA_WaitForVBL(); 
   PA_WaitForVBL();
   
   if(fatInitDefault())
      debug("Libfat init");
   else{
      debug("Libfat ERROR...");
      debug("reboot !");
      PA_WaitForVBL();
   }
   
   // Infinite loop to keep the program running
   while (1)
   {
      strcpy(home, "/");
      //did no want to use browseFile("/", path, BROWSETYPE_FILE);
      //but would not make a diference...probably
      option = browseFile(home, path, BROWSETYPE_FILE);
      
      //just to know if you escaped the function... I never did U_U
      debug("out..");
      if(!option)
         PA_Print(1, "path \"%s\"\n", path);
      else
         PA_Print(1, "ERROR %d\n", option);
      PA_WaitForVBL();
   }
   
   return 0;
} // End of main()


since the palib forum is down... and their 'new' forum is quite deserted...
well, if anyone can help, I appreciate it ^^

(well...perhaps if someone could compile this with the stable release of palib... or tell me where I can find a function that allows browsing for files/folders...)