s32 __Menu_RetrieveList(void) { fatDevice *dev = &deviceList[device]; DIR_ITER *dir = NULL; struct stat filestat; char dirpath[128], filename[1024]; u32 cnt = 0, len; /* Generate dirpath */ sprintf(dirpath, "%s:" WAD_DIRECTORY, dev->mount); /* Open directory */ dir = diropen(dirpath); if (!dir) return -1; /* Count entries */ while (!dirnext(dir, filename, &filestat)) { if (!(filestat.st_mode & S_IFDIR)) cnt++; } /* Reset directory */ dirreset(dir); /* Buffer length */ len = sizeof(fatFile) * cnt; /* Allocate memory */ fileList = (fatFile *)realloc(fileList, len); if (!fileList) { dirclose(dir); return -2; } /* Reset file counter */ fileCnt = 0; /* Get entries */ while (!dirnext(dir, filename, &filestat)) { if (!(filestat.st_mode & S_IFDIR)) { fatFile *file = &fileList[fileCnt++]; /* Copy file info */ strcpy(file->filename, filename); file->filestat = filestat; } } /* Close directory */ dirclose(dir); /* Sort list */ qsort(fileList, fileCnt, sizeof(fatFile), __Menu_EntryCmp); return 0; }
s32 __Menu_RetrieveList(char *inPath, fatFile **outbuf, u32 *outlen) { fatFile *buffer = NULL; DIR_ITER *dir = NULL; struct stat filestat; //char dirpath[256], filename[768]; u32 cnt; /* Generate dirpath */ //sprintf(dirpath, "%s:" WAD_DIRECTORY, fdev->mount); /* Open directory */ dir = diropen(inPath); if (!dir) return -1; /* Count entries */ for (cnt = 0; !dirnext(dir, gFileName, &filestat);) { // if (!(filestat.st_mode & S_IFDIR)) // wiiNinja cnt++; } if (cnt > 0) { /* Allocate memory */ buffer = malloc(sizeof(fatFile) * cnt); if (!buffer) { dirclose(dir); return -2; } /* Reset directory */ dirreset(dir); /* Get entries */ for (cnt = 0; !dirnext(dir, gFileName, &filestat);) { bool addFlag = false; if (filestat.st_mode & S_IFDIR) // wiiNinja { // Add only the item ".." which is the previous directory // AND if we're not at the root directory if ((strcmp (gFileName, "..") == 0) && (gDirLevel > 1)) addFlag = true; else if (strcmp (gFileName, ".") != 0) addFlag = true; } else { if(strlen(gFileName)>4) { if(!stricmp(gFileName+strlen(gFileName)-4, ".wad")) addFlag = true; } } if (addFlag == true) { fatFile *file = &buffer[cnt++]; /* File name */ strcpy(file->filename, gFileName); /* File stats */ file->filestat = filestat; } } /* Sort list */ qsort(buffer, cnt, sizeof(fatFile), __Menu_EntryCmp); } /* Close directory */ dirclose(dir); /* Set values */ *outbuf = buffer; *outlen = cnt; return 0; }
/** Get the file structure at dirname * @param dirname Directory to check * @return Echo_file structure, with the file names of that directory */ echo_files* get_files(const char* dirname) { /// Open the directory DIR_ITER* dir = diropen(dirname); /// It exists... if(dir != NULL) { /// New file structure echo_files* ret = new(echo_files); /// Set fields ret->num_files = 0; ret->current_dir = const_cast<char*>(dirname); ret->num_dir = 0; /// Set stats struct stat each_ent; /// Buffer to save file names char* filename = new char[MAXPATHLEN]; while(dirnext(dir, filename, &each_ent) == 0) { /// If the file is not "." if(strcmp(filename, ".")) { /// Increment number of files (both directories and regular files) ret->num_files++; /// If it's a directory... if(S_ISDIR(each_ent.st_mode)) /// ...increment the number of directories ret->num_dir++; } } /// Make a new array ret->file_names = new char*[ret->num_files]; /// First file always ".." ret->file_names[0] = ".."; /** each_dir is the directory index (1 because ".." is already filled) * each_file is the regular file index * each is one of them */ int each_dir = 1, each_file = ret->num_dir, each = 1; /// Rewind back dirreset(dir); /// For each file entry while(dirnext(dir, filename, &each_ent) == 0) { /// If it's not "." or ".." if(strcmp(filename, ".") && strcmp(filename, "..")) { /// If it's a directory... if(S_ISDIR(each_ent.st_mode)) { /// Use the directory index each = each_dir; each_dir++; } /// Else, it's a reg. file... else { /// Use the reg. file index each = each_file; each_file++; } /// Copy the file name over ret->file_names[each] = new char[strlen(filename) + 1]; memset(ret->file_names[each], 0 , strlen(filename) + 1); strcpy(ret->file_names[each], filename); } } /// Delete the buffer delete[] filename; /// Close the directory dirclose(dir); /// Sort the directories qsort(ret->file_names, ret->num_dir, sizeof(char*), cmp); /// Sort the reg. files qsort(ret->file_names + ret->num_dir , ret->num_files - ret->num_dir, sizeof(char*), cmp); /// Return files return(ret); } return(NULL); }
//Creates the random music array (fist time through) and retrieves the // filepath of the next music file to play. void get_random_file(char *filetype, char *path) { DIR_ITER *dir = NULL; char filename[1024]; int cnt = 0; int next = 0; struct stat filestat; if (fileCount==0 && !first_time) goto out; dbg_printf(gt("Music: Looking for %s files in: %s"), filetype, path); dbg_printf("\n"); // Open directory //snprintf(dirname, sizeof(dirname), "%s", path); dir = diropen(path); if (!dir) return; if (first_time) { while (!dirnext(dir, filename, &filestat)) { // Ignore invalid entries if (match_ext(filename, filetype) && !(filestat.st_mode & S_IFDIR)) fileCount++; } dbg_printf(gt("Music: Number of %s files found: %i"), filetype, fileCount); dbg_printf("\n"); first_time = false; //if no files found then no need to continue if (fileCount==0) goto out; //allocate the random music array musicArray = realloc(musicArray, fileCount * sizeof(int)); if (!musicArray) { fileCount = 0; goto out; } initializeArray(musicArray, fileCount); randomizeArray(musicArray, fileCount); //check array contents int i; dbg_printf(gt("Music: musicArray contents: ")); for (i=0; i<fileCount; i++) dbg_printf("%i ", musicArray[i]); dbg_printf("\n"); //reset the directory dirreset(dir); } if (fileCount > 0) { //get the next file index lastPlayed++; if (lastPlayed > fileCount-1) lastPlayed = 0; next = musicArray[lastPlayed]; dbg_printf(gt("Music: Next file index to play: %i"), next); dbg_printf("\n"); //iterate through and find our file while (!dirnext(dir, filename, &filestat)) { if (match_ext(filename, filetype) && !(filestat.st_mode & S_IFDIR)) { cnt++; if (cnt==next) { //save path snprintf(music_fname, sizeof(music_fname), "%s/%s", path, filename); goto out; } } } } out:; //close the directory dirclose(dir); }
int main(void) { // init PA_Lib PA_Init(); PA_InitVBL(); PA_InitText(1, 0); PA_InitText(0, 0); // init EFSlib & libfat if(EFS_Init(EFS_AND_FAT | EFS_DEFAULT_DEVICE, NULL)) { PA_OutputSimpleText(1, 0, 0, "EFS init ok"); PA_OutputText(1, 0, 1, "found NDS path: %s", efs_path); DIR_ITER* dir; struct stat st; s8 nb; FILE* file; u8* buffer; int i, size; // open a text file and read its contents file = fopen("/test.txt", "rb"); if(file != NULL) { // get file size using stat stat("/test.txt", &st); size = st.st_size; buffer = (u8*)malloc(size); fread(buffer, 1, size, file); buffer[size-1] = '\0'; PA_OutputText(1, 0, 4, "/test.txt content: '%s'", buffer); PA_OutputText(1, 10, 5, "size: %d bytes", size); free(buffer); fclose(file); } // open another file, read its content file = fopen("/folder/test.txt", "rb"); if(file != NULL) { // get file size fseek(file, 0, SEEK_END); size = ftell(file); fseek(file, 0, SEEK_SET); buffer = (u8*)malloc(size); fread(buffer, 1, size, file); buffer[size-1] = '\0'; PA_OutputText(1, 0, 7, "/folder/test.txt content:\n%s", buffer); free(buffer); fclose(file); } // reopen the file, modify its content file = fopen("/folder/test.txt", "rb+"); if(file != NULL) { nb = fwrite("16b Written OK!", 1, 16, file); PA_OutputText(1, 0, 10, "write test done! : %d bytes", nb); fclose(file); } // reopen another file, read its content again, 1 byte at a time file = fopen("/folder/dummy/.././test.txt", "rb"); // funky path to test relative path parsing if(file != NULL) { // get file size fseek(file, 0, SEEK_END); size = ftell(file); fseek(file, 0, SEEK_SET); buffer = (u8*)malloc(size); i = 0; while(i < size) { fread(&buffer[i], 1, 1, file); i++; } buffer[size-1] = '\0'; PA_OutputText(1, 0, 12, "/folder/test.txt new content:\n%s", buffer); free(buffer); fclose(file); } PA_OutputSimpleText(1, 0, 15, "Press A for directory tests."); PA_WaitFor(Pad.Newpress.A); // open root directory then list its content PA_OutputText(0, 0, 0, "Listing '/' directory:"); dir = diropen("."); if(dir != NULL) { i = 0; buffer = (u8*)malloc(EFS_MAXNAMELEN); while(!(dirnext(dir, (char*)buffer, &st))) { if(st.st_mode & S_IFDIR) PA_OutputText(0, 0, i++, "DIR : %s", buffer); else PA_OutputText(0, 0, i++, "FILE: %s, %d bytes", buffer, st.st_size); } PA_OutputSimpleText(0, 0, i++, "end of directory."); PA_OutputSimpleText(0, 0, i++, "directory reset, first file is:"); dirreset(dir); dirnext(dir, (char*)buffer, &st); if(st.st_mode & S_IFDIR) PA_OutputText(0, 0, i++, "DIR : %s", buffer); else PA_OutputText(0, 0, i++, "FILE: %s, %d bytes", buffer, st.st_size); dirclose(dir); free(buffer); } // chdir to a directory then list its content PA_OutputSimpleText(0, 0, i++, "Listing '/list/' directory:"); chdir("/list/"); dir = diropen("./"); if(dir != NULL) { buffer = (u8*)malloc(EFS_MAXNAMELEN); while(!(dirnext(dir, (char*)buffer, &st))) { if(st.st_mode & S_IFDIR) PA_OutputText(0, 0, i++, "DIR : %s", buffer); else PA_OutputText(0, 0, i++, "FILE: %s, %d bytes", buffer, st.st_size); } PA_OutputSimpleText(0, 0, i++, "end of directory."); dirclose(dir); free(buffer); } } else { PA_OutputSimpleText(1, 0, 0, "EFS init error!"); } while(1) { PA_WaitForVBL(); } return 0; }