int main(int agv,char *agc[]) { char lstname[256],dataname[256]; if (agv==1) { printf("Need Argument\n"); return -1; } if (agc[1][0]=='x') { char filename[256]; int i; strcpy(lstname,agc[2]); strcpy(dataname,agc[2]); strcat(lstname,".lst"); strcat(dataname,".wdf"); if (!file_exist(lstname)) { printf("Can't open list file[.lst]\n"); return -1; } if (!file_exist(dataname)) { printf("Can't open data file[.wdf]\n"); return -1; } _mkdir(agc[2]); FileList list; int n; n=list.ReadListOnly(lstname); std::vector<FILE_LST> old_list; int old_number=read_lst(dataname,old_list); FILE *df; df=fopen(dataname,"rb"); for (i=0;i<n;i++) { FILE *f; int j; static char buffer[1024]; strcpy(filename,agc[2]); strcat(filename,"\\"); int n; n=list.IsFileExist(i,old_list,old_number); printf("%s [%x] ",list.GetName(i),list.GetID(i)); if (n<0) { printf(" Can't find\n"); continue; } strcat(filename,list.GetName(i)); char *path=strchr(filename,'\\'); path=strchr(path+1,'\\'); if (path) { path[0]=0; _mkdir(filename); path[0]='\\'; } f=fopen(filename,"wb"); fseek(df,old_list[n].offset,SEEK_SET); for (j=0;j<old_list[n].size-1024;j+=1024) { fread(buffer,1,1024,df); fwrite(buffer,1,1024,f); } j=old_list[n].size-j; fread(buffer,1,j,df); fwrite(buffer,1,j,f); fclose(f); printf(" Extracted\n"); } fclose(df); return 0; } strcpy(lstname,agc[1]); strcpy(dataname,agc[1]); strcat(lstname,".lst"); strcat(dataname,".wdf"); if (!file_exist(lstname)) { printf("Can't open list file[.lst]\n"); return -1; } if (file_exist(dataname)) { printf("Updating Data File...\n"); update_datafile(dataname,lstname); } else { printf("Creating New Data File...\n"); create_new(dataname,lstname); } return 0; }
int RunFileBrowser(char *source, char *outname, const char *types[], const char *info) { int size = 0; int index; int offset_start, offset_end; static int max_entries = 8; int scrollModifier = 4; int justsavedromdir = 0; int scrollMult; static int spy; int y, i; // Try to get a saved romdir from a config file char* home = getenv("HOME"); char romcfgfile [128]; sprintf (romcfgfile, "%s/.fceux/romdir.cfg", home); FILE * pFile; pFile = fopen (romcfgfile,"r+"); if (pFile != NULL) { fgets (s_LastDir , 128 , pFile); fclose (pFile); } // Create file list FileList *list = new FileList(source ? source : s_LastDir, types); if (list == NULL) return 0; scrollModifier *= max_entries; RESTART: spy = 72; size = list->Size(); index = 0; offset_start = 0; offset_end = size > max_entries ? max_entries : size; g_dirty = 1; while (1) { // Parse input readkey(); // TODO - put exit keys // Go to previous folder or return ... if (parsekey(DINGOO_B)) { list->Enter(-1); goto RESTART; } // Enter folder or select rom ... if (parsekey(DINGOO_A)) { if (list->GetSize(index) == -1) { list->Enter(index); goto RESTART; } else { strncpy(outname, list->GetPath(index), 128); break; } } if (parsekey(DINGOO_X)) { return 0; } if (parsekey(DINGOO_SELECT)) { // Save the current romdir in a config file char* home = getenv("HOME"); char romcfgfile [128]; strncpy(s_LastDir, list->GetCurDir(), 128); sprintf (romcfgfile, "%s/.fceux/romdir.cfg", home); FILE * pFile; pFile = fopen (romcfgfile,"w+"); fputs (s_LastDir,pFile); fclose (pFile); justsavedromdir = 1; } if (size > 0) { // Move through file list if (parsekey(DINGOO_R, 0)) { index = size - 1; spy = 72 + 15*(max_entries-1); offset_end = size; offset_start = offset_end - max_entries; } if (parsekey(DINGOO_L, 0)) { goto RESTART; } if (parsekey(DINGOO_UP, 1)) { if (index > offset_start){ index--; spy -= 15; } else if (offset_start > 0) { index--; offset_start--; offset_end--; } else { index = size - 1; offset_end = size; offset_start = size <= max_entries ? 0 : offset_end - max_entries; spy = 72 + 15*(index - offset_start); } } if (parsekey(DINGOO_DOWN, 1)) { if (index < offset_end - 1){ index++; spy += 15; } else if (offset_end < size) { index++; offset_start++; offset_end++; } else { index = 0; offset_start = 0; offset_end = size <= max_entries ? size : max_entries; spy = 72; } } if (parsekey(DINGOO_LEFT, 1)) { if (index > offset_start) { index = offset_start; spy = 72; } else if (index - scrollModifier >= 0){ index -= scrollModifier; offset_start -= scrollModifier; offset_end = offset_start + max_entries; } else goto RESTART; } if (parsekey(DINGOO_RIGHT, 1)) { if (index < offset_end-1) { index = offset_end-1; spy = 72 + 15*(index-offset_start); } else if (offset_end + scrollModifier <= size) { index += scrollModifier; offset_end += scrollModifier; offset_start += scrollModifier; } else { index = size - 1; spy = 72 + 15*(max_entries-1); offset_end = size; offset_start = offset_end - max_entries; } } } // Draw stuff if (g_dirty) { draw_bg(g_bg); //Draw Top and Bottom Bars DrawChar(gui_screen, SP_SELECTOR, 0, 37); DrawChar(gui_screen, SP_SELECTOR, 81, 37); DrawChar(gui_screen, SP_SELECTOR, 0, 225); DrawChar(gui_screen, SP_SELECTOR, 81, 225); DrawText(gui_screen, "B - Go Back", 235, 225); DrawChar(gui_screen, SP_LOGO, 12, 9); // Draw selector DrawChar(gui_screen, SP_SELECTOR, 4, spy); DrawChar(gui_screen, SP_SELECTOR, 81, spy); DrawText(gui_screen, "ROM Browser", 8, 37); // Draw file list for (i = offset_start, y = 72; i < offset_end; i++, y += 15) { DrawText(gui_screen, list->GetName(i), 8, y); } // Draw info if (info) DrawText(gui_screen, info, 8, 225); else { if (justsavedromdir == 1){ DrawText(gui_screen, "ROM dir successfully saved!", 8, 225); } else { if (list->GetSize(index) == -1) DrawText(gui_screen, "SELECT - Save ROM dir", 8, 225); else DrawText(gui_screen, "SELECT - Save ROM dir", 8, 225); } justsavedromdir = 0; } // Draw offset marks if (offset_start > 0) DrawChar(gui_screen, SP_UPARROW, 157, 57); if (offset_end < list->Size()) DrawChar(gui_screen, SP_DOWNARROW, 157, 197); g_dirty = 0; } SDL_Delay(4); // Update real screen FCEUGUI_Flip(); } if (source == NULL) strncpy(s_LastDir, list->GetCurDir(), 128); delete list; return 1; }
void update_datafile(const char *dataname,const char *lstname) { int old_number,new_number; std::vector<FILE_LST> old_list(c_MaxFile); old_number=read_lst(dataname,old_list); if (old_number<0) { printf("[%s] is Bad\n",dataname); return; } FILE *f; FileList list; FileHeader header; int i; new_number=list.ReadList(lstname); f=fopen(dataname,"rb+"); if (f==0) { printf("[%s] can't update",dataname); return; } list.ReadHeader(f,&header); header.number=new_number; int s; for (s=i=0;i<old_number;i++) { if (list.FindFile(old_list[i].uid)==-1) { printf("A file[%x] (%d bytes) is deleted\n",old_list[i].uid,old_list[i].size); delete_file(f,old_number,old_list,i); ++s; } } if (s>0) { printf("%d files deleted\n",s); } for (i=0;i<old_number;i++) { int n; if (old_list[i].uid==0) continue; n=list.FindFile(old_list[i].uid); assert(n>=0); int space=old_list[i].size+old_list[i].space; int size=file_size(list.GetName(n)); if (size>space) { delete_file(f,old_number,old_list,i); printf("[%s] deleted\n",list.GetName(n)); } else if (size==old_list[i].size) { if (list.NeedUpdate(f,n,old_list,i)) { list.UpdateFile(f,n,old_list,i); } } else if (size<=space) { list.UpdateFile(f,n,old_list,i); } } // 寻找文件尾 for (i=0;i<old_number;i++) { if (old_list[i].uid==0) continue; if (old_list[i].offset+old_list[i].size+old_list[i].space==header.offset) break; } if (i<old_number) { header.offset=old_list[i].offset+old_list[i].size; old_list[i].space=0; list.ClearTail(old_list[i].uid); } fseek(f,header.offset,SEEK_SET); for (i=0;i<new_number;i++) { if (list.WriteFile(f,i,true)) printf("Added\n"); // 添加文件(见缝插针) } header.offset=ftell(f); list.WriteList(f); write_header(f,&header); fclose(f); }