Search
lxdream.org :: lxdream/src/gdrom/gdrom.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdrom.c
changeset 342:850502f0e8de
prev245:a1d0655a88d3
next422:61a0598e07ff
author nkeynes
date Wed Jan 31 10:58:42 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Refactor gdrom module to be more conducive to real device support
view annotate diff log raw
     2 /**
     3  * $Id: gdrom.c,v 1.12 2007-01-31 10:58:42 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,j;
    43     gdrom_image_class_t extclz = NULL;
    45     if( fd == -1 ) {
    46 	ERROR("Unable to open file '%s': %s", filename, strerror(errno));
    47 	return NULL;
    48     }
    50     f = fdopen(fd, "ro");
    53     /* try extensions */
    54     if( ext != NULL ) {
    55 	ext++; /* Skip the '.' */
    56 	for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
    57 	    if( gdrom_image_classes[i]->extension != NULL &&
    58 		strcasecmp( gdrom_image_classes[i]->extension, ext ) == 0 ) {
    59 		extclz = gdrom_image_classes[i];
    60 		if( extclz->is_valid_file(f) ) {
    61 		    disc = extclz->open_image_file(filename, f);
    62 		    if( disc != NULL )
    63 			return disc;
    64 		}
    65 		break;
    66 	    }
    67 	}
    68     }
    70     /* Okay, fall back to magic */
    71     gboolean recognized = FALSE;
    72     for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
    73 	if( gdrom_image_classes[i] != extclz &&
    74 	    gdrom_image_classes[i]->is_valid_file(f) ) {
    75 	    recognized = TRUE;
    76 	    disc = gdrom_image_classes[i]->open_image_file(filename, f);
    77 	    if( disc != NULL )
    78 		return disc;
    79 	}
    80     }
    82     if( !recognized ) {
    83 	ERROR( "Unable to open disc %s: Unsupported format", filename );
    84     }
    85     fclose(f);
    86     return NULL;
    87 }
    89 void gdrom_mount_disc( gdrom_disc_t disc ) 
    90 {
    91     gdrom_unmount_disc();
    92     gdrom_disc = disc;
    93     gdrom_image_dump_info( disc );
    94 }
    96 gdrom_disc_t gdrom_mount_image( const gchar *filename )
    97 {
    98     gdrom_disc_t disc = gdrom_image_open(filename);
    99     if( disc != NULL )
   100 	gdrom_mount_disc( disc );
   101     return disc;
   102 }
   104 void gdrom_unmount_disc( ) 
   105 {
   106     if( gdrom_disc != NULL ) {
   107 	gdrom_disc->close(gdrom_disc);
   108     }
   109     gdrom_disc = NULL;
   111 }
.