Convert routines in device.hpp into raising an exception rather than

giving a return code. Sort of a test. More may follow.
This commit is contained in:
rocky
2006-01-15 10:39:15 +00:00
parent 5ac663c6a2
commit 004152ec29
3 changed files with 64 additions and 40 deletions

View File

@@ -1,5 +1,5 @@
/*
$Id: eject.cpp,v 1.2 2005/11/11 12:26:57 rocky Exp $
$Id: eject.cpp,v 1.3 2006/01/15 10:39:15 rocky Exp $
Copyright (C) 2005 Rocky Bernstein <rocky@panix.com>
@@ -53,27 +53,25 @@ main(int argc, const char *argv[])
if (!psz_drive) {
psz_drive = device.getDefaultDevice(driver_id);
if (!psz_drive) {
printf("Can't find a CD-ROM to eject\n");
printf("Can't find a CD-ROM to perform eject operation\n");
exit(1);
}
}
ret = device.ejectMedia(psz_drive);
switch(ret) {
case DRIVER_OP_UNSUPPORTED:
printf("Eject not supported for %s.\n", psz_drive);
break;
case DRIVER_OP_SUCCESS:
printf("CD-ROM drive %s ejected.\n", psz_drive);
break;
default:
printf("Eject of CD-ROM drive %s failed.\n", psz_drive);
break;
try {
device.ejectMedia(psz_drive);
printf("CD in CD-ROM drive %s ejected.\n", psz_drive);
}
if (DRIVER_OP_SUCCESS == device.closeTray(psz_drive, driver_id)) {
printf("Closed tray of CD-ROM drive %s.\n", psz_drive);
} else {
printf("Closing tray of CD-ROM drive %s failed.\n", psz_drive);
catch ( CdioDevice::DriverOpException e ) {
printf("Ejecting CD from CD-ROM drive %s operation error:\n\t%s.\n",
psz_drive, e.get_msg());
}
try {
device.closeTray(psz_drive, driver_id);
}
catch ( CdioDevice::DriverOpException e ) {
printf("Closing CD-ROM %s tray operation error error:\n\t%s.\n",
psz_drive, e.get_msg());
}
free(psz_drive);

View File

@@ -1,7 +1,7 @@
/* -*- C++ -*-
$Id: cdio.hpp,v 1.3 2005/11/14 01:15:33 rocky Exp $
$Id: cdio.hpp,v 1.4 2006/01/15 10:39:15 rocky Exp $
Copyright (C) 2005 Rocky Bernstein <rocky@panix.com>
Copyright (C) 2005, 2006 Rocky Bernstein <rocky@panix.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -110,6 +110,22 @@ public:
p_cdio = (CdIo_t *) NULL;
};
/** Class for driver exceptions. **/
class DriverOpException
{
public:
driver_return_code_t driver_return_code;
DriverOpException( driver_return_code_t drc ) {
driver_return_code = drc;
};
driver_return_code_t get_code(void) {
return driver_return_code;
};
const char *get_msg(void) {
return cdio_driver_return_code_to_str(driver_return_code);
};
};
// Other member functions
#include "device.hpp"
#include "disc.hpp"
@@ -118,6 +134,11 @@ public:
private:
CdIo_t *p_cdio;
void throw_device_exception(driver_return_code_t drc)
{
if (DRIVER_OP_SUCCESS == drc) return;
throw DriverOpException(drc);
}
};
#endif /* __CDIO_HPP__ */

View File

@@ -1,7 +1,7 @@
/* -*- C++ -*-
$Id: device.hpp,v 1.1 2005/11/10 11:11:16 rocky Exp $
$Id: device.hpp,v 1.2 2006/01/15 10:39:15 rocky Exp $
Copyright (C) 2005 Rocky Bernstein <rocky@panix.com>
Copyright (C) 2005, 2006 Rocky Bernstein <rocky@panix.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -44,22 +44,21 @@ close()
it was DRIVER_UNKNOWN or DRIVER_DEVICE; If this is NULL, we won't
report back the driver used.
*/
driver_return_code_t closeTray (const char *psz_drive,
/*in/out*/ driver_id_t &driver_id)
void closeTray (const char *psz_drive, /*in/out*/ driver_id_t &driver_id)
{
return cdio_close_tray (psz_drive, &driver_id);
driver_return_code_t drc = cdio_close_tray (psz_drive, &driver_id);
throw_device_exception(drc);
}
/*!
Close media tray in CD drive if there is a routine to do so.
@param psz_drive the name of CD-ROM to be closed.
*/
driver_return_code_t closeTray (const char *psz_drive)
void closeTray (const char *psz_drive)
{
driver_id_t driver_id = DRIVER_UNKNOWN;
return closeTray(psz_drive, driver_id);
closeTray(psz_drive, driver_id);
}
@@ -68,10 +67,11 @@ driver_return_code_t closeTray (const char *psz_drive)
If the CD is ejected, object is destroyed.
*/
driver_return_code_t
void
ejectMedia ()
{
return cdio_eject_media(&p_cdio);
driver_return_code_t drc = cdio_eject_media(&p_cdio);
throw_device_exception(drc);
}
/*!
@@ -79,10 +79,11 @@ ejectMedia ()
If the CD is ejected, object is destroyed.
*/
driver_return_code_t
void
ejectMedia (const char *psz_drive)
{
return cdio_eject_media_drive(psz_drive);
driver_return_code_t drc = cdio_eject_media_drive(psz_drive);
throw_device_exception(drc);
}
/*!
@@ -316,10 +317,11 @@ getHWinfo ( /*out*/ cdio_hwinfo_t &hw_info )
@param i_last_session pointer to the session number to be returned.
*/
driver_return_code_t
void
getLastSession (/*out*/ lsn_t &i_last_session)
{
return cdio_get_last_session(p_cdio, &i_last_session);
driver_return_code_t drc = cdio_get_last_session(p_cdio, &i_last_session);
throw_device_exception(drc);
}
/*!
@@ -466,19 +468,21 @@ isDevice(const char *psz_source, driver_id_t driver_id)
/*!
Set the blocksize for subsequent reads.
*/
driver_return_code_t
void
setBlocksize ( int i_blocksize )
{
return cdio_set_blocksize ( p_cdio, i_blocksize );
driver_return_code_t drc = cdio_set_blocksize ( p_cdio, i_blocksize );
throw_device_exception(drc);
}
/*!
Set the drive speed.
*/
driver_return_code_t
void
setSpeed ( int i_speed )
{
return cdio_set_speed ( p_cdio, i_speed );
driver_return_code_t drc = cdio_set_speed ( p_cdio, i_speed );
throw_device_exception(drc);
}
/*!
@@ -487,9 +491,10 @@ setSpeed ( int i_speed )
@param key the key to set
@param value the value to assocaiate with key
*/
driver_return_code_t
void
setArg (const char key[], const char value[])
{
return cdio_set_arg (p_cdio, key, value);
driver_return_code_t drc = cdio_set_arg (p_cdio, key, value);
throw_device_exception(drc);
}