Search
lxdream.org :: lxdream/src/gdrom/gdrom.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdrom.c
changeset 464:8e099fad42a6
prev452:48db4ac96899
next489:45c8ddcf52cb
author nkeynes
date Wed Oct 31 11:53:35 2007 +0000 (15 years ago)
permissions -rw-r--r--
last change Fix miscellaneous warnings
view annotate diff log raw
     2 /**
     3  * $Id: gdrom.c,v 1.17 2007-10-27 05:44:54 nkeynes Exp $
     4  *
     5  * GD-Rom  access functions.
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    20 #include <stdio.h>
    21 #include <fcntl.h>
    22 #include <errno.h>
    23 #include "gdrom/ide.h"
    24 #include "gdrom/gdrom.h"
    25 #include "gdrom/packet.h"
    26 #include "dream.h"
    28 extern gdrom_disc_t gdrom_disc;
    30 gdrom_image_class_t gdrom_image_classes[] = { &linux_device_class, &nrg_image_class, &cdi_image_class, NULL };
    32 char *gdrom_mode_names[] = { "Mode1", "Mode2", "XA 1", "XA2", "Audio", "GD-Rom" };
    33 uint32_t gdrom_sector_size[] = { 2048, 2336, 2048, 2324, 2352, 2336 };
    35 gdrom_disc_t gdrom_image_open( const gchar *filename )
    36 {
    37     const gchar *ext = strrchr(filename, '.');
    38     gdrom_disc_t disc = NULL;
    40     int fd = open( filename, O_RDONLY | O_NONBLOCK );
    41     FILE *f;
    42     int i;
    43     gdrom_image_class_t extclz = NULL;
    45     if( fd == -1 ) {
    46 	return NULL;
    47     }
    49     f = fdopen(fd, "ro");
    52     /* try extensions */
    53     if( ext != NULL ) {
    54 	ext++; /* Skip the '.' */
    55 	for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
    56 	    if( gdrom_image_classes[i]->extension != NULL &&
    57 		strcasecmp( gdrom_image_classes[i]->extension, ext ) == 0 ) {
    58 		extclz = gdrom_image_classes[i];
    59 		if( extclz->is_valid_file(f) ) {
    60 		    disc = extclz->open_image_file(filename, f);
    61 		    if( disc != NULL )
    62 			return disc;
    63 		}
    64 		break;
    65 	    }
    66 	}
    67     }
    69     /* Okay, fall back to magic */
    70     gboolean recognized = FALSE;
    71     for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
    72 	if( gdrom_image_classes[i] != extclz &&
    73 	    gdrom_image_classes[i]->is_valid_file(f) ) {
    74 	    recognized = TRUE;
    75 	    disc = gdrom_image_classes[i]->open_image_file(filename, f);
    76 	    if( disc != NULL )
    77 		return disc;
    78 	}
    79     }
    81     fclose(f);
    82     return NULL;
    83 }
    85 void gdrom_mount_disc( gdrom_disc_t disc ) 
    86 {
    87     gdrom_unmount_disc();
    88     gdrom_disc = disc;
    89     gdrom_image_dump_info( disc );
    90 }
    92 gboolean gdrom_mount_image( const gchar *filename )
    93 {
    94     gdrom_disc_t disc = gdrom_image_open(filename);
    95     if( disc != NULL ) {
    96 	gdrom_mount_disc( disc );
    97 	return TRUE;
    98     }
    99     return FALSE;
   100 }
   102 void gdrom_unmount_disc( ) 
   103 {
   104     if( gdrom_disc != NULL ) {
   105 	gdrom_disc->close(gdrom_disc);
   106     }
   107     gdrom_disc = NULL;
   109 }
   111 gdrom_disc_t gdrom_get_current_disc()
   112 {
   113     return gdrom_disc;
   114 }
.