1 /****************************************************************************
4 ** Implementation of QIODevice class
8 ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
10 ** This file is part of the tools module of the Qt GUI Toolkit.
12 ** This file may be distributed under the terms of the Q Public License
13 ** as defined by Trolltech AS of Norway and appearing in the file
14 ** LICENSE.QPL included in the packaging of this file.
16 ** This file may be distributed and/or modified under the terms of the
17 ** GNU General Public License version 2 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.GPL included in the
19 ** packaging of this file.
21 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22 ** licenses may use this file in accordance with the Qt Commercial License
23 ** Agreement provided with the Software.
25 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29 ** information about Qt Commercial License Agreements.
30 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
31 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
33 ** Contact info@trolltech.com if any conditions of this licensing are
36 **********************************************************************/
38 #include "qiodevice.h"
42 \class QIODevice qiodevice.h
44 \brief The QIODevice class is the base class of I/O devices.
48 An I/O device represents a medium that one can read bytes from
49 and/or write bytes to. The QIODevice class is the abstract
50 superclass of all such devices; classes like QFile, QBuffer and
51 QSocket inherit QIODevice and implement virtual functions like
52 write() appropriately.
54 While applications sometimes use QIODevice directly, mostly it is
55 better to go through QTextStream and QDataStream, which provide
56 stream operations on any QIODevice subclass. QTextStream provides
57 text-oriented stream functionality (for human-readable ASCII files,
58 for example), while QDataStream deals with binary data in a totally
59 platform-independent manner.
61 The public member functions in QIODevice roughly fall into two
62 groups: The action functions and the state access functions. The
63 most important action functions are: <ul>
65 <li> open() opens a device for reading and/or writing, depending on
66 the argument to open().
68 <li> close() closes the device and tidies up.
70 <li> readBlock() reads a block of data from the device.
72 <li> writeBlock() writes a block of data to the device.
74 <li> readLine() reads a line (of text, usually) from the device.
76 <li> flush() ensures that all buffered data are written to the real device.
78 </ul>There are also some other, less used, action functions: <ul>
80 <li> getch() reads a single character.
82 <li> ungetch() forgets the last call to getch(), if possible.
84 <li> putch() writes a single character.
86 <li> size() returns the size of the device, if there is one.
88 <li> at() returns the current read/write pointer, if there is one
89 for this device, or it moves the pointer.
91 <li> atEnd() says whether there is more to read, if that is a
92 meaningful question for this device.
94 <li> reset() moves the read/write pointer to the start of the
95 device, if that is possible for this device.
97 </ul>The state access are all "get" functions. The QIODevice subclass
98 calls setState() to update the state, and simple access functions
99 tell the user of the device what the device's state is. Here are
100 the settings, and their associated access functions: <ul>
102 <li> Access type. Some devices are direct access (it is possible to
103 read/write anywhere) while others are sequential. QIODevice
104 provides the access functions isDirectAccess(), isSequentialAccess()
105 and isCombinedAccess() to tell users what a given I/O device
108 <li> Buffering. Some devices are accessed in raw mode while others
109 are buffered. Buffering usually provides greater efficiency,
110 particularly for small read/write operations. isBuffered() tells
111 the user whether a given device is buffered. (This can often be set
112 by the application in the call to open().)
114 <li> Synchronicity. Synchronous devices work there and then, for
115 example files. When you read from a file, the file delivers its
116 data right away. Others, such as a socket connected to a HTTP
117 server, may not deliver the data until seconds after you ask to read
118 it. isSynchronous() and isAsynchronous() tells the user how this
121 <li> CR/LF translation. For simplicity, applications often like to
122 see just a single CR/LF style, and QIODevice subclasses can provide
123 that. isTranslated() returns TRUE if this object translates CR/LF
124 to just LF. (This can often be set by the application in the call
127 <li> Accessibility. Some files cannot be written, for example.
128 isReadable(), isWritable and isReadWrite() tells the application
129 whether it can read from and write to a given device. (This can
130 often be set by the application in the call to open().)
132 <li> Finally, isOpen() returns TRUE if the device is open. This can
133 quite obviously be set using open() :)
137 QIODevice provides numerous pure virtual functions you need to
138 implement when subclassing it. Here is a skeleton subclass with all
139 the members you are certain to need, and some it's likely that you
143 class YourDevice : public QIODevice
149 bool open( int mode );
154 int at() const; // not a pure virtual function
155 bool at( int ); // not a pure virtual function
156 bool atEnd() const; // not a pure virtual function
158 int readBlock( char *data, uint maxlen );
159 int writeBlock( const char *data, uint len );
160 int readLine( char *data, uint maxlen );
168 The three non-pure virtual functions can be ignored if your device
169 is sequential (e.g. an RS-232 port).
171 \sa QDataStream, QTextStream
176 Constructs an I/O device.
179 QIODevice::QIODevice()
181 ioMode = 0; // initial mode
187 Destructs the I/O device.
190 QIODevice::~QIODevice()
196 \fn int QIODevice::flags() const
197 Returns the current I/O device flags setting.
199 Flags consists of mode flags and state flags.
205 \fn int QIODevice::mode() const
206 Returns bits OR'ed together that specify the current operation mode.
208 These are the flags that were given to the open() function.
210 The flags are: \c IO_ReadOnly, \c IO_WriteOnly, \c IO_ReadWrite,
211 \c IO_Append, \c IO_Truncate and \c IO_Translate.
215 \fn int QIODevice::state() const
216 Returns bits OR'ed together that specify the current state.
218 The flags are: \c IO_Open.
220 Subclasses may define more flags.
224 \fn bool QIODevice::isDirectAccess() const
225 Returns TRUE if the I/O device is a direct access (not sequential) device,
227 \sa isSequentialAccess()
231 \fn bool QIODevice::isSequentialAccess() const
232 Returns TRUE if the I/O device is a sequential access (not direct) device,
233 otherwise FALSE. Operations involving size() and at(int) are not valid
234 on sequential devices.
239 \fn bool QIODevice::isCombinedAccess() const
240 Returns TRUE if the I/O device is a combined access (both direct and
241 sequential) device, otherwise FALSE.
243 This access method is currently not in use.
247 \fn bool QIODevice::isBuffered() const
248 Returns TRUE if the I/O device is a buffered (not raw) device, otherwise
254 \fn bool QIODevice::isRaw() const
255 Returns TRUE if the I/O device is a raw (not buffered) device, otherwise
261 \fn bool QIODevice::isSynchronous() const
262 Returns TRUE if the I/O device is a synchronous device, otherwise
268 \fn bool QIODevice::isAsynchronous() const
269 Returns TRUE if the I/O device is a asynchronous device, otherwise
272 This mode is currently not in use.
278 \fn bool QIODevice::isTranslated() const
279 Returns TRUE if the I/O device translates carriage-return and linefeed
282 A QFile is translated if it is opened with the \c IO_Translate mode
287 \fn bool QIODevice::isReadable() const
288 Returns TRUE if the I/O device was opened using \c IO_ReadOnly or
289 \c IO_ReadWrite mode.
290 \sa isWritable(), isReadWrite()
294 \fn bool QIODevice::isWritable() const
295 Returns TRUE if the I/O device was opened using \c IO_WriteOnly or
296 \c IO_ReadWrite mode.
297 \sa isReadable(), isReadWrite()
301 \fn bool QIODevice::isReadWrite() const
302 Returns TRUE if the I/O device was opened using \c IO_ReadWrite mode.
303 \sa isReadable(), isWritable()
307 \fn bool QIODevice::isInactive() const
308 Returns TRUE if the I/O device state is 0, i.e. the device is not open.
313 \fn bool QIODevice::isOpen() const
314 Returns TRUE if the I/O device state has been opened, otherwise FALSE.
320 \fn int QIODevice::status() const
321 Returns the I/O device status.
323 The I/O device status returns an error code. If open() returns FALSE
324 or readBlock() or writeBlock() return -1, this function can be called to
325 get the reason why the operation did not succeed.
327 The status codes are:
329 <li>\c IO_Ok The operation was successful.
330 <li>\c IO_ReadError Could not read from the device.
331 <li>\c IO_WriteError Could not write to the device.
332 <li>\c IO_FatalError A fatal unrecoverable error occurred.
333 <li>\c IO_OpenError Could not open the device.
334 <li>\c IO_ConnectError Could not connect to the device.
335 <li>\c IO_AbortError The operation was unexpectedly aborted.
336 <li>\c IO_TimeOutError The operation timed out.
337 <li>\c IO_OnCloseError An unspecified error happened on close.
344 \fn void QIODevice::resetStatus()
346 Sets the I/O device status to \c IO_Ok.
353 \fn void QIODevice::setFlags( int f )
355 Used by subclasses to set the device flags.
360 Used by subclasses to set the device type.
363 void QIODevice::setType( int t )
365 #if defined(CHECK_RANGE)
366 if ( (t & IO_TypeMask) != t )
367 qWarning( "QIODevice::setType: Specified type out of range" );
369 ioMode &= ~IO_TypeMask; // reset type bits
375 Used by subclasses to set the device mode.
378 void QIODevice::setMode( int m )
380 #if defined(CHECK_RANGE)
381 if ( (m & IO_ModeMask) != m )
382 qWarning( "QIODevice::setMode: Specified mode out of range" );
384 ioMode &= ~IO_ModeMask; // reset mode bits
390 Used by subclasses to set the device state.
393 void QIODevice::setState( int s )
395 #if defined(CHECK_RANGE)
396 if ( ((uint)s & IO_StateMask) != (uint)s )
397 qWarning( "QIODevice::setState: Specified state out of range" );
399 ioMode &= ~IO_StateMask; // reset state bits
405 Used by subclasses to set the device status (not state).
408 void QIODevice::setStatus( int s )
415 \fn bool QIODevice::open( int mode )
416 Opens the I/O device using the specified \e mode.
417 Returns TRUE if successful, or FALSE if the device could not be opened.
419 The mode parameter \e m must be a combination of the following flags.
421 <li>\c IO_Raw specified raw (unbuffered) file access.
422 <li>\c IO_ReadOnly opens a file in read-only mode.
423 <li>\c IO_WriteOnly opens a file in write-only mode.
424 <li>\c IO_ReadWrite opens a file in read/write mode.
425 <li>\c IO_Append sets the file index to the end of the file.
426 <li>\c IO_Truncate truncates the file.
427 <li>\c IO_Translate enables carriage returns and linefeed translation
428 for text files under MS-DOS, Window, OS/2 and Macintosh. On Unix systems
429 this flag has no effect. Use with caution as it will also transform every linefeed
430 written to the file into a CRLF pair. This is likely to corrupt your file when
431 writing binary data to it. Cannot be combined with \c IO_Raw.
434 This virtual function must be reimplemented by all subclasses.
440 \fn void QIODevice::close()
441 Closes the I/O device.
443 This virtual function must be reimplemented by all subclasses.
449 \fn void QIODevice::flush()
451 Flushes an open I/O device.
453 This virtual function must be reimplemented by all subclasses.
458 \fn uint QIODevice::size() const
459 Virtual function that returns the size of the I/O device.
464 Virtual function that returns the current I/O device index.
466 This index is the data read/write head of the I/O device.
471 int QIODevice::at() const
477 Virtual function that sets the I/O device index to \e pos.
481 bool QIODevice::at( int pos )
483 #if defined(CHECK_RANGE)
484 if ( (uint)pos > size() ) {
485 qWarning( "QIODevice::at: Index %d out of range", pos );
494 Virtual function that returns TRUE if the I/O device index is at the
498 bool QIODevice::atEnd() const
500 if ( isSequentialAccess() || isTranslated() ) {
501 QIODevice* that = (QIODevice*)this;
502 int c = that->getch();
507 return at() == (int)size();
512 \fn bool QIODevice::reset()
513 Sets the device index to 0.
519 \fn int QIODevice::readBlock( char *data, uint maxlen )
520 Reads at most \e maxlen bytes from the I/O device into \e data and
521 returns the number of bytes actually read.
523 This virtual function must be reimplemented by all subclasses.
529 This convenience function returns all of the remaining data in the
530 device. Note that this only works for direct access devices, such
535 QByteArray QIODevice::readAll()
538 QByteArray ba(size()-at());
541 int r = readBlock( c, n );
551 \fn int QIODevice::writeBlock( const char *data, uint len )
552 Writes \e len bytes from \e p to the I/O device and returns the number of
553 bytes actually written.
555 This virtual function must be reimplemented by all subclasses.
561 This convenience function is the same as calling
562 writeBlock( data.data(), data.size() ).
564 int QIODevice::writeBlock( const QByteArray& data )
566 return writeBlock( data.data(), data.size() );
570 Reads a line of text, up to \e maxlen bytes including a terminating
571 \0. If there is a newline at the end if the line, it is not stripped.
573 Returns the number of bytes read, or -1 in case of error.
575 This virtual function can be reimplemented much more efficiently by
578 \sa readBlock(), QTextStream::readLine()
581 int QIODevice::readLine( char *data, uint maxlen )
583 if ( maxlen == 0 ) // application bug?
585 int pos = at(); // get current position
586 int s = (int)size(); // size of I/O device
590 while ( pos++ < s && --maxlen ) { // read one byte at a time
592 if ( *p++ == '\n' ) // end of line
596 return (int)((long)p - (long)data);
601 \fn int QIODevice::getch()
603 Reads a single byte/character from the I/O device.
605 Returns the byte/character read, or -1 if the end of the I/O device has been
608 This virtual function must be reimplemented by all subclasses.
610 \sa putch(), ungetch()
614 \fn int QIODevice::putch( int ch )
616 Writes the character \e ch to the I/O device.
618 Returns \e ch, or -1 if some error occurred.
620 This virtual function must be reimplemented by all subclasses.
622 \sa getch(), ungetch()
626 \fn int QIODevice::ungetch( int ch )
628 Puts the character \e ch back into the I/O device and decrements the
629 index if it is not zero.
631 This function is normally called to "undo" a getch() operation.
633 Returns \e ch, or -1 if some error occurred.
635 This virtual function must be reimplemented by all subclasses.