comparison src/xs_length.c @ 458:57836945fa71

Use new xs_* file functions for SID-file handling.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 24 Jan 2007 22:01:04 +0000
parents 230a9d79dd84
children d600f45c92d2
comparison
equal deleted inserted replaced
457:693baa150eea 458:57836945fa71
189 */ 189 */
190 gint xs_sldb_read(t_xs_sldb *db, const gchar *dbFilename) 190 gint xs_sldb_read(t_xs_sldb *db, const gchar *dbFilename)
191 { 191 {
192 FILE *inFile; 192 FILE *inFile;
193 gchar inLine[XS_BUF_SIZE]; 193 gchar inLine[XS_BUF_SIZE];
194 gint lineNum; 194 size_t lineNum;
195 t_xs_sldb_node *tmpNode; 195 t_xs_sldb_node *tmpNode;
196 assert(db); 196 assert(db);
197 197
198 /* Try to open the file */ 198 /* Try to open the file */
199 if ((inFile = fopen(dbFilename, "ra")) == NULL) { 199 if ((inFile = fopen(dbFilename, "ra")) == NULL) {
202 } 202 }
203 203
204 /* Read and parse the data */ 204 /* Read and parse the data */
205 lineNum = 0; 205 lineNum = 0;
206 206
207 while (!feof(inFile)) { 207 while (!feof(inFile) && fgets(inLine, XS_BUF_SIZE, inFile)) {
208 size_t linePos; 208 size_t linePos;
209 fgets(inLine, XS_BUF_SIZE, inFile);
210 inLine[XS_BUF_SIZE - 1] = 0; 209 inLine[XS_BUF_SIZE - 1] = 0;
211 linePos = 0; 210 linePos = 0;
212 lineNum++; 211 lineNum++;
213 212
214 xs_findnext(inLine, &linePos); 213 xs_findnext(inLine, &linePos);
434 } t_xs_psidv2_header; 433 } t_xs_psidv2_header;
435 434
436 435
437 static gint xs_get_sid_hash(const gchar *pcFilename, t_xs_md5hash hash) 436 static gint xs_get_sid_hash(const gchar *pcFilename, t_xs_md5hash hash)
438 { 437 {
439 FILE *inFile; 438 t_xs_file *inFile;
440 t_xs_md5state inState; 439 t_xs_md5state inState;
441 t_xs_psidv1_header psidH; 440 t_xs_psidv1_header psidH;
442 t_xs_psidv2_header psidH2; 441 t_xs_psidv2_header psidH2;
443 guint8 *songData; 442 guint8 *songData;
444 guint8 ib8[2], i8; 443 guint8 ib8[2], i8;
445 gint iIndex, iRes; 444 gint iIndex, iRes;
446 445
447 /* Try to open the file */ 446 /* Try to open the file */
448 if ((inFile = fopen(pcFilename, "rb")) == NULL) 447 if ((inFile = xs_fopen(pcFilename, "rb")) == NULL)
449 return -1; 448 return -1;
450 449
451 /* Read PSID header in */ 450 /* Read PSID header in */
452 xs_rd_str(inFile, psidH.magicID, sizeof(psidH.magicID)); 451 xs_fgets(psidH.magicID, sizeof(psidH.magicID), inFile);
453 if ((psidH.magicID[0] != 'P' && psidH.magicID[0] != 'R') || 452 if ((psidH.magicID[0] != 'P' && psidH.magicID[0] != 'R') ||
454 (psidH.magicID[1] != 'S') || (psidH.magicID[2] != 'I') || (psidH.magicID[3] != 'D')) { 453 (psidH.magicID[1] != 'S') || (psidH.magicID[2] != 'I') || (psidH.magicID[3] != 'D')) {
455 fclose(inFile); 454 xs_fclose(inFile);
455 xs_error(_("Not a PSID or RSID file '%s'\n"), pcFilename);
456 return -2; 456 return -2;
457 } 457 }
458 458
459 psidH.version = xs_rd_be16(inFile); 459 psidH.version = xs_fread_be16(inFile);
460 psidH.dataOffset = xs_rd_be16(inFile); 460 psidH.dataOffset = xs_fread_be16(inFile);
461 psidH.loadAddress = xs_rd_be16(inFile); 461 psidH.loadAddress = xs_fread_be16(inFile);
462 psidH.initAddress = xs_rd_be16(inFile); 462 psidH.initAddress = xs_fread_be16(inFile);
463 psidH.playAddress = xs_rd_be16(inFile); 463 psidH.playAddress = xs_fread_be16(inFile);
464 psidH.nSongs = xs_rd_be16(inFile); 464 psidH.nSongs = xs_fread_be16(inFile);
465 psidH.startSong = xs_rd_be16(inFile); 465 psidH.startSong = xs_fread_be16(inFile);
466 psidH.speed = xs_rd_be32(inFile); 466 psidH.speed = xs_fread_be32(inFile);
467 467
468 xs_rd_str(inFile, psidH.sidName, sizeof(psidH.sidName)); 468 xs_fgets(psidH.sidName, sizeof(psidH.sidName), inFile);
469 xs_rd_str(inFile, psidH.sidAuthor, sizeof(psidH.sidAuthor)); 469 xs_fgets(psidH.sidAuthor, sizeof(psidH.sidAuthor), inFile);
470 xs_rd_str(inFile, psidH.sidCopyright, sizeof(psidH.sidCopyright)); 470 xs_fgets(psidH.sidCopyright, sizeof(psidH.sidCopyright), inFile);
471 471
472 if (xs_feof(inFile) || xs_ferror(inFile)) {
473 xs_fclose(inFile);
474 xs_error(_("Error reading SID file header from '%s'\n"), pcFilename);
475 return -4;
476 }
477
472 /* Check if we need to load PSIDv2NG header ... */ 478 /* Check if we need to load PSIDv2NG header ... */
473 if (psidH.version == 2) { 479 if (psidH.version == 2) {
474 /* Yes, we need to */ 480 /* Yes, we need to */
475 psidH2.flags = xs_rd_be16(inFile); 481 psidH2.flags = xs_fread_be16(inFile);
476 psidH2.startPage = fgetc(inFile); 482 psidH2.startPage = xs_fgetc(inFile);
477 psidH2.pageLength = fgetc(inFile); 483 psidH2.pageLength = xs_fgetc(inFile);
478 psidH2.reserved = xs_rd_be16(inFile); 484 psidH2.reserved = xs_fread_be16(inFile);
479 } 485 }
480 486
481 /* Allocate buffer */ 487 /* Allocate buffer */
482 songData = (guint8 *) g_malloc(XS_SIDBUF_SIZE * sizeof(guint8)); 488 songData = (guint8 *) g_malloc(XS_SIDBUF_SIZE * sizeof(guint8));
483 if (!songData) { 489 if (!songData) {
484 fclose(inFile); 490 xs_fclose(inFile);
491 xs_error(_("Error allocating temp data buffer for file '%s'\n"), pcFilename);
485 return -3; 492 return -3;
486 } 493 }
487 494
488 /* Read data to buffer */ 495 /* Read data to buffer */
489 iRes = fread(songData, sizeof(guint8), XS_SIDBUF_SIZE, inFile); 496 iRes = xs_fread(songData, sizeof(guint8), XS_SIDBUF_SIZE, inFile);
490 fclose(inFile); 497 xs_fclose(inFile);
491 498
492 /* Initialize and start MD5-hash calculation */ 499 /* Initialize and start MD5-hash calculation */
493 xs_md5_init(&inState); 500 xs_md5_init(&inState);
494 501
495 if (psidH.loadAddress == 0) { 502 if (psidH.loadAddress == 0) {