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