Search
lxdream.org :: lxdream/src/gdrom/gdrom.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdrom.c
changeset 644:ccae4bfa5f82
prev613:c2dd87f947b2
next678:35eb00945316
author nkeynes
date Sun Mar 02 11:38:08 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Bug #59: Add provisional support for all CD read modes (not validated)
view annotate diff log raw
     2 /**
     3  * $Id$
     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[] = { &cdrom_device_class, 
    31 					      &nrg_image_class, 
    32 					      &cdi_image_class, 
    33 					      &gdi_image_class, 
    34 					      NULL };
    36 char *gdrom_mode_names[] = { "Mode 0", "Mode 1", "Mode 2", "Mode 2 Form 1", "Mode 2 Form 2", "Audio", 
    37 			     "Mode 2 semiraw", "XA Raw", "Non-XA Raw" };
    38 uint32_t gdrom_sector_size[] = { 0, 2048, 2336, 2048, 2324, 2352, 2336, 2352, 2352 };
    40 gdrom_disc_t gdrom_image_open( const gchar *filename )
    41 {
    42     const gchar *ext = strrchr(filename, '.');
    43     gdrom_disc_t disc = NULL;
    45     int fd = open( filename, O_RDONLY | O_NONBLOCK );
    46     FILE *f;
    47     int i;
    48     gdrom_image_class_t extclz = NULL;
    50     if( fd == -1 ) {
    51 	return NULL;
    52     }
    54     f = fdopen(fd, "ro");
    57     /* try extensions */
    58     if( ext != NULL ) {
    59 	ext++; /* Skip the '.' */
    60 	for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
    61 	    if( gdrom_image_classes[i]->extension != NULL &&
    62 		strcasecmp( gdrom_image_classes[i]->extension, ext ) == 0 ) {
    63 		extclz = gdrom_image_classes[i];
    64 		if( extclz->is_valid_file(f) ) {
    65 		    disc = extclz->open_image_file(filename, f);
    66 		    if( disc != NULL )
    67 			return disc;
    68 		}
    69 		break;
    70 	    }
    71 	}
    72     }
    74     /* Okay, fall back to magic */
    75     gboolean recognized = FALSE;
    76     for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
    77 	if( gdrom_image_classes[i] != extclz &&
    78 	    gdrom_image_classes[i]->is_valid_file(f) ) {
    79 	    recognized = TRUE;
    80 	    disc = gdrom_image_classes[i]->open_image_file(filename, f);
    81 	    if( disc != NULL )
    82 		return disc;
    83 	}
    84     }
    86     fclose(f);
    87     return NULL;
    88 }
    90 void gdrom_mount_disc( gdrom_disc_t disc ) 
    91 {
    92     gdrom_unmount_disc();
    93     gdrom_disc = disc;
    94     gdrom_image_dump_info( disc );
    95 }
    97 gboolean gdrom_mount_image( const gchar *filename )
    98 {
    99     gdrom_disc_t disc = gdrom_image_open(filename);
   100     if( disc != NULL ) {
   101 	gdrom_mount_disc( disc );
   102 	return TRUE;
   103     }
   104     return FALSE;
   105 }
   107 void gdrom_unmount_disc( ) 
   108 {
   109     if( gdrom_disc != NULL ) {
   110 	gdrom_disc->close(gdrom_disc);
   111     }
   112     gdrom_disc = NULL;
   114 }
   116 gdrom_disc_t gdrom_get_current_disc()
   117 {
   118     return gdrom_disc;
   119 }
   121 gchar *gdrom_get_relative_filename( const gchar *base_name, const gchar *rel_name )
   122 {
   123     gchar *dirname = g_path_get_dirname(base_name);
   124     gchar *pathname = g_strdup_printf( "%s%c%s", dirname, G_DIR_SEPARATOR, rel_name );
   125     g_free(dirname);
   126     return pathname;
   127 }
.