Original Supermodel code moved to tags/Original.

This commit is contained in:
Bart Trzynadlowski 2011-04-24 01:06:44 +00:00
parent a390d45c77
commit 1848a427ec
5 changed files with 0 additions and 2410 deletions

184
config.c
View file

@ -1,184 +0,0 @@
#include "model3.h"
#include "expat.h"
enum
{
ELEMENT_NONE = 1,
ELEMENT_CONFIG,
ELEMENT_ROMPATH,
ELEMENT_GAMELIST,
ELEMENT_BACKUPPATH,
ELEMENT_WIDTH,
ELEMENT_HEIGHT,
ELEMENT_FULLSCREEN,
ELEMENT_FPSLIMIT,
ELEMENT_STRETCH,
};
static STRING_ID element_id[] =
{
{ "config", ELEMENT_CONFIG },
{ "rom_path", ELEMENT_ROMPATH },
{ "gamelist", ELEMENT_GAMELIST },
{ "backup_path", ELEMENT_BACKUPPATH },
{ "width", ELEMENT_WIDTH },
{ "height", ELEMENT_HEIGHT },
{ "fullscreen", ELEMENT_FULLSCREEN },
{ "fps_limit", ELEMENT_FPSLIMIT },
{ "stretch", ELEMENT_STRETCH },
{ "", 0 },
};
static STRING_ID boolean_id[] =
{
{ "yes", TRUE },
{ "no", FALSE },
{ "", 0 },
};
static UINT8 *config_file;
static int current_element;
static void XMLCALL element_start(void *data, const char *el, const char **attr)
{
int element = get_string_id(el, element_id);
if (element < 0)
{
message(0, "Unknown element %s while parsing XML file\n", el);
}
else
{
current_element = element;
}
}
static void XMLCALL element_end(void *data, const char *el)
{
int element = get_string_id(el, element_id);
if (element < 0)
{
message(0, "Unknown element %s while parsing XML\n", el);
}
else
{
switch (element)
{
case ELEMENT_CONFIG:
case ELEMENT_ROMPATH:
case ELEMENT_GAMELIST:
case ELEMENT_BACKUPPATH:
case ELEMENT_WIDTH:
case ELEMENT_HEIGHT:
case ELEMENT_FULLSCREEN:
case ELEMENT_FPSLIMIT:
case ELEMENT_STRETCH:
{
current_element = ELEMENT_NONE;
break;
}
}
}
}
static void XMLCALL character_data(void *data, const char *s, int len)
{
char temp[2000];
memcpy(temp, s, len);
temp[len] = 0;
switch (current_element)
{
case ELEMENT_ROMPATH:
{
strcpy(m3_config.rom_path, temp);
break;
}
case ELEMENT_GAMELIST:
{
strcpy(m3_config.rom_list, temp);
break;
}
case ELEMENT_BACKUPPATH:
{
strcpy(m3_config.backup_path, temp);
break;
}
case ELEMENT_WIDTH:
{
int width;
sscanf(temp, "%d", &width);
m3_config.width = width;
break;
}
case ELEMENT_HEIGHT:
{
int height;
sscanf(temp, "%d", &height);
m3_config.height = height;
break;
}
case ELEMENT_FULLSCREEN:
{
BOOL fullscreen = get_string_id(temp, boolean_id);
m3_config.fullscreen = fullscreen;
break;
}
case ELEMENT_FPSLIMIT:
{
BOOL fpslimit = get_string_id(temp, boolean_id);
m3_config.fps_limit = fpslimit;
break;
}
case ELEMENT_STRETCH:
{
BOOL stretch = get_string_id(temp, boolean_id);
m3_config.stretch = stretch;
break;
}
}
}
BOOL parse_config(const char *config_name)
{
XML_Parser parser;
int length;
FILE *file;
file = open_file(FILE_READ|FILE_BINARY, "%s", config_name);
if (file == NULL)
{
message(0, "Couldn't open %s", config_name);
return FALSE;
}
length = (int)get_open_file_size(file);
config_file = (UINT8*)malloc(length);
if (!read_from_file(file, config_file, length))
{
message(0, "I/O error while reading %s", config_name);
return FALSE;
}
close_file(file);
// parse the XML file
parser = XML_ParserCreate(NULL);
XML_SetElementHandler(parser, element_start, element_end);
XML_SetCharacterDataHandler(parser, character_data);
if (XML_Parse(parser, config_file, length, 1) != XML_STATUS_OK)
{
message(0, "Error while parsing the XML file %s", config_name);
return FALSE;
}
XML_ParserFree(parser);
return TRUE;
}

View file

@ -1,22 +0,0 @@
<?xml version="1.0"?>
<!DOCTYPE config [
<!ELEMENT config (rom_path?, gamelist?, backup_path?, width?, height?, fullscreen?, fps_limit?)>
<!ELEMENT rom_path (#PCDATA)>
<!ELEMENT gamelist (#PCDATA)>
<!ELEMENT backup_path (#PCDATA)>
<!ELEMENT width (#PCDATA)>
<!ELEMENT height (#PCDATA)>
<!ELEMENT fullscreen (#PCDATA)>
<!ELEMENT fps_limit (#PCDATA)>
]>
<config>
<rom_path>c:\devmame\roms</rom_path>
<gamelist>games.xml</gamelist>
<backup_path>c:\code\supermodel2\backup</backup_path>
<width>1280</width>
<height>1024</height>
<fullscreen>yes</fullscreen>
<fps_limit>no</fps_limit>
<stretch>yes</stretch>
</config>

499
file.c
View file

@ -1,499 +0,0 @@
/*
* Sega Model 3 Emulator
* Copyright (C) 2003 Bart Trzynadlowski, Ville Linde, Stefano Teso
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License Version 2 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program (license.txt); if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* file.c
*
* File access and ZIP file support
*
*/
#include "model3.h"
#include "file.h"
#include "unzip/unzip.h" /* ZLIB */
static unzFile zip_file;
/* zip_get_file_size
*
* Gets the uncompressed size of a file in the ZIP file
* Returns the file size or 0 if the file is not found (or the file size is 0)
*/
static UINT32 zip_get_file_size(const char* file)
{
unz_file_info file_info;
if( unzLocateFile(zip_file, file, 2) != UNZ_OK )
return 0;
if( unzGetCurrentFileInfo(zip_file, &file_info, NULL, 0, NULL, 0, NULL, 0) != UNZ_OK )
return 0;
return file_info.uncompressed_size;
}
/* zip_read_file
*
* Reads the specified file to a buffer
* Returns TRUE if successful otherwise FALSE
*/
static BOOL zip_read_file(const char* file, UINT8* buffer, UINT32 size)
{
if( unzLocateFile(zip_file, file, 2) != UNZ_OK )
return FALSE;
if( unzOpenCurrentFile(zip_file) != UNZ_OK )
return FALSE;
if( unzReadCurrentFile(zip_file, buffer, size) < 0 ) {
unzCloseCurrentFile(zip_file);
return FALSE;
}
unzCloseCurrentFile(zip_file);
return TRUE;
}
/* zip_find_name_for_crc
*
* Finds a matching file name for a crc
* Returns TRUE if crc was found. The file name is copied to the supplied string pointer.
*
*/
static BOOL zip_find_name_for_crc(UINT32 crc, char* file)
{
unz_file_info file_info;
char filename[4096];
if( unzGoToFirstFile(zip_file) != UNZ_OK )
return FALSE;
do
{
if( unzGetCurrentFileInfo(zip_file, &file_info, filename, sizeof(filename), NULL, 0, NULL, 0) != UNZ_OK )
return FALSE;
if( file_info.crc == crc ) {
strcpy( file, filename );
return TRUE;
}
} while( unzGoToNextFile(zip_file) == UNZ_OK );
return FALSE;
}
/* zip_open
*
* Opens a zip file. Must be called before any of the above functions !
*/
static BOOL zip_open(const char* file)
{
zip_file = unzOpen(file);
if( zip_file == NULL )
return FALSE;
return TRUE;
}
/* zip_close
*
* Closes the zip file
*/
static void zip_close(void)
{
unzClose(zip_file);
}
/* File access functions */
static BOOL zip_directory = FALSE;
static char directory_path[4096];
/* TODO: set_directory and set_directory_zip could be merged together */
/* set_directory
*
* Sets the current directory
*
*/
BOOL set_directory(char* path, ...)
{
char string[4096];
va_list l;
va_start(l, path);
vsprintf(string, path, l);
va_end(l);
zip_directory = FALSE;
strcpy( directory_path, string );
return TRUE;
}
/* set_directory_zip
*
* Sets a zip file as current directory
*
*/
BOOL set_directory_zip(char* file, ...)
{
char string[4096];
va_list l;
va_start(l, file);
vsprintf(string, file, l);
va_end(l);
/* Close the old zip file */
zip_close();
if( !zip_open(string) )
return FALSE;
zip_directory = TRUE;
strcpy( directory_path, string );
return TRUE;
}
/* get_file_size
*
* Returns the file size
*
*/
size_t get_file_size(const char* file)
{
FILE* f;
size_t length = 0;
char path[4096];
if( zip_directory ) {
length = zip_get_file_size(file);
}
else {
sprintf( path, "%s/%s", directory_path, file );
f = fopen( path, "rb" );
if( f == NULL )
return 0;
fseek( f, 0, SEEK_END );
length = ftell(f);
fclose(f);
}
return length;
}
/* read_file
*
* Reads the content of the file to a buffer
*
*/
BOOL read_file(const char* file, UINT8* buffer, UINT32 size)
{
FILE* f;
char path[4096];
if( zip_directory ) {
return zip_read_file( file, buffer, size );
}
else {
/* Build a full path to file */
strcpy( path, directory_path );
strcat( path, "/" );
strcat( path, file );
f = fopen( path, "rb" );
if( f == NULL )
return FALSE;
fseek( f, 0, SEEK_SET );
if( fread(buffer, sizeof(UINT8), size, f) != (size_t)size ) {
fclose(f);
return FALSE;
}
fclose(f);
}
return TRUE;
}
/* write_file
*
* Writes the contents of a buffer to a file
*
*/
BOOL write_file(const char* file, UINT8* buffer, UINT32 size)
{
FILE* f;
char path[4096];
/* Whoops. No saving to a zip file :) */
if( zip_directory )
return FALSE;
/* Build a full path to file */
strcpy( path, directory_path );
strcat( path, "/" );
strcat( path, file );
f = fopen( path, "wb" );
fseek( f, 0, SEEK_SET );
if( fwrite(buffer, sizeof(UINT8), size, f) != (size_t)size ) {
fclose(f);
return FALSE;
}
fclose(f);
return TRUE;
}
/* get_file_size_crc
*
* Returns the file size of a file specified by crc
*
*/
size_t get_file_size_crc(UINT32 crc)
{
char file[4096];
/* only zip files supported */
if( !zip_directory )
return 0;
if( !zip_find_name_for_crc(crc, file) )
return 0;
return zip_get_file_size(file);
}
/* read_file_crc
*
* Reads the contents of a file specified by crc
*
*/
BOOL read_file_crc(UINT32 crc, UINT8* buffer, UINT32 size)
{
char file[4096];
/* only zip files supported */
if( !zip_directory )
return FALSE;
if( !zip_find_name_for_crc(crc, file) )
return FALSE;
return zip_read_file(file, buffer, size);
}
/* open_file
*
* Opens a file
*
*/
FILE* open_file(UINT32 flags, char* file, ...)
{
char string[4096];
char path[4096];
char mode[3];
char* m = mode;
va_list l;
/* zip files are not supported this way */
if( zip_directory )
return 0;
va_start(l, file);
vsprintf(string, file, l);
va_end(l);
/* Build full path */
if (strlen(directory_path) <= 0)
{
sprintf(path, "%s", string);
}
else
{
sprintf(path, "%s/%s", directory_path, string);
}
/* Set file access mode */
if( flags & FILE_READ )
sprintf(&m[0], "r");
else
sprintf(&m[0], "w");
if( flags & FILE_BINARY )
sprintf(&m[1], "b" );
else
sprintf(&m[1], "t" );
m[2] = '\0';
return fopen(path, m);
}
/* close_file
*
* Closes the file
*
*/
void close_file(FILE* file)
{
fclose(file);
}
/* read_from_file
*
* Reads n bytes from file to a buffer
*
*/
BOOL read_from_file(FILE* file, UINT8* buffer, UINT32 size)
{
if( file == NULL )
return FALSE;
if( fread(buffer, sizeof(UINT8), size, file) != (size_t)size )
return FALSE;
return TRUE;
}
/* write_to_file
*
* Writes n bytes to file
*
*/
BOOL write_to_file(FILE* file, UINT8* buffer, UINT32 size)
{
if( file == NULL )
return FALSE;
if( fwrite(buffer, sizeof(UINT8), size, file) != (size_t)size )
return FALSE;
return TRUE;
}
/* get_file_size
*
* Gets the file size
*
*/
size_t get_open_file_size(FILE *file)
{
size_t length;
int current_pos = ftell(file);
fseek(file, 0, SEEK_END);
length = ftell(file);
fseek(file, current_pos, SEEK_SET);
return length;
}
/*
* Load a file to a buffer.
*/
int load_file(char *path, UINT8 * dest, int size)
{
FILE * fp;
int length;
if(path == NULL || dest == NULL)
return(-1);
if((fp = fopen(path, "rb")) == NULL)
return(-1);
fseek(fp, 0, SEEK_END);
length = ftell(fp);
fseek(fp, 0, SEEK_SET);
if (length <= 0)
return -1;
if (fread(dest, 1, size, fp) != (size_t)length) // file size mismatch
return -1;
return length;
}
static void word_swap(UINT8 *src, int size)
{
while (size -= 4)
{
*((UINT32 *) src) = BSWAP32(*((UINT32 *) src));
src += sizeof(UINT32);
}
}
/*
* Save a buffer to a file.
*/
void save_file(char *path, UINT8 *src, int size, BOOL byte_reversed)
{
FILE * fp;
if (byte_reversed)
{
word_swap(src, size);
}
fp = fopen(path, "wb");
if(fp != NULL)
{
fwrite(src, 1, size, fp);
fclose(fp);
message(0, "Wrote %s", path);
}
else
{
message(0, "Failed to write %s", path);
}
if (byte_reversed)
{
word_swap(src, size);
}
}

50
file.h
View file

@ -1,50 +0,0 @@
/*
* Sega Model 3 Emulator
* Copyright (C) 2003 Bart Trzynadlowski, Ville Linde, Stefano Teso
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License Version 2 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program (license.txt); if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef INCLUDED_FILE_H
#define INCLUDED_FILE_H
#include "model3.h"
#define FILE_READ 0x1
#define FILE_WRITE 0x2
#define FILE_BINARY 0x4
#define FILE_TEXT 0x8
/* Functions */
FILE* open_file(UINT32, char*, ...);
void close_file(FILE*);
BOOL read_from_file(FILE*, UINT8*, UINT32);
BOOL write_to_file(FILE*, UINT8*, UINT32);
size_t get_open_file_size(FILE *);
size_t get_file_size(const char*);
BOOL read_file(const char*, UINT8*, UINT32);
BOOL write_file(const char*, UINT8*, UINT32);
size_t get_file_size_crc(UINT32);
BOOL read_file_crc(UINT32, UINT8*, UINT32);
BOOL set_directory(char*, ...);
BOOL set_directory_zip(char*, ...);
int load_file(char *path, UINT8 * dest, int size);
void save_file(char *path, UINT8 *src, int size, BOOL byte_reversed);
#endif /* INCLUDED_FILE_H */

1655
games.xml

File diff suppressed because it is too large Load diff