casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
blockio.h
Go to the documentation of this file.
1 //# blockio.h:
2 //# Copyright (C) 1993,1994,1995,1996,1999
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 #ifndef FITS_BLOCKIO_H
28 #define FITS_BLOCKIO_H
29 
30 //# Include this file first, because it may set LFS variables used by cfitsio.
31 #include <casacore/casa/aips.h>
32 
33 //# Make sure that cfitsio does not declare the wcs headers.
34 extern "C"{
35 #include <fitsio.h> //# header file from cfitsio
36 #include <fitsio2.h> //# using core functions of cfitsio
37 }
38 
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <fcntl.h>
42 
44 
45 namespace casacore { //# NAMESPACE CASACORE - BEGIN
46 
47 //----------------------------------------------------------------------------
48 //<category lib=aips module=FITS sect="Blocked I/O">
49 //<summary> fixed-length blocked sequentual I/O base class </summary>
50 //<synopsis>
51 // BlockIO is a low level base class that implements fixed-length
52 // blocked sequential I/O. Its derived classes, BlockInput and BlockOutput
53 // are used by the FitsInput and FitsOutput classes. Users will hardly ever
54 // need to use this class directly.
55 //</synopsis>
56 //<todo>
57 // <li> ifdef kludges until OS dependent flags are developed
58 // for the compilation system.
59 //</todo>
60 
61 class BlockIO {
62  public:
63  // error return code
66  int err() const { return (int)m_err_status; }
67 
68  // number of physical blocks read/written
69  int blockno() const { return m_block_no; }
70 
71  // reset the m_iosize data member
72  void reset_iosize() { m_iosize = 0; }
73 
74  // get the total bytes of data in m_buffer
75  int iosize() const { return m_iosize; }
76 
77  // get the current read position within m_buffer
78  int current() const { return m_current; }
79 
80  // get m_buffer
81  char* buffer() const { return m_buffer; }
82 
83  // number of logical records read/written
84  int recno() const { return m_rec_no; }
85 
86  // name of file associated with I/O stream, if applicable
87  const char *fname() const { return m_filename; }
88 
89  // fits_close_file() does not work for reasons that the file pointer does not have the
90  // knowledge of chdu which were written with write_hdr() not write_***_hdr(). So create
91  // our own close_file() method.
92  int close_file( fitsfile *fptr, int *status);
93  // file descriptor associated with I/O stream, if applicable
94  int fdes() const { return m_fd; }
95  // get the fitsfile pointer
96  fitsfile *getfptr() const { return m_fptr; }
97  void setfptr( fitsfile* ffp );
98  protected:
99  // Construction can be done either from a filename with open options
100  // or from a file descriptor.
101  //
102  // The remaining arguments are the the logical record size and number
103  // of records that make up a physical record followed by the
104  // output stream that is used to write error messages to.
105  //<group>
106  BlockIO(const char *, int, int, int = 1,
108  BlockIO(int, int, int = 1,
110  virtual ~BlockIO();
111  //</group>
112 
113  char *m_filename; // name of file
114  int m_options; // options on open statement
115  const int m_recsize; // size in bytes of a logical record
116  const int m_nrec; // maximum number of logical records
117  const int m_blocksize; // size in bytes of physical records
118  FITSErrorHandler m_errfn; // FITS error handler function
119  IOErrs m_err_status; // error number
120  int m_fd; // file descriptor
121  char *m_buffer; // the actual data buffer itself
122  int m_block_no; // number of physical blocks read/written
123  int m_rec_no; // number of logical records read/written
124  int m_current; // offset to current logical record
125  // size of record in buffer
126  int m_iosize;
127  // using fitsfile structure from cfitsio of NASA
128  fitsfile *m_fptr;
129 
130  // set the error message and error number for later recovery
131  void errmsg(IOErrs, const char *);
132 };
133 
134 //<summary> fixed-length blocked sequential input base class</summary>
135 //<prerequisite>
136 // <li> BlockIO
137 //</prerequisite>
138 
139 class BlockInput : public BlockIO {
140  public:
141  // Construction can be done either from a filename or from
142  // a file descriptor.
143  //
144  // The remaining arguments are the the logical record size and number
145  // of records that make up a physical record followed by the
146  // output stream that is used to write error messages to.
147  //<group>
148  BlockInput(const char *, int, int = 1,
150  BlockInput(int, int, int = 1,
152  virtual ~BlockInput();
153  //</group>
154 
155  // read the next logical record or first
156  // skip N logical records and then read the next one.
157  // (note it is not possible to skip a record without
158  // reading a record).
159  //<note role=caution> these functions return a pointer to an
160  // internal record. The user must make sure that
161  // after destruction of this class no dangling pointers
162  // are left.
163  //</note>
164  //<group>
165  virtual char *read(); // read a physical block.
166  virtual char *skip(int);
167  //</group>
168 };
169 
170 //<summary> fixed-length blocked sequential output base class</summary>
171 //<prerequisite>
172 // <li> BlockIO
173 //</prerequisite>
174 
175 class BlockOutput : public BlockIO {
176  public:
177  // Construction can be done either from a filename or from
178  // a file descriptor.
179  //
180  // The remaining arguments are the the logical record size and number
181  // of records that make up a physical record followed by the
182  // output stream that is used to write error messages to.
183  //<group>
184  BlockOutput(const char *, int, int = 1,
186  BlockOutput(int, int, int = 1,
188  virtual ~BlockOutput();
189  void flush_buffer();
190  //</group>
191 
192  // write the next logical record. The input must point
193  // to a logical record
194  virtual int write(char *);
195 };
196 
197 } //# NAMESPACE CASACORE - END
198 
199 # endif
200 
int iosize() const
get the total bytes of data in m_buffer
Definition: blockio.h:75
BlockOutput(const char *, int, int=1, FITSErrorHandler errhandler=FITSError::defaultHandler)
Construction can be done either from a filename or from a file descriptor.
char * m_buffer
Definition: blockio.h:121
BlockInput(const char *, int, int=1, FITSErrorHandler errhandler=FITSError::defaultHandler)
Construction can be done either from a filename or from a file descriptor.
IOErrs m_err_status
Definition: blockio.h:119
virtual char * skip(int)
FITSErrorHandler m_errfn
Definition: blockio.h:118
const char * fname() const
name of file associated with I/O stream, if applicable
Definition: blockio.h:87
int close_file(fitsfile *fptr, int *status)
fits_close_file() does not work for reasons that the file pointer does not have the knowledge of chdu...
void reset_iosize()
reset the m_iosize data member
Definition: blockio.h:72
void(* FITSErrorHandler)(const char *errMessage, FITSError::ErrorLevel severity)
Define a typedef for the handler function signature for convenience.
Definition: FITSError.h:111
char * m_filename
Definition: blockio.h:113
const int m_recsize
Definition: blockio.h:115
fixed-length blocked sequential output base class
Definition: blockio.h:175
fitsfile * m_fptr
using fitsfile structure from cfitsio of NASA
Definition: blockio.h:128
const int m_blocksize
Definition: blockio.h:117
virtual char * read()
read the next logical record or first skip N logical records and then read the next one...
const int m_nrec
Definition: blockio.h:116
BlockIO(const char *, int, int, int=1, FITSErrorHandler errhandler=FITSError::defaultHandler)
Construction can be done either from a filename with open options or from a file descriptor.
char * buffer() const
get m_buffer
Definition: blockio.h:81
int fdes() const
file descriptor associated with I/O stream, if applicable
Definition: blockio.h:94
fitsfile * getfptr() const
get the fitsfile pointer
Definition: blockio.h:96
void setfptr(fitsfile *ffp)
fixed-length blocked sequential input base class
Definition: blockio.h:139
int recno() const
number of logical records read/written
Definition: blockio.h:84
int m_iosize
size of record in buffer
Definition: blockio.h:126
int err() const
Definition: blockio.h:66
int current() const
get the current read position within m_buffer
Definition: blockio.h:78
virtual int write(char *)
write the next logical record.
IOErrs
error return code
Definition: blockio.h:64
virtual ~BlockIO()
void errmsg(IOErrs, const char *)
set the error message and error number for later recovery
int blockno() const
number of physical blocks read/written
Definition: blockio.h:69
static void defaultHandler(const char *errMessage, ErrorLevel severity)
The default error handler.