From 91147fd3912b2f95b06ad820dd0c1bcc413a891d Mon Sep 17 00:00:00 2001 From: rocky Date: Fri, 11 Nov 2005 12:26:57 +0000 Subject: [PATCH] include/cdio++ Add pre- and post- increment/decrement operators for libcdio enums that it makes sense to iterate over. example/C++/OO/*: use these example/*: match up C and C++ programs better. --- example/C++/OO/cdtext.cpp | 21 ++++++++++++---- example/C++/OO/device.cpp | 18 +++++++++++--- example/C++/OO/eject.cpp | 4 ++- example/cdtext.c | 14 ++++++++--- example/device.c | 23 ++++++++++-------- example/eject.c | 4 ++- include/cdio++/Makefile.am | 3 ++- include/cdio++/cdio.hpp | 6 ++++- include/cdio++/enum.hpp | 50 ++++++++++++++++++++++++++++++++++++++ include/cdio/cdtext.h | 7 ++++-- include/cdio/device.h | 4 ++- 11 files changed, 126 insertions(+), 28 deletions(-) create mode 100644 include/cdio++/enum.hpp diff --git a/example/C++/OO/cdtext.cpp b/example/C++/OO/cdtext.cpp index 325be8da..296627f1 100644 --- a/example/C++/OO/cdtext.cpp +++ b/example/C++/OO/cdtext.cpp @@ -1,5 +1,5 @@ /* - $Id: cdtext.cpp,v 1.1 2005/11/10 11:11:15 rocky Exp $ + $Id: cdtext.cpp,v 1.2 2005/11/11 12:26:57 rocky Exp $ Copyright (C) 2005 Rocky Bernstein @@ -18,9 +18,22 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* Simple program to list CD-Text info of a Compact Disc using libcdio. */ +/* Simple program to list CD-Text info of a Compact Disc using + libcdio. See also corresponding C program of a similar name. + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_STDIO_H #include +#endif + +#ifdef HAVE_SYS_TYPES_H #include +#endif + #include /* Set up a CD-DA image to test on which is in the libcdio distribution. */ @@ -33,12 +46,10 @@ print_cdtext_track_info(CdioDevice *device, track_t i_track, cdtext_t *cdtext = device->getCdtext(0); if (NULL != cdtext) { cdtext_field_t i; - int j=0; printf("%s\n", psz_msg); - for (j=0; j < MAX_CDTEXT_FIELDS; j++) { - i = (cdtext_field_t) j; + for (i= (cdtext_field_t) MIN_CDTEXT_FIELD; i < MAX_CDTEXT_FIELDS; i++) { if (cdtext->field[i]) { printf("\t%s: %s\n", cdtext_field2str(i), cdtext->field[i]); } diff --git a/example/C++/OO/device.cpp b/example/C++/OO/device.cpp index 66251bfd..46b9b8ce 100644 --- a/example/C++/OO/device.cpp +++ b/example/C++/OO/device.cpp @@ -1,5 +1,5 @@ /* - $Id: device.cpp,v 1.1 2005/11/10 11:11:15 rocky Exp $ + $Id: device.cpp,v 1.2 2005/11/11 12:26:57 rocky Exp $ Copyright (C) 2005 Rocky Bernstein @@ -18,8 +18,9 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* Simple program to show drivers installed and what the default - CD-ROM drive is. */ +/* Simple program to show drivers installed and what the default + CD-ROM drive is. See also corresponding C program of a similar + name. */ #ifdef HAVE_CONFIG_H # include "config.h" @@ -123,5 +124,16 @@ main(int argc, const char *argv[]) } else { printf("Problem in trying to find a driver.\n\n"); } + + { + driver_id_t driver_id; + for (driver_id=CDIO_MIN_DRIVER; driver_id<=CDIO_MAX_DRIVER; driver_id++) + if (cdio_have_driver(driver_id)) + printf("We have: %s\n", cdio_driver_describe(driver_id)); + else + printf("We don't have: %s\n", cdio_driver_describe(driver_id)); + } + + return 0; } diff --git a/example/C++/OO/eject.cpp b/example/C++/OO/eject.cpp index 96b1f0bd..c5e05f70 100644 --- a/example/C++/OO/eject.cpp +++ b/example/C++/OO/eject.cpp @@ -1,5 +1,5 @@ /* - $Id: eject.cpp,v 1.1 2005/11/10 11:11:15 rocky Exp $ + $Id: eject.cpp,v 1.2 2005/11/11 12:26:57 rocky Exp $ Copyright (C) 2005 Rocky Bernstein @@ -22,6 +22,8 @@ If a single argument is given, it is used as the CD-ROM device to eject/close. Otherwise a CD-ROM drive will be scanned for. + + See also corresponding C program of a similar name. */ #ifdef HAVE_CONFIG_H # include "config.h" diff --git a/example/cdtext.c b/example/cdtext.c index d5f08dfd..458a1511 100644 --- a/example/cdtext.c +++ b/example/cdtext.c @@ -1,5 +1,5 @@ /* - $Id: cdtext.c,v 1.2 2005/01/04 04:40:22 rocky Exp $ + $Id: cdtext.c,v 1.3 2005/11/11 12:26:57 rocky Exp $ Copyright (C) 2004, 2005 Rocky Bernstein @@ -18,12 +18,20 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* Simple program to list CD-Text info of a Compact Disc using libcdio. */ +/* Simple program to list CD-Text info of a Compact Disc using + libcdio. See also corresponding C++ programs of similar names. */ + #ifdef HAVE_CONFIG_H # include "config.h" #endif + +#ifdef HAVE_STDIO_H #include +#endif + +#ifdef HAVE_SYS_TYPES_H #include +#endif #include #include @@ -77,7 +85,7 @@ main(int argc, const char *argv[]) cdio_destroy(p_cdio); } - p_cdio = cdio_open (NULL, DRIVER_UNKNOWN); + p_cdio = cdio_open (NULL, DRIVER_DEVICE); i_first_track = cdio_get_first_track_num(p_cdio); i_tracks = cdio_get_num_tracks(p_cdio); diff --git a/example/device.c b/example/device.c index 0fd9e135..52d855c8 100644 --- a/example/device.c +++ b/example/device.c @@ -1,5 +1,5 @@ /* - $Id: device.c,v 1.1 2005/02/06 15:09:10 rocky Exp $ + $Id: device.c,v 1.2 2005/11/11 12:26:57 rocky Exp $ Copyright (C) 2003, 2004, 2005 Rocky Bernstein @@ -18,8 +18,9 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* Simple program to show drivers installed and what the default - CD-ROM drive is. */ +/* Simple program to show drivers installed and what the default + CD-ROM drive is. See also corresponding C++ programs of similar + names .*/ #ifdef HAVE_CONFIG_H # include "config.h" #endif @@ -100,7 +101,6 @@ int main(int argc, const char *argv[]) { CdIo_t *p_cdio = cdio_open (NULL, DRIVER_UNKNOWN); - driver_id_t driver_id; if (NULL != p_cdio) { char *default_device = cdio_get_default_device(p_cdio); @@ -123,11 +123,14 @@ main(int argc, const char *argv[]) } else { printf("Problem in trying to find a driver.\n\n"); } - - for (driver_id=CDIO_MIN_DRIVER; driver_id<=CDIO_MAX_DRIVER; driver_id++) - if (cdio_have_driver(driver_id)) - printf("We have: %s\n", cdio_driver_describe(driver_id)); - else - printf("We don't have: %s\n", cdio_driver_describe(driver_id)); + + { + driver_id_t driver_id; + for (driver_id=CDIO_MIN_DRIVER; driver_id<=CDIO_MAX_DRIVER; driver_id++) + if (cdio_have_driver(driver_id)) + printf("We have: %s\n", cdio_driver_describe(driver_id)); + else + printf("We don't have: %s\n", cdio_driver_describe(driver_id)); + } return 0; } diff --git a/example/eject.c b/example/eject.c index c92ffd14..d525c73f 100644 --- a/example/eject.c +++ b/example/eject.c @@ -1,5 +1,5 @@ /* - $Id: eject.c,v 1.1 2005/11/07 07:44:00 rocky Exp $ + $Id: eject.c,v 1.2 2005/11/11 12:26:57 rocky Exp $ Copyright (C) 2005 Rocky Bernstein @@ -22,6 +22,8 @@ If a single argument is given, it is used as the CD-ROM device to eject/close. Otherwise a CD-ROM drive will be scanned for. + + See also corresponding C++ program of a similar name. */ #ifdef HAVE_CONFIG_H # include "config.h" diff --git a/include/cdio++/Makefile.am b/include/cdio++/Makefile.am index b34992b0..3c2a5001 100644 --- a/include/cdio++/Makefile.am +++ b/include/cdio++/Makefile.am @@ -1,4 +1,4 @@ -# $Id: Makefile.am,v 1.1 2005/11/10 11:11:16 rocky Exp $ +# $Id: Makefile.am,v 1.2 2005/11/11 12:26:57 rocky Exp $ # # Copyright (C) 2005 Rocky Bernstein # @@ -28,5 +28,6 @@ libcdioinclude_HEADERS = \ device.hpp \ devices.hpp \ disc.hpp \ + enum.hpp \ read.hpp \ track.hpp diff --git a/include/cdio++/cdio.hpp b/include/cdio++/cdio.hpp index cadd16cf..9f0eea87 100644 --- a/include/cdio++/cdio.hpp +++ b/include/cdio++/cdio.hpp @@ -1,5 +1,5 @@ /* -*- C++ -*- - $Id: cdio.hpp,v 1.1 2005/11/10 11:11:16 rocky Exp $ + $Id: cdio.hpp,v 1.2 2005/11/11 12:26:57 rocky Exp $ Copyright (C) 2005 Rocky Bernstein @@ -29,6 +29,10 @@ #include +// Make pre- and post-increment operators for enums in libcdio where it +// makes sense. +#include + class Cdio { public: diff --git a/include/cdio++/enum.hpp b/include/cdio++/enum.hpp new file mode 100644 index 00000000..88ccc85f --- /dev/null +++ b/include/cdio++/enum.hpp @@ -0,0 +1,50 @@ +/* -*- C++ -*- + $Id: enum.hpp,v 1.1 2005/11/11 12:26:57 rocky Exp $ + + Copyright (C) 2005 Rocky Bernstein + + 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 + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +/** \file enum.hpp + * + * \brief C++ header for pre- and post-increment operators for + * enumerations defined in libcdio that it makes sense to iterate over. + */ + +#define ENUM_ITERATE_FNS(type) \ + inline \ + type &operator++(type &t) \ + { \ + return t = type(t + 1); \ + } \ + inline \ + type &operator++(type &t, int) \ + { \ + return t = type(t + 1); \ + } \ + inline \ + type &operator--(type &t) \ + { \ + return t = type(t - 1); \ + } \ + inline \ + type &operator--(type &t, int) \ + { \ + return t = type(t - 1); \ + } + +ENUM_ITERATE_FNS(cdtext_field_t) +ENUM_ITERATE_FNS(driver_id_t) diff --git a/include/cdio/cdtext.h b/include/cdio/cdtext.h index fbf49ebd..165dc5b6 100644 --- a/include/cdio/cdtext.h +++ b/include/cdio/cdtext.h @@ -1,5 +1,5 @@ /* - $Id: cdtext.h,v 1.12 2005/11/10 00:44:40 rocky Exp $ + $Id: cdtext.h,v 1.13 2005/11/11 12:26:57 rocky Exp $ Copyright (C) 2004, 2005 Rocky Bernstein adapted from cuetools @@ -37,6 +37,7 @@ extern "C" { #endif /* __cplusplus */ #define MAX_CDTEXT_FIELDS 13 +#define MIN_CDTEXT_FIELD 0 /*! \brief structure for holding CD-Text information @@ -46,7 +47,9 @@ extern "C" { char *field[MAX_CDTEXT_FIELDS]; }; - /*! \brief A list of all of the CD-Text fields */ + /*! \brief A list of all of the CD-Text fields. Because + the interval has no gaps, we can use ++ to iterate over fields. + */ typedef enum { CDTEXT_ARRANGER = 0, /**< name(s) of the arranger(s) */ CDTEXT_COMPOSER = 1, /**< name(s) of the composer(s) */ diff --git a/include/cdio/device.h b/include/cdio/device.h index 9801e799..1f78e79d 100644 --- a/include/cdio/device.h +++ b/include/cdio/device.h @@ -1,5 +1,5 @@ /* -*- c -*- - $Id: device.h,v 1.25 2005/11/07 07:44:00 rocky Exp $ + $Id: device.h,v 1.26 2005/11/11 12:26:57 rocky Exp $ Copyright (C) 2005 Rocky Bernstein @@ -136,6 +136,8 @@ extern "C" { * different than what is available on a given host. * * Order is a little significant since the order is used in scans. + * Also the enumeration values are assumed not to leave any holes so + * we can use ++ to iterate over values. * We have to start with DRIVER_UNKNOWN and devices should come before * disk-image readers. By putting something towards the top (a lower * enumeration number), in an iterative scan we prefer that to