⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filer.c

📁 psp上的reader 这个是以月光老大的cnreader为基础 增加了zip文件浏览功能
💻 C
字号:
#include "main.h"


#define MAX_ENTRY 1024
#define MAXPATH   256

#define LIST_CX 10
#define LIST_CY 128
#define LIST_COUNT 13
#define LIST_HALFCOUNT (LIST_COUNT/2)
#define LIST_ROWHEIGHT 18

typedef struct _stExt{
	char *szExt;
	int nExtId;
} STEXT;

STEXT stExtentions[3]= {
	"txt",EXT_TXT,
	"zip",EXT_ZIP,
	NULL, EXT_UNKNOWN
};

SceIoDirent files[MAX_ENTRY];
int nfiles;
int selfile;

//return file 's size 
long searchFile(const char *path) 
{
	int fd;
	long ret=0;

	char *p;
	char tname[256];
	strcpy(tname,path);
	p = strrchr(tname,'?');
	if(p)
	{
		//zip file search
		*p=0;p++;
		unzFile hUnzip = unzOpen(tname);
		if (!hUnzip)	return 0;

		if (unzLocateFile(hUnzip,p, 1) == UNZ_OK)
		{
			unz_file_info fileInfo;
			if (unzGetCurrentFileInfo(hUnzip, &fileInfo, NULL, 0, NULL, 0, NULL, 0) != UNZ_OK)
			{
				return 0;
			}
			ret = fileInfo.uncompressed_size;
		}

		unzClose(hUnzip);
	}
	else
	{
		p = strrchr(tname,'/');
		*p=0;

		fd = sceIoDopen(tname);
		nfiles = 0;
		while(nfiles<MAX_ENTRY){
			if(sceIoDread(fd, &files[nfiles])<=0) break;
			if(!stricmp(files[nfiles].d_name,p+1))
			{
				ret=files[nfiles].d_stat.st_size;
				break;
			}
			nfiles++;
		}
		sceIoDclose(fd);
	}
	return ret;
}

int getExtId(const char *szFilePath) {
	char *pszExt;
	int i;
	if((pszExt = strrchr(szFilePath, '.'))) {
		pszExt++;
		for (i = 0; stExtentions[i].nExtId != EXT_UNKNOWN; i++) {
			if (!stricmp(stExtentions[i].szExt,pszExt)) {
				return stExtentions[i].nExtId;
			}
		}
	}
	return EXT_UNKNOWN;
}

int getZipedFiles(const char *path , SceIoDirent* files , int size) 
{
	int ifiles = 0;

	unzFile hUnzip = unzOpen(path);
	if (!hUnzip){
		char tname[256];
		sprintf(tname,"打开%s时出错,按任意键返回",path);
		debug_disp(tname);
		return 0;
	}

	unz_file_info fileInfo;
	char zipedfilename[264];
	memset(zipedfilename,0,sizeof zipedfilename);

	strcpy(files[ifiles].d_name,"..");
	files[ifiles].d_stat.st_attr = FIO_SO_IFDIR;
	ifiles++;

	do{
		if (unzGetCurrentFileInfo(hUnzip, &fileInfo, zipedfilename, sizeof zipedfilename, NULL, 0, NULL, 0) != UNZ_OK)
		{
			char tname[256];
			sprintf(tname,"读取%s时出错,按任意键返回",path);
			debug_disp(tname);
			return 0;
		}
		strcpy(files[ifiles].d_name,zipedfilename);
		files[ifiles].d_stat.st_attr = 0;
		files[ifiles].d_stat.st_size = fileInfo.uncompressed_size;
		if(getExtId(files[ifiles].d_name) == EXT_TXT)
		{
			ifiles++;
		}
	} while (unzGoToNextFile(hUnzip) != UNZ_END_OF_LIST_OF_FILE && ifiles < size);

	unzClose(hUnzip);

	return ifiles;

}
void getDir(const char *path) 
{
	int fd, b=0;
	char *p;

	nfiles = 0;

	//read ziped files
	if(getExtId(path)==EXT_ZIP)
	{
		nfiles = getZipedFiles(path,files , sizeof files);
		return;
	}

	if(strcmp(path,"ms0:/"))
	{
		strcpy(files[nfiles].d_name,"..");
		files[nfiles].d_stat.st_attr = FIO_SO_IFDIR;
		nfiles++;
	}

	fd = sceIoDopen(path);
	while(nfiles<MAX_ENTRY){
		if(sceIoDread(fd, &files[nfiles])<=0) break;
		if(files[nfiles].d_name[0] == '.') continue;
		
		if(files[nfiles].d_stat.st_attr & FIO_SO_IFDIR){
			strcat(files[nfiles].d_name, "/");
			nfiles++;
			continue;
		}
		if(getExtId(files[nfiles].d_name) != EXT_UNKNOWN) nfiles++;
	}
	sceIoDclose(fd);
}

void drawFileList(void)
{	
	int i;
	unsigned char name[256];

	pgFillvram(rgb2col(0,0,0));

	chDrawRec(0,18,SCREEN_WIDTH,1, 0xffff,1);

	memset(name,0,256);
	jis2cjk(DirPath,name);
	chDrawString(0,0,0xffff,name,1,1);	
	pgPrint(41,0,0x5555,PSPVER);

	chDrawRec(0,254,SCREEN_WIDTH,1, 0xffff,1);
	chDrawString(180,256,0xffff,"○确定  ×取消",1,1);
	//chDrawString(160,256,0x27af,"  CONFIRM    CANCEL",1,1);

	chDrawRec(4,LIST_CY-1,SCREEN_WIDTH-8,18, 0x8888,1);
	chDrawRecLine(4,LIST_CY-1,SCREEN_WIDTH-8,18, 0xffff);	

	//read zip file?
	bool isZip = false;
	if(getExtId(DirPath)==EXT_ZIP)
	{
		isZip = true;
	}

	memset(name,0,256);
	if(isZip)
		strcpy(name,files[selfile].d_name);
	else
		jis2cjk(files[selfile].d_name,name);
	chDrawString(LIST_CX,LIST_CY,0xffff, name,1,1);
	//cout_num(char* str,int num);

	for(i=1;i<=LIST_HALFCOUNT;i++)
	{
		if((selfile-i)>=0)
		{
			memset(name,0,256);
			if(isZip)
				strcpy(name,files[selfile-i].d_name);
			else
				jis2cjk(files[selfile-i].d_name,name);
			chDrawString(LIST_CX,LIST_CY-i*LIST_ROWHEIGHT,0x8888,name,1,1);
		}
	}
	for(i=1;i<=LIST_HALFCOUNT;i++)
	{
		if((selfile+i)<nfiles)
		{
			memset(name,0,256);
			if(isZip)
				strcpy(name,files[selfile+i].d_name);
			else
				jis2cjk(files[selfile+i].d_name,name);
			chDrawString(LIST_CX,LIST_CY+i*LIST_ROWHEIGHT,0x8888,name,1,1);		
		}
	}
	//disp_bus(true);

	pgScreenFlipV();

}
int getFile(char *out)
{
	//char path[MAXPATH], oldDir[MAXNAME], *p;
	char *p;
	//strcpy(path, BookMarkPath);
	getDir(DirPath);
	drawFileList();
	for(;;)
	{
		switch(Control())
		{
		case 1:
			//pgPrint(1,7,0xffff,"You pressed square!");
			//sendFileToIrda(&(files[selfile]));
			//drawFileList();
			break;
		case 2:
			//pgPrint(1,7,0xffff,"You pressed triangle!");
			//receiveFileFromIrda();
			//drawFileList();
			break;
		case 3:
			//pgPrint(1,7,0xffff,"You pressed circle!");

			if(files[selfile].d_stat.st_attr & FIO_SO_IFDIR)
			{
				if(!strcmp(files[selfile].d_name,".."))
				{
					if(strcmp(DirPath,"ms0:/"))
					{
						p=strrchr(DirPath,'/');
						if(*(p+1)==0)
						{
							*p=0;
							p=strrchr(DirPath,'/');
						}
						p++;
						*p=0;
						getDir(DirPath);
						selfile=0;
						drawFileList();
					}					
				}else{
					strcat(DirPath,files[selfile].d_name);
					getDir(DirPath);
					selfile=0;
					drawFileList();
				}
			}				
			else
			{
				memset(out,0,0x108);

				strcpy(out, DirPath);
				strcat(out, files[selfile].d_name);

				//check zip file
				if(getExtId(out)==EXT_ZIP)
				{
					strcat(DirPath,files[selfile].d_name);
					getDir(DirPath);
					selfile=0;
					drawFileList();
					continue;
				}
				memset(out,0,0x108);
				
				strcpy(out, DirPath);
				if(getExtId(DirPath)==EXT_ZIP)
				{
					strcat(out,"?");
				}
				strcat(out, files[selfile].d_name);

				gBookInf.filesize = files[selfile].d_stat.st_size;
				return 1;
			}
			break;
		case 4:
			//pgPrint(1,7,0xffff,"You pressed cross!");		
			return 0;				
		case 5:
			//pgPrint(1,7,0xffff,"You pressed up!");
			if((selfile-1)>=0)
			{
				selfile--;				
				drawFileList();
			}				
			break;
		case 6:
			//pgPrint(1,7,0xffff,"You pressed down!");
			if((selfile+1)<nfiles)
			{
				selfile++;
				drawFileList();
			}
			break;
		case 7:
			//pgPrint(1,7,0xffff,"You pressed left!");
			if(selfile>0)
			{
				if((selfile-LIST_HALFCOUNT)>=0)
				{
					selfile-=LIST_HALFCOUNT;										
				}
				else
				{
					selfile=0;
				}
				drawFileList();
			}
			break;
		case 8:
			//pgPrint(1,7,0xffff,"You pressed right!");	
			if(selfile<(nfiles-1))
			{
				if((selfile+LIST_HALFCOUNT)<nfiles)
				{
					selfile+=LIST_HALFCOUNT;						
				}
				else
				{
					selfile = nfiles-1;
				}
				drawFileList();
			}
			break;
		case 9:
			//pgPrint(1,7,0xffff,"You pressed start!");				
			break;
		case 10:
			//pgPrint(1,7,0xffff,"You pressed selfileect!");	
			break;
		case 11:
			//pgPrint(1,7,0xffff,"You pressed left trigger!");			
			break;
		case 12:
			//pgPrint(1,7,0xffff,"You pressed right trigger!");			
			break;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -