casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
blockio.h
Go to the documentation of this file.
00001 //# blockio.h:
00002 //# Copyright (C) 1993,1994,1995,1996,1999
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: blockio.h 20329 2008-06-06 07:59:22Z gervandiepen $
00027 #ifndef FITS_BLOCKIO_H
00028 #define FITS_BLOCKIO_H
00029 
00030 //# Include this file first, because it may set LFS variables used by cfitsio.
00031 #include <casa/aips.h>
00032 
00033 //# Make sure that cfitsio does not declare the wcs headers.
00034 extern "C"{
00035 #include <fitsio.h>  //# header file from cfitsio
00036 #include <fitsio2.h> //# using core functions of cfitsio
00037 }
00038 
00039 #include <stdlib.h>
00040 #include <unistd.h>
00041 #include <fcntl.h>
00042 
00043 #include <fits/FITS/FITSError.h>
00044 
00045 namespace casa { //# NAMESPACE CASA - BEGIN
00046 
00047 //----------------------------------------------------------------------------
00048 //<category lib=aips module=FITS sect="Blocked I/O">   
00049 //<summary> fixed-length blocked sequentual I/O base class </summary> 
00050 //<synopsis>
00051 // BlockIO is a low level base class that implements fixed-length 
00052 // blocked sequential I/O. Its derived classes, BlockInput and BlockOutput
00053 // are used by the FitsInput and FitsOutput classes. Users will hardly ever
00054 // need to use this class directly.
00055 //</synopsis>
00056 //<todo>
00057 // <li> ifdef kludges until OS dependent flags are developed
00058 //       for the compilation system.
00059 //</todo>
00060 
00061 class BlockIO {
00062     public:
00063         // error return code
00064         enum IOErrs { OK, NOSUCHFILE, NOMEM, OPENERR, CLOSEERR, 
00065                 READERR, WRITEERR };
00066         int err() const { return (int)m_err_status; }
00067 
00068         //  number of physical blocks read/written
00069         int blockno() const { return m_block_no; }
00070         
00071         // reset the m_iosize data member
00072         void reset_iosize() { m_iosize = 0; }
00073         
00074         // get the total bytes of data in m_buffer
00075         int iosize() const { return m_iosize; }
00076         
00077         // get the current read position within m_buffer
00078         int current() const { return m_current; }
00079         
00080         // get m_buffer
00081         char* buffer() const { return m_buffer; }
00082 
00083         //  number of logical records read/written
00084         int recno() const { return m_rec_no; }
00085 
00086         // name of file associated with I/O stream, if applicable
00087         const char *fname() const { return m_filename; }
00088 
00089         // fits_close_file() does not work for reasons that the file pointer does not have the 
00090         // knowledge of chdu which were written with write_hdr() not write_***_hdr(). So create
00091         // our own close_file() method.
00092         int close_file( fitsfile *fptr, int *status);
00093         // file descriptor associated with I/O stream, if applicable
00094         int fdes() const { return m_fd; }
00095         // get the fitsfile pointer
00096         fitsfile *getfptr() const { return m_fptr; } 
00097         void setfptr( fitsfile* ffp );
00098     protected:
00099         // Construction can be done either from a filename with open options 
00100         // or from a file descriptor.
00101         //
00102         // The remaining arguments are the the logical record size and number
00103         // of records that make up a physical record followed by the 
00104         // output stream that is used to write error messages to.
00105         //<group>
00106         BlockIO(const char *, int, int, int = 1, 
00107                 FITSErrorHandler errhandler = FITSError::defaultHandler);
00108         BlockIO(int,               int, int = 1, 
00109                 FITSErrorHandler errhandler = FITSError::defaultHandler);
00110         virtual ~BlockIO();
00111         //</group>
00112 
00113         char *m_filename;                    // name of file
00114         int m_options;                  // options on open statement
00115         const int m_recsize;         // size in bytes of a logical record
00116         const int m_nrec;                    // maximum number of logical records
00117         const int m_blocksize;    // size in bytes of physical records
00118    FITSErrorHandler m_errfn; // FITS error handler function
00119         IOErrs m_err_status;    // error number
00120         int m_fd;                          // file descriptor
00121         char *m_buffer;         // the actual data buffer itself
00122         int m_block_no;         // number of physical blocks read/written
00123         int m_rec_no;              // number of logical records read/written
00124         int m_current;          // offset to current logical record
00125         // size of record in buffer
00126         int m_iosize;
00127         // using fitsfile structure from cfitsio of NASA
00128         fitsfile *m_fptr;               
00129 
00130         // set the error message and error number for later recovery
00131         void errmsg(IOErrs, const char *);
00132 };
00133 
00134 //<summary> fixed-length blocked sequential input base class</summary>
00135 //<prerequisite>
00136 //   <li> BlockIO
00137 //</prerequisite>
00138 
00139 class BlockInput : public BlockIO {
00140     public:
00141         // Construction can be done either from a filename or from
00142         // a file descriptor.
00143         //
00144         // The remaining arguments are the the logical record size and number
00145         // of records that make up a physical record followed by the 
00146         // output stream that is used to write error messages to.
00147         //<group>
00148         BlockInput(const char *, int, int = 1, 
00149                    FITSErrorHandler errhandler = FITSError::defaultHandler);
00150         BlockInput(int,          int, int = 1, 
00151                    FITSErrorHandler errhandler = FITSError::defaultHandler);
00152         virtual ~BlockInput();
00153         //</group>
00154 
00155         // read the next logical record or first
00156         // skip N logical records and then read the next one.
00157         // (note it is not possible to skip a record without
00158         // reading a record). 
00159         //<note role=caution> these functions return a pointer to an
00160         // internal record. The user must make sure that
00161         // after destruction of this class no dangling pointers
00162         // are left.
00163         //</note>
00164         //<group>
00165         virtual char *read();       // read a physical block.  
00166         virtual char *skip(int);    
00167         //</group>
00168 };
00169 
00170 //<summary> fixed-length blocked sequential output base class</summary>
00171 //<prerequisite>
00172 //   <li> BlockIO
00173 //</prerequisite>             
00174 
00175 class BlockOutput : public BlockIO {
00176     public:
00177         // Construction can be done either from a filename or from
00178         // a file descriptor.
00179         //
00180         // The remaining arguments are the the logical record size and number
00181         // of records that make up a physical record followed by the 
00182         // output stream that is used to write error messages to.
00183         //<group>
00184         BlockOutput(const char *, int, int = 1,
00185                     FITSErrorHandler errhandler = FITSError::defaultHandler);
00186         BlockOutput(int,          int, int = 1,
00187                     FITSErrorHandler errhandler = FITSError::defaultHandler);
00188         virtual ~BlockOutput();
00189         void flush_buffer();
00190         //</group>
00191 
00192         // write the next logical record. The input must point
00193         // to a logical record
00194         virtual int write(char *);
00195 };
00196 
00197 } //# NAMESPACE CASA - END
00198 
00199 # endif
00200