summaryrefslogtreecommitdiffstats
path: root/tqtinterface/qt4/src/tools/tqtextstream.cpp
blob: 1225f7bf1d624bdad8de33af978583be0e8b44ae (plain)
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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
/****************************************************************************
**
** Implementation of TQTextStream class
**
** Created : 940922
**
** 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 "tqtextstream.h"

#ifndef TQT_NO_TEXTSTREAM
#include "tqtextcodec.h"
#include "tqregexp.h"
#include "tqbuffer.h"
#include "tqfile.h"
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#ifndef TQ_OS_TEMP
#include <locale.h>
#endif

#if defined(TQ_OS_WIN32)
#include "tqt_windows.h"
#endif

/*!
    \class TQTextStream tqtextstream.h
    \reentrant
    \brief The TQTextStream class provides basic functions for reading
    and writing text using a TQIODevice.

    \ingroup io
    \ingroup text
    \mainclass

    The text stream class has a functional interface that is very
    similar to that of the standard C++ iostream class.

    TQt provides several global functions similar to the ones in iostream:
    \table
    \header \i Function \i Meaning
    \row \i bin \i sets the TQTextStream to read/write binary numbers
    \row \i oct \i sets the TQTextStream to read/write octal numbers
    \row \i dec \i sets the TQTextStream to read/write decimal numbers
    \row \i hex \i sets the TQTextStream to read/write hexadecimal numbers
    \row \i endl \i forces a line break
    \row \i flush \i forces the TQIODevice to flush any buffered data
    \row \i ws \i eats any available whitespace (on input)
    \row \i reset \i resets the TQTextStream to its default mode (see reset())
    \row \i qSetW(int) \i sets the \link width() field width \endlink
    to the given argument
    \row \i qSetFill(int) \i sets the \link fill() fill character
    \endlink to the given argument
    \row \i qSetPrecision(int) \i sets the \link precision() precision
    \endlink to the given argument
    \endtable

    \warning By default TQTextStream will automatically detect whether
    integers in the stream are in decimal, octal, hexadecimal or
    binary format when reading from the stream. In particular, a
    leading '0' signifies octal, i.e. the sequence "0100" will be
    interpreted as 64.

    The TQTextStream class reads and writes text; it is not appropriate
    for dealing with binary data (but TQDataStream is).

    By default, output of Unicode text (i.e. TQString) is done using
    the local 8-bit encoding. This can be changed using the
    setEncoding() method. For input, the TQTextStream will auto-detect
    standard Unicode "byte order marked" text files; otherwise the
    local 8-bit encoding is used.

    The TQIODevice is set in the constructor, or later using
    setDevice(). If the end of the input is reached atEnd() returns
    TRUE. Data can be read into variables of the appropriate type
    using the operator>>() overloads, or read in its entirety into a
    single string using read(), or read a line at a time using
    readLine(). Whitespace can be skipped over using skipWhiteSpace().
    You can set flags for the stream using flags() or setf(). The
    stream also supports width(), precision() and fill(); use reset()
    to reset the defaults.

    \sa TQDataStream
*/

/*!
    \enum TQTextStream::Encoding

    \value Locale
    \value Latin1
    \value Unicode
    \value UnicodeNetworkOrder
    \value UnicodeReverse
    \value RawUnicode
    \value UnicodeUTF8

    See setEncoding() for an explanation of the encodings.
*/

/*
  \class TQTSManip

  \brief The TQTSManip class is an internal helper class for the
  TQTextStream.

  It is generally a very bad idea to use this class directly in
  application programs.

  \internal

  This class makes it possible to give the TQTextStream function objects
  with arguments, like this:
  \code
    TQTextStream cout( stdout, IO_WriteOnly );
    cout << setprecision( 8 );		// TQTSManip used here!
    cout << 3.14159265358979323846;
  \endcode

  The setprecision() function returns a TQTSManip object.
  The TQTSManip object contains a pointer to a member function in
  TQTextStream and an integer argument.
  When serializing a TQTSManip into a TQTextStream, the function
  is executed with the argument.
*/

/*! \fn TQTSManip::TQTSManip( TQTSMFI m, int a )

  Constructs a TQTSManip object which will call \a m (a member function
  in TQTextStream which accepts a single int) with argument \a a when
  TQTSManip::exec() is called. Used internally in e.g. endl:

  \code
    s << "some text" << endl << "more text";
  \endcode
*/

/*! \fn void TQTSManip::exec( TQTextStream& s )

  Calls the member function specified in the constructor, for object
  \a s. Used internally in e.g. endl:

  \code
    s << "some text" << endl << "more text";
  \endcode
*/


/*****************************************************************************
  TQTextStream member functions
 *****************************************************************************/

#if defined(TQT_CHECK_STATE)
#undef  CHECK_STREAM_PRECOND
#define CHECK_STREAM_PRECOND  if ( !dev ) {				\
				qWarning( "TQTextStream: No tqdevice" );	\
				return *this; }
#else
#define CHECK_STREAM_PRECOND
#endif


#define I_SHORT		0x0010
#define I_INT		0x0020
#define I_LONG		0x0030
#define I_TYPE_MASK	0x00f0

#define I_BASE_2	TQTS::bin
#define I_BASE_8	TQTS::oct
#define I_BASE_10	TQTS::dec
#define I_BASE_16	TQTS::hex
#define I_BASE_MASK	(TQTS::bin | TQTS::oct | TQTS::dec | TQTS::hex)

#define I_SIGNED	0x0100
#define I_UNSIGNED	0x0200
#define I_SIGN_MASK	0x0f00


static const TQChar TQEOF = TQChar((ushort)0xffff); //guaranteed not to be a character.
static const uint getline_buf_size = 256; // bufsize used by ts_getline()

const int TQTextStream::basefield   = I_BASE_MASK;
const int TQTextStream::adjustfield = ( TQTextStream::left |
				       TQTextStream::right |
				       TQTextStream::internal );
const int TQTextStream::floatfield  = ( TQTextStream::scientific |
				       TQTextStream::fixed );


class TQTextStreamPrivate {
public:
#ifndef TQT_NO_TEXTCODEC
    TQTextStreamPrivate()
	: decoder( 0 ), encoder( 0 ), sourceType( NotSet ) { }
    ~TQTextStreamPrivate() {
	delete decoder;
	delete encoder;
    }
    TQTextDecoder *decoder;
    TQTextEncoder *encoder;
#else
    TQTextStreamPrivate() : sourceType( NotSet ) { }
    ~TQTextStreamPrivate() { }
#endif
    TQString ungetcBuf;

    enum SourceType { NotSet, IODevice, String, ByteArray, File };
    SourceType sourceType;
};


// skips whitespace and returns the first non-whitespace character
TQChar TQTextStream::eat_ws()
{
    TQChar c;
    do { c = ts_getc(); } while ( c != TQEOF && ts_isspace(c) );
    return c;
}

void TQTextStream::init()
{
    // ### ungetcBuf = TQEOF;
    dev = 0;
    owndev = FALSE;
    mapper = 0;
    d = new TQTextStreamPrivate;
    doUnicodeHeader = TRUE; // autodetect
    latin1 = TRUE; // should use locale?
    internalOrder = TQChar::networkOrdered();
    networkOrder = TRUE;
}

/*!
    Constructs a data stream that has no IO tqdevice.
*/

TQTextStream::TQTextStream()
{
    init();
    setEncoding( Locale );
    reset();
    d->sourceType = TQTextStreamPrivate::NotSet;
}

/*!
    Constructs a text stream that uses the IO tqdevice \a iod.
*/

TQTextStream::TQTextStream( QIODevice *iod )
{
    init();
    setEncoding( Locale );
    dev = TQT_TQIODEVICE(iod);
    reset();
    d->sourceType = TQTextStreamPrivate::IODevice;
}

/*!
    Constructs a text stream that uses the IO tqdevice \a iod.
*/

TQTextStream::TQTextStream( QFile *iod )
{
    init();
    setEncoding( Locale );
    dev = TQT_TQIODEVICE(iod);
    reset();
    d->sourceType = TQTextStreamPrivate::IODevice;
}

// TODO: use special-case handling of this case in TQTextStream, and
//	 simplify this class to only deal with TQChar or TQString data.
class TQStringBuffer : public TQIODevice {
public:
    TQStringBuffer( TQString* str );
    ~TQStringBuffer();
    bool  open( int m );
    void  close();
    void  flush();
#ifdef USE_QT4
    qint64  size() const;
#else
    Offset  size() const;
#endif
    Offset  at()   const;
    bool  at( Offset pos );
#ifdef USE_QT4
    qint64 readData( char *p, qint64 len );
    qint64 writeData( const char *p, qint64 len );
#else
    TQ_LONG readBlock( char *p, TQ_ULONG len );
    TQ_LONG writeBlock( const char *p, TQ_ULONG len );
#endif
    int   getch();
    int   putch( int ch );
    int   ungetch( int ch );
protected:
    TQString* s;

private:
    TQStringBuffer( const TQStringBuffer & );
    TQStringBuffer &operator=( const TQStringBuffer & );
};


TQStringBuffer::TQStringBuffer( TQString* str )
{
    s = str;
}

TQStringBuffer::~TQStringBuffer()
{
}


bool TQStringBuffer::open( int m )
{
    if ( !s ) {
#if defined(TQT_CHECK_STATE)
	qWarning( "TQStringBuffer::open: No string" );
#endif
	return FALSE;
    }
    if ( isOpen() ) {
#if defined(TQT_CHECK_STATE)
	qWarning( "TQStringBuffer::open: Buffer already open" );
#endif
	return FALSE;
    }
    setMode( m );
    if ( m & IO_Truncate )
	s->truncate( 0 );

    if ( m & IO_Append ) {
	seek(s->length()*sizeof(TQChar));
    } else {
	seek(0);
    }
    setState( IO_Open );
    resetqStatus();
    return TRUE;
}

void TQStringBuffer::close()
{
    if ( isOpen() ) {
	setFlags( IO_Direct );
	seek(0);
    }
}

void TQStringBuffer::flush()
{
}

#ifdef USE_QT4
qint64 TQStringBuffer::size() const
#else
TQIODevice::Offset TQStringBuffer::size() const
#endif
{
    return s ? s->length()*sizeof(TQChar) : 0;
}

TQIODevice::Offset  TQStringBuffer::at()   const
{
    return pos();
}

bool TQStringBuffer::at( Offset pos )
{
#if defined(TQT_CHECK_STATE)
    if ( !isOpen() ) {
	qWarning( "TQStringBuffer::at: Buffer is not open" );
	return FALSE;
    }
#endif
    if ( pos >= s->length()*2 ) {
#if defined(TQT_CHECK_RANGE)
#if defined(TQT_ABI_QT4)
	qWarning( "TQStringBuffer::at: Index %lld out of range", pos );
#else
	qWarning( "TQStringBuffer::at: Index %lu out of range", pos );
#endif
#endif
	return FALSE;
    }
    seek(pos);
    return TRUE;
}

#ifdef USE_QT4
qint64 TQStringBuffer::readData( char *p, qint64 len )
#else
TQ_LONG TQStringBuffer::readBlock( char *p, TQ_ULONG len )
#endif
{
#if defined(TQT_CHECK_STATE)
    TQ_CHECK_PTR( p );
    if ( !isOpen() ) {
	qWarning( "TQStringBuffer::readBlock: Buffer not open" );
	return -1;
    }
    if ( !isReadable() ) {
	qWarning( "TQStringBuffer::readBlock: Read operation not permitted" );
	return -1;
    }
#endif
    if ( pos() + len > s->length()*sizeof(TQChar) ) {
	// overflow
	if ( (uint)pos() >= s->length()*sizeof(TQChar) ) {
	    setqStatus( IO_ReadError );
	    return -1;
	} else {
	    len = s->length()*2 - (uint)pos();
	}
    }
    memcpy( p, ((const char*)(s->tqunicode()))+pos(), len );
    seek(pos()+len);
    return len;
}

#ifdef USE_QT4
qint64 TQStringBuffer::writeData( const char *p, qint64 len )
#else
TQ_LONG TQStringBuffer::writeBlock( const char *p, TQ_ULONG len )
#endif
{
#if defined(TQT_CHECK_NULL)
    if ( p == 0 && len != 0 )
	qWarning( "TQStringBuffer::writeBlock: Null pointer error" );
#endif
#if defined(TQT_CHECK_STATE)
    if ( !isOpen() ) {
	qWarning( "TQStringBuffer::writeBlock: Buffer not open" );
	return -1;
    }
    if ( !isWritable() ) {
	qWarning( "TQStringBuffer::writeBlock: Write operation not permitted" );
	return -1;
    }
    if ( pos()&1 ) {
	qWarning( "TQStringBuffer::writeBlock: non-even index - non Unicode" );
	return -1;
    }
    if ( len&1 ) {
	qWarning( "TQStringBuffer::writeBlock: non-even length - non Unicode" );
	return -1;
    }
#endif
    s->replace(pos()/2, len/2, (TQChar*)p, len/2);
    seek(pos()+len);
    return len;
}

int TQStringBuffer::getch()
{
#if defined(TQT_CHECK_STATE)
    if ( !isOpen() ) {
	qWarning( "TQStringBuffer::getch: Buffer not open" );
	return -1;
    }
    if ( !isReadable() ) {
	qWarning( "TQStringBuffer::getch: Read operation not permitted" );
	return -1;
    }
#endif
    if ( (uint)pos() >= s->length()*2 ) {           // overflow
	setqStatus( IO_ReadError );
	return -1;
    }
    int retval = (int)((const uchar *)s->tqunicode())[pos()];
    seek(pos()+1);
    return retval;
}

int TQStringBuffer::putch( int ch )
{
    char c = ch;
    if ( writeBlock(&c,1) < 0 )
	return -1;
    else
	return ch;
}

int TQStringBuffer::ungetch( int ch )
{
#if defined(TQT_CHECK_STATE)
    if ( !isOpen() ) {
	qWarning( "TQStringBuffer::ungetch: Buffer not open" );
	return -1;
    }
    if ( !isReadable() ) {
	qWarning( "TQStringBuffer::ungetch: Read operation not permitted" );
	return -1;
    }
#endif
    if ( ch != -1 ) { // something to do with eof
	if ( pos() )
	    seek(pos()-1);
	else
	    ch = -1;
    }
    return ch;
}


/*!
    Constructs a text stream that operates on the Unicode TQString, \a
    str, through an internal tqdevice. The \a filemode argument is
    passed to the tqdevice's open() function; see \l{TQIODevice::mode()}.

    If you set an encoding or codec with setEncoding() or setCodec(),
    this setting is ignored for text streams that operate on TQString.

    Example:
    \code
    TQString str;
    TQTextStream ts( &str, IO_WriteOnly );
    ts << "pi = " << 3.14; // str == "pi = 3.14"
    \endcode

    Writing data to the text stream will modify the contents of the
    string. The string will be expanded when data is written beyond
    the end of the string. Note that the string will not be truncated:
    \code
    TQString str = "pi = 3.14";
    TQTextStream ts( &str, IO_WriteOnly );
    ts <<  "2+2 = " << 2+2; // str == "2+2 = 414"
    \endcode

    Note that because TQString is Unicode, you should not use
    readRawBytes() or writeRawBytes() on such a stream.
*/

TQTextStream::TQTextStream( TQString* str, int filemode )
{
    // TODO: optimize for this case as it becomes more common
    //        (see TQStringBuffer above)
    init();
    dev = new TQStringBuffer( str );
    ((TQStringBuffer *)dev)->open( filemode );
    owndev = TRUE;
    setEncoding(RawUnicode);
    reset();
    d->sourceType = TQTextStreamPrivate::String;
}

/*! \obsolete

  This constructor is equivalent to the constructor taking a TQString*
  parameter.
*/

TQTextStream::TQTextStream( TQString& str, int filemode )
{
    init();
    dev = new TQStringBuffer( &str );
    ((TQStringBuffer *)dev)->open( filemode );
    owndev = TRUE;
    setEncoding(RawUnicode);
    reset();
    d->sourceType = TQTextStreamPrivate::String;
}

/*!
    Constructs a text stream that operates on the byte array, \a a,
    through an internal TQBuffer tqdevice. The \a mode argument is passed
    to the tqdevice's open() function; see \l{TQIODevice::mode()}.

    Example:
    \code
    TQByteArray array;
    TQTextStream ts( array, IO_WriteOnly );
    ts << "pi = " << 3.14 << '\0'; // array == "pi = 3.14"
    \endcode

    Writing data to the text stream will modify the contents of the
    array. The array will be expanded when data is written beyond the
    end of the string.

    Same example, using a TQBuffer:
    \code
    TQByteArray array;
    TQBuffer buf( array );
    buf.open( IO_WriteOnly );
    TQTextStream ts( &buf );
    ts << "pi = " << 3.14 << '\0'; // array == "pi = 3.14"
    buf.close();
    \endcode
*/

TQTextStream::TQTextStream( TQByteArray a, int mode )
{
    init();
    dev = TQT_TQIODEVICE(new TQBuffer( a ));
    ((TQBuffer *)dev)->open( (QIODevice::OpenModeFlag)mode );
    owndev = TRUE;
    setEncoding( Latin1 ); //### Locale???
    reset();
    d->sourceType = TQTextStreamPrivate::ByteArray;
}

/*!
    Constructs a text stream that operates on an existing file handle
    \a fh through an internal TQFile tqdevice. The \a mode argument is
    passed to the tqdevice's open() function; see \l{TQIODevice::mode()}.

    Note that if you create a TQTextStream \c cout or another name that
    is also used for another variable of a different type, some
    linkers may confuse the two variables, which will often cause
    crashes.
*/

TQTextStream::TQTextStream( FILE *fh, int mode )
{
    init();
    setEncoding( Locale ); //###
    dev = TQT_TQIODEVICE(new TQFile);
    ((TQFile *)dev)->open( mode, fh );
    owndev = TRUE;
    reset();
    d->sourceType = TQTextStreamPrivate::File;
}

/*!
    Destroys the text stream.

    The destructor does not affect the current IO tqdevice.
*/

TQTextStream::~TQTextStream()
{
    if ( owndev )
	delete dev;
    delete d;
}

/*!
    Positions the read pointer at the first non-whitespace character.
*/
void TQTextStream::skipWhiteSpace()
{
    ts_ungetc( eat_ws() );
}

/*!
    Internal.
*/
int TQTextStream::dev_getch() {
	if (d->sourceType == TQTextStreamPrivate::String)
		return static_cast<TQStringBuffer*>(dev)->getch();
	else
		return dev->getch();
}

/*!
    Internal.
*/
int TQTextStream::dev_ungetch(int ch) {
	if (d->sourceType == TQTextStreamPrivate::String)
		return static_cast<TQStringBuffer*>(dev)->ungetch(ch);
	else
		return dev->ungetch(ch);
}

/*!
    Tries to read \a len characters from the stream and stores them in
    \a buf. Returns the number of characters really read.

    \warning There will no TQEOF appended if the read reaches the end
    of the file. EOF is reached when the return value does not equal
    \a len.
*/
uint TQTextStream::ts_getbuf( TQChar* buf, uint len )
{
    if ( len < 1 )
	return 0;

    uint rnum = 0;   // the number of TQChars really read

    if ( d && d->ungetcBuf.length() ) {
	while ( rnum < len && rnum < d->ungetcBuf.length() ) {
	    *buf = d->ungetcBuf.constref( rnum );
	    buf++;
	    rnum++;
	}
	d->ungetcBuf = d->ungetcBuf.mid( rnum );
	if ( rnum >= len )
	    return rnum;
    }

    // we use dev_ungetch() for one of the bytes of the tqunicode
    // byte-order mark, but a local unget hack for the other byte:
    int ungetHack = EOF;

    if ( doUnicodeHeader ) {
	doUnicodeHeader = FALSE; // only at the top
	int c1 = dev_getch();
	if ( c1 == EOF )
	    return rnum;
	int c2 = dev_getch();
	if ( c1 == 0xfe && c2 == 0xff ) {
	    mapper = 0;
	    latin1 = FALSE;
	    internalOrder = TQChar::networkOrdered();
	    networkOrder = TRUE;
	} else if ( c1 == 0xff && c2 == 0xfe ) {
	    mapper = 0;
	    latin1 = FALSE;
	    internalOrder = !TQChar::networkOrdered();
	    networkOrder = FALSE;
	} else {
	    if ( c2 != EOF ) {
	 	dev_ungetch( c2 );
		ungetHack = c1;
	    } else {
		/*
		  A small bug might hide here. If only the first byte
		  of a file has made it so far, and that first byte
		  is half of the byte-order mark, then the utfness
		  will not be detected.
		*/
	 	dev_ungetch( c1 );
	    }
	}
    }

#ifndef TQT_NO_TEXTCODEC
    if ( mapper ) {
	bool shortRead = FALSE;
	if ( !d->decoder )
	    d->decoder = mapper->makeDecoder();
	while( rnum < len ) {
	    TQString s;
	    bool readBlock = !( len == 1+rnum );
	    for (;;) {
		// for efficiency: normally read a whole block
		if ( readBlock ) {
		    // guess buffersize; this may be wrong (too small or too
		    // big). But we can handle this (either iterate reading
		    // or use ungetcBuf).
		    // Note that this might cause problems for codecs where
		    // one byte can result in >1 Unicode Characters if bytes
		    // are written to the stream in the meantime (loss of
		    // synchronicity).
		    uint rlen = len - rnum;
		    char *cbuf = new char[ rlen ];
		    if ( ungetHack != EOF ) {
			rlen = 1+dev->readBlock( cbuf+1, rlen-1 );
			cbuf[0] = (char)ungetHack;
			ungetHack = EOF;
		    } else {
			rlen = dev->readBlock( cbuf, rlen );
		    }
		    s  += d->decoder->toUnicode( cbuf, rlen );
		    delete[] cbuf;
		    // use buffered reading only for the first time, because we
		    // have to get the stream synchronous again (this is easier
		    // with single character reading)
		    readBlock = FALSE;
		}
		// get stream (and codec) in sync
		int c;
		if ( ungetHack == EOF ) {
		    c = dev_getch();
		} else {
		    c = ungetHack;
		    ungetHack = EOF;
		}
		if ( c == EOF ) {
		    shortRead = TRUE;
		    break;
		}
		char b = c;
		uint lengthBefore = s.length();
		s += d->decoder->toUnicode( &b, 1 );
		if ( s.length() > lengthBefore )
		    break; // it seems we are in sync now
	    }
	    uint i = 0;
	    uint end = TQMIN( len-rnum, s.length() );
	    while( i < end ) {
		*buf = s.constref(i++);
		buf++;
	    }
	    rnum += end;
	    if ( s.length() > i ) {
		// could be = but append is clearer
		d->ungetcBuf.append( s.mid( i ) );
	    }
	    if ( shortRead )
		return rnum;
	}
    } else
#endif
    if ( latin1 ) {
	if ( len == 1+rnum ) {
	    // use this method for one character because it is more efficient
	    // (arnt doubts whether it makes a difference, but lets it stand)
	    int c = (ungetHack == EOF) ? dev_getch() : ungetHack;
	    if ( c != EOF ) {
		*buf = (char)c;
		buf++;
		rnum++;
	    }
	} else {
	    if ( ungetHack != EOF ) {
		*buf = (char)ungetHack;
		buf++;
		rnum++;
		ungetHack = EOF;
	    }
	    char *cbuf = new char[len - rnum];
	    while ( !dev->atEnd() && rnum < len ) {
		uint rlen = len - rnum;
		rlen = dev->readBlock( cbuf, rlen );
		char *it = cbuf;
		char *end = cbuf + rlen;
		while ( it < end ) {
		    *buf = *it;
		    buf++;
		    it++;
		}
		rnum += rlen;
	    }
	    delete[] cbuf;
	}
    } else { // UCS-2 or UTF-16
	if ( len == 1+rnum ) {
	    int c1 = (ungetHack == EOF) ? dev_getch() : ungetHack;
	    if ( c1 == EOF )
		return rnum;
	    int c2 = dev_getch();
	    if ( c2 == EOF )
		return rnum;

	    if ( networkOrder ) {
		*buf = TQChar( c2, c1 );
	    } else {
		*buf = TQChar( c1, c2 );
	    }
	    buf++;
	    rnum++;
	} else {
	    char *cbuf = new char[ 2*( len - rnum ) ]; // for paranoids: overflow possible
	    while ( !dev->atEnd() && rnum < len ) {
		uint rlen = 2 * ( len-rnum );
		if ( ungetHack != EOF ) {
		    rlen = 1+dev->readBlock( cbuf+1, rlen-1 );
		    cbuf[0] = (char)ungetHack;
		    ungetHack = EOF;
		} else {
		    rlen = dev->readBlock( cbuf, rlen );
		}
		// We can't use an odd number of bytes, so put it back. But
		// do it only if we are capable of reading more -- normally
		// there should not be an odd number, but the file might be
		// truncated or not in UTF-16...
		if ( (rlen & 1) == 1 )
		    if ( !dev->atEnd() )
			dev_ungetch( cbuf[--rlen] );
		uint i = 0;
		if ( networkOrder ) {
		    while( i < rlen ) {
			*buf = TQChar( cbuf[i+1], cbuf[i] );
			buf++;
			i+=2;
		    }
		} else {
		    while( i < rlen ) {
			*buf = TQChar( cbuf[i], cbuf[i+1] );
			buf++;
			i+=2;
		    }
		}
		rnum += i/2;
	    }
	    delete[] cbuf;
	}
    }
    return rnum;
}

/*!
    Tries to read one line, but at most len characters from the stream
    and stores them in \a buf.

    Returns the number of characters really read. Newlines are not
    stripped.

    There will be a TQEOF appended if the read reaches the end of file;
    this is different to ts_getbuf().

    This function works only if a newline (as byte) is also a newline
    (as resulting character) since it uses TQIODevice::readLine(). So
    use it only for such codecs where this is true!

    This function is (almost) a no-op for UTF 16. Don't use it if
    doUnicodeHeader is TRUE!
*/
uint TQTextStream::ts_getline( TQChar* buf )
{
    uint rnum=0;   // the number of TQChars really read
    char cbuf[ getline_buf_size+1 ];

    if ( d && d->ungetcBuf.length() ) {
	while( rnum < getline_buf_size && rnum < d->ungetcBuf.length() ) {
	    buf[rnum] = d->ungetcBuf.constref(rnum);
	    rnum++;
	}
	d->ungetcBuf = d->ungetcBuf.mid( rnum );
	if ( rnum >= getline_buf_size )
	    return rnum;
    }

#ifndef TQT_NO_TEXTCODEC
    if ( mapper ) {
	if ( !d->decoder )
	    d->decoder = mapper->makeDecoder();
	TQString s;
	bool readBlock = TRUE;
	for (;;) {
	    // for efficiency: try to read a line
	    if ( readBlock ) {
		int rlen = getline_buf_size - rnum;
		rlen = dev->readLine( cbuf, rlen+1 );
		if ( rlen == -1 )
		    rlen = 0;
		s  += d->decoder->toUnicode( cbuf, rlen );
		readBlock = FALSE;
	    }
	    if ( dev->atEnd()
		    || s.at( s.length()-1 ) == '\n'
		    || s.at( s.length()-1 ) == '\r'
	       ) {
		break;
	    } else {
		// get stream (and codec) in sync
		int c;
		c = dev_getch();
		if ( c == EOF ) {
		    break;
		}
		char b = c;
		uint lengthBefore = s.length();
		s  += d->decoder->toUnicode( &b, 1 );
		if ( s.length() > lengthBefore )
		    break; // it seems we are in sync now
	    }
	}
	uint i = 0;
	while( rnum < getline_buf_size && i < s.length() )
	    buf[rnum++] = s.constref(i++);
	if ( s.length() > i )
	    // could be = but append is clearer
	    d->ungetcBuf.append( s.mid( i ) );
	if ( rnum < getline_buf_size && dev->atEnd() )
	    buf[rnum++] = TQEOF;
    } else
#endif
    if ( latin1 ) {
	int rlen = getline_buf_size - rnum;
	rlen = dev->readLine( cbuf, rlen+1 );
	if ( rlen == -1 )
	    rlen = 0;
	char *end = cbuf+rlen;
	char *it = cbuf;
	buf +=rnum;
	while ( it != end ) {
	    buf->setCell( *(it++) );
	    buf->setRow( 0 );
	    buf++;
	}
	rnum += rlen;
	if ( rnum < getline_buf_size && dev->atEnd() )
	    buf[1] = TQEOF;
    }
    return rnum;
}


/*!
    Puts one character into the stream.
*/
void TQTextStream::ts_putc( TQChar c )
{
#ifndef TQT_NO_TEXTCODEC
    if ( mapper ) {
	if ( !d->encoder )
	    d->encoder = mapper->makeEncoder();
	int len = 1;
	TQString s = c;
	TQCString block = d->encoder->fromUnicode( s, len );
	dev->writeBlock( block, len );
    } else
#endif
    if ( latin1 ) {
	if ( c.row() )
	    dev->putch( '?' ); // unknown character
	else
	    dev->putch( c.cell() );
    } else {
	if ( doUnicodeHeader ) {
	    doUnicodeHeader = FALSE;
	    ts_putc( TQChar::byteOrderMark );
	}
	if ( internalOrder ) {
	    // this case is needed by TQStringBuffer
	    dev->writeBlock( (char*)&c, sizeof(TQChar) );
	} else if ( networkOrder ) {
	    dev->putch( c.row() );
	    dev->putch( c.cell() );
	} else {
	    dev->putch( c.cell() );
	    dev->putch( c.row() );
	}
    }
}

/*!
    Puts one character into the stream.
*/
void TQTextStream::ts_putc( int ch )
{
    ts_putc( TQChar((ushort)ch) );
}

bool TQTextStream::ts_isdigit( TQChar c )
{
    return c.isDigit();
}

bool TQTextStream::ts_isspace( TQChar c )
{
    return c.isSpace();
}

void TQTextStream::ts_ungetc( TQChar c )
{
    if ( c.tqunicode() == 0xffff )
	return;

    d->ungetcBuf.prepend( c );
}



/*!
    Reads \a len bytes from the stream into \a s and returns a
    reference to the stream.

    The buffer \a s must be preallocated.

    Note that no encoding is done by this function.

    \warning The behavior of this function is undefined unless the
    stream's encoding is set to Unicode or Latin1.

    \sa TQIODevice::readBlock()
*/

TQTextStream &TQTextStream::readRawBytes( char *s, uint len )
{
    dev->readBlock( s, len );
    return *this;
}

/*!
    Writes the \a len bytes from \a s to the stream and returns a
    reference to the stream.

    Note that no encoding is done by this function.

    \sa TQIODevice::writeBlock()
*/

TQTextStream &TQTextStream::writeRawBytes( const char* s, uint len )
{
    dev->writeBlock( s, len );
    return *this;
}


TQTextStream &TQTextStream::writeBlock( const char* p, uint len )
{
    if ( doUnicodeHeader ) {
	doUnicodeHeader = FALSE;
	if ( !mapper && !latin1 )
	    ts_putc( TQChar::byteOrderMark );
    }
    // TQCString and const char * are treated as Latin-1
    if ( !mapper && latin1 ) {
	dev->writeBlock( p, len );
    } else if ( !mapper && internalOrder ) {
	TQChar *u = new TQChar[len];
	for ( uint i = 0; i < len; i++ )
	    u[i] = p[i];
	dev->writeBlock( (char*)u, len * sizeof(TQChar) );
	delete [] u;
    }
#ifndef TQT_NO_TEXTCODEC
    else if (mapper) {
        if (!d->encoder)
            d->encoder = mapper->makeEncoder();
        TQString s = TQString::tqfromLatin1(p, len);
        int l = len;
        TQCString block = d->encoder->fromUnicode(s, l);
        dev->writeBlock(block, l);
    }
#endif
    else {
	for ( uint i = 0; i < len; i++ )
	    ts_putc( (uchar)p[i] );
    }
    return *this;
}

TQTextStream &TQTextStream::writeBlock( const TQChar* p, uint len )
{
#ifndef TQT_NO_TEXTCODEC
    if ( mapper ) {
	if ( !d->encoder )
	    d->encoder = mapper->makeEncoder();
	TQConstString s( p, len );
	int l = len;
	TQCString block = d->encoder->fromUnicode( s.string(), l );
	dev->writeBlock( block, l );
    } else
#endif
    if ( latin1 ) {
	char *str = TQString::tqunicodeToLatin1( p, len );
	dev->writeBlock( str, len );
	delete [] str;
    } else if ( internalOrder ) {
	if ( doUnicodeHeader ) {
	    doUnicodeHeader = FALSE;
	    ts_putc( TQChar::byteOrderMark );
	}
	dev->writeBlock( (char*)p, sizeof(TQChar)*len );
    } else {
	for (uint i=0; i<len; i++)
	    ts_putc( p[i] );
    }
    return *this;
}

/*!
    Resets the text stream.

    \list
    \i All flags are set to 0.
    \i The field width is set to 0.
    \i The fill character is set to ' ' (Space).
    \i The precision is set to 6.
    \endlist

    \sa setf(), width(), fill(), precision()
*/

void TQTextStream::reset()
{
    fflags = 0;
    fwidth = 0;
    fillchar = ' ';
    fprec = 6;
}

/*!
    \fn TQIODevice *TQTextStream::tqdevice() const

    Returns the IO tqdevice currently set.

    \sa setDevice(), unsetDevice()
*/

/*!
    Sets the IO tqdevice to \a iod.

    \sa tqdevice(), unsetDevice()
*/

void TQTextStream::setDevice( TQIODevice *iod )
{
    if ( owndev ) {
	delete dev;
	owndev = FALSE;
    }
    dev = iod;
    d->sourceType = TQTextStreamPrivate::IODevice;
}

/*!
    Unsets the IO tqdevice. Equivalent to setDevice( 0 ).

    \sa tqdevice(), setDevice()
*/

void TQTextStream::unsetDevice()
{
    setDevice( 0 );
    d->sourceType = TQTextStreamPrivate::NotSet;
}

/*!
    \fn bool TQTextStream::atEnd() const

    Returns TRUE if the IO tqdevice has reached the end position (end of
    the stream or file) or if there is no IO tqdevice set; otherwise
    returns FALSE.

    \sa TQIODevice::atEnd()
*/

/*!\fn bool TQTextStream::eof() const

  \obsolete

  This function has been renamed to atEnd().

  \sa TQIODevice::atEnd()
*/

/*****************************************************************************
  TQTextStream read functions
 *****************************************************************************/


/*!
    \overload

    Reads a char \a c from the stream and returns a reference to the
    stream. Note that whitespace is skipped.
*/

TQTextStream &TQTextStream::operator>>( char &c )
{
    CHECK_STREAM_PRECOND
    c = eat_ws();
    return *this;
}

/*!
    Reads a char \a c from the stream and returns a reference to the
    stream. Note that whitespace is \e not skipped.
*/

TQTextStream &TQTextStream::operator>>( TQChar &c )
{
    CHECK_STREAM_PRECOND
    c = ts_getc();
    return *this;
}


ulong TQTextStream::input_bin()
{
    ulong val = 0;
    TQChar ch = eat_ws();
    int dv = ch.digitValue();
    while (  dv == 0 || dv == 1 ) {
	val = ( val << 1 ) + dv;
	ch = ts_getc();
	dv = ch.digitValue();
    }
    if ( ch != TQEOF )
	ts_ungetc( ch );
    return val;
}

ulong TQTextStream::input_oct()
{
    ulong val = 0;
    TQChar ch = eat_ws();
    int dv = ch.digitValue();
    while ( dv >= 0 && dv <= 7 ) {
	val = ( val << 3 ) + dv;
	ch = ts_getc();
	dv = ch.digitValue();
    }
    if ( dv == 8 || dv == 9 ) {
	while ( ts_isdigit(ch) )
	    ch = ts_getc();
    }
    if ( ch != TQEOF )
	ts_ungetc( ch );
    return val;
}

ulong TQTextStream::input_dec()
{
    ulong val = 0;
    TQChar ch = eat_ws();
    int dv = ch.digitValue();
    while ( ts_isdigit(ch) ) {
	val = val * 10 + dv;
	ch = ts_getc();
	dv = ch.digitValue();
    }
    if ( ch != TQEOF )
	ts_ungetc( ch );
    return val;
}

ulong TQTextStream::input_hex()
{
    ulong val = 0;
    TQChar ch = eat_ws();
    char c = ch;
    while ( isxdigit((uchar) c) ) {
	val <<= 4;
	if ( ts_isdigit(c) )
	    val += c - '0';
	else
	    val += 10 + tolower( (uchar) c ) - 'a';
	c = ch = ts_getc();
    }
    if ( ch != TQEOF )
	ts_ungetc( ch );
    return val;
}

long TQTextStream::input_int()
{
    long val;
    TQChar ch;
    char c;
    switch ( flags() & basefield ) {
    case bin:
	val = (long)input_bin();
	break;
    case oct:
	val = (long)input_oct();
	break;
    case dec:
	c = ch = eat_ws();
	if ( ch == TQEOF ) {
	    val = 0;
	} else {
	    if ( !(c == '-' || c == '+') )
		ts_ungetc( ch );
	    if ( c == '-' ) {
		ulong v = input_dec();
		if ( v ) {		// ensure that LONG_MIN can be read
		    v--;
		    val = -((long)v) - 1;
		} else {
		    val = 0;
		}
	    } else {
		val = (long)input_dec();
	    }
	}
	break;
    case hex:
	val = (long)input_hex();
	break;
    default:
	val = 0;
	c = ch = eat_ws();
	if ( c == '0' ) {		// bin, oct or hex
	    c = ch = ts_getc();
	    if ( tolower((uchar) c) == 'x' )
		val = (long)input_hex();
	    else if ( tolower((uchar) c) == 'b' )
		val = (long)input_bin();
	    else {			// octal
		ts_ungetc( ch );
		if ( c >= '0' && c <= '7' ) {
		    val = (long)input_oct();
		} else {
		    val = 0;
		}
	    }
	} else if ( ts_isdigit(ch) ) {
	    ts_ungetc( ch );
	    val = (long)input_dec();
	} else if ( c == '-' || c == '+' ) {
	    ulong v = input_dec();
	    if ( c == '-' ) {
		if ( v ) {		// ensure that LONG_MIN can be read
		    v--;
		    val = -((long)v) - 1;
		} else {
		    val = 0;
		}
	    } else {
		val = (long)v;
	    }
	}
    }
    return val;
}

//
// We use a table-driven FSM to parse floating point numbers
// strtod() cannot be used directly since we're reading from a TQIODevice
//

double TQTextStream::input_double()
{
    const int Init	 = 0;			// states
    const int Sign	 = 1;
    const int Mantissa	 = 2;
    const int Dot	 = 3;
    const int Abscissa	 = 4;
    const int ExpMark	 = 5;
    const int ExpSign	 = 6;
    const int Exponent	 = 7;
    const int Done	 = 8;

    const int InputSign	 = 1;			// input tokens
    const int InputDigit = 2;
    const int InputDot	 = 3;
    const int InputExp	 = 4;

    static const uchar table[8][5] = {
     /* None	 InputSign   InputDigit InputDot InputExp */
	{ 0,	    Sign,     Mantissa,	 Dot,	   0,	   }, // Init
	{ 0,	    0,	      Mantissa,	 Dot,	   0,	   }, // Sign
	{ Done,	    Done,     Mantissa,	 Dot,	   ExpMark,}, // Mantissa
	{ 0,	    0,	      Abscissa,	 0,	   0,	   }, // Dot
	{ Done,	    Done,     Abscissa,	 Done,	   ExpMark,}, // Abscissa
	{ 0,	    ExpSign,  Exponent,	 0,	   0,	   }, // ExpMark
	{ 0,	    0,	      Exponent,	 0,	   0,	   }, // ExpSign
	{ Done,	    Done,     Exponent,	 Done,	   Done	   }  // Exponent
    };

    int state = Init;				// parse state
    int input;					// input token

    char buf[256];
    int i = 0;
    TQChar c = eat_ws();

    for (;;) {

	switch ( c ) {
	    case '+':
	    case '-':
		input = InputSign;
		break;
	    case '0': case '1': case '2': case '3': case '4':
	    case '5': case '6': case '7': case '8': case '9':
		input = InputDigit;
		break;
	    case '.':
		input = InputDot;
		break;
	    case 'e':
	    case 'E':
		input = InputExp;
		break;
	    default:
		input = 0;
		break;
	}

	state = table[state][input];

	if  ( state == 0 || state == Done || i > 250 ) {
	    if ( i > 250 ) {			// ignore rest of digits
		do { c = ts_getc(); } while ( c != TQEOF && ts_isdigit(c) );
	    }
	    if ( c != TQEOF )
		ts_ungetc( c );
	    buf[i] = '\0';
	    char *end;
	    return strtod( buf, &end );
	}

	buf[i++] = c;
	c = ts_getc();
    }

#if !defined(TQ_CC_EDG)
    return 0.0;
#endif
}


/*!
    \overload

    Reads a signed \c short integer \a i from the stream and returns a
    reference to the stream. See flags() for an explanation of the
    expected input format.
*/

TQTextStream &TQTextStream::operator>>( signed short &i )
{
    CHECK_STREAM_PRECOND
    i = (signed short)input_int();
    return *this;
}


/*!
    \overload

    Reads an unsigned \c short integer \a i from the stream and
    returns a reference to the stream. See flags() for an explanation
    of the expected input format.
*/

TQTextStream &TQTextStream::operator>>( unsigned short &i )
{
    CHECK_STREAM_PRECOND
    i = (unsigned short)input_int();
    return *this;
}


/*!
    \overload

    Reads a signed \c int \a i from the stream and returns a reference
    to the stream. See flags() for an explanation of the expected
    input format.
*/

TQTextStream &TQTextStream::operator>>( signed int &i )
{
    CHECK_STREAM_PRECOND
    i = (signed int)input_int();
    return *this;
}


/*!
    \overload

    Reads an unsigned \c int \a i from the stream and returns a
    reference to the stream. See flags() for an explanation of the
    expected input format.
*/

TQTextStream &TQTextStream::operator>>( unsigned int &i )
{
    CHECK_STREAM_PRECOND
    i = (unsigned int)input_int();
    return *this;
}


/*!
    \overload

    Reads a signed \c long int \a i from the stream and returns a
    reference to the stream. See flags() for an explanation of the
    expected input format.
*/

TQTextStream &TQTextStream::operator>>( signed long &i )
{
    CHECK_STREAM_PRECOND
    i = (signed long)input_int();
    return *this;
}


/*!
    \overload

    Reads an unsigned \c long int \a i from the stream and returns a
    reference to the stream. See flags() for an explanation of the
    expected input format.
*/

TQTextStream &TQTextStream::operator>>( unsigned long &i )
{
    CHECK_STREAM_PRECOND
    i = (unsigned long)input_int();
    return *this;
}


/*!
    \overload

    Reads a \c float \a f from the stream and returns a reference to
    the stream. See flags() for an explanation of the expected input
    format.
*/

TQTextStream &TQTextStream::operator>>( float &f )
{
    CHECK_STREAM_PRECOND
    f = (float)input_double();
    return *this;
}


/*!
    \overload

    Reads a \c double \a f from the stream and returns a reference to
    the stream. See flags() for an explanation of the expected input
    format.
*/

TQTextStream &TQTextStream::operator>>( double &f )
{
    CHECK_STREAM_PRECOND
    f = input_double();
    return *this;
}


/*!
    \overload

    Reads a "word" from the stream into \a s and returns a reference
    to the stream.

    A word consists of characters for which isspace() returns FALSE.
*/

TQTextStream &TQTextStream::operator>>( char *s )
{
    CHECK_STREAM_PRECOND
    int maxlen = width( 0 );
    TQChar c = eat_ws();
    if ( !maxlen )
	maxlen = -1;
    while ( c != TQEOF ) {
	if ( ts_isspace(c) || maxlen-- == 0 ) {
	    ts_ungetc( c );
	    break;
	}
	*s++ = c;
	c = ts_getc();
    }

    *s = '\0';
    return *this;
}

/*!
    \overload

    Reads a "word" from the stream into \a str and returns a reference
    to the stream.

    A word consists of characters for which isspace() returns FALSE.
*/

TQTextStream &TQTextStream::operator>>( TQString &str )
{
    CHECK_STREAM_PRECOND
    str=TQString::tqfromLatin1("");
    TQChar	c = eat_ws();

    while ( c != TQEOF ) {
	if ( ts_isspace(c) ) {
	    ts_ungetc( c );
	    break;
	}
	str += c;
	c = ts_getc();
    }
    return *this;
}

/*!
    \overload

    Reads a "word" from the stream into \a str and returns a reference
    to the stream.

    A word consists of characters for which isspace() returns FALSE.
*/

TQTextStream &TQTextStream::operator>>( TQCString &str )
{
    CHECK_STREAM_PRECOND
    TQCString  *dynbuf = 0;
    const int buflen = 256;
    char      buffer[buflen];
    char     *s = buffer;
    int	      i = 0;
    TQChar	      c = eat_ws();

    while ( c != TQEOF ) {
	if ( ts_isspace(c) ) {
	    ts_ungetc( c );
	    break;
	}
	if ( i >= buflen-1 ) {
	    if ( !dynbuf )  {			// create dynamic buffer
		dynbuf = new TQCString(buflen*2);
		memcpy( dynbuf->data(), s, i );	// copy old data
	    } else if ( i >= (int)dynbuf->size()-1 ) {
		dynbuf->resize( dynbuf->size()*2 );
	    }
	    s = dynbuf->data();
	}
	s[i++] = c;
	c = ts_getc();
    }
    str.resize( i+1 );
    memcpy( str.data(), s, i );
    delete dynbuf;
    return *this;
}


/*!
    Reads a line from the stream and returns a string containing the
    text.

    The returned string does not contain any trailing newline or
    carriage return. Note that this is different from
    TQIODevice::readLine(), which does not strip the newline at the end
    of the line.

    On EOF you will get a TQString that is null. On reading an empty
    line the returned TQString is empty but not null.

    \sa TQIODevice::readLine()
*/

TQString TQTextStream::readLine()
{
#if defined(TQT_CHECK_STATE)
    if ( !dev ) {
	qWarning( "TQTextStream::readLine: No tqdevice" );
	return TQString::null;
    }
#endif
    bool readCharByChar = TRUE;
    TQString result;
#if 0
    if ( !doUnicodeHeader && (
	    (latin1) ||
	    (mapper != 0 && mapper->mibEnum() == 106 ) // UTF 8
       ) ) {
	readCharByChar = FALSE;
	// use optimized read line
	TQChar c[getline_buf_size];
	int pos = 0;
	bool eof = FALSE;

	for (;;) {
	    pos = ts_getline( c );
	    if ( pos == 0 ) {
		// something went wrong; try fallback
		readCharByChar = TRUE;
		//dev->resetqStatus();
		break;
	    }
	    if ( c[pos-1] == TQEOF || c[pos-1] == '\n' ) {
		if ( pos>2 && c[pos-1]==TQEOF && c[pos-2]=='\n' ) {
		    result += TQString( c, pos-2 );
		} else if ( pos > 1 ) {
		    result += TQString( c, pos-1 );
		}
		if ( pos == 1 && c[pos-1] == TQEOF )
		    eof = TRUE;
		break;
	    } else {
		result += TQString( c, pos );
	    }
	}
	if ( eof && result.isEmpty() )
	    return TQString::null;
    }
#endif
    if ( readCharByChar ) {
	const int buf_size = 256;
	TQChar c[buf_size];
	int pos = 0;

	c[pos] = ts_getc();
	if ( c[pos] == TQEOF )
	    return TQString::null;

	while ( c[pos] != TQEOF && c[pos] != '\n' ) {
	    if ( c[pos] == '\r' ) { // ( handle mac and dos )
		TQChar nextc = ts_getc();
		if ( nextc != '\n' )
		    ts_ungetc( nextc );
		break;
	    }
	    pos++;
	    if ( pos >= buf_size ) {
		result += TQString( c, pos );
		pos = 0;
	    }
	    c[pos] = ts_getc();
	}
	result += TQString( c, pos );
    }

    return result;
}


/*!
    Reads the entire stream from the current position, and returns a string
    containing the text.

    \sa TQIODevice::readLine()
*/

TQString TQTextStream::read()
{
#if defined(TQT_CHECK_STATE)
    if ( !dev ) {
	qWarning( "TQTextStream::read: No tqdevice" );
	return TQString::null;
    }
#endif
    TQString    result;
    const uint bufsize = 512;
    TQChar      buf[bufsize];
    uint       i, num, start;
    bool       skipped_cr = FALSE;

    for (;;) {
	num = ts_getbuf(buf,bufsize);
	// convert dos (\r\n) and mac (\r) style eol to unix style (\n)
	start = 0;
	for ( i=0; i<num; i++ ) {
	    if ( buf[i] == '\r' ) {
		// Only skip single cr's preceding lf's
		if ( skipped_cr ) {
		    result += buf[i];
		    start++;
		} else {
		    result += TQString( &buf[start], i-start );
		    start = i+1;
		    skipped_cr = TRUE;
		}
	    } else {
		if ( skipped_cr ) {
		    if ( buf[i] != '\n' ) {
			// Should not have skipped it
			result += '\n';
		    }
		    skipped_cr = FALSE;
		}
	    }
	}
	if ( start < num )
	    result += TQString( &buf[start], i-start );
	if ( num != bufsize ) // if ( EOF )
	    break;
    }
    return result;
}



/*****************************************************************************
  TQTextStream write functions
 *****************************************************************************/

/*!
    Writes character \c char to the stream and returns a reference to
    the stream.

    The character \a c is assumed to be Latin1 encoded independent of
    the Encoding set for the TQTextStream.
*/
TQTextStream &TQTextStream::operator<<( TQChar c )
{
    CHECK_STREAM_PRECOND
    ts_putc( c );
    return *this;
}

/*!
    \overload

    Writes character \a c to the stream and returns a reference to the
    stream.
*/
TQTextStream &TQTextStream::operator<<( char c )
{
    CHECK_STREAM_PRECOND
    unsigned char uc = (unsigned char) c;
    ts_putc( uc );
    return *this;
}

TQTextStream &TQTextStream::output_int( int format, ulong n, bool neg )
{
    static const char hexdigits_lower[] = "0123456789abcdef";
    static const char hexdigits_upper[] = "0123456789ABCDEF";
    CHECK_STREAM_PRECOND
    char buf[76];
    register char *p;
    int	  len;
    const char *hexdigits;

    switch ( flags() & I_BASE_MASK ) {

	case I_BASE_2:				// output binary number
	    switch ( format & I_TYPE_MASK ) {
		case I_SHORT: len=16; break;
		case I_INT:   len=sizeof(int)*8; break;
		case I_LONG:  len=32; break;
		default:      len = 0;
	    }
	    p = &buf[74];			// go reverse order
	    *p = '\0';
	    while ( len-- ) {
		*--p = (char)(n&1) + '0';
		n >>= 1;
		if ( !n )
		    break;
	    }
	    if ( flags() & showbase ) {		// show base
		*--p = (flags() & uppercase) ? 'B' : 'b';
		*--p = '0';
	    }
	    break;

	case I_BASE_8:				// output octal number
	    p = &buf[74];
	    *p = '\0';
	    do {
		*--p = (char)(n&7) + '0';
		n >>= 3;
	    } while ( n );
	    if ( flags() & showbase )
		*--p = '0';
	    break;

	case I_BASE_16:				// output hexadecimal number
	    p = &buf[74];
	    *p = '\0';
	    hexdigits = (flags() & uppercase) ?
		hexdigits_upper : hexdigits_lower;
	    do {
		*--p = hexdigits[(int)n&0xf];
		n >>= 4;
	    } while ( n );
	    if ( flags() & showbase ) {
		*--p = (flags() & uppercase) ? 'X' : 'x';
		*--p = '0';
	    }
	    break;

	default:				// decimal base is default
	    p = &buf[74];
	    *p = '\0';
	    if ( neg )
		n = (ulong)(-(long)n);
	    do {
		*--p = ((int)(n%10)) + '0';
		n /= 10;
	    } while ( n );
	    if ( neg )
		*--p = '-';
	    else if ( flags() & showpos )
		*--p = '+';
	    if ( (flags() & internal) && fwidth && !ts_isdigit(*p) ) {
		ts_putc( *p );			// special case for internal
		++p;				//   padding
		fwidth--;
		return *this << (const char*)p;
	    }
    }
    if ( fwidth ) {				// adjustment required
	if ( !(flags() & left) ) {		// but NOT left adjustment
	    len = tqstrlen(p);
	    int padlen = fwidth - len;
	    if ( padlen <= 0 ) {		// no padding required
		writeBlock( p, len );
	    } else if ( padlen < (int)(p-buf) ) { // speeds up padding
		memset( p-padlen, (char)fillchar, padlen );
		writeBlock( p-padlen, padlen+len );
	    }
	    else				// standard padding
		*this << (const char*)p;
	}
	else
	    *this << (const char*)p;
	fwidth = 0;				// reset field width
    }
    else
	writeBlock( p, tqstrlen(p) );
    return *this;
}


/*!
    \overload

    Writes a \c short integer \a i to the stream and returns a
    reference to the stream.
*/

TQTextStream &TQTextStream::operator<<( signed short i )
{
    return output_int( I_SHORT | I_SIGNED, i, i < 0 );
}


/*!
    \overload

    Writes an \c unsigned \c short integer \a i to the stream and
    returns a reference to the stream.
*/

TQTextStream &TQTextStream::operator<<( unsigned short i )
{
    return output_int( I_SHORT | I_UNSIGNED, i, FALSE );
}


/*!
    \overload

    Writes an \c int \a i to the stream and returns a reference to the
    stream.
*/

TQTextStream &TQTextStream::operator<<( signed int i )
{
    return output_int( I_INT | I_SIGNED, i, i < 0 );
}


/*!
    \overload

    Writes an \c unsigned \c int \a i to the stream and returns a
    reference to the stream.
*/

TQTextStream &TQTextStream::operator<<( unsigned int i )
{
    return output_int( I_INT | I_UNSIGNED, i, FALSE );
}


/*!
    \overload

    Writes a \c long \c int \a i to the stream and returns a reference
    to the stream.
*/

TQTextStream &TQTextStream::operator<<( signed long i )
{
    return output_int( I_LONG | I_SIGNED, i, i < 0 );
}


/*!
    \overload

    Writes an \c unsigned \c long \c int \a i to the stream and
    returns a reference to the stream.
*/

TQTextStream &TQTextStream::operator<<( unsigned long i )
{
    return output_int( I_LONG | I_UNSIGNED, i, FALSE );
}


/*!
    \overload

    Writes a \c float \a f to the stream and returns a reference to
    the stream.
*/

TQTextStream &TQTextStream::operator<<( float f )
{
    return *this << (double)f;
}

/*!
    \overload

    Writes a \c double \a f to the stream and returns a reference to
    the stream.
*/

TQTextStream &TQTextStream::operator<<( double f )
{
    CHECK_STREAM_PRECOND
    char f_char;
    char format[16];
    if ( (flags()&floatfield) == fixed )
	f_char = 'f';
    else if ( (flags()&floatfield) == scientific )
	f_char = (flags() & uppercase) ? 'E' : 'e';
    else
	f_char = (flags() & uppercase) ? 'G' : 'g';
    register char *fs = format;			// generate format string
    *fs++ = '%';				//   "%.<prec>l<f_char>"
    *fs++ = '.';
    int prec = precision();
    if ( prec > 99 )
	prec = 99;
    if ( prec >= 10 ) {
	*fs++ = prec / 10 + '0';
	*fs++ = prec % 10 + '0';
    } else {
	*fs++ = prec + '0';
    }
    *fs++ = 'l';
    *fs++ = f_char;
    *fs = '\0';
    TQString num;
    num.sprintf(format, f);			// convert to text
    if ( fwidth )				// padding
	*this << num.latin1();
    else					// just write it
	writeBlock(num.latin1(), num.length());
    return *this;
}


/*!
    \overload

    Writes a string to the stream and returns a reference to the
    stream.

    The string \a s is assumed to be Latin1 encoded independent of the
    Encoding set for the TQTextStream.
*/

TQTextStream &TQTextStream::operator<<( const char* s )
{
    CHECK_STREAM_PRECOND
    char padbuf[48];
    uint len = tqstrlen( s );			// don't write null terminator
    if ( fwidth ) {				// field width set
	int padlen = fwidth - len;
	fwidth = 0;				// reset width
	if ( padlen > 0 ) {
	    char *ppad;
	    if ( padlen > 46 ) {		// create extra big fill buffer
		ppad = new char[padlen];
		TQ_CHECK_PTR( ppad );
	    } else {
		ppad = padbuf;
	    }
	    memset( ppad, (char)fillchar, padlen );	// fill with fillchar
	    if ( !(flags() & left) ) {
		writeBlock( ppad, padlen );
		padlen = 0;
	    }
	    writeBlock( s, len );
	    if ( padlen )
		writeBlock( ppad, padlen );
	    if ( ppad != padbuf )		// delete extra big fill buf
		delete[] ppad;
	    return *this;
	}
    }
    writeBlock( s, len );
    return *this;
}

/*!
    \overload

    Writes \a s to the stream and returns a reference to the stream.

    The string \a s is assumed to be Latin1 encoded independent of the
    Encoding set for the TQTextStream.
*/

TQTextStream &TQTextStream::operator<<( const TQCString & s )
{
    return operator<<(s.data());
}

/*!
    \overload

    Writes \a s to the stream and returns a reference to the stream.
*/

TQTextStream &TQTextStream::operator<<( const TQString& s )
{
    if ( !mapper && latin1 )
	return operator<<(s.latin1());
    CHECK_STREAM_PRECOND
    TQString s1 = s;
    if ( fwidth ) {				// field width set
	if ( !(flags() & left) ) {
	    s1 = s.rightJustify(fwidth, (char)fillchar);
	} else {
	    s1 = s.leftJustify(fwidth, (char)fillchar);
	}
	fwidth = 0;				// reset width
    }
    writeBlock( s1.tqunicode(), s1.length() );
    return *this;
}


/*!
    \overload

    Writes a pointer to the stream and returns a reference to the
    stream.

    The \a ptr is output as an unsigned long hexadecimal integer.
*/

TQTextStream &TQTextStream::operator<<( void *ptr )
{
    int f = flags();
    setf( hex, basefield );
    setf( showbase );
    unsetf( uppercase );
    output_int( I_LONG | I_UNSIGNED, (ulong)ptr, FALSE );
    flags( f );
    return *this;
}


/*!
    \fn int TQTextStream::flags() const

    Returns the current stream flags. The default value is 0.

    \table
    \header \i Flag \i Meaning
    \row \i \c skipws \i Not currently used; whitespace always skipped
    \row \i \c left \i Numeric fields are left-aligned
    \row \i \c right
	 \i Not currently used (by default, numerics are right-aligned)
    \row \i \c internal \i Puts any padding spaces between +/- and value
    \row \i \c bin \i Output \e and input only in binary
    \row \i \c oct \i Output \e and input only in octal
    \row \i \c dec \i Output \e and input only in decimal
    \row \i \c hex \i Output \e and input only in hexadecimal
    \row \i \c showbase
	 \i Annotates numeric outputs with 0b, 0, or 0x if in \c bin,
	 \c oct, or \c hex format
    \row \i \c showpoint \i Not currently used
    \row \i \c uppercase \i Uses 0B and 0X rather than 0b and 0x
    \row \i \c showpos \i Shows + for positive numeric values
    \row \i \c scientific \i Uses scientific notation for floating point values
    \row \i \c fixed \i Uses fixed-point notation for floating point values
    \endtable

    Note that unless \c bin, \c oct, \c dec, or \c hex is set, the
    input base is octal if the value starts with 0, hexadecimal if it
    starts with 0x, binary if it starts with 0b, and decimal
    otherwise.

    \sa setf(), unsetf()
*/

/*!
    \fn int TQTextStream::flags( int f )

    \overload

    Sets the stream flags to \a f. Returns the previous stream flags.

    \sa setf(), unsetf(), flags()
*/

/*!
    \fn int TQTextStream::setf( int bits )

    Sets the stream flag bits \a bits. Returns the previous stream
    flags.

    Equivalent to \c{flags( flags() | bits )}.

    \sa setf(), unsetf()
*/

/*!
    \fn int TQTextStream::setf( int bits, int tqmask )

    \overload

    Sets the stream flag bits \a bits with a bit tqmask \a tqmask. Returns
    the previous stream flags.

    Equivalent to \c{flags( (flags() & ~tqmask) | (bits & tqmask) )}.

    \sa setf(), unsetf()
*/

/*!
    \fn int TQTextStream::unsetf( int bits )

    Clears the stream flag bits \a bits. Returns the previous stream
    flags.

    Equivalent to \c{flags( flags() & ~tqmask )}.

    \sa setf()
*/

/*!
    \fn int TQTextStream::width() const

    Returns the field width. The default value is 0.
*/

/*!
    \fn int TQTextStream::width( int w )

    \overload

    Sets the field width to \a w. Returns the previous field width.
*/

/*!
    \fn int TQTextStream::fill() const

    Returns the fill character. The default value is ' ' (space).
*/

/*!
    \overload int TQTextStream::fill( int f )

    Sets the fill character to \a f. Returns the previous fill character.
*/

/*!
    \fn int TQTextStream::precision() const

    Returns the precision. The default value is 6.
*/

/*!
    \fn int TQTextStream::precision( int p )

    \overload

    Sets the precision to \a p. Returns the previous precision setting.
*/


 /*****************************************************************************
  TQTextStream manipulators
 *****************************************************************************/

TQTextStream &bin( TQTextStream &s )
{
    s.setf(TQTS::bin,TQTS::basefield);
    return s;
}

TQTextStream &oct( TQTextStream &s )
{
    s.setf(TQTS::oct,TQTS::basefield);
    return s;
}

TQTextStream &dec( TQTextStream &s )
{
    s.setf(TQTS::dec,TQTS::basefield);
    return s;
}

TQTextStream &hex( TQTextStream &s )
{
    s.setf(TQTS::hex,TQTS::basefield);
    return s;
}

TQTextStream &endl( TQTextStream &s )
{
    return s << '\n';
}

TQTextStream &flush( TQTextStream &s )
{
    if ( s.tqdevice() )
	s.tqdevice()->flush();
    return s;
}

TQTextStream &ws( TQTextStream &s )
{
    s.skipWhiteSpace();
    return s;
}

TQTextStream &reset( TQTextStream &s )
{
    s.reset();
    return s;
}


/*!
    \class TQTextIStream tqtextstream.h
    \reentrant
    \brief The TQTextIStream class is a convenience class for input streams.

    \ingroup io
    \ingroup text

    This class provides a shorthand for creating simple input
    \l{TQTextStream}s without having to pass a \e mode argument to the
    constructor.

    This class makes it easy, for example, to write things like this:
    \code
    TQString data = "123 456";
    int a, b;
    TQTextIStream(&data) >> a >> b;
    \endcode

    \sa TQTextOStream
*/

/*!
    \fn TQTextIStream::TQTextIStream( const TQString *s )

    Constructs a stream to read from the string \a s.
*/
/*!
    \fn TQTextIStream::TQTextIStream( TQByteArray ba )

    Constructs a stream to read from the array \a ba.
*/
/*!
    \fn TQTextIStream::TQTextIStream( FILE *f )

    Constructs a stream to read from the file \a f.
*/


/*!
    \class TQTextOStream
    \reentrant
    \brief The TQTextOStream class is a convenience class for output streams.

    \ingroup io
    \ingroup text

    This class provides a shorthand for creating simple output
    \l{TQTextStream}s without having to pass a \e mode argument to the
    constructor.

    This makes it easy for example, to write things like this:
    \code
    TQString result;
    TQTextOStream(&result) << "pi = " << 3.14;
    \endcode
*/

/*!
    \fn TQTextOStream::TQTextOStream( TQString *s )

    Constructs a stream to write to string \a s.
*/
/*!
    \fn TQTextOStream::TQTextOStream( TQByteArray ba )

    Constructs a stream to write to the array \a ba.
*/
/*!
    \fn TQTextOStream::TQTextOStream( FILE *f )

    Constructs a stream to write to the file \a f.
*/



/*!
  Sets the encoding of this stream to \a e, where \a e is one of the
  following values:
  \table
  \header \i Encoding \i Meaning
  \row \i Locale
       \i Uses local file format (Latin1 if locale is not set), but
       autodetecting Unicode(utf16) on input.
  \row \i Unicode
       \i Uses Unicode(utf16) for input and output. Output will be
       written in the order most efficient for the current platform
       (i.e. the order used internally in TQString).
  \row \i UnicodeUTF8
       \i Using Unicode(utf8) for input and output. If you use it for
       input it will autodetect utf16 and use it instead of utf8.
  \row \i Latin1
       \i ISO-8859-1. Will not autodetect utf16.
  \row \i UnicodeNetworkOrder
       \i Uses network order Unicode(utf16) for input and output.
       Useful when reading Unicode data that does not start with the
       byte order marker.
  \row \i UnicodeReverse
       \i Uses reverse network order Unicode(utf16) for input and
       output. Useful when reading Unicode data that does not start
       with the byte order marker or when writing data that should be
       read by buggy Windows applications.
  \row \i RawUnicode
       \i Like Unicode, but does not write the byte order marker nor
       does it auto-detect the byte order. Useful only when writing to
       non-persistent storage used by a single process.
  \endtable

  \c Locale and all Unicode encodings, except \c RawUnicode, will look
  at the first two bytes in an input stream to determine the byte
  order. The initial byte order marker will be stripped off before
  data is read.

  Note that this function should be called before any data is read to
  or written from the stream.

  \sa setCodec()
*/

void TQTextStream::setEncoding( Encoding e )
{
    if ( d->sourceType == TQTextStreamPrivate::String )
	return;

    switch ( e ) {
    case Unicode:
	mapper = 0;
	latin1 = FALSE;
	doUnicodeHeader = TRUE;
	internalOrder = TRUE;
	networkOrder = TQChar::networkOrdered();
	break;
    case UnicodeUTF8:
#ifndef TQT_NO_TEXTCODEC
	mapper = TQTextCodec::codecForMib( 106 );
	latin1 = FALSE;
	doUnicodeHeader = TRUE;
	internalOrder = TRUE;
	networkOrder = TQChar::networkOrdered();
#else
	mapper = 0;
	latin1 = TRUE;
	doUnicodeHeader = TRUE;
#endif
	break;
    case UnicodeNetworkOrder:
	mapper = 0;
	latin1 = FALSE;
	doUnicodeHeader = TRUE;
	internalOrder = TQChar::networkOrdered();
	networkOrder = TRUE;
	break;
    case UnicodeReverse:
	mapper = 0;
	latin1 = FALSE;
	doUnicodeHeader = TRUE;
	internalOrder = !TQChar::networkOrdered();
	networkOrder = FALSE;
	break;
    case RawUnicode:
	mapper = 0;
	latin1 = FALSE;
	doUnicodeHeader = FALSE;
	internalOrder = TRUE;
	networkOrder = TQChar::networkOrdered();
	break;
    case Locale:
	latin1 = TRUE; // fallback to Latin-1
#ifndef TQT_NO_TEXTCODEC
	mapper = TQTextCodec::codecForLocale();
	// optimized Latin-1 processing
#if defined(TQ_OS_WIN32)
	if ( GetACP() == 1252 )
	    mapper = 0;
#endif
	if ( mapper && mapper->mibEnum() == 4 )
#endif
	    mapper = 0;

	doUnicodeHeader = TRUE; // If it reads as Unicode, accept it
	break;
    case Latin1:
	mapper = 0;
	doUnicodeHeader = FALSE;
	latin1 = TRUE;
	break;
    }
}


#ifndef TQT_NO_TEXTCODEC
/*!
    Sets the codec for this stream to \a codec. Will not try to
    autodetect Unicode.

    Note that this function should be called before any data is read
    to/written from the stream.

    \sa setEncoding(), codec()
*/

void TQTextStream::setCodec( TQTextCodec *codec )
{
    if ( d->sourceType == TQTextStreamPrivate::String )
	return; // TQString does not need any codec
    mapper = codec;
    latin1 = ( codec->mibEnum() == 4 );
    if ( latin1 )
	mapper = 0;
    doUnicodeHeader = FALSE;
}

/*!
  Returns the codec actually used for this stream.

  If Unicode is automatically detected in input, a codec with \link
  TQTextCodec::name() name() \endlink "ISO-10646-UCS-2" is returned.

  \sa setCodec()
*/

TQTextCodec *TQTextStream::codec()
{
    if ( mapper ) {
	return mapper;
    } else {
	// 4 is "ISO 8859-1", 1000 is "ISO-10646-UCS-2"
	return TQTextCodec::codecForMib( latin1 ? 4 : 1000 );
    }
}

#endif

#endif // TQT_NO_TEXTSTREAM