casacore
Loading...
Searching...
No Matches
FiledesIO.h
Go to the documentation of this file.
1// # FiledesIO.h: Class for unbuffered IO on a file
2// # Copyright (C) 1996,1997,1999,2001
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
26#ifndef CASA_FILEDESIO_H
27#define CASA_FILEDESIO_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/IO/ByteIO.h>
32#include <casacore/casa/BasicSL/String.h>
33
34namespace casacore { // # NAMESPACE CASACORE - BEGIN
35
36// <summary>
37// Class for unbuffered IO on a file.
38// </summary>
39
40// <use visibility=export>
41
42// <reviewed reviewer="Friso Olnon" date="1996/11/06" tests="tByteIO" demos="">
43// </reviewed>
44
45// <prerequisite>
46// <li> <linkto class=ByteIO>ByteIO</linkto> class
47// <li> file descriptors
48// </prerequisite>
49
50// <synopsis>
51// This class is a specialization of class
52// <linkto class=ByteIO>ByteIO</linkto>. It uses a file descriptor
53// to read/write data.
54// <p>
55// The file associated with the file descriptor has to be opened
56// before hand.
57// The constructor will determine automatically if the file is
58// readable, writable and seekable.
59// Note that on destruction the file descriptor is NOT closed.
60// </synopsis>
61
62// <example>
63// This example shows how FiledesIO can be used with an fd.
64// It uses the fd for a regular file, which could be done in an easier
65// way using class <linkto class=RegularFileIO>RegularFileIO</linkto>.
66// However, when using pipes or sockets, this would be the only way.
67// <srcblock>
68// // Get a file descriptor for the file.
69// int fd = open ("file.name");
70// // Use that as the source of AipsIO (which will also use CanonicalIO).
71// FiledesIO fio (fd);
72// AipsIO stream (&fio);
73// // Read the data.
74// Int vali;
75// Bool valb;
76// stream >> vali >> valb;
77// </srcblock>
78// </example>
79
80// <motivation>
81// Make it possible to use the Casacore IO functionality on any file.
82// In this way any device can be hooked to the IO framework.
83// </motivation>
84
85class FiledesIO : public ByteIO {
86 public:
87 // Default constructor.
88 // A stream can be attached using the attach function.
90
91 // Construct from the given file descriptor.
92 // The file name is only used in possible error messages.
93 explicit FiledesIO(int fd, const String& fileName = String());
94
95 // Attach to the given file descriptor.
96 // An exception is thrown if it is not in a detached state.
97 // The file name is only used in error messages.
98 void attach(int fd, const String& fileName);
99
100 // Detach from the file descriptor. The file is not closed.
101 void detach();
102
103 // The destructor detaches, but does not close the file.
104 virtual ~FiledesIO();
105
106 // Write the number of bytes.
107 virtual void write(Int64 size, const void* buf);
108
109 // Write the number of bytes at offset from start of the file.
110 // The file offset is not changed
111 virtual void pwrite(Int64 size, Int64 offset, const void* buf);
112
113 // Read <src>size</src> bytes from the descriptor. Returns the number of
114 // bytes actually read or a negative number if an error occurred. Will throw
115 // an Exception (AipsError) if the requested number of bytes could not be
116 // read, or an error occured, unless throwException is set to False. Will
117 // always throw an exception if the descriptor is not readable or the
118 // system call returned an undocumented value.
119 virtual Int64 read(Int64 size, void* buf, Bool throwException = True);
120
121 // Like read except reads from offset of the start of the file.
122 // The file offset is not changed
123 virtual Int64 pread(Int64 size, Int64 offset, void* buf, Bool throwException = True);
124
125 // Get the length of the byte stream.
126 virtual Int64 length();
127
128 // Is the IO stream readable?
129 virtual Bool isReadable() const;
130
131 // Is the IO stream writable?
132 virtual Bool isWritable() const;
133
134 // Is the IO stream seekable?
135 virtual Bool isSeekable() const;
136
137 // Set that the IO stream is writable.
139
140 // Get the file name of the file attached.
141 virtual String fileName() const;
142
143 // Fsync the file (i.e. force the data to be physically written).
144 virtual void fsync();
145
146 // Truncate the file to the given size.
147 virtual void truncate(Int64 size);
148
149 // Some static convenience functions for file create/open/close.
150 // Close is only done if the fd is non-negative.
151 // <group>
152 static int create(const Char* name, int mode = 0666);
153 static int open(const Char* name, Bool writable = False, Bool throwExcp = True);
154 static void close(int fd);
155 // </group>
156
157 protected:
158 // Get the file descriptor.
159 int fd() const { return itsFile; }
160
161 // Determine if the file descriptor is readable and/or writable.
162 void fillRWFlags(int fd);
163
164 // Determine if the file is seekable.
166
167 // Reset the position pointer to the given value. It returns the
168 // new position.
170
171 private:
177
178 // Copy constructor, should not be used.
179 FiledesIO(const FiledesIO& that);
180
181 // Assignment, should not be used.
183};
184
185} // namespace casacore
186
187#endif
SeekOption
Define the possible seek options.
Definition ByteIO.h:77
ByteIO()
The constructor does nothing.
Definition ByteIO.h:167
int fd() const
Get the file descriptor.
Definition FiledesIO.h:159
static int create(const Char *name, int mode=0666)
Some static convenience functions for file create/open/close.
virtual void pwrite(Int64 size, Int64 offset, const void *buf)
Write the number of bytes at offset from start of the file.
virtual void truncate(Int64 size)
Truncate the file to the given size.
FiledesIO()
Default constructor.
virtual void write(Int64 size, const void *buf)
Write the number of bytes.
FiledesIO & operator=(const FiledesIO &that)
Assignment, should not be used.
void attach(int fd, const String &fileName)
Attach to the given file descriptor.
virtual Bool isReadable() const
Is the IO stream readable?
void detach()
Detach from the file descriptor.
void fillRWFlags(int fd)
Determine if the file descriptor is readable and/or writable.
virtual ~FiledesIO()
The destructor detaches, but does not close the file.
static int open(const Char *name, Bool writable=False, Bool throwExcp=True)
virtual Int64 pread(Int64 size, Int64 offset, void *buf, Bool throwException=True)
Like read except reads from offset of the start of the file.
FiledesIO(const FiledesIO &that)
Copy constructor, should not be used.
virtual Bool isSeekable() const
Is the IO stream seekable?
virtual String fileName() const
Get the file name of the file attached.
virtual void fsync()
Fsync the file (i.e.
FiledesIO(int fd, const String &fileName=String())
Construct from the given file descriptor.
virtual Int64 read(Int64 size, void *buf, Bool throwException=True)
Read size bytes from the descriptor.
virtual Int64 length()
Get the length of the byte stream.
void setWritable()
Set that the IO stream is writable.
Definition FiledesIO.h:138
static void close(int fd)
virtual Int64 doSeek(Int64 offset, ByteIO::SeekOption)
Reset the position pointer to the given value.
virtual Bool isWritable() const
Is the IO stream writable?
void fillSeekable()
Determine if the file is seekable.
String: the storage and methods of handling collections of characters.
Definition String.h:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
int offset(int, int) const
compute a linear offset from array indicies
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size).
Definition aipsxtype.h:36
String name() const
Return the name of the field.
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
size_t size() const
Definition Block.h:566
const Bool True
Definition aipstype.h:41
char Char
Definition aipstype.h:44