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
22 #define _GNU_SOURCE // for asprintf
30 #include <sys/types.h>
46 static char BaseDirectory[2048];
48 static char FileExt[2048]; /* Includes the . character, as in ".nes" */
50 static char FileBaseDirectory[2048];
52 void FCEUI_SetBaseDirectory(char *dir)
54 strncpy(BaseDirectory,dir,2047);
55 BaseDirectory[2047]=0;
58 static char *odirs[FCEUIOD__COUNT]={0,0,0,0,0}; // odirs, odors. ^_^
60 void FCEUI_SetDirOverride(int which, char *n)
62 // FCEU_PrintError("odirs[%d]=%s->%s", which, odirs[which], n);
63 if(which < FCEUIOD__COUNT)
67 if(FCEUGameInfo) /* Rebuild cache of present states/movies. */
69 if(which==FCEUIOD_STATE)
71 else if(which == FCEUIOD_MISC)
72 FCEUMOV_CheckMovies();
78 static int asprintf(char **strp, const char *fmt, ...)
84 if(!(*strp=malloc(2048)))
86 ret=vsnprintf(*strp,2048,fmt,ap);
92 char* FCEU_GetPath(int type)
97 case FCEUMKF_STATE:if(odirs[FCEUIOD_STATE])
98 ret=strdup(odirs[FCEUIOD_STATE]);
100 asprintf(&ret,"%s"PSS"movie",BaseDirectory);
102 case FCEUMKF_MOVIE:if(odirs[FCEUIOD_MISC])
103 ret=strdup(odirs[FCEUIOD_MISC]);
105 asprintf(&ret,"%s"PSS"movie",BaseDirectory);
111 char *FCEU_MakePath(int type, const char* filebase)
117 case FCEUMKF_MOVIE:if(odirs[FCEUIOD_MISC])
118 asprintf(&ret,"%s"PSS"%s",odirs[FCEUIOD_MISC],filebase);
120 asprintf(&ret,"%s"PSS"movie"PSS"%s",BaseDirectory,filebase);
126 char *FCEU_MakeFName(int type, int id1, char *cd1)
133 case FCEUMKF_NPTEMP: asprintf(&ret,"%s"PSS"m590plqd94fo.tmp",BaseDirectory);break;
134 case FCEUMKF_MOVIE:if(id1>=0)
136 if(odirs[FCEUIOD_MISC])
137 asprintf(&ret,"%s"PSS"%s.%d.fcm",odirs[FCEUIOD_MISC],FileBase,id1);
139 asprintf(&ret,"%s"PSS"movie"PSS"%s.%d.fcm",BaseDirectory,FileBase,id1);
140 if(stat(ret,&tmpstat)==-1)
142 if(odirs[FCEUIOD_MISC])
143 asprintf(&ret,"%s"PSS"%s.%d.fcm",odirs[FCEUIOD_MISC],FileBase,id1);
145 asprintf(&ret,"%s"PSS"movie"PSS"%s.%d.fcm",BaseDirectory,FileBase,id1);
150 if(odirs[FCEUIOD_MISC])
151 asprintf(&ret,"%s"PSS"%s.fcm",odirs[FCEUIOD_MISC],FileBase);
153 asprintf(&ret,"%s"PSS"movie"PSS"%s.fcm",BaseDirectory,FileBase);
156 case FCEUMKF_STATE:if(odirs[FCEUIOD_STATE])
158 asprintf(&ret,"%s"PSS"%s.fc%d",odirs[FCEUIOD_STATE],FileBase,id1);
159 // FCEU_PrintError("A");
163 asprintf(&ret,"%s"PSS"fcs"PSS"%s.fc%d",BaseDirectory,FileBase,id1);
164 // FCEU_PrintError("B");
166 if(stat(ret,&tmpstat)==-1)
168 if(odirs[FCEUIOD_STATE])
170 asprintf(&ret,"%s"PSS"%s.fc%d",odirs[FCEUIOD_STATE],FileBase,id1);
171 // FCEU_PrintError("C");
175 asprintf(&ret,"%s"PSS"fcs"PSS"%s.fc%d",BaseDirectory,FileBase,id1);
176 // FCEU_PrintError("D");
181 if(FSettings.SnapName)
183 if(odirs[FCEUIOD_SNAPS])
184 asprintf(&ret,"%s"PSS"%s-%d.%s",odirs[FCEUIOD_SNAPS],FileBase,id1,cd1);
186 asprintf(&ret,"%s"PSS"snaps"PSS"%s-%d.%s",BaseDirectory,FileBase,id1,cd1);
190 if(odirs[FCEUIOD_SNAPS])
191 asprintf(&ret,"%s"PSS"%d.%s",odirs[FCEUIOD_SNAPS],id1,cd1);
193 asprintf(&ret,"%s"PSS"snaps"PSS"%d.%s",BaseDirectory,id1,cd1);
196 case FCEUMKF_FDS:if(odirs[FCEUIOD_NV])
197 asprintf(&ret,"%s"PSS"%s.fds",odirs[FCEUIOD_NV],FileBase);
199 asprintf(&ret,"%s"PSS"sav"PSS"%s.fds",BaseDirectory,FileBase);
201 case FCEUMKF_SAV:if(odirs[FCEUIOD_NV])
202 asprintf(&ret,"%s"PSS"%s.%s",odirs[FCEUIOD_NV],FileBase,cd1);
204 asprintf(&ret,"%s"PSS"sav"PSS"%s.%s",BaseDirectory,FileBase,cd1);
207 if(odirs[FCEUIOD_CHEATS])
208 asprintf(&ret,"%s"PSS"%s.cht",odirs[FCEUIOD_CHEATS],FileBase);
210 asprintf(&ret,"%s"PSS"cheats"PSS"%s.cht",BaseDirectory,FileBase);
212 case FCEUMKF_IPS: asprintf(&ret,"%s"PSS"%s%s.ips",FileBaseDirectory,FileBase,FileExt);
214 case FCEUMKF_GGROM:asprintf(&ret,"%s"PSS"gg.rom",BaseDirectory);break;
215 case FCEUMKF_FDSROM:asprintf(&ret,"%s"PSS"disksys.rom",BaseDirectory);break;
216 case FCEUMKF_PALETTE:
217 if(odirs[FCEUIOD_MISC])
218 asprintf(&ret,"%s"PSS"%s.pal",odirs[FCEUIOD_MISC],FileBase);
220 asprintf(&ret,"%s"PSS"pal"PSS"%s.pal",BaseDirectory,FileBase);
222 case FCEUMKF_MOVIEGLOB:
223 if(odirs[FCEUIOD_MISC])
224 // asprintf(&ret,"%s"PSS"%s*.fcm",odirs[FCEUIOD_MISC],FileBase);
225 asprintf(&ret,"%s"PSS"*.???",odirs[FCEUIOD_MISC]);
227 // asprintf(&ret,"%s"PSS"fcs"PSS"%s*.fcm",BaseDirectory,FileBase);
228 asprintf(&ret,"%s"PSS"movie"PSS"*.???",BaseDirectory);
230 case FCEUMKF_MOVIEGLOB2:
231 asprintf(&ret,"%s"PSS"*.???",BaseDirectory);
233 case FCEUMKF_STATEGLOB:
234 if(odirs[FCEUIOD_STATE])
235 asprintf(&ret,"%s"PSS"%s*.fc?",odirs[FCEUIOD_STATE],FileBase);
237 asprintf(&ret,"%s"PSS"fcs"PSS"%s*.fc?",BaseDirectory,FileBase);
243 void GetFileBase(const char *f)
245 const char *tp1,*tp3;
248 tp1=((char *)strrchr(f,':'));
250 tp1=((char *)strrchr(f,'/'));
252 tp1=((char *)strrchr(f,'\\'));
254 tp3=((char *)strrchr(f,'/'));
261 strcpy(FileBaseDirectory,".");
265 memcpy(FileBaseDirectory,f,tp1-f);
266 FileBaseDirectory[tp1-f]=0;
270 if(((tp3=strrchr(f,'.'))!=NULL) && (tp3>tp1))
272 memcpy(FileBase,tp1,tp3-tp1);
278 strcpy(FileBase,tp1);
283 uint32 uppow2(uint32 n)