Example #1
0
int DirGetContents( const char *path, const char *filter, fileInfo_t *fileInfo, int maxItems )
{
	int		c, i;
	char	*ptr;
	int		numRead;
	int		index	= 0;

	if( !path || !fileInfo || !maxItems )
		return -1;

	if( (ptr = strchr( path, '/' )) == NULL ) {
		printf("DirGetContents : Invalid Path (ptr = NULL)\n");
		return -1;
	}

	c = ptr - path;

	// try to read in dir from cd/dvd
	if( !strncmp( path, "cdfs:/", c ) ) 
	{
		// just make sure we are initialized
		CD_Init();

		struct TocEntry *tocEntries = (struct TocEntry*) malloc( sizeof(struct TocEntry) * maxItems );
		
		if( !tocEntries )
			return -1;

		CDVD_FlushCache();
		numRead = CDVD_GetDir( ptr, NULL, CDVD_GET_FILES_AND_DIRS, tocEntries, maxItems, NULL );
		CDVD_Stop();

		index	= 0;
		ptr		= NULL;

		if( filter )
			ptr = (char*) malloc( strlen(filter) + 1 );

		for( i = 0; i < numRead; i++ )
		{
			if( index >= maxItems )
				break;

			if( !strcmp( tocEntries[i].filename, ".." ) || !strcmp( tocEntries[i].filename, "." ) )
				continue;

			// check for filters
			c = 1;

			if( filter && !(tocEntries[i].fileProperties & FLAG_DIRECTORY) ) 
			{
				strcpy( ptr, filter );

				c = 0;
				char *token = strtok( ptr, " " );

				while( token ) {
					// found matching extension
					if( CmpFileExtension( tocEntries[i].filename, token ) ) {
						c = 1;
						break;
					}

					token = strtok( NULL, " " );
				}
			}

			if( c == 1 )
			{
				strncpy( fileInfo[index].name, tocEntries[i].filename, sizeof(fileInfo[index].name) );

				fileInfo[index].size	= tocEntries[i].fileSize;
				fileInfo[index].flags	= tocEntries[i].fileProperties;

				index++;
			}
		}

		if( ptr )
			free(ptr);

		free(tocEntries);
	}
	else if( !strncmp( path, "pfs", 3 ) )
	{
		// try to read in dir from hdd
		int hDir = fileXioDopen( path );
		int nRet;
		iox_dirent_t dirEntry;

		if( hDir < 0 )
			return -1;

		index	= 0;
		ptr		= NULL;

		if( filter )
			ptr = (char*) malloc( strlen(filter) + 1 );

		do {
			if( !(nRet = fileXioDread( hDir, &dirEntry )) )
				break;

			if(!strcmp( dirEntry.name, "." ) || !strcmp( dirEntry.name, ".."))
				continue;

			if( index >= maxItems )
				break;

			if( FIO_S_ISDIR(dirEntry.stat.mode) )
				fileInfo[index].flags = FLAG_DIRECTORY;
			else
				fileInfo[index].flags = 0;

			// check for filters
			c = 1;

			if( filter && !(fileInfo[index].flags & FLAG_DIRECTORY) )
			{
				strcpy( ptr, filter );

				c = 0;
				char *token = strtok( ptr, " " );

				while( token ) {
					// found matching extension
					if( CmpFileExtension( dirEntry.name, token ) ) {
						c = 1;
						break;
					}

					token = strtok( NULL, " " );
				}
			}
			
			if( c == 1 )
			{
				strncpy( fileInfo[index].name, dirEntry.name, sizeof(fileInfo[index].name) );
				fileInfo[index].size = dirEntry.stat.size;

				index++;
			}

		} while( nRet > 0 );

		if( ptr )
			free(ptr);

		fileXioDclose( hDir );
	}
	else if( !strncmp( path, "mc0:/", c ) || !strncmp( path, "mc1:/", c ) )
	{
		// try to read in dir from memory card
		int		nPort;
		char	mcPath[256];
		mcTable mcEntries[MAX_DIR_FILES] __attribute__((aligned(64)));

		if( !strncmp( path, "mc0:/", c ) )
			nPort = 0;
		else
			nPort = 1;

		strcpy( mcPath, ptr );
		strcat( mcPath, "*" );

		mcGetDir( nPort, 0, mcPath, 0, MAX_DIR_FILES, mcEntries );
		mcSync( 0, NULL, &numRead );

		index	= 0;
		ptr		= NULL;

		if( filter )
			ptr = (char*) malloc( strlen(filter) + 1 );

		for( i = 0; i < numRead; i++ )
		{
			if( index >= maxItems )
				break;

			if( !strcmp( mcEntries[i].name, "." ) || !strcmp( mcEntries[i].name, "..") )
				continue;

			if( mcEntries[i].attrFile & MC_ATTR_SUBDIR )
				fileInfo[index].flags = FLAG_DIRECTORY;
			else
				fileInfo[index].flags = 0;

			// check for filters
			c = 1;

			if( filter && !(mcEntries[i].attrFile & MC_ATTR_SUBDIR) )
			{
				strcpy( ptr, filter );

				c = 0;
				char *token = strtok( ptr, " " );

				while( token ) {
					// found matching extension
					if( CmpFileExtension( mcEntries[i].name, token ) ) {
						c = 1;
						break;
					}

					token = strtok( NULL, " " );
				}
			}

			if( c == 1 )
			{
				strncpy( fileInfo[index].name, mcEntries[i].name, sizeof(fileInfo[index].name) );

				fileInfo[index].size	= mcEntries[i].fileSizeByte;

				index++;
			}
		}

		if( ptr )
			free(ptr);
	}
	else if( !strncmp( path, "mass:/", c ) ) 
	{
		// try to read in dir from USB device
		int nRet;
		fat_dir_record dirEntry;

		// returns number of entries in directory
		nRet = usb_mass_getFirstDirentry( ptr, &dirEntry );

		index	= 0;
		ptr		= NULL;

		if( filter )
			ptr = (char*) malloc( strlen(filter) + 1 );

		// loop through all entries in directory
		while( nRet > 0 ) {

			if(!strcmp( dirEntry.name, "." ) || !strcmp( dirEntry.name, "..")) {
				nRet = usb_mass_getNextDirentry(&dirEntry);
				continue;
			}

			// ignore volume label
			if( dirEntry.attr & USB_VOLUME ) {
				nRet = usb_mass_getNextDirentry(&dirEntry);
				continue;
			}

			if( index >= maxItems )
				break;

			if( dirEntry.attr & USB_DIRECTORY )
				fileInfo[index].flags = FLAG_DIRECTORY;
			else
				fileInfo[index].flags = 0;

			// check for filters
			c = 1;

			if( filter && !(fileInfo[index].flags & FLAG_DIRECTORY) )
			{
				strcpy( ptr, filter );

				c = 0;
				char *token = strtok( ptr, " " );

				while( token ) {
					// found matching extension
					if( CmpFileExtension( dirEntry.name, token ) ) {
						c = 1;
						break;
					}

					token = strtok( NULL, " " );
				}
			}
			
			if( c == 1 )
			{
				strncpy( fileInfo[index].name, dirEntry.name, sizeof(fileInfo[index].name) );
				fileInfo[index].size = dirEntry.size;

				index++;
			}

			nRet = usb_mass_getNextDirentry( &dirEntry );
		}

		if( ptr )
			free(ptr);
	}
	else if( !strncmp( path, "smb", 3 ) )
	{
		// read from a samba share
		int hDir = smbc_opendir( path );
		const struct smbc_dirent *dirEntry;

		if( hDir < 0 )
			return -1;

		index	= 0;
		ptr		= NULL;

		if( filter )
			ptr = (char*) malloc( strlen(filter) + 1 );

		while( (dirEntry = smbc_readdir( hDir )) != NULL )
		{
			if(!strcmp( dirEntry->name, "." ) || !strcmp( dirEntry->name, ".."))
				continue;

			if( index >= maxItems )
				break;

			if( dirEntry->smbc_type == SMBC_DIR )
				fileInfo[index].flags = FLAG_DIRECTORY;
			else
				fileInfo[index].flags = 0;

			// check for filters
			c = 1;

			if( filter && !(fileInfo[index].flags & FLAG_DIRECTORY) )
			{
				strcpy( ptr, filter );

				c = 0;
				char *token = strtok( ptr, " " );

				while( token ) {
					// found matching extension
					if( CmpFileExtension( dirEntry->name, token ) ) {
						c = 1;
						break;
					}

					token = strtok( NULL, " " );
				}
			}
			
			if( c == 1 )
			{
				strncpy( fileInfo[index].name, dirEntry->name, sizeof(fileInfo[index].name) );
				fileInfo[index].size = 0; // fixme

				index++;
			}

		};

		if( ptr )
			free(ptr);

		smbc_closedir( hDir );
	}

	return index;
}
Example #2
0
/****************************************************************************
 * This function will read the contents of a directory and store the		*
 * contents in the folder structure.  The ext parameter works as a filter	* 
 * to read in only certain file extension types.  After reading the			* 
 * directory, the function will sort the contents.							* 
 ****************************************************************************/
int readDirectory(char *ext, int media)
{
	int ret = 1;
	iox_dirent_t directory;
	int size;
	char folderName[255];
	unsigned int numToc, index;
	char *extcmp;
	folder.fIndex = 0;
	struct TocEntry cdDirectory[255];
	size = strlen(ext);

	switch (media)
	{
	case 0:
		{
			while (ret > 0)
			{
				ret = fileXioDread(folder.iDir, &directory);
				if (ret > 0)
				{
					if (FIO_S_ISDIR(directory.stat.mode))  //is a directory
					{
						strcpy(folder.object[folder.fIndex].name, "");
						strcat(folder.object[folder.fIndex].name, "/");
						strcat(folder.object[folder.fIndex].name, directory.name);
						folder.object[folder.fIndex].type = 0;
						folder.object[folder.fIndex].count = folder.fIndex;
						folder.fIndex++;
					}
					else if (FIO_S_ISREG(directory.stat.mode)) //is a file
					{
						if (size > ret)
							size = 0;
						extcmp = &directory.name[ret-size];
						if (strcasecmp(extcmp, ext) == 0)
						{
							strcpy(folder.object[folder.fIndex].name, directory.name);
							folder.object[folder.fIndex].type = 1;
							folder.object[folder.fIndex].count = folder.fIndex;
							folder.fIndex++;
						}
					}
				}
			}
			folder.fMax = folder.fIndex;
			folder.fIndex = 0;
			sortFolder();
			for (ret = 0; ret < folder.fMax; ret++)
			{
				if (folder.object[ret].type == 0)
				{
					strcpy(folderName, &folder.object[ret].name[1]);
					strcpy(folder.object[ret].name, folderName);
				}
			}
			closeDirectory(MODE_HDD);
			return folder.fMax;
		}
	case 1:
		{
			numToc = CDVD_GetDir(&folder.directory[5], NULL, CDVD_GET_FILES_AND_DIRS, cdDirectory, 254, NULL);
			CDVD_Stop();

#define CD_S_ISDIR(x) x & 2
#define CD_S_ISFILE(x) !CD_S_ISDIR(x)

			for (index=0; index<numToc; index++)
			{
				if (CD_S_ISDIR(cdDirectory[index].fileProperties)) //is a folder
				{
					strcpy(folder.object[folder.fIndex].name, "");
					strcat(folder.object[folder.fIndex].name, "/");
					strcat(folder.object[folder.fIndex].name, cdDirectory[index].filename);
					folder.object[folder.fIndex].type = 0;
					folder.object[folder.fIndex].count = folder.fIndex;
					folder.fIndex++;
				}
				else
				{
					ret = strlen(cdDirectory[index].filename);
					if (size > ret)
						size = 0;
					extcmp = &cdDirectory[index].filename[ret-size];
					if (strcasecmp(extcmp, ext) == 0)
					{
						strcpy(folder.object[folder.fIndex].name, cdDirectory[index].filename);
						folder.object[folder.fIndex].type = 1;
						folder.object[folder.fIndex].count = folder.fIndex;
						folder.fIndex++;
					}
				}
			}
			folder.fMax = folder.fIndex;
			folder.fIndex = 0;
			sortFolder();
			for (ret = 0; ret < folder.fMax; ret++)
			{
				if (folder.object[ret].type == 0)
				{
					strcpy(folderName, &folder.object[ret].name[1]);
					strcpy(folder.object[ret].name, folderName);
				}
			}
			return folder.fMax;
		}
	}
	return 0;
}