casacore
Loading...
Searching...
No Matches
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: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25#ifndef FITS_BLOCKIO_H
26#define FITS_BLOCKIO_H
27
28// # Include this file first, because it may set LFS variables used by cfitsio.
29#include <casacore/casa/aips.h>
30
31// # Make sure that cfitsio does not declare the wcs headers.
32extern "C" {
33#include <fitsio.h> //# header file from cfitsio
34#include <fitsio2.h> //# using core functions of cfitsio
35}
36
37#include <stdlib.h>
38#include <unistd.h>
39#include <fcntl.h>
40
41#include <casacore/fits/FITS/FITSError.h>
42
43namespace casacore { // # NAMESPACE CASACORE - BEGIN
44
45//----------------------------------------------------------------------------
46//<category lib=aips module=FITS sect="Blocked I/O">
47//<summary> fixed-length blocked sequentual I/O base class </summary>
48//<synopsis>
49// BlockIO is a low level base class that implements fixed-length
50// blocked sequential I/O. Its derived classes, BlockInput and BlockOutput
51// are used by the FitsInput and FitsOutput classes. Users will hardly ever
52// need to use this class directly.
53//</synopsis>
54//<todo>
55// <li> ifdef kludges until OS dependent flags are developed
56// for the compilation system.
57//</todo>
58
59class BlockIO {
60 public:
61 // error return code
63 int err() const { return (int)m_err_status; }
64
65 // number of physical blocks read/written
66 int blockno() const { return m_block_no; }
67
68 // reset the m_iosize data member
69 void reset_iosize() { m_iosize = 0; }
70
71 // get the total bytes of data in m_buffer
72 int iosize() const { return m_iosize; }
73
74 // get the current read position within m_buffer
75 int current() const { return m_current; }
76
77 // get m_buffer
78 char *buffer() const { return m_buffer; }
79
80 // number of logical records read/written
81 int recno() const { return m_rec_no; }
82
83 // name of file associated with I/O stream, if applicable
84 const char *fname() const { return m_filename; }
85
86 // fits_close_file() does not work for reasons that the file pointer does not have the
87 // knowledge of chdu which were written with write_hdr() not write_***_hdr(). So create
88 // our own close_file() method.
89 int close_file(fitsfile *fptr, int *status);
90 // file descriptor associated with I/O stream, if applicable
91 int fdes() const { return m_fd; }
92 // get the fitsfile pointer
93 fitsfile *getfptr() const { return m_fptr; }
94 void setfptr(fitsfile *ffp);
95
96 protected:
97 // Construction can be done either from a filename with open options
98 // or from a file descriptor.
99 //
100 // The remaining arguments are the the logical record size and number
101 // of records that make up a physical record followed by the
102 // output stream that is used to write error messages to.
103 //<group>
104 BlockIO(const char *, int, int, int = 1, FITSErrorHandler errhandler = FITSError::defaultHandler);
105 BlockIO(int, int, int = 1, FITSErrorHandler errhandler = FITSError::defaultHandler);
106 virtual ~BlockIO();
107 //</group>
108
109 char *m_filename; // name of file
110 int m_options; // options on open statement
111 const int m_recsize; // size in bytes of a logical record
112 const int m_nrec; // maximum number of logical records
113 const int m_blocksize; // size in bytes of physical records
114 FITSErrorHandler m_errfn; // FITS error handler function
115 IOErrs m_err_status; // error number
116 int m_fd; // file descriptor
117 char *m_buffer; // the actual data buffer itself
118 int m_block_no; // number of physical blocks read/written
119 int m_rec_no; // number of logical records read/written
120 int m_current; // offset to current logical record
121 // size of record in buffer
123 // using fitsfile structure from cfitsio of NASA
124 fitsfile *m_fptr;
125
126 // set the error message and error number for later recovery
127 void errmsg(IOErrs, const char *);
128};
129
130//<summary> fixed-length blocked sequential input base class</summary>
131//<prerequisite>
132// <li> BlockIO
133//</prerequisite>
134
135class BlockInput : public BlockIO {
136 public:
137 // Construction can be done either from a filename or from
138 // a file descriptor.
139 //
140 // The remaining arguments are the the logical record size and number
141 // of records that make up a physical record followed by the
142 // output stream that is used to write error messages to.
143 //<group>
144 BlockInput(const char *, int, int = 1, FITSErrorHandler errhandler = FITSError::defaultHandler);
145 BlockInput(int, int, int = 1, FITSErrorHandler errhandler = FITSError::defaultHandler);
146 virtual ~BlockInput();
147 //</group>
148
149 // read the next logical record or first
150 // skip N logical records and then read the next one.
151 // (note it is not possible to skip a record without
152 // reading a record).
153 //<note role=caution> these functions return a pointer to an
154 // internal record. The user must make sure that
155 // after destruction of this class no dangling pointers
156 // are left.
157 //</note>
158 //<group>
159 virtual char *read(); // read a physical block.
160 virtual char *skip(int);
161 //</group>
162};
163
164//<summary> fixed-length blocked sequential output base class</summary>
165//<prerequisite>
166// <li> BlockIO
167//</prerequisite>
168
169class BlockOutput : public BlockIO {
170 public:
171 // Construction can be done either from a filename or from
172 // a file descriptor.
173 //
174 // The remaining arguments are the the logical record size and number
175 // of records that make up a physical record followed by the
176 // output stream that is used to write error messages to.
177 //<group>
178 BlockOutput(const char *, int, int = 1, FITSErrorHandler errhandler = FITSError::defaultHandler);
180 virtual ~BlockOutput();
182 //</group>
183
184 // write the next logical record. The input must point
185 // to a logical record
186 virtual int write(char *);
187};
188
189} // namespace casacore
190
191#endif
IOErrs m_err_status
Definition blockio.h:115
fitsfile * getfptr() const
get the fitsfile pointer
Definition blockio.h:93
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...
const char * fname() const
name of file associated with I/O stream, if applicable
Definition blockio.h:84
char * m_filename
Definition blockio.h:109
IOErrs
error return code
Definition blockio.h:62
int err() const
Definition blockio.h:63
void reset_iosize()
reset the m_iosize data member
Definition blockio.h:69
int iosize() const
get the total bytes of data in m_buffer
Definition blockio.h:72
virtual ~BlockIO()
const int m_recsize
Definition blockio.h:111
int current() const
get the current read position within m_buffer
Definition blockio.h:75
BlockIO(int, int, int=1, FITSErrorHandler errhandler=FITSError::defaultHandler)
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.
const int m_nrec
Definition blockio.h:112
int m_iosize
size of record in buffer
Definition blockio.h:122
const int m_blocksize
Definition blockio.h:113
int fdes() const
file descriptor associated with I/O stream, if applicable
Definition blockio.h:91
fitsfile * m_fptr
using fitsfile structure from cfitsio of NASA
Definition blockio.h:124
FITSErrorHandler m_errfn
Definition blockio.h:114
void setfptr(fitsfile *ffp)
int blockno() const
number of physical blocks read/written
Definition blockio.h:66
char * buffer() const
get m_buffer
Definition blockio.h:78
void errmsg(IOErrs, const char *)
set the error message and error number for later recovery
int recno() const
number of logical records read/written
Definition blockio.h:81
virtual char * read()
read the next logical record or first skip N logical records and then read the next one.
BlockInput(int, int, int=1, FITSErrorHandler errhandler=FITSError::defaultHandler)
virtual char * skip(int)
BlockInput(const char *, int, int=1, FITSErrorHandler errhandler=FITSError::defaultHandler)
Construction can be done either from a filename or from a file descriptor.
virtual int write(char *)
write the next logical record.
BlockOutput(const char *, int, int=1, FITSErrorHandler errhandler=FITSError::defaultHandler)
Construction can be done either from a filename or from a file descriptor.
BlockOutput(int, int, int=1, FITSErrorHandler errhandler=FITSError::defaultHandler)
static void defaultHandler(const char *errMessage, ErrorLevel severity)
The default error handler.
void(* FITSErrorHandler)(const char *errMessage, FITSError::ErrorLevel severity)
Define a typedef for the handler function signature for convenience.
Definition FITSError.h:106
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28