casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TapeIO.h
Go to the documentation of this file.
00001 //# TapeIO.h: Class for IO on a tape device.
00002 //# Copyright (C) 1999,2001
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: TapeIO.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $
00027 
00028 #ifndef CASA_TAPEIO_H
00029 #define CASA_TAPEIO_H
00030 
00031 #include <casa/aips.h>
00032 #include <casa/IO/ByteIO.h>
00033 #include <casa/BasicSL/String.h>
00034 
00035 namespace casa { //# NAMESPACE CASA - BEGIN
00036 
00037 class Path;
00038 
00039 // <summary>Class for IO on a tape device</summary>
00040 
00041 // <use visibility=export>
00042 
00043 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00044 // </reviewed>
00045 
00046 // <prerequisite> 
00047 //    <li> <linkto class=ByteIO>ByteIO</linkto> class
00048 //    <li> Tape descriptors
00049 // </prerequisite>
00050 
00051 // <synopsis> 
00052 // This class is a specialization of class
00053 // <linkto class=ByteIO>ByteIO</linkto>. It uses a file descriptor
00054 // to read/write data.
00055 // <p>
00056 // The file associated with the file descriptor has to be opened
00057 // before hand.
00058 // The constructor will determine automatically if the file is
00059 // readable, writable and seekable.
00060 // Note that on destruction the file descriptor is NOT closed.
00061 // </synopsis>
00062 
00063 // <example>
00064 // This example shows how FiledesIO can be used with an fd.
00065 // It uses the fd for a regular file, which could be done in an easier
00066 // way using class <linkto class=RegularFileIO>RegularFileIO</linkto>.
00067 // However, when using pipes or sockets, this would be the only way.
00068 // <srcblock>
00069 //    // Get a file descriptor for the file.
00070 //    int fd = open ("file.name");
00071 //    // Use that as the source of AipsIO (which will also use CanonicalIO).
00072 //    FiledesIO fio (fd);
00073 //    AipsIO stream (&fio);
00074 //    // Read the data.
00075 //    Int vali;
00076 //    Bool valb;
00077 //    stream >> vali >> valb;
00078 // </srcblock>
00079 // </example>
00080 
00081 // <motivation> 
00082 // Make it possible to use the AIPS++ IO functionality on any file.
00083 // In this way any device can be hooked to the IO framework.
00084 // </motivation>
00085 
00086 
00087 class TapeIO: public ByteIO
00088 {
00089 public: 
00090   // Default constructor.
00091   // A stream can be attached using the attach function.
00092   TapeIO();
00093 
00094   // Construct from the given file descriptor. The file descriptor must have
00095   // been obtained using the TapeIO::open static function. When constructed
00096   // this way the class will not take over the file descriptor and hence not
00097   // close the Tape device when this class is destroyed.
00098   explicit TapeIO(int fd);
00099 
00100   // Construct from the given device. The device must point to a tape device
00101   // and if requested it is checked if the device is writeable. Throws an
00102   // exception if the device could not be opened correctly. When constructed
00103   // this way the class will close the Tape device when this class is destroyed
00104   // or the TapeIO object is attached to a new file descriptor.
00105   TapeIO(const Path& device, Bool writable = False);
00106 
00107   // The destructor will only close the file if the appropriate constructor, or
00108   // attach function, was used.
00109   virtual ~TapeIO();
00110     
00111   // Attach to the given file descriptor. The file descriptor will not be
00112   // closed when this class is destroyed.
00113   void attach(int fd);
00114 
00115   // Attach to the given tape device. The tape will be closed when this class
00116   // is destroyed or the TapeIO object is attached to a new descriptor.
00117   void attach(const Path& device, Bool writable = False);
00118 
00119   // Write the specified number of bytes.
00120   virtual void write(uInt size, const void* buf);
00121 
00122   // Read <src>size</src> bytes from the tape. Returns the number of bytes
00123   // actually read or a negative number if an error occured. Will throw an
00124   // exception (AipsError) if the requested number of bytes could not be read,
00125   // or an error occured, unless throwException is set to False. Will always
00126   // throw an exception if the tape is not readable or the system call returns
00127   // an undocumented value. Returns zero if the tape is at the end of the
00128   // current file (and size is non-zero and throwException is False).
00129   virtual Int read(uInt size, void* buf, Bool throwException=True);    
00130 
00131   // Rewind the tape device to the beginning.
00132   virtual void rewind();
00133   
00134   // skip the specified number of files (ie tape marks) on the tape. Throws an
00135   // exception if you try to skip past the last filemark.
00136   virtual void skip(uInt howMany=1);
00137   
00138   // write the specified number of filemarks.
00139   virtual void mark(uInt howMany=1);
00140   
00141   // returns True if the tape device is configured to use a fixed block size
00142   Bool fixedBlocks() const;
00143 
00144   // returns the block size in bytes. Returns zero if the device is configured
00145   // to use variable length blocks.
00146   uInt fixedBlockSize() const;
00147 
00148   // Configure the tape device to use fixed length blocks of the specified
00149   // size. The size must be bigger than zero (dugh!). Values bigger than 64k
00150   // may cause problems on some systems. Currently this function only does
00151   // anything under Solaris and Linux systems.
00152   void setFixedBlockSize(uInt sizeInBytes);
00153 
00154   // Configure the tape device to use variable length blocks. Currently this
00155   // function only does anything under Solaris and Linux systems.
00156   void setVariableBlockSize();
00157 
00158   // Get the length of the tape device.  Not a meaningful function for this
00159   // class and this function always returns -1.
00160   virtual Int64 length();
00161   
00162   // Is the tape device readable?
00163   virtual Bool isReadable() const;
00164   
00165   // Is the tape device writable?
00166   virtual Bool isWritable() const;
00167   
00168   // Is the tape device seekable?
00169   virtual Bool isSeekable() const;
00170   
00171   // Get the name of the attached device or return a zero length string if it
00172   // cannot be determined.
00173   virtual String fileName() const;
00174 
00175   // Some static convenience functions for file descriptor opening &
00176   // closing. The open function returns a file descriptor and the close
00177   // function requires a file descriptor as an argument.
00178   // <group>
00179   static int open(const Path& device, Bool writable = False);
00180   static void close(int fd);
00181   // </group>
00182 
00183 protected:
00184   // Detach the FILE. Close it when it is owned.
00185   void detach();
00186 
00187   // Determine if the file is readable and/or writable.
00188   void fillRWFlags();
00189 
00190   // Determine if the file is seekable.
00191   void fillSeekable();
00192 
00193   // Reset the position pointer to the given value. It returns the new
00194   // position. May not work on all Tape devices use the isSeekable(0 member
00195   // function to see if this function is usuable. Otherwise an Exception
00196   // (AipsError) is thrown. 
00197   virtual Int64 doSeek(Int64 offset, ByteIO::SeekOption);
00198   
00199 private:
00200   // The following functions are made private so that the compiler does not
00201   // generate default ones. They cannot be used and are not defined.
00202   TapeIO (const TapeIO& that);
00203   TapeIO& operator= (const TapeIO& that);
00204 
00205   void setBlockSize(uInt sizeInBytes);
00206   uInt getBlockSize() const;
00207 
00208   int         itsDevice;
00209   Bool        itsOwner;
00210   Bool        itsReadable;
00211   Bool        itsWritable;
00212   Bool        itsSeekable;
00213   String      itsDeviceName;
00214 };
00215 
00216 
00217 
00218 
00219 } //# NAMESPACE CASA - END
00220 
00221 #endif