summaryrefslogtreecommitdiff
path: root/qmake/tools/qiodevice.cpp
Unidiff
Diffstat (limited to 'qmake/tools/qiodevice.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--qmake/tools/qiodevice.cpp755
1 files changed, 755 insertions, 0 deletions
diff --git a/qmake/tools/qiodevice.cpp b/qmake/tools/qiodevice.cpp
new file mode 100644
index 0000000..93969e4
--- a/dev/null
+++ b/qmake/tools/qiodevice.cpp
@@ -0,0 +1,755 @@
1/****************************************************************************
2** $Id$
3**
4** Implementation of QIODevice class
5**
6** Created : 940913
7**
8** Copyright (C) 1992-2002 Trolltech AS. All rights reserved.
9**
10** This file is part of the tools module of the Qt GUI Toolkit.
11**
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.
15**
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.
20**
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.
24**
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.
27**
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.
32**
33** Contact info@trolltech.com if any conditions of this licensing are
34** not clear to you.
35**
36**********************************************************************/
37
38#include "qiodevice.h"
39
40/*!
41 \class QIODevice qiodevice.h
42 \reentrant
43
44 \brief The QIODevice class is the base class of I/O devices.
45
46 \ingroup io
47
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 such as QFile, QBuffer and
51 QSocket inherit QIODevice and implement virtual functions such as
52 write() appropriately.
53
54 Although applications sometimes use QIODevice directly, it is
55 usually better to use QTextStream and QDataStream, which provide
56 stream operations on any QIODevice subclass. QTextStream provides
57 text-oriented stream functionality (for human-readable ASCII
58 files, for example), whereas QDataStream deals with binary data in
59 a totally platform-independent manner.
60
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:
64
65 \list
66
67 \i open() opens a device for reading and/or writing, depending on
68 the mode argument.
69
70 \i close() closes the device and tidies up (e.g. flushes buffered
71 data)
72
73 \i readBlock() reads a block of data from the device.
74
75 \i writeBlock() writes a block of data to the device.
76
77 \i readLine() reads a line (of text, usually) from the device.
78
79 \i flush() ensures that all buffered data are written to the real device.
80
81 \endlist
82
83 There are also some other, less used, action functions:
84
85 \list
86
87 \i getch() reads a single character.
88
89 \i ungetch() forgets the last call to getch(), if possible.
90
91 \i putch() writes a single character.
92
93 \i size() returns the size of the device, if there is one.
94
95 \i at() returns the current read/write pointer's position, if there
96 is one for this device, or it moves the pointer if given an offset.
97
98 \i atEnd() indicates whether there is more to read, if this is
99 meaningful for this device.
100
101 \i reset() moves the read/write pointer to the start of the
102 device, if that is possible for this device.
103
104 \endlist
105
106 The state access are all "get" functions. The QIODevice subclass
107 calls setState() to update the state, and simple access functions
108 tell the user of the device what the device's state is. Here are
109 the settings, and their associated access functions:
110
111 \list
112
113 \i Access type. Some devices are direct access (it is possible
114 to read/write anywhere), whereas others are sequential. QIODevice
115 provides the access functions (isDirectAccess(),
116 isSequentialAccess(), and isCombinedAccess()) to tell users what a
117 given I/O device supports.
118
119 \i Buffering. Some devices are accessed in raw mode, whereas
120 others are buffered. Buffering usually provides greater
121 efficiency, particularly for small read/write operations.
122 isBuffered() tells the user whether a given device is buffered.
123 (This can often be set by the application in the call to open().)
124
125 \i Synchronicity. Synchronous devices work immediately (for
126 example, files). When you read from a file, the file delivers its
127 data straight away. Other kinds of device, such as a socket
128 connected to a HTTP server, may not deliver the data until seconds
129 after you ask to read it. isSynchronous() and isAsynchronous()
130 tell the user how this device operates.
131
132 \i CR/LF translation. For simplicity, applications often like to
133 see just a single CR/LF style, and QIODevice subclasses can
134 provide this. isTranslated() returns TRUE if this object
135 translates CR/LF to just LF. (This can often be set by the
136 application in the call to open().)
137
138 \i Permissions. Some files cannot be written. For example,
139 isReadable(), isWritable() and isReadWrite() tell the application
140 whether it can read from and write to a given device. (This can
141 often be set by the application in the call to open().)
142
143 \i Finally, isOpen() returns TRUE if the device is open, i.e.
144 after an open() call.
145
146 \endlist
147
148 QIODevice provides numerous pure virtual functions that you need
149 to implement when subclassing it. Here is a skeleton subclass with
150 all the members you are sure to need and some that you will
151 probably need:
152
153 \code
154 class MyDevice : public QIODevice
155 {
156 public:
157 MyDevice();
158 ~MyDevice();
159
160 bool open( int mode );
161 void close();
162 void flush();
163
164 uint size() const;
165 int at() const;// non-pure virtual
166 bool at( int ); // non-pure virtual
167 bool atEnd() const;// non-pure virtual
168
169 int readBlock( char *data, uint maxlen );
170 int writeBlock( const char *data, uint len );
171 int readLine( char *data, uint maxlen );
172
173 int getch();
174 int putch( int );
175 int ungetch( int );
176 };
177 \endcode
178
179 The three non-pure virtual functions need not be reimplemented for
180 sequential devices.
181
182 \sa QDataStream, QTextStream
183*/
184
185/*!
186 \enum QIODevice::Offset
187
188 The offset within the device.
189*/
190
191
192/*!
193 Constructs an I/O device.
194*/
195
196QIODevice::QIODevice()
197{
198 ioMode = 0; // initial mode
199 ioSt = IO_Ok;
200 ioIndex = 0;
201}
202
203/*!
204 Destroys the I/O device.
205*/
206
207QIODevice::~QIODevice()
208{
209}
210
211
212/*!
213 \fn int QIODevice::flags() const
214
215 Returns the current I/O device flags setting.
216
217 Flags consists of mode flags and state flags.
218
219 \sa mode(), state()
220*/
221
222/*!
223 \fn int QIODevice::mode() const
224
225 Returns bits OR'ed together that specify the current operation
226 mode.
227
228 These are the flags that were given to the open() function.
229
230 The flags are \c IO_ReadOnly, \c IO_WriteOnly, \c IO_ReadWrite,
231 \c IO_Append, \c IO_Truncate and \c IO_Translate.
232*/
233
234/*!
235 \fn int QIODevice::state() const
236
237 Returns bits OR'ed together that specify the current state.
238
239 The flags are: \c IO_Open.
240
241 Subclasses may define additional flags.
242*/
243
244/*!
245 \fn bool QIODevice::isDirectAccess() const
246
247 Returns TRUE if the I/O device is a direct access device;
248 otherwise returns FALSE, i.e. if the device is a sequential access
249 device.
250
251 \sa isSequentialAccess()
252*/
253
254/*!
255 \fn bool QIODevice::isSequentialAccess() const
256
257 Returns TRUE if the device is a sequential access device;
258 otherwise returns FALSE, i.e. if the device is a direct access
259 device.
260
261 Operations involving size() and at(int) are not valid on
262 sequential devices.
263
264 \sa isDirectAccess()
265*/
266
267/*!
268 \fn bool QIODevice::isCombinedAccess() const
269
270 Returns TRUE if the I/O device is a combined access (both direct
271 and sequential) device; otherwise returns FALSE.
272
273 This access method is currently not in use.
274*/
275
276/*!
277 \fn bool QIODevice::isBuffered() const
278
279 Returns TRUE if the I/O device is a buffered device; otherwise
280 returns FALSE, i.e. the device is a raw device.
281
282 \sa isRaw()
283*/
284
285/*!
286 \fn bool QIODevice::isRaw() const
287
288 Returns TRUE if the device is a raw device; otherwise returns
289 FALSE, i.e. if the device is a buffered device.
290
291 \sa isBuffered()
292*/
293
294/*!
295 \fn bool QIODevice::isSynchronous() const
296
297 Returns TRUE if the I/O device is a synchronous device; otherwise
298 returns FALSE, i.e. the device is an asynchronous device.
299
300 \sa isAsynchronous()
301*/
302
303/*!
304 \fn bool QIODevice::isAsynchronous() const
305
306 Returns TRUE if the device is an asynchronous device; otherwise
307 returns FALSE, i.e. if the device is a synchronous device.
308
309 This mode is currently not in use.
310
311 \sa isSynchronous()
312*/
313
314/*!
315 \fn bool QIODevice::isTranslated() const
316
317 Returns TRUE if the I/O device translates carriage-return and
318 linefeed characters; otherwise returns FALSE.
319
320 A QFile is translated if it is opened with the \c IO_Translate
321 mode flag.
322*/
323
324/*!
325 \fn bool QIODevice::isReadable() const
326
327 Returns TRUE if the I/O device was opened using \c IO_ReadOnly or
328 \c IO_ReadWrite mode; otherwise returns FALSE.
329
330 \sa isWritable(), isReadWrite()
331*/
332
333/*!
334 \fn bool QIODevice::isWritable() const
335
336 Returns TRUE if the I/O device was opened using \c IO_WriteOnly or
337 \c IO_ReadWrite mode; otherwise returns FALSE.
338
339 \sa isReadable(), isReadWrite()
340*/
341
342/*!
343 \fn bool QIODevice::isReadWrite() const
344
345 Returns TRUE if the I/O device was opened using \c IO_ReadWrite
346 mode; otherwise returns FALSE.
347
348 \sa isReadable(), isWritable()
349*/
350
351/*!
352 \fn bool QIODevice::isInactive() const
353
354 Returns TRUE if the I/O device state is 0, i.e. the device is not
355 open; otherwise returns FALSE.
356
357 \sa isOpen()
358*/
359
360/*!
361 \fn bool QIODevice::isOpen() const
362
363 Returns TRUE if the I/O device has been opened; otherwise returns
364 FALSE.
365
366 \sa isInactive()
367*/
368
369
370/*!
371 \fn int QIODevice::status() const
372
373 Returns the I/O device status.
374
375 The I/O device status returns an error code. If open() returns
376 FALSE or readBlock() or writeBlock() return -1, this function can
377 be called to find out the reason why the operation failed.
378
379 \keyword IO_Ok
380 \keyword IO_ReadError
381 \keyword IO_WriteError
382 \keyword IO_FatalError
383 \keyword IO_OpenError
384 \keyword IO_ConnectError
385 \keyword IO_AbortError
386 \keyword IO_TimeOutError
387 \keyword IO_UnspecifiedError
388
389 The status codes are:
390 \table
391 \header \i Status code \i Meaning
392 \row \i \c IO_Ok \i The operation was successful.
393 \row \i \c IO_ReadError \i Could not read from the device.
394 \row \i \c IO_WriteError \i Could not write to the device.
395 \row \i \c IO_FatalError \i A fatal unrecoverable error occurred.
396 \row \i \c IO_OpenError \i Could not open the device.
397 \row \i \c IO_ConnectError \i Could not connect to the device.
398 \row \i \c IO_AbortError \i The operation was unexpectedly aborted.
399 \row \i \c IO_TimeOutError \i The operation timed out.
400 \row \i \c IO_UnspecifiedError \i An unspecified error happened on close.
401 \endtable
402
403 \sa resetStatus()
404*/
405
406/*!
407 \fn void QIODevice::resetStatus()
408
409 Sets the I/O device status to \c IO_Ok.
410
411 \sa status()
412*/
413
414
415/*!
416 \fn void QIODevice::setFlags( int f )
417 \internal
418 Used by subclasses to set the device flags.
419*/
420
421/*!
422 \internal
423 Used by subclasses to set the device type.
424*/
425
426void QIODevice::setType( int t )
427{
428#if defined(QT_CHECK_RANGE)
429 if ( (t & IO_TypeMask) != t )
430 qWarning( "QIODevice::setType: Specified type out of range" );
431#endif
432 ioMode &= ~IO_TypeMask; // reset type bits
433 ioMode |= t;
434}
435
436/*!
437 \internal
438 Used by subclasses to set the device mode.
439*/
440
441void QIODevice::setMode( int m )
442{
443#if defined(QT_CHECK_RANGE)
444 if ( (m & IO_ModeMask) != m )
445 qWarning( "QIODevice::setMode: Specified mode out of range" );
446#endif
447 ioMode &= ~IO_ModeMask; // reset mode bits
448 ioMode |= m;
449}
450
451/*!
452 \internal
453 Used by subclasses to set the device state.
454*/
455
456void QIODevice::setState( int s )
457{
458#if defined(QT_CHECK_RANGE)
459 if ( ((uint)s & IO_StateMask) != (uint)s )
460 qWarning( "QIODevice::setState: Specified state out of range" );
461#endif
462 ioMode &= ~IO_StateMask; // reset state bits
463 ioMode |= (uint)s;
464}
465
466/*!
467 \internal
468 Used by subclasses to set the device status (not state) to \a s.
469*/
470
471void QIODevice::setStatus( int s )
472{
473 ioSt = s;
474}
475
476
477/*!
478 \fn bool QIODevice::open( int mode )
479
480 Opens the I/O device using the specified \a mode. Returns TRUE if
481 the device was successfully opened; otherwise returns FALSE.
482
483 The mode parameter \a mode must be an OR'ed combination of the
484 following flags.
485 \table
486 \header \i Mode flags \i Meaning
487 \row \i \c IO_Raw \i specifies raw (unbuffered) file access.
488 \row \i \c IO_ReadOnly \i opens a file in read-only mode.
489 \row \i \c IO_WriteOnly \i opens a file in write-only mode.
490 \row \i \c IO_ReadWrite \i opens a file in read/write mode.
491 \row \i \c IO_Append \i sets the file index to the end of the file.
492 \row \i \c IO_Truncate \i truncates the file.
493 \row \i \c IO_Translate \i enables carriage returns and linefeed
494 translation for text files under MS-DOS, Windows and Macintosh. On
495 Unix systems this flag has no effect. Use with caution as it will
496 also transform every linefeed written to the file into a CRLF
497 pair. This is likely to corrupt your file if you write write
498 binary data. Cannot be combined with \c IO_Raw.
499 \endtable
500
501 This virtual function must be reimplemented by all subclasses.
502
503 \sa close()
504*/
505
506/*!
507 \fn void QIODevice::close()
508
509 Closes the I/O device.
510
511 This virtual function must be reimplemented by all subclasses.
512
513 \sa open()
514*/
515
516/*!
517 \fn void QIODevice::flush()
518
519 Flushes an open I/O device.
520
521 This virtual function must be reimplemented by all subclasses.
522*/
523
524
525/*!
526 \fn QIODevice::Offset QIODevice::size() const
527
528 Virtual function that returns the size of the I/O device.
529
530 \sa at()
531*/
532
533/*!
534 Virtual function that returns the current I/O device position.
535
536 This is the position of the data read/write head of the I/O
537 device.
538
539 \sa size()
540*/
541
542QIODevice::Offset QIODevice::at() const
543{
544 return ioIndex;
545}
546
547
548/*
549 The following is a "bad" overload, since it does "not behave essentially
550 the same" like the above. So don't use \overload in the documentation of
551 this function and we have to live with the qdoc warning which is generated
552 for this.
553*/
554/*!
555 Virtual function that sets the I/O device position to \a pos.
556 Returns TRUE if the position was successfully set, i.e. \a pos is
557 within range; otherwise returns FALSE.
558
559 \sa size()
560*/
561
562bool QIODevice::at( Offset pos )
563{
564#if defined(QT_CHECK_RANGE)
565 if ( pos > size() ) {
566#if defined(QT_LARGEFILE_SUPPORT) && defined(QT_ABI_64BITOFFSET)
567 qWarning( "QIODevice::at: Index %llu out of range", pos );
568#else
569 qWarning( "QIODevice::at: Index %lu out of range", pos );
570#endif
571 return FALSE;
572 }
573#endif
574 ioIndex = pos;
575 return TRUE;
576}
577
578/*!
579 Virtual function that returns TRUE if the I/O device position is
580 at the end of the input; otherwise returns FALSE.
581*/
582
583bool QIODevice::atEnd() const
584{
585 if ( isSequentialAccess() || isTranslated() ) {
586 QIODevice* that = (QIODevice*)this;
587 int c = that->getch();
588 bool result = c < 0;
589 that->ungetch(c);
590 return result;
591 } else {
592 return at() == size();
593 }
594}
595
596/*!
597 \fn bool QIODevice::reset()
598
599 Sets the device index position to 0.
600
601 \sa at()
602*/
603
604
605/*!
606 \fn int QIODevice::readBlock( char *data, Q_ULONG maxlen )
607
608 Reads at most \a maxlen bytes from the I/O device into \a data and
609 returns the number of bytes actually read.
610
611 This function should return -1 if a fatal error occurs and should
612 return 0 if there are no bytes to read.
613
614 The device must be opened for reading, and \a data must not be 0.
615
616 This virtual function must be reimplemented by all subclasses.
617
618 \sa writeBlock() isOpen() isReadable()
619*/
620
621/*!
622 This convenience function returns all of the remaining data in the
623 device.
624*/
625QByteArray QIODevice::readAll()
626{
627 if ( isDirectAccess() ) {
628 // we know the size
629 int n = size()-at(); // ### fix for 64-bit or large files?
630 int totalRead = 0;
631 QByteArray ba( n );
632 char* c = ba.data();
633 while ( n ) {
634 int r = readBlock( c, n );
635 if ( r < 0 )
636 return QByteArray();
637 n -= r;
638 c += r;
639 totalRead += r;
640 // If we have a translated file, then it is possible that
641 // we read less bytes than size() reports
642 if ( atEnd() ) {
643 ba.resize( totalRead );
644 break;
645 }
646 }
647 return ba;
648 } else {
649 // read until we reach the end
650 const int blocksize = 512;
651 int nread = 0;
652 QByteArray ba;
653 while ( !atEnd() ) {
654 ba.resize( nread + blocksize );
655 int r = readBlock( ba.data()+nread, blocksize );
656 if ( r < 0 )
657 return QByteArray();
658 nread += r;
659 }
660 ba.resize( nread );
661 return ba;
662 }
663}
664
665/*!
666 \fn int QIODevice::writeBlock( const char *data, Q_ULONG len )
667
668 Writes \a len bytes from \a data to the I/O device and returns the
669 number of bytes actually written.
670
671 This function should return -1 if a fatal error occurs.
672
673 This virtual function must be reimplemented by all subclasses.
674
675 \sa readBlock()
676*/
677
678/*!
679 \overload
680
681 This convenience function is the same as calling writeBlock(
682 data.data(), data.size() ).
683*/
684Q_LONG QIODevice::writeBlock( const QByteArray& data )
685{
686 return writeBlock( data.data(), data.size() );
687}
688
689/*!
690 Reads a line of text, (or up to \a maxlen bytes if a newline isn't
691 encountered) plus a terminating '\0' into \a data. If there is a
692 newline at the end if the line, it is not stripped.
693
694 Returns the number of bytes read including the terminating '\0',
695 or -1 if an error occurred.
696
697 This virtual function can be reimplemented much more efficiently
698 by the most subclasses.
699
700 \sa readBlock(), QTextStream::readLine()
701*/
702
703Q_LONG QIODevice::readLine( char *data, Q_ULONG maxlen )
704{
705 if ( maxlen == 0 ) // application bug?
706 return 0;
707 char *p = data;
708 while ( --maxlen && (readBlock(p,1)>0) ) {// read one byte at a time
709 if ( *p++ == '\n' ) // end of line
710 break;
711 }
712 *p++ = '\0';
713 return p - data;
714}
715
716
717/*!
718 \fn int QIODevice::getch()
719
720 Reads a single byte/character from the I/O device.
721
722 Returns the byte/character read, or -1 if the end of the I/O
723 device has been reached.
724
725 This virtual function must be reimplemented by all subclasses.
726
727 \sa putch(), ungetch()
728*/
729
730/*!
731 \fn int QIODevice::putch( int ch )
732
733 Writes the character \a ch to the I/O device.
734
735 Returns \a ch, or -1 if an error occurred.
736
737 This virtual function must be reimplemented by all subclasses.
738
739 \sa getch(), ungetch()
740*/
741
742/*!
743 \fn int QIODevice::ungetch( int ch )
744
745 Puts the character \a ch back into the I/O device and decrements
746 the index position if it is not zero.
747
748 This function is normally called to "undo" a getch() operation.
749
750 Returns \a ch, or -1 if an error occurred.
751
752 This virtual function must be reimplemented by all subclasses.
753
754 \sa getch(), putch()
755*/