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
85
86class FiledesIO: public ByteIO
87{
88public:
89 // Default constructor.
90 // A stream can be attached using the attach function.
92
93 // Construct from the given file descriptor.
94 // The file name is only used in possible error messages.
95 explicit FiledesIO (int fd, const String& fileName=String());
96
97 // Attach to the given file descriptor.
98 // An exception is thrown if it is not in a detached state.
99 // The file name is only used in error messages.
100 void attach (int fd, const String& fileName);
101
102 // Detach from the file descriptor. The file is not closed.
103 void detach();
104
105 // The destructor detaches, but does not close the file.
106 virtual ~FiledesIO();
107
108 // Write the number of bytes.
109 virtual void write (Int64 size, const void* buf);
110
111 // Write the number of bytes at offset from start of the file.
112 // The file offset is not changed
113 virtual void pwrite (Int64 size, Int64 offset, const void* buf);
114
115 // Read <src>size</src> bytes from the descriptor. Returns the number of
116 // bytes actually read or a negative number if an error occurred. Will throw
117 // an Exception (AipsError) if the requested number of bytes could not be
118 // read, or an error occured, unless throwException is set to False. Will
119 // always throw an exception if the descriptor is not readable or the
120 // system call returned an undocumented value.
121 virtual Int64 read (Int64 size, void* buf, Bool throwException=True);
122
123 // Like read except reads from offset of the start of the file.
124 // The file offset is not changed
125 virtual Int64 pread (Int64 size, Int64 offset, void* buf, Bool throwException=True);
126
127 // Get the length of the byte stream.
128 virtual Int64 length();
129
130 // Is the IO stream readable?
131 virtual Bool isReadable() const;
132
133 // Is the IO stream writable?
134 virtual Bool isWritable() const;
135
136 // Is the IO stream seekable?
137 virtual Bool isSeekable() const;
138
139 // Set that the IO stream is writable.
141 { itsWritable = True; }
142
143 // Get the file name of the file attached.
144 virtual String fileName() const;
145
146 // Fsync the file (i.e. force the data to be physically written).
147 virtual void fsync();
148
149 // Truncate the file to the given size.
150 virtual void truncate (Int64 size);
151
152 // Some static convenience functions for file create/open/close.
153 // Close is only done if the fd is non-negative.
154 // <group>
155 static int create (const Char* name, int mode = 0666);
156 static int open (const Char* name, Bool writable = False,
157 Bool throwExcp = True);
158 static void close (int fd);
159 // </group>
160
161
162protected:
163 // Get the file descriptor.
164 int fd() const
165 { return itsFile; }
166
167 // Determine if the file descriptor is readable and/or writable.
168 void fillRWFlags (int fd);
169
170 // Determine if the file is seekable.
172
173 // Reset the position pointer to the given value. It returns the
174 // new position.
176
177private:
183
184 // Copy constructor, should not be used.
185 FiledesIO (const FiledesIO& that);
186
187 // Assignment, should not be used.
189};
190
191
192} //# NAMESPACE CASACORE - END
193
194#endif
SeekOption
Define the possible seek options.
Definition ByteIO.h:80
int fd() const
Get the file descriptor.
Definition FiledesIO.h:164
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:140
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:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition aipsxtype.h:36
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41
char Char
Definition aipstype.h:44