| 1 |
/**************************************************************************** |
| 2 |
* datafile.c |
| 3 |
* History database engine |
| 4 |
* |
| 5 |
* Token parsing by Neil Bradley |
| 6 |
* Modifications and higher-level functions by John Butler |
| 7 |
****************************************************************************/ |
| 8 |
|
| 9 |
#include <assert.h> |
| 10 |
#include <ctype.h> |
| 11 |
#include "osd_cpu.h" |
| 12 |
#include "driver.h" |
| 13 |
#include "datafile.h" |
| 14 |
|
| 15 |
|
| 16 |
/**************************************************************************** |
| 17 |
* token parsing constants |
| 18 |
****************************************************************************/ |
| 19 |
#ifndef TRUE |
| 20 |
#define TRUE 1 |
| 21 |
#endif |
| 22 |
|
| 23 |
#ifndef FALSE |
| 24 |
#define FALSE 0 |
| 25 |
#endif |
| 26 |
|
| 27 |
#define CR 0x0d /* '\n' and '\r' meanings are swapped in some */ |
| 28 |
#define LF 0x0a /* compilers (e.g., Mac compilers) */ |
| 29 |
|
| 30 |
enum |
| 31 |
{ |
| 32 |
TOKEN_COMMA, |
| 33 |
TOKEN_EQUALS, |
| 34 |
TOKEN_SYMBOL, |
| 35 |
TOKEN_LINEBREAK, |
| 36 |
TOKEN_INVALID=-1 |
| 37 |
}; |
| 38 |
|
| 39 |
#define MAX_TOKEN_LENGTH 256 |
| 40 |
|
| 41 |
|
| 42 |
/**************************************************************************** |
| 43 |
* datafile constants |
| 44 |
****************************************************************************/ |
| 45 |
#ifdef MAME_FIX |
| 46 |
static int MAX_DATAFILE_ENTRIES = 0; |
| 47 |
static void set_datafile_entries(void); |
| 48 |
#ifdef MAME32JP |
| 49 |
#define MAX_MENUIDX_ENTRIES 64 |
| 50 |
#endif |
| 51 |
#else |
| 52 |
#define MAX_DATAFILE_ENTRIES 3000 |
| 53 |
#endif |
| 54 |
#define DATAFILE_TAG '$' |
| 55 |
|
| 56 |
const char *DATAFILE_TAG_KEY = "$info"; |
| 57 |
const char *DATAFILE_TAG_BIO = "$bio"; |
| 58 |
const char *DATAFILE_TAG_MAME = "$mame"; |
| 59 |
#ifdef MAME32JP |
| 60 |
const char *DATAFILE_TAG_COMMAND = "$cmd"; |
| 61 |
const char *DATAFILE_TAG_END = "$end"; |
| 62 |
#endif |
| 63 |
|
| 64 |
const char *history_filename = NULL; |
| 65 |
const char *mameinfo_filename = NULL; |
| 66 |
#ifdef MAME32JP |
| 67 |
const char *command_filename = NULL; |
| 68 |
#endif |
| 69 |
|
| 70 |
#ifdef MAME_FIX |
| 71 |
static struct tDatafileIndex *hist_idx = 0; |
| 72 |
static struct tDatafileIndex *mame_idx = 0; |
| 73 |
#ifdef MAME32JP |
| 74 |
static struct tDatafileIndex *cmnd_idx = 0; |
| 75 |
static struct tMenuIndex *menu_idx = 0; |
| 76 |
|
| 77 |
static int mame32jp_wrap; |
| 78 |
#endif |
| 79 |
#endif |
| 80 |
|
| 81 |
/**************************************************************************** |
| 82 |
* private data for parsing functions |
| 83 |
****************************************************************************/ |
| 84 |
static void *fp; /* Our file pointer */ |
| 85 |
static long dwFilePos; /* file position */ |
| 86 |
static UINT8 bToken[MAX_TOKEN_LENGTH]; /* Our current token */ |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
/************************************************************************** |
| 91 |
************************************************************************** |
| 92 |
* |
| 93 |
* Parsing functions |
| 94 |
* |
| 95 |
************************************************************************** |
| 96 |
**************************************************************************/ |
| 97 |
|
| 98 |
/**************************************************************************** |
| 99 |
* GetNextToken - Pointer to the token string pointer |
| 100 |
* Pointer to position within file |
| 101 |
* |
| 102 |
* Returns token, or TOKEN_INVALID if at end of file |
| 103 |
****************************************************************************/ |
| 104 |
static UINT32 GetNextToken(UINT8 **ppszTokenText, long *pdwPosition) |
| 105 |
{ |
| 106 |
UINT32 dwLength; /* Length of symbol */ |
| 107 |
long dwPos; /* Temporary position */ |
| 108 |
UINT8 *pbTokenPtr = bToken; /* Point to the beginning */ |
| 109 |
UINT8 bData; /* Temporary data byte */ |
| 110 |
#ifdef MAME32JP |
| 111 |
UINT8 space, character; |
| 112 |
|
| 113 |
if (mame32jp_wrap) |
| 114 |
{ |
| 115 |
space = '\t'; |
| 116 |
character = ' ' - 1; |
| 117 |
} |
| 118 |
else |
| 119 |
{ |
| 120 |
space = ' '; |
| 121 |
character = ' '; |
| 122 |
} |
| 123 |
#endif |
| 124 |
|
| 125 |
while (1) |
| 126 |
{ |
| 127 |
bData = osd_fgetc(fp); /* Get next character */ |
| 128 |
|
| 129 |
/* If we're at the end of the file, bail out */ |
| 130 |
|
| 131 |
if (osd_feof(fp)) |
| 132 |
return(TOKEN_INVALID); |
| 133 |
|
| 134 |
/* If it's not whitespace, then let's start eating characters */ |
| 135 |
|
| 136 |
#ifdef MAME32JP |
| 137 |
if (space != bData && '\t' != bData) |
| 138 |
#else |
| 139 |
if (' ' != bData && '\t' != bData) |
| 140 |
#endif |
| 141 |
{ |
| 142 |
/* Store away our file position (if given on input) */ |
| 143 |
|
| 144 |
if (pdwPosition) |
| 145 |
*pdwPosition = dwFilePos; |
| 146 |
|
| 147 |
/* If it's a separator, special case it */ |
| 148 |
|
| 149 |
if (',' == bData || '=' == bData) |
| 150 |
{ |
| 151 |
*pbTokenPtr++ = bData; |
| 152 |
*pbTokenPtr = '\0'; |
| 153 |
++dwFilePos; |
| 154 |
|
| 155 |
if (',' == bData) |
| 156 |
return(TOKEN_COMMA); |
| 157 |
else |
| 158 |
return(TOKEN_EQUALS); |
| 159 |
} |
| 160 |
|
| 161 |
/* Otherwise, let's try for a symbol */ |
| 162 |
|
| 163 |
#ifdef MAME32JP |
| 164 |
if (bData > character) |
| 165 |
#else |
| 166 |
if (bData > ' ') |
| 167 |
#endif |
| 168 |
{ |
| 169 |
dwLength = 0; /* Assume we're 0 length to start with */ |
| 170 |
|
| 171 |
/* Loop until we've hit something we don't understand */ |
| 172 |
|
| 173 |
while (bData != ',' && |
| 174 |
bData != '=' && |
| 175 |
#ifdef MAME32JP |
| 176 |
bData != space && |
| 177 |
#else |
| 178 |
bData != ' ' && |
| 179 |
#endif |
| 180 |
bData != '\t' && |
| 181 |
bData != '\n' && |
| 182 |
bData != '\r' && |
| 183 |
osd_feof(fp) == 0) |
| 184 |
{ |
| 185 |
++dwFilePos; |
| 186 |
*pbTokenPtr++ = bData; /* Store our byte */ |
| 187 |
++dwLength; |
| 188 |
assert(dwLength < MAX_TOKEN_LENGTH); |
| 189 |
bData = osd_fgetc(fp); |
| 190 |
} |
| 191 |
|
| 192 |
/* If it's not the end of the file, put the last received byte */ |
| 193 |
/* back. We don't want to touch the file position, though if */ |
| 194 |
/* we're past the end of the file. Otherwise, adjust it. */ |
| 195 |
|
| 196 |
if (0 == osd_feof(fp)) |
| 197 |
{ |
| 198 |
osd_ungetc(bData, fp); |
| 199 |
} |
| 200 |
|
| 201 |
/* Null terminate the token */ |
| 202 |
|
| 203 |
*pbTokenPtr = '\0'; |
| 204 |
|
| 205 |
/* Connect up the */ |
| 206 |
|
| 207 |
if (ppszTokenText) |
| 208 |
*ppszTokenText = bToken; |
| 209 |
|
| 210 |
return(TOKEN_SYMBOL); |
| 211 |
} |
| 212 |
|
| 213 |
/* Not a symbol. Let's see if it's a cr/cr, lf/lf, or cr/lf/cr/lf */ |
| 214 |
/* sequence */ |
| 215 |
|
| 216 |
if (LF == bData) |
| 217 |
{ |
| 218 |
/* Unix style perhaps? */ |
| 219 |
|
| 220 |
bData = osd_fgetc(fp); /* Peek ahead */ |
| 221 |
osd_ungetc(bData, fp); /* Force a retrigger if subsequent LF's */ |
| 222 |
|
| 223 |
if (LF == bData) /* Two LF's in a row - it's a UNIX hard CR */ |
| 224 |
{ |
| 225 |
++dwFilePos; |
| 226 |
*pbTokenPtr++ = bData; /* A real linefeed */ |
| 227 |
*pbTokenPtr = '\0'; |
| 228 |
return(TOKEN_LINEBREAK); |
| 229 |
} |
| 230 |
|
| 231 |
/* Otherwise, fall through and keep parsing. */ |
| 232 |
|
| 233 |
} |
| 234 |
else |
| 235 |
if (CR == bData) /* Carriage return? */ |
| 236 |
{ |
| 237 |
/* Figure out if it's Mac or MSDOS format */ |
| 238 |
|
| 239 |
++dwFilePos; |
| 240 |
bData = osd_fgetc(fp); /* Peek ahead */ |
| 241 |
|
| 242 |
/* We don't need to bother with EOF checking. It will be 0xff if */ |
| 243 |
/* it's the end of the file and will be caught by the outer loop. */ |
| 244 |
|
| 245 |
if (CR == bData) /* Mac style hard return! */ |
| 246 |
{ |
| 247 |
/* Do not advance the file pointer in case there are successive */ |
| 248 |
/* CR/CR sequences */ |
| 249 |
|
| 250 |
/* Stuff our character back upstream for successive CR's */ |
| 251 |
|
| 252 |
osd_ungetc(bData, fp); |
| 253 |
|
| 254 |
*pbTokenPtr++ = bData; /* A real carriage return (hard) */ |
| 255 |
*pbTokenPtr = '\0'; |
| 256 |
return(TOKEN_LINEBREAK); |
| 257 |
} |
| 258 |
else |
| 259 |
if (LF == bData) /* MSDOS format! */ |
| 260 |
{ |
| 261 |
#ifdef MAME32JP |
| 262 |
if (mame32jp_wrap) |
| 263 |
{ |
| 264 |
osd_ungetc(bData, fp); |
| 265 |
|
| 266 |
*pbTokenPtr++ = bData; /* A real carriage return (hard) */ |
| 267 |
*pbTokenPtr = '\0'; |
| 268 |
|
| 269 |
return(TOKEN_LINEBREAK); |
| 270 |
} |
| 271 |
else |
| 272 |
{ |
| 273 |
#endif |
| 274 |
++dwFilePos; /* Our file position to reset to */ |
| 275 |
dwPos = dwFilePos; /* Here so we can reposition things */ |
| 276 |
|
| 277 |
/* Look for a followup CR/LF */ |
| 278 |
|
| 279 |
bData = osd_fgetc(fp); /* Get the next byte */ |
| 280 |
|
| 281 |
if (CR == bData) /* CR! Good! */ |
| 282 |
{ |
| 283 |
bData = osd_fgetc(fp); /* Get the next byte */ |
| 284 |
|
| 285 |
/* We need to do this to pick up subsequent CR/LF sequences */ |
| 286 |
|
| 287 |
osd_fseek(fp, dwPos, SEEK_SET); |
| 288 |
|
| 289 |
if (pdwPosition) |
| 290 |
*pdwPosition = dwPos; |
| 291 |
|
| 292 |
if (LF == bData) /* LF? Good! */ |
| 293 |
{ |
| 294 |
*pbTokenPtr++ = '\r'; |
| 295 |
*pbTokenPtr++ = '\n'; |
| 296 |
*pbTokenPtr = '\0'; |
| 297 |
|
| 298 |
return(TOKEN_LINEBREAK); |
| 299 |
} |
| 300 |
} |
| 301 |
else |
| 302 |
{ |
| 303 |
--dwFilePos; |
| 304 |
osd_ungetc(bData, fp); /* Put the character back. No good */ |
| 305 |
} |
| 306 |
} |
| 307 |
#ifdef MAME32JP |
| 308 |
} |
| 309 |
#endif |
| 310 |
else |
| 311 |
{ |
| 312 |
--dwFilePos; |
| 313 |
osd_ungetc(bData, fp); /* Put the character back. No good */ |
| 314 |
} |
| 315 |
|
| 316 |
/* Otherwise, fall through and keep parsing */ |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
++dwFilePos; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
/**************************************************************************** |
| 326 |
* ParseClose - Closes the existing opened file (if any) |
| 327 |
****************************************************************************/ |
| 328 |
static void ParseClose(void) |
| 329 |
{ |
| 330 |
/* If the file is open, time for fclose. */ |
| 331 |
|
| 332 |
if (fp) |
| 333 |
{ |
| 334 |
osd_fclose(fp); |
| 335 |
} |
| 336 |
|
| 337 |
fp = NULL; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
/**************************************************************************** |
| 342 |
* ParseOpen - Open up file for reading |
| 343 |
****************************************************************************/ |
| 344 |
static UINT8 ParseOpen(const char *pszFilename) |
| 345 |
{ |
| 346 |
/* Open file up in binary mode */ |
| 347 |
|
| 348 |
fp = osd_fopen (NULL, pszFilename, OSD_FILETYPE_HISTORY, 0); |
| 349 |
|
| 350 |
/* If this is NULL, return FALSE. We can't open it */ |
| 351 |
|
| 352 |
if (NULL == fp) |
| 353 |
{ |
| 354 |
return(FALSE); |
| 355 |
} |
| 356 |
|
| 357 |
/* Otherwise, prepare! */ |
| 358 |
|
| 359 |
dwFilePos = 0; |
| 360 |
return(TRUE); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
/**************************************************************************** |
| 365 |
* ParseSeek - Move the file position indicator |
| 366 |
****************************************************************************/ |
| 367 |
static UINT8 ParseSeek(long offset, int whence) |
| 368 |
{ |
| 369 |
int result = osd_fseek(fp, offset, whence); |
| 370 |
|
| 371 |
if (0 == result) |
| 372 |
{ |
| 373 |
dwFilePos = osd_ftell(fp); |
| 374 |
} |
| 375 |
return (UINT8)result; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
/************************************************************************** |
| 381 |
************************************************************************** |
| 382 |
* |
| 383 |
* Datafile functions |
| 384 |
* |
| 385 |
************************************************************************** |
| 386 |
**************************************************************************/ |
| 387 |
|
| 388 |
|
| 389 |
/************************************************************************** |
| 390 |
* ci_strcmp - case insensitive string compare |
| 391 |
* |
| 392 |
* Returns zero if s1 and s2 are equal, ignoring case |
| 393 |
**************************************************************************/ |
| 394 |
static int ci_strcmp (const char *s1, const char *s2) |
| 395 |
{ |
| 396 |
int c1, c2; |
| 397 |
|
| 398 |
while ((c1 = tolower(*s1)) == (c2 = tolower(*s2))) |
| 399 |
{ |
| 400 |
if (!c1) |
| 401 |
return 0; |
| 402 |
|
| 403 |
s1++; |
| 404 |
s2++; |
| 405 |
} |
| 406 |
|
| 407 |
return (c1 - c2); |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
/************************************************************************** |
| 412 |
* ci_strncmp - case insensitive character array compare |
| 413 |
* |
| 414 |
* Returns zero if the first n characters of s1 and s2 are equal, |
| 415 |
* ignoring case. |
| 416 |
**************************************************************************/ |
| 417 |
static int ci_strncmp (const char *s1, const char *s2, int n) |
| 418 |
{ |
| 419 |
int c1, c2; |
| 420 |
|
| 421 |
while (n) |
| 422 |
{ |
| 423 |
if ((c1 = tolower (*s1)) != (c2 = tolower (*s2))) |
| 424 |
return (c1 - c2); |
| 425 |
else if (!c1) |
| 426 |
break; |
| 427 |
--n; |
| 428 |
|
| 429 |
s1++; |
| 430 |
s2++; |
| 431 |
} |
| 432 |
return 0; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
/************************************************************************** |
| 437 |
* index_datafile |
| 438 |
* Create an index for the records in the currently open datafile. |
| 439 |
* |
| 440 |
* Returns 0 on error, or the number of index entries created. |
| 441 |
**************************************************************************/ |
| 442 |
static int index_datafile (struct tDatafileIndex **_index) |
| 443 |
{ |
| 444 |
struct tDatafileIndex *idx; |
| 445 |
int count = 0; |
| 446 |
UINT32 token = TOKEN_SYMBOL; |
| 447 |
|
| 448 |
/* rewind file */ |
| 449 |
if (ParseSeek (0L, SEEK_SET)) return 0; |
| 450 |
|
| 451 |
/* allocate index */ |
| 452 |
idx = *_index = malloc (MAX_DATAFILE_ENTRIES * sizeof (struct tDatafileIndex)); |
| 453 |
if (NULL == idx) return 0; |
| 454 |
|
| 455 |
/* loop through datafile */ |
| 456 |
while ((count < (MAX_DATAFILE_ENTRIES - 1)) && TOKEN_INVALID != token) |
| 457 |
{ |
| 458 |
long tell; |
| 459 |
char *s; |
| 460 |
|
| 461 |
token = GetNextToken ((UINT8 **)&s, &tell); |
| 462 |
if (TOKEN_SYMBOL != token) continue; |
| 463 |
|
| 464 |
/* DATAFILE_TAG_KEY identifies the driver */ |
| 465 |
if (!ci_strncmp (DATAFILE_TAG_KEY, s, strlen (DATAFILE_TAG_KEY))) |
| 466 |
{ |
| 467 |
token = GetNextToken ((UINT8 **)&s, &tell); |
| 468 |
if (TOKEN_EQUALS == token) |
| 469 |
{ |
| 470 |
int done = 0; |
| 471 |
int i; |
| 472 |
|
| 473 |
token = GetNextToken ((UINT8 **)&s, &tell); |
| 474 |
while (!done && TOKEN_SYMBOL == token) |
| 475 |
{ |
| 476 |
/* search for matching driver name */ |
| 477 |
for (i = 0; drivers[i]; i++) |
| 478 |
{ |
| 479 |
if (!ci_strcmp (s, drivers[i]->name)) |
| 480 |
{ |
| 481 |
/* found correct driver -- fill in index entry */ |
| 482 |
idx->driver = drivers[i]; |
| 483 |
idx->offset = tell; |
| 484 |
idx++; |
| 485 |
count++; |
| 486 |
done = 1; |
| 487 |
break; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
if (!done) |
| 492 |
{ |
| 493 |
token = GetNextToken ((UINT8 **)&s, &tell); |
| 494 |
if (TOKEN_COMMA == token) |
| 495 |
token = GetNextToken ((UINT8 **)&s, &tell); |
| 496 |
else |
| 497 |
done = 1; /* end of key field */ |
| 498 |
} |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
/* mark end of index */ |
| 505 |
idx->offset = 0L; |
| 506 |
idx->driver = 0; |
| 507 |
return count; |
| 508 |
} |
| 509 |
|
| 510 |
#ifdef MAME32JP |
| 511 |
/************************************************************************** |
| 512 |
* index_menuidx |
| 513 |
* |
| 514 |
* |
| 515 |
* Returns 0 on error, or the number of index entries created. |
| 516 |
**************************************************************************/ |
| 517 |
static int index_menuidx (const struct GameDriver *drv, struct tDatafileIndex *d_idx, struct tMenuIndex **_index) |
| 518 |
{ |
| 519 |
struct tMenuIndex *m_idx; |
| 520 |
int m_count = 0; |
| 521 |
UINT32 token = TOKEN_SYMBOL; |
| 522 |
|
| 523 |
long tell; |
| 524 |
long cmdtag_offset = 0; |
| 525 |
char *s; |
| 526 |
|
| 527 |
mame32jp_wrap = 1; |
| 528 |
|
| 529 |
/* find driver in datafile index */ |
| 530 |
while (d_idx->driver) |
| 531 |
{ |
| 532 |
if (d_idx->driver == drv || d_idx->driver == drv->clone_of) break; |
| 533 |
d_idx++; |
| 534 |
} |
| 535 |
if (d_idx->driver == 0) return 0; /* driver not found in Data_file_index */ |
| 536 |
|
| 537 |
/* seek to correct point in datafile */ |
| 538 |
if (ParseSeek (d_idx->offset, SEEK_SET)) return 0; |
| 539 |
|
| 540 |
/* allocate index */ |
| 541 |
m_idx = *_index = malloc(MAX_MENUIDX_ENTRIES * sizeof (struct tMenuIndex)); |
| 542 |
if (NULL == m_idx) return 0; |
| 543 |
|
| 544 |
/* loop through between $cmd= */ |
| 545 |
token = GetNextToken ((UINT8 **)&s, &tell); |
| 546 |
while ((m_count < (MAX_MENUIDX_ENTRIES - 1)) && TOKEN_INVALID != token) |
| 547 |
{ |
| 548 |
if (!ci_strncmp (DATAFILE_TAG_KEY, s, strlen (DATAFILE_TAG_KEY))) |
| 549 |
break; |
| 550 |
|
| 551 |
/* DATAFILE_TAG_COMMAND identifies the driver */ |
| 552 |
if (!ci_strncmp (DATAFILE_TAG_COMMAND, s, strlen (DATAFILE_TAG_COMMAND))) |
| 553 |
{ |
| 554 |
cmdtag_offset = tell; |
| 555 |
token = GetNextToken ((UINT8 **)&s, &tell); |
| 556 |
|
| 557 |
if (TOKEN_EQUALS == token) |
| 558 |
{ |
| 559 |
token = GetNextToken ((UINT8 **)&s, &tell); |
| 560 |
m_idx->menuitem = malloc(strlen(s)+1); |
| 561 |
strcpy(m_idx->menuitem, s); |
| 562 |
|
| 563 |
m_idx->offset = cmdtag_offset; |
| 564 |
|
| 565 |
m_idx++; |
| 566 |
m_count++; |
| 567 |
} |
| 568 |
else |
| 569 |
{ |
| 570 |
while (TOKEN_SYMBOL != token) |
| 571 |
token = GetNextToken ((UINT8 **)&s, &tell); |
| 572 |
|
| 573 |
m_idx->menuitem = malloc( strlen( s ) + 1 ); |
| 574 |
strcpy( m_idx->menuitem, s ); |
| 575 |
|
| 576 |
m_idx->offset = cmdtag_offset; |
| 577 |
|
| 578 |
m_idx++; |
| 579 |
m_count++; |
| 580 |
} |
| 581 |
} |
| 582 |
token = GetNextToken ((UINT8 **)&s, &tell); |
| 583 |
} |
| 584 |
|
| 585 |
/* mark end of index */ |
| 586 |
m_idx->offset = 0L; |
| 587 |
m_idx->menuitem = NULL; |
| 588 |
|
| 589 |
return m_count; |
| 590 |
} |
| 591 |
#endif |
| 592 |
|
| 593 |
/************************************************************************** |
| 594 |
* load_datafile_text |
| 595 |
* |
| 596 |
* Loads text field for a driver into the buffer specified. Specify the |
| 597 |
* driver, a pointer to the buffer, the buffer size, the index created by |
| 598 |
* index_datafile(), and the desired text field (e.g., DATAFILE_TAG_BIO). |
| 599 |
* |
| 600 |
* Returns 0 if successful. |
| 601 |
**************************************************************************/ |
| 602 |
static int load_datafile_text (const struct GameDriver *drv, char *buffer, int bufsize, |
| 603 |
struct tDatafileIndex *idx, const char *tag) |
| 604 |
{ |
| 605 |
int offset = 0; |
| 606 |
int found = 0; |
| 607 |
UINT32 token = TOKEN_SYMBOL; |
| 608 |
UINT32 prev_token = TOKEN_SYMBOL; |
| 609 |
#ifdef MAME32JP |
| 610 |
int first = 1; |
| 611 |
#endif |
| 612 |
|
| 613 |
*buffer = '\0'; |
| 614 |
|
| 615 |
/* find driver in datafile index */ |
| 616 |
while (idx->driver) |
| 617 |
{ |
| 618 |
if (idx->driver == drv) break; |
| 619 |
idx++; |
| 620 |
} |
| 621 |
if (idx->driver == 0) return 1; /* driver not found in index */ |
| 622 |
|
| 623 |
/* seek to correct point in datafile */ |
| 624 |
if (ParseSeek (idx->offset, SEEK_SET)) return 1; |
| 625 |
|
| 626 |
/* read text until buffer is full or end of entry is encountered */ |
| 627 |
while (TOKEN_INVALID != token) |
| 628 |
{ |
| 629 |
char *s; |
| 630 |
int len; |
| 631 |
long tell; |
| 632 |
|
| 633 |
token = GetNextToken ((UINT8 **)&s, &tell); |
| 634 |
if (TOKEN_INVALID == token) continue; |
| 635 |
|
| 636 |
if (found) |
| 637 |
{ |
| 638 |
/* end entry when a tag is encountered */ |
| 639 |
if (TOKEN_SYMBOL == token && DATAFILE_TAG == s[0] && TOKEN_LINEBREAK == prev_token) break; |
| 640 |
|
| 641 |
prev_token = token; |
| 642 |
|
| 643 |
/* translate platform-specific linebreaks to '\n' */ |
| 644 |
if (TOKEN_LINEBREAK == token) |
| 645 |
strcpy (s, "\n"); |
| 646 |
|
| 647 |
/* append a space to words */ |
| 648 |
#ifdef MAME32JP |
| 649 |
if (!mame32jp_wrap && TOKEN_LINEBREAK != token) |
| 650 |
#else |
| 651 |
if (TOKEN_LINEBREAK != token) |
| 652 |
#endif |
| 653 |
strcat (s, " "); |
| 654 |
|
| 655 |
/* remove extraneous space before commas */ |
| 656 |
if (TOKEN_COMMA == token) |
| 657 |
{ |
| 658 |
--buffer; |
| 659 |
--offset; |
| 660 |
*buffer = '\0'; |
| 661 |
} |
| 662 |
|
| 663 |
/* add this word to the buffer */ |
| 664 |
len = strlen (s); |
| 665 |
if ((len + offset) >= bufsize) break; |
| 666 |
strcpy (buffer, s); |
| 667 |
buffer += len; |
| 668 |
offset += len; |
| 669 |
} |
| 670 |
else |
| 671 |
{ |
| 672 |
if (TOKEN_SYMBOL == token) |
| 673 |
{ |
| 674 |
/* looking for requested tag */ |
| 675 |
if (!ci_strncmp (tag, s, strlen (tag))) |
| 676 |
#ifdef MAME32JP |
| 677 |
{ |
| 678 |
#endif |
| 679 |
found = 1; |
| 680 |
#ifdef MAME32JP |
| 681 |
if (first && mame32jp_wrap) |
| 682 |
{ |
| 683 |
fseek(fp, 2l, SEEK_CUR); |
| 684 |
first = 0; |
| 685 |
} |
| 686 |
} |
| 687 |
#endif |
| 688 |
else if (!ci_strncmp (DATAFILE_TAG_KEY, s, strlen (DATAFILE_TAG_KEY))) |
| 689 |
break; /* error: tag missing */ |
| 690 |
} |
| 691 |
} |
| 692 |
} |
| 693 |
return (!found); |
| 694 |
} |
| 695 |
|
| 696 |
#ifdef MAME32JP |
| 697 |
/************************************************************************** |
| 698 |
* load_datafile_text_ex |
| 699 |
* |
| 700 |
* Loads text field for a driver into the buffer specified. Specify the |
| 701 |
* driver, a pointer to the buffer, the buffer size, the index created by |
| 702 |
* index_datafile(), and the desired text field (e.g., DATAFILE_TAG_BIO). |
| 703 |
* |
| 704 |
* Returns 0 if successful. |
| 705 |
**************************************************************************/ |
| 706 |
static int load_datafile_text_ex (const struct GameDriver *drv, char *buffer, int bufsize, |
| 707 |
const char *tag, struct tMenuIndex *m_idx, const int menu_sel) |
| 708 |
{ |
| 709 |
int offset = 0; |
| 710 |
int found = 0; |
| 711 |
UINT32 token = TOKEN_SYMBOL; |
| 712 |
UINT32 prev_token = TOKEN_SYMBOL; |
| 713 |
int first = 1; |
| 714 |
|
| 715 |
*buffer = '\0'; |
| 716 |
|
| 717 |
/* seek to correct point in datafile */ |
| 718 |
if (ParseSeek ((m_idx + menu_sel)->offset, SEEK_SET)) return 1; |
| 719 |
|
| 720 |
/* read text until buffer is full or end of entry is encountered */ |
| 721 |
while (TOKEN_INVALID != token) |
| 722 |
{ |
| 723 |
char *s; |
| 724 |
int len; |
| 725 |
long tell; |
| 726 |
|
| 727 |
token = GetNextToken ((UINT8 **)&s, &tell); |
| 728 |
if (TOKEN_INVALID == token) continue; |
| 729 |
|
| 730 |
if (found) |
| 731 |
{ |
| 732 |
/* end entry when a tag is encountered */ |
| 733 |
if (TOKEN_SYMBOL == token && !ci_strncmp (DATAFILE_TAG_END, s, strlen (DATAFILE_TAG_END))) break; |
| 734 |
|
| 735 |
prev_token = token; |
| 736 |
|
| 737 |
/* translate platform-specific linebreaks to '\n' */ |
| 738 |
if (TOKEN_LINEBREAK == token) |
| 739 |
strcpy (s, "\n"); |
| 740 |
|
| 741 |
/* append a space to words */ |
| 742 |
if (!mame32jp_wrap && TOKEN_LINEBREAK != token) |
| 743 |
strcat (s, " "); |
| 744 |
|
| 745 |
/* remove extraneous space before commas */ |
| 746 |
if (TOKEN_COMMA == token) |
| 747 |
{ |
| 748 |
--buffer; |
| 749 |
--offset; |
| 750 |
*buffer = '\0'; |
| 751 |
} |
| 752 |
|
| 753 |
/* add this word to the buffer */ |
| 754 |
len = strlen (s); |
| 755 |
if ((len + offset) >= bufsize) break; |
| 756 |
strcpy (buffer, s); |
| 757 |
buffer += len; |
| 758 |
offset += len; |
| 759 |
} |
| 760 |
else |
| 761 |
{ |
| 762 |
if (TOKEN_SYMBOL == token) |
| 763 |
{ |
| 764 |
/* looking for requested tag */ |
| 765 |
if (!ci_strncmp (tag, s, strlen (tag))) |
| 766 |
{ |
| 767 |
found = 1; |
| 768 |
token = GetNextToken ((UINT8 **)&s, &tell); |
| 769 |
if ( TOKEN_EQUALS == token ) |
| 770 |
{ |
| 771 |
token = GetNextToken ((UINT8 **)&s, &tell); |
| 772 |
token = GetNextToken ((UINT8 **)&s, &tell); |
| 773 |
} |
| 774 |
|
| 775 |
if (first) first = 0; |
| 776 |
} |
| 777 |
else if (!ci_strncmp (DATAFILE_TAG_KEY, s, strlen (DATAFILE_TAG_KEY))) |
| 778 |
break; /* error: tag missing */ |
| 779 |
} |
| 780 |
} |
| 781 |
} |
| 782 |
return (!found); |
| 783 |
} |
| 784 |
#endif |
| 785 |
|
| 786 |
|
| 787 |
/************************************************************************** |
| 788 |
* load_driver_history |
| 789 |
* Load history text for the specified driver into the specified buffer. |
| 790 |
* Combines $bio field of HISTORY.DAT with $mame field of MAMEINFO.DAT. |
| 791 |
* |
| 792 |
* Returns 0 if successful. |
| 793 |
* |
| 794 |
* NOTE: For efficiency the indices are never freed (intentional leak). |
| 795 |
**************************************************************************/ |
| 796 |
int load_driver_history (const struct GameDriver *drv, char *buffer, int bufsize) |
| 797 |
{ |
| 798 |
#ifndef MAME_FIX |
| 799 |
static struct tDatafileIndex *hist_idx = null; |
| 800 |
static struct tDatafileIndex *mame_idx = null; |
| 801 |
#endif |
| 802 |
int history = 0, mameinfo = 0; |
| 803 |
int err; |
| 804 |
|
| 805 |
#ifdef MAME_FIX |
| 806 |
if (MAX_DATAFILE_ENTRIES == 0) |
| 807 |
set_datafile_entries(); |
| 808 |
#endif |
| 809 |
|
| 810 |
*buffer = 0; |
| 811 |
#ifdef MAME32JP |
| 812 |
mame32jp_wrap = 0; |
| 813 |
#endif |
| 814 |
|
| 815 |
if(!history_filename) |
| 816 |
history_filename = "history.dat"; |
| 817 |
|
| 818 |
/* try to open history datafile */ |
| 819 |
if (ParseOpen (history_filename)) |
| 820 |
{ |
| 821 |
/* create index if necessary */ |
| 822 |
if (hist_idx) |
| 823 |
history = 1; |
| 824 |
else |
| 825 |
history = (index_datafile (&hist_idx) != 0); |
| 826 |
|
| 827 |
/* load history text */ |
| 828 |
if (hist_idx) |
| 829 |
{ |
| 830 |
const struct GameDriver *gdrv; |
| 831 |
|
| 832 |
gdrv = drv; |
| 833 |
do |
| 834 |
{ |
| 835 |
err = load_datafile_text (gdrv, buffer, bufsize, |
| 836 |
hist_idx, DATAFILE_TAG_BIO); |
| 837 |
gdrv = gdrv->clone_of; |
| 838 |
} while (err && gdrv); |
| 839 |
|
| 840 |
if (err) history = 0; |
| 841 |
} |
| 842 |
ParseClose (); |
| 843 |
} |
| 844 |
|
| 845 |
if(!mameinfo_filename) |
| 846 |
mameinfo_filename = "mameinfo.dat"; |
| 847 |
|
| 848 |
/* try to open mameinfo datafile */ |
| 849 |
if (ParseOpen (mameinfo_filename)) |
| 850 |
{ |
| 851 |
/* create index if necessary */ |
| 852 |
if (mame_idx) |
| 853 |
mameinfo = 1; |
| 854 |
else |
| 855 |
mameinfo = (index_datafile (&mame_idx) != 0); |
| 856 |
|
| 857 |
/* load informational text (append) */ |
| 858 |
if (mame_idx) |
| 859 |
{ |
| 860 |
int len = strlen (buffer); |
| 861 |
const struct GameDriver *gdrv; |
| 862 |
|
| 863 |
gdrv = drv; |
| 864 |
do |
| 865 |
{ |
| 866 |
err = load_datafile_text (gdrv, buffer+len, bufsize-len, |
| 867 |
mame_idx, DATAFILE_TAG_MAME); |
| 868 |
gdrv = gdrv->clone_of; |
| 869 |
} while (err && gdrv); |
| 870 |
|
| 871 |
if (err) mameinfo = 0; |
| 872 |
} |
| 873 |
ParseClose (); |
| 874 |
} |
| 875 |
|
| 876 |
return (history == 0 && mameinfo == 0); |
| 877 |
} |
| 878 |
|
| 879 |
#ifdef MAME32JP |
| 880 |
/************************************************************************** |
| 881 |
* load_driver_command |
| 882 |
**************************************************************************/ |
| 883 |
int load_driver_command (const struct GameDriver *drv, char *buffer, int bufsize) |
| 884 |
{ |
| 885 |
int command = 0; |
| 886 |
int err = 0; |
| 887 |
|
| 888 |
*buffer = 0; |
| 889 |
mame32jp_wrap = 1; |
| 890 |
|
| 891 |
/* try to open command datafile */ |
| 892 |
if(!command_filename) |
| 893 |
command_filename = "command.dat"; |
| 894 |
if (ParseOpen (command_filename)) |
| 895 |
{ |
| 896 |
/* create index if necessary */ |
| 897 |
if (cmnd_idx) |
| 898 |
command = 1; |
| 899 |
else |
| 900 |
command = (index_datafile (&cmnd_idx) != 0); |
| 901 |
|
| 902 |
/* load informational text (append) */ |
| 903 |
if (cmnd_idx) |
| 904 |
{ |
| 905 |
const struct GameDriver *gdrv; |
| 906 |
|
| 907 |
gdrv = drv; |
| 908 |
do |
| 909 |
{ |
| 910 |
err = load_datafile_text (gdrv, buffer, bufsize, |
| 911 |
cmnd_idx, DATAFILE_TAG_COMMAND); |
| 912 |
gdrv = gdrv->clone_of; |
| 913 |
} while (err && gdrv); |
| 914 |
|
| 915 |
if (err) command = 0; |
| 916 |
} |
| 917 |
ParseClose (); |
| 918 |
} |
| 919 |
|
| 920 |
return (command == 0); |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
/************************************************************************** |
| 925 |
* load_driver_command_ex |
| 926 |
**************************************************************************/ |
| 927 |
int load_driver_command_ex (const struct GameDriver *drv, char *buffer, int bufsize, const int menu_sel) |
| 928 |
{ |
| 929 |
int command = 0; |
| 930 |
int err = 0; |
| 931 |
|
| 932 |
*buffer = 0; |
| 933 |
mame32jp_wrap = 1; |
| 934 |
|
| 935 |
/* try to open command datafile */ |
| 936 |
if(!command_filename) |
| 937 |
command_filename = "command.dat"; |
| 938 |
if (ParseOpen (command_filename)) |
| 939 |
{ |
| 940 |
/* create index if necessary */ |
| 941 |
if (cmnd_idx) |
| 942 |
command = 1; |
| 943 |
else |
| 944 |
command = (index_datafile (&cmnd_idx) != 0); |
| 945 |
|
| 946 |
/* create menu_index if necessary */ |
| 947 |
if (menu_idx) |
| 948 |
command = 1; |
| 949 |
else |
| 950 |
command = (index_menuidx (drv, cmnd_idx, &menu_idx) != 0); |
| 951 |
|
| 952 |
/* load informational text (append) */ |
| 953 |
if (cmnd_idx) |
| 954 |
{ |
| 955 |
const struct GameDriver *gdrv; |
| 956 |
|
| 957 |
gdrv = drv; |
| 958 |
do |
| 959 |
{ |
| 960 |
err = load_datafile_text_ex (gdrv, buffer, bufsize, |
| 961 |
DATAFILE_TAG_COMMAND, menu_idx, menu_sel); |
| 962 |
gdrv = gdrv->clone_of; |
| 963 |
} while (err && gdrv); |
| 964 |
|
| 965 |
if (err) command = 0; |
| 966 |
} |
| 967 |
ParseClose (); |
| 968 |
} |
| 969 |
|
| 970 |
return (command == 0); |
| 971 |
} |
| 972 |
|
| 973 |
|
| 974 |
/************************************************************************** |
| 975 |
* command_sub_menu |
| 976 |
**************************************************************************/ |
| 977 |
UINT8 command_sub_menu(const struct GameDriver *drv, const char *menu_item[]) |
| 978 |
{ |
| 979 |
int command = 0; |
| 980 |
UINT8 total = 0; |
| 981 |
static const struct GameDriver *last_drv = 0; |
| 982 |
|
| 983 |
/* try to open command datafile */ |
| 984 |
if(!command_filename) |
| 985 |
command_filename = "command.dat"; |
| 986 |
if (ParseOpen (command_filename)) |
| 987 |
{ |
| 988 |
/* create index if necessary */ |
| 989 |
if (cmnd_idx) |
| 990 |
command = 1; |
| 991 |
else |
| 992 |
command = (index_datafile (&cmnd_idx) != 0); |
| 993 |
|
| 994 |
command = (index_menuidx (drv, cmnd_idx, &menu_idx) != 0); |
| 995 |
} |
| 996 |
ParseClose (); |
| 997 |
|
| 998 |
if(menu_idx && command == TRUE) |
| 999 |
{ |
| 1000 |
struct tMenuIndex *m_idx = menu_idx; |
| 1001 |
|
| 1002 |
while(m_idx->menuitem != NULL) |
| 1003 |
{ |
| 1004 |
menu_item[total++] = m_idx->menuitem; |
| 1005 |
m_idx++; |
| 1006 |
} |
| 1007 |
last_drv = drv; |
| 1008 |
return total; |
| 1009 |
} |
| 1010 |
else |
| 1011 |
return 0; |
| 1012 |
} |
| 1013 |
#endif |
| 1014 |
|
| 1015 |
#ifdef MAME_FIX |
| 1016 |
/************************************************************************** |
| 1017 |
* set_datafile_entries |
| 1018 |
**************************************************************************/ |
| 1019 |
static void set_datafile_entries(void) |
| 1020 |
{ |
| 1021 |
int i = 0; |
| 1022 |
|
| 1023 |
while (drivers[i] != 0) |
| 1024 |
i++; |
| 1025 |
|
| 1026 |
MAX_DATAFILE_ENTRIES = i; |
| 1027 |
} |
| 1028 |
|
| 1029 |
/************************************************************************** |
| 1030 |
* free_datafile |
| 1031 |
**************************************************************************/ |
| 1032 |
void free_datafile(void) |
| 1033 |
{ |
| 1034 |
if (hist_idx) |
| 1035 |
{ |
| 1036 |
free(hist_idx); |
| 1037 |
hist_idx = 0; |
| 1038 |
} |
| 1039 |
if (mame_idx) |
| 1040 |
{ |
| 1041 |
free(mame_idx); |
| 1042 |
mame_idx = 0; |
| 1043 |
} |
| 1044 |
#ifdef MAME32JP |
| 1045 |
if (cmnd_idx) |
| 1046 |
{ |
| 1047 |
free(cmnd_idx); |
| 1048 |
cmnd_idx = 0; |
| 1049 |
} |
| 1050 |
if (menu_idx) |
| 1051 |
{ |
| 1052 |
free(menu_idx); |
| 1053 |
menu_idx = 0; |
| 1054 |
} |
| 1055 |
#endif |
| 1056 |
} |
| 1057 |
#endif |