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