casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
LargeFiledesIO.h
Go to the documentation of this file.
00001 //# FiledesIO.h: Class for IO on a large file descriptor
00002 //# Copyright (C) 1996,1997,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: LargeFiledesIO.h 20868 2010-03-08 07:50:40Z gervandiepen $
00027 
00028 #ifndef CASA_LARGEFILEDESIO_H
00029 #define CASA_LARGEFILEDESIO_H
00030 
00031 //# Includes
00032 #include <casa/aips.h>
00033 #include <casa/IO/ByteIO.h>
00034 #include <casa/BasicSL/String.h>
00035 
00036 namespace casa { //# NAMESPACE CASA - BEGIN
00037 
00038 // <summary>
00039 // Class for IO on a large file descriptor.
00040 // </summary>
00041 
00042 // <use visibility=export>
00043 
00044 // <reviewed reviewer="Friso Olnon" date="1996/11/06" tests="tByteIO" demos="">
00045 // </reviewed>
00046 
00047 // <prerequisite> 
00048 //    <li> <linkto class=ByteIO>ByteIO</linkto> class
00049 //    <li> file descriptors
00050 // </prerequisite>
00051 
00052 // <synopsis> 
00053 // This class is a specialization of class
00054 // <linkto class=ByteIO>ByteIO</linkto>. It uses a file descriptor
00055 // to read/write data.
00056 // <p>
00057 // The file associated with the file descriptor has to be opened
00058 // before hand.
00059 // The constructor will determine automatically if the file is
00060 // readable, writable and seekable.
00061 // Note that on destruction the file descriptor is NOT closed.
00062 // </synopsis>
00063 
00064 // <example>
00065 // This example shows how FiledesIO can be used with an fd.
00066 // It uses the fd for a regular file, which could be done in an easier
00067 // way using class <linkto class=RegularFileIO>RegularFileIO</linkto>.
00068 // However, when using pipes or sockets, this would be the only way.
00069 // <srcblock>
00070 //    // Get a file descriptor for the file.
00071 //    int fd = open ("file.name");
00072 //    // Use that as the source of AipsIO (which will also use CanonicalIO).
00073 //    FiledesIO fio (fd);
00074 //    AipsIO stream (&fio);
00075 //    // Read the data.
00076 //    Int vali;
00077 //    Bool valb;
00078 //    stream >> vali >> valb;
00079 // </srcblock>
00080 // </example>
00081 
00082 // <motivation> 
00083 // Make it possible to use the AIPS++ IO functionality on any file.
00084 // In this way any device can be hooked to the IO framework.
00085 // </motivation>
00086 
00087 
00088 class LargeFiledesIO: public ByteIO
00089 {
00090 public: 
00091     // Default constructor.
00092     // A stream can be attached using the attach function.
00093     LargeFiledesIO();
00094 
00095     // Construct from the given file descriptor.
00096     // The file name is only used in possible error messages.
00097     explicit LargeFiledesIO (int fd, const String& fileName=String());
00098 
00099     // Attach to the given file descriptor.
00100     // An exception is thrown if it is not in a detached state.
00101     // The file name is only used in error messages.
00102     void attach (int fd, const String& fileName);
00103 
00104     // The destructor does not close the file.
00105     ~LargeFiledesIO();
00106 
00107     // Write the number of bytes.
00108     virtual void write (uInt size, const void* buf);
00109 
00110     // Read <src>size</src> bytes from the descriptor. Returns the number of
00111     // bytes actually read or a negative number if an error occured. Will throw
00112     // an Exception (AipsError) if the requested number of bytes could not be
00113     // read, or an error occured, unless throwException is set to False. Will
00114     // always throw an exception if the descriptor is not readable or the
00115     // system call returned an undocumented value.
00116     virtual Int read (uInt size, void* buf, Bool throwException=True);    
00117 
00118     // Get the length of the byte stream.
00119     virtual Int64 length();
00120        
00121     // Is the IO stream readable?
00122     virtual Bool isReadable() const;
00123 
00124     // Is the IO stream writable?
00125     virtual Bool isWritable() const;
00126 
00127     // Is the IO stream seekable?
00128     virtual Bool isSeekable() const;
00129 
00130     // Get the file name of the file attached.
00131     const String& fileName() const
00132       { return itsFileName; }
00133 
00134     // Some static convenience functions for file create/open/close.
00135     // <group>
00136     static int create (const Char* name, int mode = 0644);
00137     static int open   (const Char* name, Bool writable = False,
00138                        Bool throwExcp = True);
00139     static void close (int fd);
00140     // </group>
00141 
00142 
00143 protected:
00144     // Get the file descriptor.
00145     int fd() const
00146       { return itsFile; }
00147 
00148     // Detach from the file descriptor. It is not closed.
00149     void detach();
00150 
00151     // Determine if the file descriptor is readable and/or writable.
00152     void fillRWFlags (int fd);
00153 
00154     // Determine if the file is seekable.
00155     void fillSeekable();
00156 
00157     // Reset the position pointer to the given value. It returns the
00158     // new position.
00159     virtual Int64 doSeek (Int64 offset, ByteIO::SeekOption);
00160 
00161 private:
00162     Bool   itsSeekable;
00163     Bool   itsReadable;
00164     Bool   itsWritable;
00165     int    itsFile;
00166     String itsFileName;
00167 
00168     // Copy constructor, should not be used.
00169     LargeFiledesIO (const LargeFiledesIO& that);
00170 
00171     // Assignment, should not be used.
00172     LargeFiledesIO& operator= (const LargeFiledesIO& that);
00173 };
00174 
00175 
00176 
00177 
00178 } //# NAMESPACE CASA - END
00179 
00180 #endif