Example #1
0
hash_collection &emu_file::hashes(const char *types)
{
	// determine which hashes we need
	astring needed;
	for (const char *scan = types; *scan != 0; scan++)
		if (m_hashes.hash(*scan) == NULL)
			needed.cat(*scan);

	// if we need nothing, skip it
	if (!needed)
		return m_hashes;

	// load the ZIP file if needed
	if (m_zipfile != NULL && load_zipped_file() != FILERR_NONE)
		return m_hashes;
	if (m_file == NULL)
		return m_hashes;

	// if we have ZIP data, just hash that directly
	if (m_zipdata != NULL)
	{
		m_hashes.compute(m_zipdata, m_ziplength, needed);
		return m_hashes;
	}

	// read the data if we can
	const UINT8 *filedata = (const UINT8 *)core_fbuffer(m_file);
	if (filedata == NULL)
		return m_hashes;

	// compute the hash
	m_hashes.compute(filedata, core_fsize(m_file), needed);
	return m_hashes;
}
Example #2
0
int file_unzip(char *zipfile,char *filename,char *tmpdir)
{
    FILE *fp;
    unsigned char *mem;
    char *tmpfile;
    int size;
    int i;

	tmpfile=(char *) malloc(strlen(filename)+strlen(tmpdir)+2);
	strcpy(tmpfile,tmpdir);
	strcat(tmpfile,"/");
	strcat(tmpfile,filename);
	tmpfile[strlen(tmpfile)]='\0';


    	if (load_zipped_file (zipfile, filename, &mem, &size)==0){
	    fp=fopen(tmpfile,"wb");
	    for(i=0;i<size;i++)
		fputc(mem[i],fp);
	    fclose (fp);
	    return 0;
	}
	else{
	    printf("SDLengine error: file not found  %s\n",filename);
	    return -1;
	}

}
Example #3
0
bool emu_file::compressed_file_ready(void)
{
	// load the ZIP file now if we haven't yet
	if (m_zipfile && (load_zipped_file() != osd_file::error::NONE))
		return true;

	return false;
}
Example #4
0
emu_file::operator core_file *()
{
	// load the ZIP file now if we haven't yet
	if (m_zipfile != NULL && load_zipped_file() != FILERR_NONE)
		return NULL;

	// return the core file
	return m_file;
}
Example #5
0
emu_file::operator core_file &()
{
	// load the ZIP file now if we haven't yet
	if (m_zipfile != NULL && load_zipped_file() != FILERR_NONE)
		throw emu_fatalerror("operator core_file & used on invalid file");

	// return the core file
	return *m_file;
}
Example #6
0
bool emu_file::compressed_file_ready(void)
{
    // load the ZIP file now if we haven't yet
    if (m__7zfile != NULL && load__7zped_file() != FILERR_NONE)
        return true;

    if (m_zipfile != NULL && load_zipped_file() != FILERR_NONE)
        return true;

    return false;
}
Example #7
0
UINT64 emu_file::tell()
{
	// load the ZIP file now if we haven't yet
	if (m_zipfile != NULL && load_zipped_file() != FILERR_NONE)
		return 0;

	// tell if we can
	if (m_file != NULL)
		return core_ftell(m_file);

	return 0;
}
Example #8
0
char *emu_file::gets(char *s, int n)
{
	// load the ZIP file now if we haven't yet
	if (m_zipfile != NULL && load_zipped_file() != FILERR_NONE)
		return NULL;

	// read the data if we can
	if (m_file != NULL)
		return core_fgets(s, n, m_file);

	return NULL;
}
Example #9
0
int emu_file::getc()
{
	// load the ZIP file now if we haven't yet
	if (m_zipfile != NULL && load_zipped_file() != FILERR_NONE)
		return EOF;

	// read the data if we can
	if (m_file != NULL)
		return core_fgetc(m_file);

	return EOF;
}
Example #10
0
int emu_file::ungetc(int c)
{
	// load the ZIP file now if we haven't yet
	if (m_zipfile != NULL && load_zipped_file() != FILERR_NONE)
		return 1;

	// read the data if we can
	if (m_file != NULL)
		return core_ungetc(c, m_file);

	return 1;
}
Example #11
0
UINT32 emu_file::read(void *buffer, UINT32 length)
{
	// load the ZIP file now if we haven't yet
	if (m_zipfile != NULL && load_zipped_file() != FILERR_NONE)
		return 0;

	// read the data if we can
	if (m_file != NULL)
		return core_fread(m_file, buffer, length);

	return 0;
}
Example #12
0
bool emu_file::eof()
{
	// load the ZIP file now if we haven't yet
	if (m_zipfile != NULL && load_zipped_file() != FILERR_NONE)
		return 0;

	// return EOF if we can
	if (m_file != NULL)
		return core_feof(m_file);

	return 0;
}
Example #13
0
int emu_file::seek(INT64 offset, int whence)
{
	// load the ZIP file now if we haven't yet
	if (m_zipfile != NULL && load_zipped_file() != FILERR_NONE)
		return 1;

	// seek if we can
	if (m_file != NULL)
		return core_fseek(m_file, offset, whence);

	return 1;
}
Example #14
0
osd_file::error emu_file::attempt_zipped()
{
	typedef util::archive_file::error (*open_func)(const std::string &filename, util::archive_file::ptr &result);
	char const *const suffixes[] = { ".zip", ".7z" };
	open_func const open_funcs[ARRAY_LENGTH(suffixes)] = { &util::archive_file::open_zip, &util::archive_file::open_7z };

	// loop over archive types
	std::string const savepath(m_fullpath);
	std::string filename;
	for (unsigned i = 0; i < ARRAY_LENGTH(suffixes); i++, m_fullpath = savepath, filename.clear())
	{
		// loop over directory parts up to the start of filename
		while (1)
		{
			// find the final path separator
			auto const dirsep = m_fullpath.find_last_of(PATH_SEPARATOR[0]);
			if (dirsep == std::string::npos)
				break;

			if (restrict_to_mediapath() && !part_of_mediapath(m_fullpath))
				break;

			// insert the part from the right of the separator into the head of the filename
			if (!filename.empty()) filename.insert(0, 1, '/');
			filename.insert(0, m_fullpath.substr(dirsep + 1, std::string::npos));

			// remove this part of the filename and append an archive extension
			m_fullpath.resize(dirsep);
			m_fullpath.append(suffixes[i]);

			// attempt to open the archive file
			util::archive_file::ptr zip;
			util::archive_file::error ziperr = open_funcs[i](m_fullpath, zip);

			// chop the archive suffix back off the filename before continuing
			m_fullpath = m_fullpath.substr(0, dirsep);

			// if we failed to open this file, continue scanning
			if (ziperr != util::archive_file::error::NONE)
				continue;

			int header = -1;

			// see if we can find a file with the right name and (if available) CRC
			if (m_openflags & OPEN_FLAG_HAS_CRC) header = zip->search(m_crc, filename, false);
			if (header < 0 && (m_openflags & OPEN_FLAG_HAS_CRC)) header = zip->search(m_crc, filename, true);

			// if that failed, look for a file with the right CRC, but the wrong filename
			if (header < 0 && (m_openflags & OPEN_FLAG_HAS_CRC)) header = zip->search(m_crc);

			// if that failed, look for a file with the right name;
			// reporting a bad checksum is more helpful and less confusing than reporting "ROM not found"
			if (header < 0) header = zip->search(filename, false);
			if (header < 0) header = zip->search(filename, true);

			// if we got it, read the data
			if (header >= 0)
			{
				m_zipfile = std::move(zip);
				m_ziplength = m_zipfile->current_uncompressed_length();

				// build a hash with just the CRC
				m_hashes.reset();
				m_hashes.add_crc(m_zipfile->current_crc());
				return (m_openflags & OPEN_FLAG_NO_PRELOAD) ? osd_file::error::NONE : load_zipped_file();
			}

			// close up the archive file and try the next level
			zip.reset();
		}
	}
	return osd_file::error::NOT_FOUND;
}
Example #15
0
osd_file::error emu_file::attempt__7zped()
{
	std::string filename;

	// loop over directory parts up to the start of filename
	while (1)
	{
		// find the final path separator
		int dirsep = m_fullpath.find_last_of(PATH_SEPARATOR[0]);
		if (dirsep == -1)
			return osd_file::error::NOT_FOUND;

		if (restrict_to_mediapath())
			if ( !part_of_mediapath(m_fullpath) )
				return osd_file::error::NOT_FOUND;

		// insert the part from the right of the separator into the head of the filename
		if (filename.length() > 0)
			filename.insert(0, "/");
		filename.insert(0, m_fullpath.substr(dirsep + 1, -1));

		// remove this part of the filename and append a .7z extension
		m_fullpath = m_fullpath.substr(0, dirsep).append(".7z");

		// attempt to open the _7Z file
		util::archive_file::ptr _7z;
		util::archive_file::error _7zerr = util::archive_file::open_7z(m_fullpath, _7z);

		// chop the ._7z back off the filename before continuing
		m_fullpath = m_fullpath.substr(0, dirsep);

		// if we failed to open this file, continue scanning
		if (_7zerr != util::archive_file::error::NONE)
			continue;

		int fileno = -1;

		// see if we can find a file with the right name and (if available) crc
		if (m_openflags & OPEN_FLAG_HAS_CRC) fileno = _7z->search(m_crc, filename);

		// if that failed, look for a file with the right crc, but the wrong filename
		if ((fileno < 0) && (m_openflags & OPEN_FLAG_HAS_CRC)) fileno = _7z->search(m_crc);

		// if that failed, look for a file with the right name; reporting a bad checksum
		// is more helpful and less confusing than reporting "rom not found"
		if (fileno < 0) fileno = _7z->search(filename);

		if (fileno >= 0)
		{
			m_zipfile = std::move(_7z);
			m_ziplength = m_zipfile->current_uncompressed_length();

			// build a hash with just the CRC
			m_hashes.reset();
			m_hashes.add_crc(m_zipfile->current_crc());
			return (m_openflags & OPEN_FLAG_NO_PRELOAD) ? osd_file::error::NONE : load_zipped_file();
		}

		// close up the _7Z file and try the next level
		_7z.reset();
	}
}
Example #16
0
file_error emu_file::attempt_zipped()
{
    astring filename;

    // loop over directory parts up to the start of filename
    while (1)
    {
        // find the final path separator
        int dirsep = m_fullpath.rchr(0, PATH_SEPARATOR[0]);
        if (dirsep == -1)
            return FILERR_NOT_FOUND;

        // insert the part from the right of the separator into the head of the filename
        if (filename.len() > 0)
            filename.ins(0, "/");
        filename.inssubstr(0, m_fullpath, dirsep + 1, -1);

        // remove this part of the filename and append a .zip extension
        m_fullpath.substr(0, dirsep).cat(".zip");

        // attempt to open the ZIP file
        zip_file *zip;
        zip_error ziperr = zip_file_open(m_fullpath, &zip);

        // chop the .zip back off the filename before continuing
        m_fullpath.substr(0, dirsep);

        // if we failed to open this file, continue scanning
        if (ziperr != ZIPERR_NONE)
            continue;

        // see if we can find a file with the right name and (if available) crc
        const zip_file_header *header;
        for (header = zip_file_first_file(zip); header != NULL; header = zip_file_next_file(zip))
            if (zip_filename_match(*header, filename) && (!(m_openflags & OPEN_FLAG_HAS_CRC) || header->crc == m_crc))
                break;

        // if that failed, look for a file with the right crc, but the wrong filename
        if (header == NULL && (m_openflags & OPEN_FLAG_HAS_CRC))
            for (header = zip_file_first_file(zip); header != NULL; header = zip_file_next_file(zip))
                if (header->crc == m_crc && !zip_header_is_path(*header))
                    break;

        // if that failed, look for a file with the right name; reporting a bad checksum
        // is more helpful and less confusing than reporting "rom not found"
        if (header == NULL)
            for (header = zip_file_first_file(zip); header != NULL; header = zip_file_next_file(zip))
                if (zip_filename_match(*header, filename))
                    break;

        // if we got it, read the data
        if (header != NULL)
        {
            m_zipfile = zip;
            m_ziplength = header->uncompressed_length;

            // build a hash with just the CRC
            m_hashes.reset();
            m_hashes.add_crc(header->crc);
            return (m_openflags & OPEN_FLAG_NO_PRELOAD) ? FILERR_NONE : load_zipped_file();
        }

        // close up the ZIP file and try the next level
        zip_file_close(zip);
    }
}
Example #17
0
void *osd_fopen(const char *game,const char *filename,int filetype,int _write)
{
	char name[256];
	char *gamename;
	int found = 0;
	FakeFileHandle *f;

	f = (FakeFileHandle *)gp2x_malloc(sizeof(FakeFileHandle));
	if (!f)
		return 0;
	fast_memset(f,0,sizeof(FakeFileHandle));

	gamename = (char *)game;

	switch (filetype)
	{
		case OSD_FILETYPE_ROM:

			/* only for reading */
			if (_write)
				break;

			if (!found) {
				/* try with a .zip extension */
				sprintf(name,"%s.zip\0", gamename);
				/*if (cache_stat(name)==0)*/ {
					sprintf(name,"%sroms/%s.zip\0", gp2x_path_mame, gamename);
					if (load_zipped_file(name, filename, &f->data, &f->length)==0) {
						f->type = kZippedFile;
						f->offset = 0;
						f->crc = crc32 (0L, f->data, f->length);
						found = 1;
					}
				}
			}
			if (!found) {
				if (cache_stat(gamename)==0) {
					sprintf(name,"%sroms/%s/%s\0",gp2x_path_mame, gamename, filename);
					if (checksum_file (name, &f->data, &f->length, &f->crc)==0) {
						f->type = kRAMFile;
						f->offset = 0;
						found = 1;
					}
				}
			}


			break;
		case OSD_FILETYPE_SAMPLE:
			if (!found) {
				/* try with a .zip extension */
				sprintf(name,"%s.zip\0", gamename);
				/*if (cache_stat_sample(name)==0)*/ {
					sprintf(name,"%ssamples/%s.zip\0", gp2x_path_mame, gamename);
					if (load_zipped_file(name, filename, &f->data, &f->length)==0) {
						f->type = kZippedFile;
						f->offset = 0;
						f->crc = crc32 (0L, f->data, f->length);
						found = 1;
					}
				}
			}
			if (!found) {
				sprintf(name,"%ssamples/%s/%s\0",gp2x_path_mame, gamename, filename);
				if (checksum_file (name, &f->data, &f->length, &f->crc)==0) {
					f->type = kRAMFile;
					f->offset = 0;
					found = 1;
				}
			}
			break;
		case OSD_FILETYPE_HIGHSCORE:
			if (!found) {
				sprintf(name,"%shi/%s.hi", gp2x_path_mame, gamename);
				f->type = kPlainFile;
				f->file = fopen(name,_write ? "wb" : "rb");
				found = f->file!=0;
			}
			break;
		case OSD_FILETYPE_CONFIG:
			break;
		case OSD_FILETYPE_INPUTLOG:
			break;
		case OSD_FILETYPE_STATE:
			break;
		case OSD_FILETYPE_ARTWORK:
			break;
	}

	if (!found) {
		gp2x_free(f);
		return 0;
	}

	return f;
}
Example #18
0
/* AM 980919 update */
void *osd_fopen(const char *game,const char *filename,int filetype,int _write)
{
	char name[256];
	char *gamename;
	int found = 0;
	int  indx;
	struct stat stat_buffer;
	FakeFileHandle *f;
	int pathc;
	char** pathv;
   #ifdef MESS
		char file[MAXPATHL];
		char *extension;
   #endif

	f = (FakeFileHandle *)malloc(sizeof(FakeFileHandle));
	if (!f)
		return 0;
    memset(f,0,sizeof(FakeFileHandle));

	gamename = (char *)game;

	/* Support "-romdir" yuck. */
	if (alternate_name)
		gamename = alternate_name;

	switch (filetype)
	{
		case OSD_FILETYPE_ROM:
		case OSD_FILETYPE_SAMPLE:
#ifdef MESS
      case OSD_FILETYPE_ROM_CART:
#endif

      /* only for reading */
			if (_write)
				break;

         if (filetype==OSD_FILETYPE_ROM
#ifdef MESS
         	|| OSD_FILETYPE_ROM_CART
#endif
			)
			{
				pathc = rompathc;
				pathv = rompathv;
			} else {
				pathc = samplepathc;
				pathv = samplepathv;
			}

         for (indx=0;indx<pathc && !found; ++indx) {
				const char* dir_name = pathv[indx];

				if (!found) {
					sprintf(name,"%s/%s",dir_name,gamename);
					if (cache_stat(name,&stat_buffer)==0 && (stat_buffer.st_mode & S_IFDIR)) {
						sprintf(name,"%s/%s/%s",dir_name,gamename,filename);
						if (filetype==OSD_FILETYPE_ROM)	{
							if (checksum_file (name, &f->data, &f->length, &f->crc)==0) {
								f->type = kRAMFile;
								f->offset = 0;
								found = 1;
							}
						}
						else {
							f->type = kPlainFile;
							f->file = fopen(name,"rb");
							found = f->file!=0;
						}
					}
				}

#ifdef MESS
				/* Zip cart support for MESS */
				if (!found && filetype == OSD_FILETYPE_ROM_CART)
				{
					extension = strrchr(name, '.');		/* find extension       */
					if (extension) *extension = '\0';	/* drop extension       */
					sprintf(name,"%s.zip", name);		/* add .zip for zipfile */
					if (cache_stat(name,&stat_buffer)==0) {
						if (load_zipped_file(name, filename, &f->data, &f->length)==0) {
							if (errorlog)
								fprintf(errorlog,"Using (osd_fopen) zip file for %s\n", filename);
							f->type = kZippedFile;
							f->offset = 0;
							f->crc = crc32 (0L, f->data, f->length);
							found = 1;
						}
					}
				}

#endif


				if (!found) {
					/* try with a .zip extension */
					sprintf(name,"%s/%s.zip", dir_name, gamename);
					if (cache_stat(name,&stat_buffer)==0) {
						if (load_zipped_file(name, filename, &f->data, &f->length)==0) {
							if (errorlog)
								fprintf(errorlog,"Using (osd_fopen) zip file for %s\n", filename);
							f->type = kZippedFile;
							f->offset = 0;
							f->crc = crc32 (0L, f->data, f->length);
							found = 1;
						}
					}
				}

				if (!found) {
					/* try with a .zip directory (if ZipMagic is installed) */
					sprintf(name,"%s/%s.zip",dir_name,gamename);
					if (cache_stat(name,&stat_buffer)==0 && (stat_buffer.st_mode & S_IFDIR)) {
						sprintf(name,"%s/%s.zip/%s",dir_name,gamename,filename);
						if (filetype==OSD_FILETYPE_ROM)	{
							if (checksum_file (name, &f->data, &f->length, &f->crc)==0) {
								f->type = kRAMFile;
								f->offset = 0;
								found = 1;
							}
						}
						else {
							f->type = kPlainFile;
							f->file = fopen(name,"rb");
							found = f->file!=0;
						}
					}
				}

/* ZipFolders support disabled for rom and sample load.
   There is no reason to keep it because now zip files are fully supported. */
#if 0
				if (!found) {
					/* try with a .zif directory (if ZipFolders is installed) */
					sprintf(name,"%s/%s.zif",dir_name,gamename);
					if (cache_stat(name,&stat_buffer)==0) {
						sprintf(name,"%s/%s.zif/%s",dir_name,gamename,filename);
						if (filetype==OSD_FILETYPE_ROM)	{
							if (checksum_file (name, &f->data, &f->length, &f->crc)==0) {
								f->type = kRAMFile;
								f->offset = 0;
								found = 1;
							}
						}
						else {
							f->type = kPlainFile;
							f->file = fopen(name,"rb");
							found = f->file!=0;
						}
					}
				}
#endif
         }
			break;
#ifdef MESS
      case OSD_FILETYPE_IMAGE:

			if(errorlog) fprintf(errorlog,"Open IMAGE '%s' for %s\n", filename, game);
            strcpy(file, filename);

			do {
				for (indx=0; indx < rompathc && !found; ++indx)
            {
					const char* dir_name = rompathv[indx];

					if (!found) {
						sprintf(name, "%s/%s", dir_name, gamename);
						if(errorlog) fprintf(errorlog,"Trying %s\n", name);
						if (cache_stat(name,&stat_buffer)==0 && (stat_buffer.st_mode & S_IFDIR)) {
                            sprintf(name,"%s/%s/%s", dir_name, gamename, file);
							f->file = fopen(name,_write ? "r+b" : "rb");
							found = f->file!=0;
						}
					}

 /******************************************************/
 				/* Zip IMAGE support for MESS */
 				if (filetype == OSD_FILETYPE_IMAGE && !_write) {
 					extension = strrchr(name, '.');		/* find extension       */
 					if (extension) *extension = '\0';	/* drop extension       */
 					sprintf(name,"%s.zip", name);		/* add .zip for zipfile */
 					if (cache_stat(name,&stat_buffer)==0) {
 						if (load_zipped_file(name, filename, &f->data, &f->length)==0) {
 							if (errorlog)
 								fprintf(errorlog,"Using (osd_fopen) zip file for %s\n", filename);
 							f->type = kZippedFile;
 							f->offset = 0;
 							f->crc = crc32 (0L, f->data, f->length);
 							found = 1;
 						}
 					}
 				}

/******************************************************/

					if (!found && !_write) {
						/* try with a .zip extension */
						sprintf(name, "%s/%s.zip", dir_name, gamename);
						if (errorlog) fprintf(errorlog,"Trying %s\n", name);
                  if (cache_stat(name,&stat_buffer)==0) {
							if (load_zipped_file(name, file, &f->data, &f->length)==0) {
								if (errorlog) fprintf(errorlog,"Using (osd_fopen) zip file for %s\n", filename);
								f->type = kZippedFile;
								f->offset = 0;
                                f->crc = crc32 (0L, f->data, f->length);
								found = 1;
							}
						}
					}

					if (!found) {
						/* try with a .zip directory (if ZipMagic is installed) */
						sprintf(name, "%s/%s.zip", dir_name, gamename);
						if (errorlog) fprintf(errorlog,"Trying %s\n", name);
                  if (cache_stat(name,&stat_buffer)==0 && (stat_buffer.st_mode & S_IFDIR)) {
							sprintf(name,"%s/%s.zip/%s",dir_name,gamename,file);
							f->file = fopen(name,_write ? "r+b" : "rb");
                            found = f->file!=0;
						}
					}
               if (found)
               {
               if (errorlog) fprintf(errorlog,"IMAGE %s FOUND in %s!\n",file,name);
               }

				}

                extension = strrchr(file, '.');
				if (extension) *extension = '\0';

			} while (!found && extension);
	      break;

#endif


		case OSD_FILETYPE_HIGHSCORE:
			if (mame_highscore_enabled()) {
				if (!found) {
					sprintf(name,"%s/%s.hi",hidir,gamename);
					f->type = kPlainFile;
					f->file = fopen(name,_write ? "wb" : "rb");
					found = f->file!=0;
				}

				if (!found) {
					/* try with a .zip directory (if ZipMagic is installed) */
					sprintf(name,"%s.zip/%s.hi",hidir,gamename);
					f->type = kPlainFile;
					f->file = fopen(name,_write ? "wb" : "rb");
					found = f->file!=0;
				}

				if (!found) {
					/* try with a .zif directory (if ZipFolders is installed) */
					sprintf(name,"%s.zif/%s.hi",hidir,gamename);
					f->type = kPlainFile;
					f->file = fopen(name,_write ? "wb" : "rb");
					found = f->file!=0;
				}
			}
			break;
		case OSD_FILETYPE_CONFIG:
			sprintf(name,"%s/%s.cfg",cfgdir,gamename);
			f->type = kPlainFile;
			f->file = fopen(name,_write ? "wb" : "rb");
			found = f->file!=0;

			if (!found) {
				/* try with a .zip directory (if ZipMagic is installed) */
				sprintf(name,"%s.zip/%s.cfg",cfgdir,gamename);
				f->type = kPlainFile;
				f->file = fopen(name,_write ? "wb" : "rb");
				found = f->file!=0;
			}

			if (!found) {
				/* try with a .zif directory (if ZipFolders is installed) */
				sprintf(name,"%s.zif/%s.cfg",cfgdir,gamename);
				f->type = kPlainFile;
				f->file = fopen(name,_write ? "wb" : "rb");
				found = f->file!=0;
			}
			break;
		case OSD_FILETYPE_INPUTLOG:
			sprintf(name,"%s/%s.inp", inpdir, gamename);
			f->type = kPlainFile;
			f->file = fopen(name,_write ? "wb" : "rb");
			found = f->file!=0;
			break;
		case OSD_FILETYPE_STATE:
			sprintf(name,"%s/%s.sta",stadir,gamename);
			f->file = fopen(name,_write ? "wb" : "rb");
			found = !(f->file == 0);
			if (!found)
			{
				/* try with a .zip directory (if ZipMagic is installed) */
				sprintf(name,"%s.zip/%s.sta",stadir,gamename);
				f->file = fopen(name,_write ? "wb" : "rb");
				found = !(f->file == 0);
            }
			if (!found)
			{
				/* try with a .zif directory (if ZipFolders is installed) */
				sprintf(name,"%s.zif/%s.sta",stadir,gamename);
				f->file = fopen(name,_write ? "wb" : "rb");
				found = !(f->file == 0);
            }
			break;
		case OSD_FILETYPE_ARTWORK:
			/* only for reading */
			if (_write)
				break;

			sprintf(name,"%s/%s", artworkdir, filename);
			f->type = kPlainFile;
			f->file = fopen(name,_write ? "wb" : "rb");
			found = f->file!=0;
			break;
		case OSD_FILETYPE_MEMCARD:
			sprintf(name, "%s/%s",memcarddir,filename);
			f->type = kPlainFile;
			f->file = fopen(name,_write ? "wb" : "rb");
			found = f->file!=0;
			break;
		case OSD_FILETYPE_SCREENSHOT:
			/* only for writing */
			if (!_write)
				break;

			sprintf(name,"%s/%s.png", screenshotdir, filename);
			f->type = kPlainFile;
			f->file = fopen(name,_write ? "wb" : "rb");
			found = f->file!=0;
			break;
	}

	if (!found) {
		free(f);
		return 0;
	}

	return f;
}
Example #19
0
/* AM 980919 update */
void *osd_fopen (const char *game, const char *filename, int filetype, int _write)
{
	char name[256];
	char *gamename;
	int found = 0;
	int indx;
	struct stat stat_buffer;
	FakeFileHandle *f;
	int pathc;
	char **pathv;


	f = (FakeFileHandle *) malloc(sizeof (FakeFileHandle));
	if( !f )
	{
		logerror("osd_fopen: failed to mallocFakeFileHandle!\n");
        return 0;
	}
	memset (f, 0, sizeof (FakeFileHandle));

	gamename = (char *) game;

	/* Support "-romdir" yuck. */
	if( alternate_name )
	{
		LOG(("osd_fopen: -romdir overrides '%s' by '%s'\n", gamename, alternate_name));
        gamename = alternate_name;
	}

	switch( filetype )
	{
	case OSD_FILETYPE_ROM:
	case OSD_FILETYPE_SAMPLE:

		/* only for reading */
		if( _write )
		{
			logerror("osd_fopen: type %02x write not supported\n",filetype);
            break;
		}

		if( filetype == OSD_FILETYPE_SAMPLE )
		{
			LOG(("osd_fopen: using samplepath\n"));
            pathc = samplepathc;
            pathv = samplepathv;
        }
		else
		{
			LOG(("osd_fopen: using rompath\n"));
            pathc = rompathc;
            pathv = rompathv;
		}

		for( indx = 0; indx < pathc && !found; ++indx )
		{
			const char *dir_name = pathv[indx];

			if( !found )
			{
				sprintf (name, "%s/%s", dir_name, gamename);
				LOG(("Trying %s\n", name));
                if( cache_stat (name, &stat_buffer) == 0 && (stat_buffer.st_mode & S_IFDIR) )
				{
					sprintf (name, "%s/%s/%s", dir_name, gamename, filename);
					if( filetype == OSD_FILETYPE_ROM )
					{
						if( checksum_file (name, &f->data, &f->length, &f->crc) == 0 )
						{
							f->type = kRAMFile;
							f->offset = 0;
							found = 1;
						}
					}
					else
					{
						f->type = kPlainFile;
						f->file = fopen (name, "rb");
						found = f->file != 0;
					}
				}
			}

			if( !found )
			{
				/* try with a .zip extension */
				sprintf (name, "%s/%s.zip", dir_name, gamename);
				LOG(("Trying %s file\n", name));
                if( cache_stat (name, &stat_buffer) == 0 )
				{
					if( load_zipped_file (name, filename, &f->data, &f->length) == 0 )
					{
						LOG(("Using (osd_fopen) zip file for %s\n", filename));
						f->type = kZippedFile;
						f->offset = 0;
						f->crc = crc32 (0L, f->data, f->length);
						found = 1;
					}
				}
			}

			if( !found )
			{
				/* try with a .zip directory (if ZipMagic is installed) */
				sprintf (name, "%s/%s.zip", dir_name, gamename);
				LOG(("Trying %s directory\n", name));
                if( cache_stat (name, &stat_buffer) == 0 && (stat_buffer.st_mode & S_IFDIR) )
				{
					sprintf (name, "%s/%s.zip/%s", dir_name, gamename, filename);
					if( filetype == OSD_FILETYPE_ROM )
					{
						if( checksum_file (name, &f->data, &f->length, &f->crc) == 0 )
						{
							f->type = kRAMFile;
							f->offset = 0;
							found = 1;
						}
					}
					else
					{
						f->type = kPlainFile;
						f->file = fopen (name, "rb");
						found = f->file != 0;
					}
				}
			}
		}

		break;

#ifdef MESS
	case OSD_FILETYPE_IMAGE_R:

		/* only for reading */
		if( _write )
		{
			logerror("osd_fopen: type %02x write not supported\n",filetype);
            break;
		}
        else
		{
			LOG(("osd_fopen: using rompath\n"));
            pathc = rompathc;
            pathv = rompathv;
		}

		LOG(("Open IMAGE_R '%s' for %s\n", filename, game));
        for( indx = 0; indx < pathc && !found; ++indx )
		{
			const char *dir_name = pathv[indx];

			/* this section allows exact path from .cfg */
			if( !found )
			{
				sprintf(name,"%s",dir_name);
				if( cache_stat(name,&stat_buffer) == 0 && (stat_buffer.st_mode & S_IFDIR) )
				{
					sprintf(name,"%s/%s",dir_name,filename);
					if( filetype == OSD_FILETYPE_ROM )
					{
						if( checksum_file (name, &f->data, &f->length, &f->crc) == 0 )
						{
							f->type = kRAMFile;
							f->offset = 0;
							found = 1;
						}
					}
					else
					{
						f->type = kPlainFile;
						f->file = fopen(name,"rb");
						found = f->file!=0;
					}
				}
			}

			if( !found )
			{
				sprintf (name, "%s/%s", dir_name, gamename);
				LOG(("Trying %s directory\n", name));
                if( cache_stat (name, &stat_buffer) == 0 && (stat_buffer.st_mode & S_IFDIR) )
				{
					sprintf (name, "%s/%s/%s", dir_name, gamename, filename);
					LOG(("Trying %s file\n", name));
                    if( filetype == OSD_FILETYPE_ROM )
					{
						if( checksum_file(name, &f->data, &f->length, &f->crc) == 0 )
						{
							f->type = kRAMFile;
							f->offset = 0;
							found = 1;
						}
					}
					else
					{
						f->type = kPlainFile;
						f->file = fopen (name, "rb");
						found = f->file != 0;
					}
				}
			}

			/* Zip cart support for MESS */
			if( !found && filetype == OSD_FILETYPE_IMAGE_R )
			{
				char *extension = strrchr (name, '.');    /* find extension */
				if( extension )
					strcpy (extension, ".zip");
				else
					strcat (name, ".zip");
				LOG(("Trying %s file\n", name));
				if( cache_stat(name, &stat_buffer) == 0 )
				{
					if( load_zipped_file(name, filename, &f->data, &f->length) == 0 )
					{
						LOG(("Using (osd_fopen) zip file for %s\n", filename));
						f->type = kZippedFile;
						f->offset = 0;
						f->crc = crc32 (0L, f->data, f->length);
						found = 1;
					}
				}
			}

			if( !found )
			{
				/* try with a .zip extension */
				sprintf (name, "%s/%s.zip", dir_name, gamename);
				LOG(("Trying %s file\n", name));
				if( cache_stat(name, &stat_buffer) == 0 )
				{
					if( load_zipped_file(name, filename, &f->data, &f->length) == 0 )
					{
						LOG(("Using (osd_fopen) zip file for %s\n", filename));
						f->type = kZippedFile;
						f->offset = 0;
						f->crc = crc32 (0L, f->data, f->length);
						found = 1;
					}
				}
			}
    	}
    break; /* end of IMAGE_R */

	case OSD_FILETYPE_IMAGE_RW:
		{
			static char *write_modes[] = {"rb","wb","r+b","r+b","w+b"};
            char file[256];
			char *extension;

			LOG(("Open IMAGE_RW '%s' for %s mode '%s'\n", filename, game, write_modes[_write]));
			strcpy (file, filename);

			do
            {
			/* 29-05-00 Lee Ward: Reversed the search order. */
            for (indx=rompathc-1; indx>=0; --indx)
			{
				const char *dir_name = rompathv[indx];

					/* Exact path support */

					/* 29-05-00 Lee Ward: Changed the search order to prevent new files
					   being created in the application root as default */

					if( !found )
					{
						sprintf (name, "%s/%s", dir_name, gamename);
						LOG(("Trying %s directory\n", name));
						if( cache_stat(name, &stat_buffer) == 0 && (stat_buffer.st_mode & S_IFDIR) )
						{
							sprintf (name, "%s/%s/%s", dir_name, gamename, file);
							LOG(("Trying %s file\n", name));
                                                        f->file = fopen (name, write_modes[_write]);
							found = f->file != 0;
							if( !found && _write == 3 )
							{
								f->file = fopen(name, write_modes[4]);
								found = f->file != 0;
                                                         }
                                                 }
					}

					/* Steph - Zip disk images support for MESS */
					if( !found && !_write )
					{
						extension = strrchr (name, '.');    /* find extension */
						/* add .zip for zipfile */
						if( extension )
							strcpy(extension, ".zip");
						else
							strcat(extension, ".zip");
						LOG(("Trying %s file\n", name));
						if( cache_stat(name, &stat_buffer) == 0 )
						{
							if( load_zipped_file(name, filename, &f->data, &f->length) == 0 )
							{
								LOG(("Using (osd_fopen) zip file for %s\n", filename));
								f->type = kZippedFile;
								f->offset = 0;
								f->crc = crc32(0L, f->data, f->length);
								found = 1;
							}
						}
					}

					if (!found)
					{
						sprintf(name, "%s", dir_name);
						LOG(("Trying %s directory\n", name));
						if( cache_stat(name,&stat_buffer) == 0 && (stat_buffer.st_mode & S_IFDIR) )
						{
							sprintf(name,"%s/%s", dir_name, file);
							LOG(("Trying %s file\n", name));
                                                        f->file = fopen(name, write_modes[_write]);
							found = f->file != 0;
							if( !found && _write == 3 )
							{
								f->file = fopen(name, write_modes[4]);
								found = f->file != 0;
                                                         }
						}
					}

                    if( !found && !_write )
                    {
                        extension = strrchr (name, '.');    /* find extension */
                        /* add .zip for zipfile */
                        if( extension )
                            strcpy(extension, ".zip");
                        else
                            strcat(extension, ".zip");
						LOG(("Trying %s file\n", name));
						if( cache_stat(name, &stat_buffer) == 0 )
                        {
							if( load_zipped_file(name, filename, &f->data, &f->length) == 0 )
                            {
                                LOG(("Using (osd_fopen) zip file for %s\n", filename));
                                f->type = kZippedFile;
                                f->offset = 0;
								f->crc = crc32(0L, f->data, f->length);
                                found = 1;
                            }
                        }
                    }

					if( !found && !_write )
					{
						/* try with a .zip extension */
						sprintf (name, "%s/%s.zip", dir_name, gamename);
						LOG(("Trying %s file\n", name));
						if( cache_stat (name, &stat_buffer) == 0 )
						{
							if( load_zipped_file (name, file, &f->data, &f->length) == 0 )
							{
								LOG(("Using (osd_fopen) zip file for %s\n", filename));
								f->type = kZippedFile;
								f->offset = 0;
								f->crc = crc32 (0L, f->data, f->length);
								found = 1;
							}
						}
					}

					if( !found )
					{
						/* try with a .zip directory (if ZipMagic is installed) */
						sprintf (name, "%s/%s.zip", dir_name, gamename);
						LOG(("Trying %s ZipMagic directory\n", name));
						if( cache_stat (name, &stat_buffer) == 0 && (stat_buffer.st_mode & S_IFDIR) )
						{
							sprintf (name, "%s/%s.zip/%s", dir_name, gamename, file);
							LOG(("Trying %s\n", name));
							f->file = fopen (name, write_modes[_write]);
							found = f->file != 0;
							if( !found && _write == 3 )
							{
								f->file = fopen(name, write_modes[4]);
								found = f->file != 0;
                            }
                        }
					}
					if( found )
						LOG(("IMAGE_RW %s FOUND in %s!\n", file, name));
				}

				extension = strrchr (file, '.');
				if( extension )
					*extension = '\0';
			} while( !found && extension );
		}
		break;
#endif	/* MESS */


	case OSD_FILETYPE_NVRAM:
		if( !found )
		{
			sprintf (name, "%s/%s.nv", nvdir, gamename);
			f->type = kPlainFile;
			f->file = fopen (name, _write ? "wb" : "rb");
			found = f->file != 0;
		}

		if( !found )
		{
			/* try with a .zip directory (if ZipMagic is installed) */
			sprintf (name, "%s.zip/%s.nv", nvdir, gamename);
			f->type = kPlainFile;
			f->file = fopen (name, _write ? "wb" : "rb");
			found = f->file != 0;
		}

		if( !found )
		{
			/* try with a .zif directory (if ZipFolders is installed) */
			sprintf (name, "%s.zif/%s.nv", nvdir, gamename);
			f->type = kPlainFile;
			f->file = fopen (name, _write ? "wb" : "rb");
			found = f->file != 0;
		}
		break;

	case OSD_FILETYPE_HIGHSCORE:
		if( mame_highscore_enabled () )
		{
			if( !found )
			{
				sprintf (name, "%s/%s.hi", hidir, gamename);
				f->type = kPlainFile;
				f->file = fopen (name, _write ? "wb" : "rb");
				found = f->file != 0;
			}

			if( !found )
			{
				/* try with a .zip directory (if ZipMagic is installed) */
				sprintf (name, "%s.zip/%s.hi", hidir, gamename);
				f->type = kPlainFile;
				f->file = fopen (name, _write ? "wb" : "rb");
				found = f->file != 0;
			}

			if( !found )
			{
				/* try with a .zif directory (if ZipFolders is installed) */
				sprintf (name, "%s.zif/%s.hi", hidir, gamename);
				f->type = kPlainFile;
				f->file = fopen (name, _write ? "wb" : "rb");
				found = f->file != 0;
			}
		}
		break;

    case OSD_FILETYPE_CONFIG:
		sprintf (name, "%s/%s.cfg", cfgdir, gamename);
		f->type = kPlainFile;
		f->file = fopen (name, _write ? "wb" : "rb");
		found = f->file != 0;

		if( !found )
		{
			/* try with a .zip directory (if ZipMagic is installed) */
			sprintf (name, "%s.zip/%s.cfg", cfgdir, gamename);
			f->type = kPlainFile;
			f->file = fopen (name, _write ? "wb" : "rb");
			found = f->file != 0;
		}

		if( !found )
		{
			/* try with a .zif directory (if ZipFolders is installed) */
			sprintf (name, "%s.zif/%s.cfg", cfgdir, gamename);
			f->type = kPlainFile;
			f->file = fopen (name, _write ? "wb" : "rb");
			found = f->file != 0;
		}
		break;

	case OSD_FILETYPE_INPUTLOG:
		sprintf (name, "%s/%s.inp", inpdir, gamename);
		f->type = kPlainFile;
		f->file = fopen (name, _write ? "wb" : "rb");
		found = f->file != 0;

        if( !found )
		{
			/* try with a .zip directory (if ZipMagic is installed) */
			sprintf (name, "%s.zip/%s.cfg", inpdir, gamename);
			f->type = kPlainFile;
			f->file = fopen (name, _write ? "wb" : "rb");
			found = f->file != 0;
		}

		if( !found )
		{
			/* try with a .zif directory (if ZipFolders is installed) */
			sprintf (name, "%s.zif/%s.cfg", inpdir, gamename);
			f->type = kPlainFile;
			f->file = fopen (name, _write ? "wb" : "rb");
			found = f->file != 0;
        }

		if( !_write )
		{
			char file[256];
			sprintf (file, "%s.inp", gamename);
            sprintf (name, "%s/%s.zip", inpdir, gamename);
			LOG(("Trying %s in %s\n", file, name));
            if( cache_stat (name, &stat_buffer) == 0 )
			{
				if( load_zipped_file (name, file, &f->data, &f->length) == 0 )
				{
					LOG(("Using (osd_fopen) zip file %s for %s\n", name, file));
					f->type = kZippedFile;
					f->offset = 0;
					found = 1;
				}
			}
		}

        break;

	case OSD_FILETYPE_STATE:
		sprintf (name, "%s/%s.sta", stadir, gamename);
		f->file = fopen (name, _write ? "wb" : "rb");
		found = !(f->file == 0);
		if( !found )
		{
			/* try with a .zip directory (if ZipMagic is installed) */
			sprintf (name, "%s.zip/%s.sta", stadir, gamename);
			f->file = fopen (name, _write ? "wb" : "rb");
			found = !(f->file == 0);
		}
		if( !found )
		{
			/* try with a .zif directory (if ZipFolders is installed) */
			sprintf (name, "%s.zif/%s.sta", stadir, gamename);
			f->file = fopen (name, _write ? "wb" : "rb");
			found = !(f->file == 0);
		}
		break;

	case OSD_FILETYPE_ARTWORK:
		/* only for reading */
		if( _write )
		{
			logerror("osd_fopen: type %02x write not supported\n",filetype);
            break;
		}
		sprintf (name, "%s/%s", artworkdir, filename);
		f->type = kPlainFile;
		f->file = fopen (name, _write ? "wb" : "rb");
		found = f->file != 0;
		if( !found )
		{
			/* try with a .zip directory (if ZipMagic is installed) */
			sprintf (name, "%s.zip/%s.png", artworkdir, filename);
			f->type = kPlainFile;
			f->file = fopen (name, _write ? "wb" : "rb");
			found = f->file != 0;
		}

		if( !found )
		{
			/* try with a .zif directory (if ZipFolders is installed) */
			sprintf (name, "%s.zif/%s.png", artworkdir, filename);
			f->type = kPlainFile;
			f->file = fopen (name, _write ? "wb" : "rb");
			found = f->file != 0;
        }

		if( !found )
		{
			char file[256], *extension;
			sprintf(file, "%s", filename);
            sprintf(name, "%s/%s", artworkdir, filename);
            extension = strrchr(name, '.');
			if( extension )
				strcpy (extension, ".zip");
			else
				strcat (name, ".zip");
			LOG(("Trying %s in %s\n", file, name));
            if( cache_stat (name, &stat_buffer) == 0 )
			{
				if( load_zipped_file (name, file, &f->data, &f->length) == 0 )
				{
					LOG(("Using (osd_fopen) zip file %s\n", name));
					f->type = kZippedFile;
					f->offset = 0;
					found = 1;
				}
			}
			if( !found )
			{
				sprintf(name, "%s/%s.zip", artworkdir, game);
				LOG(("Trying %s in %s\n", file, name));
				if( cache_stat (name, &stat_buffer) == 0 )
				{
					if( load_zipped_file (name, file, &f->data, &f->length) == 0 )
					{
						LOG(("Using (osd_fopen) zip file %s\n", name));
						f->type = kZippedFile;
						f->offset = 0;
						found = 1;
					}
				}
            }
        }
        break;

	case OSD_FILETYPE_MEMCARD:
		sprintf (name, "%s/%s", memcarddir, filename);
		f->type = kPlainFile;
		f->file = fopen (name, _write ? "wb" : "rb");
		found = f->file != 0;
		break;

	case OSD_FILETYPE_SCREENSHOT:
		/* only for writing */
		if( !_write )
		{
			logerror("osd_fopen: type %02x read not supported\n",filetype);
			break;
		}

		sprintf (name, "%s/%s.png", screenshotdir, filename);
		f->type = kPlainFile;
		f->file = fopen (name, _write ? "wb" : "rb");
		found = f->file != 0;
		break;

	case OSD_FILETYPE_HIGHSCORE_DB:
	case OSD_FILETYPE_HISTORY:
		/* only for reading */
		if( _write )
		{
			logerror("osd_fopen: type %02x write not supported\n",filetype);
            break;
		}
		f->type = kPlainFile;
		/* open as ASCII files, not binary like the others */
		f->file = fopen (filename, _write ? "w" : "r");
		found = f->file != 0;
        break;

	/* Steph */
	case OSD_FILETYPE_CHEAT:
		sprintf (name, "%s/%s", cheatdir, filename);
		f->type = kPlainFile;
		/* open as ASCII files, not binary like the others */
		f->file = fopen (filename, _write ? "a" : "r");
		found = f->file != 0;
        break;

	case OSD_FILETYPE_LANGUAGE:
		/* only for reading */
		if( _write )
		{
			logerror("osd_fopen: type %02x write not supported\n",filetype);
            break;
		}
		sprintf (name, "%s.lng", filename);
		f->type = kPlainFile;
		/* open as ASCII files, not binary like the others */
		f->file = fopen (name, _write ? "w" : "r");
		found = f->file != 0;
logerror("fopen %s = %08x\n",name,(int)f->file);
        break;

	default:
		logerror("osd_fopen(): unknown filetype %02x\n",filetype);
	}

	if( !found )
	{
		free(f);
		return 0;
	}

	return f;
}