1 /* FCE Ultra - NES/Famicom Emulator
3 * Copyright notice for this file:
4 * Copyright (C) 2002 Xodnizel
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #define _GNU_SOURCE // for asprintf
27 #include <sys/types.h>
43 static char BaseDirectory[2048];
45 static char FileExt[2048]; /* Includes the . character, as in ".nes" */
47 static char FileBaseDirectory[2048];
49 void FCEUI_SetBaseDirectory(char *dir)
51 strncpy(BaseDirectory,dir,2047);
52 BaseDirectory[2047]=0;
55 static char *odirs[FCEUIOD__COUNT]={0,0,0,0,0}; // odirs, odors. ^_^
57 void FCEUI_SetDirOverride(int which, char *n)
59 // FCEU_PrintError("odirs[%d]=%s->%s", which, odirs[which], n);
60 if(which < FCEUIOD__COUNT)
64 if(FCEUGameInfo) /* Rebuild cache of present states/movies. */
66 if(which==FCEUIOD_STATE)
68 else if(which == FCEUIOD_MISC)
69 FCEUMOV_CheckMovies();
75 static int asprintf(char **strp, const char *fmt, ...)
81 if(!(*strp=malloc(2048)))
83 ret=vsnprintf(*strp,2048,fmt,ap);
89 char* FCEU_GetPath(int type)
94 case FCEUMKF_STATE:if(odirs[FCEUIOD_STATE])
95 ret=strdup(odirs[FCEUIOD_STATE]);
97 asprintf(&ret,"%s"PSS"movie",BaseDirectory);
99 case FCEUMKF_MOVIE:if(odirs[FCEUIOD_MISC])
100 ret=strdup(odirs[FCEUIOD_MISC]);
102 asprintf(&ret,"%s"PSS"movie",BaseDirectory);
108 char *FCEU_MakePath(int type, const char* filebase)
114 case FCEUMKF_MOVIE:if(odirs[FCEUIOD_MISC])
115 asprintf(&ret,"%s"PSS"%s",odirs[FCEUIOD_MISC],filebase);
117 asprintf(&ret,"%s"PSS"movie"PSS"%s",BaseDirectory,filebase);
123 char *FCEU_MakeFName(int type, int id1, char *cd1)
130 case FCEUMKF_NPTEMP: asprintf(&ret,"%s"PSS"m590plqd94fo.tmp",BaseDirectory);break;
131 case FCEUMKF_MOVIE:if(id1>=0)
133 if(odirs[FCEUIOD_MISC])
134 asprintf(&ret,"%s"PSS"%s.%d.fcm",odirs[FCEUIOD_MISC],FileBase,id1);
136 asprintf(&ret,"%s"PSS"movie"PSS"%s.%d.fcm",BaseDirectory,FileBase,id1);
137 if(stat(ret,&tmpstat)==-1)
139 if(odirs[FCEUIOD_MISC])
140 asprintf(&ret,"%s"PSS"%s.%d.fcm",odirs[FCEUIOD_MISC],FileBase,id1);
142 asprintf(&ret,"%s"PSS"movie"PSS"%s.%d.fcm",BaseDirectory,FileBase,id1);
147 if(odirs[FCEUIOD_MISC])
148 asprintf(&ret,"%s"PSS"%s.fcm",odirs[FCEUIOD_MISC],FileBase);
150 asprintf(&ret,"%s"PSS"movie"PSS"%s.fcm",BaseDirectory,FileBase);
153 case FCEUMKF_STATE:if(odirs[FCEUIOD_STATE])
155 asprintf(&ret,"%s"PSS"%s.fc%d",odirs[FCEUIOD_STATE],FileBase,id1);
156 // FCEU_PrintError("A");
160 asprintf(&ret,"%s"PSS"fcs"PSS"%s.fc%d",BaseDirectory,FileBase,id1);
161 // FCEU_PrintError("B");
163 if(stat(ret,&tmpstat)==-1)
165 if(odirs[FCEUIOD_STATE])
167 asprintf(&ret,"%s"PSS"%s.fc%d",odirs[FCEUIOD_STATE],FileBase,id1);
168 // FCEU_PrintError("C");
172 asprintf(&ret,"%s"PSS"fcs"PSS"%s.fc%d",BaseDirectory,FileBase,id1);
173 // FCEU_PrintError("D");
178 if(FSettings.SnapName)
180 if(odirs[FCEUIOD_SNAPS])
181 asprintf(&ret,"%s"PSS"%s-%d.%s",odirs[FCEUIOD_SNAPS],FileBase,id1,cd1);
183 asprintf(&ret,"%s"PSS"snaps"PSS"%s-%d.%s",BaseDirectory,FileBase,id1,cd1);
187 if(odirs[FCEUIOD_SNAPS])
188 asprintf(&ret,"%s"PSS"%d.%s",odirs[FCEUIOD_SNAPS],id1,cd1);
190 asprintf(&ret,"%s"PSS"snaps"PSS"%d.%s",BaseDirectory,id1,cd1);
193 case FCEUMKF_FDS:if(odirs[FCEUIOD_NV])
194 asprintf(&ret,"%s"PSS"%s.fds",odirs[FCEUIOD_NV],FileBase);
196 asprintf(&ret,"%s"PSS"sav"PSS"%s.fds",BaseDirectory,FileBase);
198 case FCEUMKF_SAV:if(odirs[FCEUIOD_NV])
199 asprintf(&ret,"%s"PSS"%s.%s",odirs[FCEUIOD_NV],FileBase,cd1);
201 asprintf(&ret,"%s"PSS"sav"PSS"%s.%s",BaseDirectory,FileBase,cd1);
202 if(stat(ret,&tmpstat)==-1)
204 if(odirs[FCEUIOD_NV])
205 asprintf(&ret,"%s"PSS"%s.%s",odirs[FCEUIOD_NV],FileBase,cd1);
207 asprintf(&ret,"%s"PSS"sav"PSS"%s.%s",BaseDirectory,FileBase,cd1);
211 if(odirs[FCEUIOD_CHEATS])
212 asprintf(&ret,"%s"PSS"%s.cht",odirs[FCEUIOD_CHEATS],FileBase);
214 asprintf(&ret,"%s"PSS"cheats"PSS"%s.cht",BaseDirectory,FileBase);
216 case FCEUMKF_IPS: asprintf(&ret,"%s"PSS"%s%s.ips",FileBaseDirectory,FileBase,FileExt);
218 case FCEUMKF_GGROM:asprintf(&ret,"%s"PSS"gg.rom",BaseDirectory);break;
219 case FCEUMKF_FDSROM:asprintf(&ret,"%s"PSS"disksys.rom",BaseDirectory);break;
220 case FCEUMKF_PALETTE:
221 if(odirs[FCEUIOD_MISC])
222 asprintf(&ret,"%s"PSS"%s.pal",odirs[FCEUIOD_MISC],FileBase);
224 asprintf(&ret,"%s"PSS"%s.pal",BaseDirectory,FileBase);
226 case FCEUMKF_MOVIEGLOB:
227 if(odirs[FCEUIOD_MISC])
228 // asprintf(&ret,"%s"PSS"%s*.fcm",odirs[FCEUIOD_MISC],FileBase);
229 asprintf(&ret,"%s"PSS"*.???",odirs[FCEUIOD_MISC]);
231 // asprintf(&ret,"%s"PSS"fcs"PSS"%s*.fcm",BaseDirectory,FileBase);
232 asprintf(&ret,"%s"PSS"movie"PSS"*.???",BaseDirectory);
234 case FCEUMKF_MOVIEGLOB2:
235 asprintf(&ret,"%s"PSS"*.???",BaseDirectory);
237 case FCEUMKF_STATEGLOB:
238 if(odirs[FCEUIOD_STATE])
239 asprintf(&ret,"%s"PSS"%s*.fc?",odirs[FCEUIOD_STATE],FileBase);
241 asprintf(&ret,"%s"PSS"fcs"PSS"%s*.fc?",BaseDirectory,FileBase);
247 void GetFileBase(const char *f)
249 const char *tp1,*tp3;
252 tp1=((char *)strrchr(f,':'));
254 tp1=((char *)strrchr(f,'/'));
256 tp1=((char *)strrchr(f,'\\'));
258 tp3=((char *)strrchr(f,'/'));
265 strcpy(FileBaseDirectory,".");
269 memcpy(FileBaseDirectory,f,tp1-f);
270 FileBaseDirectory[tp1-f]=0;
274 if(((tp3=strrchr(f,'.'))!=NULL) && (tp3>tp1))
276 memcpy(FileBase,tp1,tp3-tp1);
282 strcpy(FileBase,tp1);
287 uint32 uppow2(uint32 n)