File: ipstrms.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (1792 lines) | stat: -rw-r--r-- 51,580 bytes parent folder | download | duplicates (2)
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
{******************************************************************}
{*     IPSTRMS.PAS - Various stream classes                       *}
{******************************************************************}

{ $Id: ipstrms.pas 56391 2017-11-13 17:33:30Z juha $ }

(* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is TurboPower Internet Professional
 *
 * The Initial Developer of the Original Code is
 * TurboPower Software
 *
 * Portions created by the Initial Developer are Copyright (C) 2000-2002
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Markus Kaemmerer <mk@happyarts.de> SourceForge: mkaemmerer
 *
 * ***** END LICENSE BLOCK ***** *)

{ Global defines potentially affecting this unit }

{$I IPDEFINE.INC}

unit IpStrms;
  {- Ansi text stream class}

interface

uses
  SysUtils, Classes,
  {$IFDEF IP_LAZARUS}
  // LCL
  LCLType,
  // LazUtils
  FPCAdds, LazFileUtils, IntegerList,
  {$ELSE}
  Windows, // put Windows behind Classes because of THandle
  {$ENDIF}
  IpUtils,
  IpConst;

const
  IpFileOpenFailed = THandle(-1);

{ TIpMemMapStream  }
type
  TIpMemMapStream = class(TStream)
  protected
    FCanGrow : Boolean;
      { If True then the map file can grow if the user writes past the
        current end of the stream. Note that growing may be expensive
        time-wise. }
    FDataSize : Longint;
      { The amount of data actually written to the stream. }
    FGrowthFactor : Double;
      { The factor by which the map file is to be grow each time a size
        increase is needed. }
    FReadOnly : Boolean;
      { If set to True then file is to be opened for read-only access. }
    FSize : Longint;
      { The current size of the mapped file. When creating files, the size
        must be pre-set. The size is fixed unless the CanGrow property is
        set to True. }
    mmFileExists : Boolean;
      { Set to True if the file existed when the open method was called. }
    mmFileHandle : THandle;
    mmFileIsTemp : Boolean;
      { If set to True then file was created by this stream. }
    mmFileName : string;
    mmMapHandle : THandle;
    mmPointer : Pointer;
      { Pointer to the beginning of the file. }
    mmPos : Longint;
      { Current position in the file. }

    { Verification methods }
    procedure CheckClosed(const aMethodName : string);
    procedure CheckFileName;

    procedure CloseFile;
    procedure CloseMap;

    procedure OpenFile;
    procedure OpenMap;

    procedure Resize(const NewSize : Longint);
    procedure SetSize(NewSize : Longint); override;
  public
    constructor Create(const FileName : string;
                       const ReadOnly, Temporary : Boolean);
    destructor Destroy; override;
    procedure Open;
      { After the stream has been created, call this method to open the file. }
    function Read(var Buffer; Count: Longint): Longint; override;
    function Write(const Buffer; Count: Longint): Longint; override;
    function Seek(Offset: Longint; Origin: Word): Longint; override;

    property ReadOnly : Boolean read FReadOnly;
      { Returns True if the file is being opened in read-only mode. }

    property CanGrow : Boolean read FCanGrow write FCanGrow;
      { If True then the mapped stream can grow in size when data is written
        past the current end of the stream. Note this involves closing &
        reopening the map which may be expensive time-wise.
        Defaults to True. }

    property DataSize : Longint read FDataSize;
      { The amount of data actually written to the stream. It is calculated
        based upon the highest position to which data was written.
        For example, if an app seeks to position 100 and writes 100 bytes
        of data then the data size is 201. }

    property GrowthFactor : Double read FGrowthFactor write FGrowthFactor;
      { The factor by which the stream will be grown in size if CanGrow is
        True and data is written past the current end of stream.
        Defaults to 0.25. }

    property Memory: Pointer read mmPointer;
      { Points to the memory associated with the file. }

    property Size : Longint read FSize write SetSize;
      { For temporary files, specify the maximum size of the file via this
        property. }
  end;

{ TIpBufferedStream }
type
  TIpBufferedStream = class(TStream)
    private {- property variables }
      FBufCount: Longint;
      FBuffer  : PAnsiChar;
      FBufOfs  : Longint;
      FBufPos  : Longint;
      FBufSize : Longint;
      FDirty   : Boolean;
      FSize    : {$IFDEF IP_LAZARUS}TStreamSeekType{$ELSE}longint{$ENDIF};
      FStream  : TStream;

    protected {- methods }
      procedure bsInitForNewStream; virtual;
      procedure bsReadFromStream;
      procedure bsSetStream(aValue : TStream);
      procedure bsWriteToStream;

    public {- methods }
      constructor Create(aStream : TStream);
      constructor CreateEmpty;
      destructor  Destroy; override;

      procedure Flush;                                                 {!!.12}
        { Flush any unwritten changes to the stream. }
      procedure FreeStream;
      function ReadChar(var aCh : AnsiChar) : Boolean;
      function Read(var Buffer; Count : Longint) : Longint; override;
      function Seek(Offset : Longint; Origin : word) : Longint; override;
      function Write(const Buffer; Count : Longint) : Longint; override;

    public {-properties }
      property FastSize: {$IFDEF IP_LAZARUS}TStreamSeekType{$ELSE}longint{$ENDIF}
        read FSize;
      property Stream : TStream
        read FStream write bsSetStream;
   end;


{ TIpAnsiTextStream }
type
  TIpAnsiTextStream = class(TIpBufferedStream)
    private {- property variables }
      FLineEndCh   : AnsiChar;
      FLineLen     : Integer;
      FLineTerm    : TIpLineTerminator;
      FFixedLine   : PAnsiChar;
      FLineCount   : Longint;
      FLineCurrent : Longint;
      FLineCurOfs  : Longint;
      FLineIndex   : TIntegerList;
      FLineInxStep : Longint;
      FLineInxTop  : Integer;

    protected {- methods }
      procedure atsGetLine(var aStartPos, aEndPos, aLen : Longint);
      function  atsGetLineCount : Longint;
      procedure atsResetLineIndex;
      procedure atsSetLineTerm(aValue : TIpLineTerminator);
      procedure atsSetLineEndCh(aValue : AnsiChar);
      procedure atsSetLineLen(aValue : Integer);

    public {- properties }
      property FixedLineLength : Integer
        read FLineLen write atsSetLineLen;
      property LineCount : Longint
        read atsGetLineCount;
      property LineTermChar : AnsiChar
        read FLineEndCh write atsSetLineEndCh;
      property LineTerminator : TIpLineTerminator
        read FLineTerm write atsSetLineTerm;

    public {- methods }
      constructor Create(aStream : TStream);
      destructor Destroy; override;

      function  AtEndOfStream : Boolean;
      procedure bsInitForNewStream; override;                        {!!.01}
      function  ReadLine : string;
      function  ReadLineArray(aCharArray : PAnsiChar; aLen : Longint) : Longint;
      function  ReadLineZ(aSt : PAnsiChar; aMaxLen : Longint) : PAnsiChar;
      function  SeekNearestLine(aOffset : Longint) : Longint;
      function  SeekLine(aLineNum : Longint) : Longint;
      procedure WriteLine(const aSt : string);
      procedure WriteLineArray(aCharArray : PAnsiChar; aLen : Longint);
      procedure WriteLineZ(aSt : PAnsiChar);
  end;

{ TIpDownloadFileStream }
type
  TIpDownloadFileStream = class(TStream)
    private
      FHandle   : THandle;
      FPath     : string;
      FFileName : string;
      FRenamed  : boolean;
    protected
      procedure dfsMakeTempFile(const aPath : string);         
    public
      constructor Create(const aPath : string);
      destructor Destroy; override;

      function Read(var Buffer; Count : Longint) : Longint; override;
      procedure Rename(aNewName : string);
      procedure Move(aNewName: string);
      function Seek(Offset : Longint; Origin : Word) : Longint; override;
      function Write(const Buffer; Count : Longint) : Longint; override;

      property Handle : THandle read FHandle;
      property FileName : string read FFileName;
  end;


{ TIpByteStream }
type
  TIpByteStream = class
    private {variables}
      FStream  : TStream;
      BufEnd   : Integer;
      BufPos   : Integer;
      Buffer   : array[0..1023] of Byte;
    protected {methods}
      function GetPosition : Integer;
      function GetSize : Integer;
    public {methods}
      constructor Create(aStream : TStream);
      destructor Destroy; override;
      function Read(var b :Byte) : Boolean;
    public {properties}
      property Position : Integer
        read GetPosition;
      property Size : longint
        read GetSize;
  end;




implementation

const
  LineTerm : array [TIpLineTerminator] of
               array [0..1] of AnsiChar =
                 ('', #13, #10, #13#10, '');

const
  LineIndexCount = 1024;
  LineIndexMax   = pred(LineIndexCount);


{--- Helper routines ---------------------------------------------------------}

function MinLong(A, B : Longint) : Longint;
begin
  if A < B then
    Result := A
  else
    Result := B;
end;

{-----------------------------------------------------------------------------}
{                          TIpMemMapStream                                    }
{-----------------------------------------------------------------------------}

constructor TIpMemMapStream.Create(const FileName : string;
                                   const ReadOnly, Temporary : Boolean);
begin
  inherited Create;

  FCanGrow := True;
  FDataSize := 0;
  FGrowthFactor := 0.25;
  FReadOnly := ReadOnly;
  FSize := 64 * 1024;
  mmFileName := FileName;
  mmFileIsTemp := Temporary;
end;

{-----------------------------------------------------------------------------}

destructor TIpMemMapStream.Destroy;
begin
  CloseMap;
  CloseFile;
  
  { If map file was temporary then get rid of it. }
  if mmFileIsTemp and FileExistsUTF8(mmFileName) then
    DeleteFileUTF8(mmFileName);

  inherited;
end;

{-----------------------------------------------------------------------------}

procedure TIpMemMapStream.CheckClosed(const aMethodName : string);
begin
  if mmFileHandle <> 0 then
    raise EIpBaseException.CreateFmt(SMemMapMustBeClosed, [aMethodName]);
end;

{-----------------------------------------------------------------------------}

procedure TIpMemMapStream.CheckFileName;
begin
  if mmFileName = '' then
    raise EIpBaseException.Create(SMemMapFilenameRequired);
end;

{-----------------------------------------------------------------------------}

procedure TIpMemMapStream.CloseFile;
begin
  {$IFDEF IP_LAZARUS}
  writeln('TIpMemMapStream.CloseFile ToDo');
  {$ELSE}
  if mmFileHandle <> 0 then
    CloseHandle(mmFileHandle);
  {$ENDIF}
end;

{-----------------------------------------------------------------------------}

procedure TIpMemMapStream.CloseMap;
begin
  {$IFDEF IP_LAZARUS}
  writeln('TIpMemMapStream.CloseMap ToDo');
  {$ELSE}
  FlushViewOfFile(mmPointer, 0);
  UnMapViewOfFile(mmPointer);
  if mmMapHandle <> 0 then
    CloseHandle(mmMapHandle);
  {$ENDIF}
end;

{-----------------------------------------------------------------------------}

procedure TIpMemMapStream.Open;
begin
  OpenFile;
  OpenMap;
end;

{-----------------------------------------------------------------------------}

procedure TIpMemMapStream.OpenFile;
{$IFDEF IP_LAZARUS}
begin
  writeln('TIpMemMapStream.OpenFile ToDo');
end;
{$ELSE}
var
  CreateMode,
  Flags,
  OpenMode : DWORD;
begin

  { Check requirements. }
  CheckFileName;
  CheckClosed('Open');

  { Are we opening an existing file or creating a new file? }
  if not FileExistsUTF8(mmFileName) then
    CreateMode:= CREATE_ALWAYS
  else
    CreateMode := OPEN_EXISTING;

  OpenMode := GENERIC_READ;
  if FReadOnly then
    Flags := FILE_ATTRIBUTE_NORMAL or FILE_FLAG_SEQUENTIAL_SCAN
  else begin
    OpenMode := OpenMode or GENERIC_WRITE;
    Flags := FILE_ATTRIBUTE_NORMAL or FILE_FLAG_RANDOM_ACCESS;
  end;

  mmFileExists := (CreateMode = OPEN_EXISTING);

  mmFileHandle := CreateFile(PChar(mmFileName),
                             OpenMode,
                             0,  { exclusive }
                             nil,
                             CreateMode,
                             Flags,
                             0);

  if mmFileHandle = INVALID_HANDLE_VALUE then
    { Raise exception. }
    raise EIpBaseException.Create(SysErrorMessage(GetLastError) + SFilename +
                                  mmFileName);
end;
{$ENDIF}

{-----------------------------------------------------------------------------}

procedure TIpMemMapStream.OpenMap;
{$IFDEF IP_LAZARUS}
begin
  writeln('TIpMemMapStream.OpenMap ToDo');
end;
{$ELSE}
var
  AccessMode,
  ProtectMode,
  SizeHigh : DWORD;
  Size : DWORD;
begin
  { If this was an existing file then get the size of the file. }
  if mmFileExists then begin
    SizeHigh := 0;
    Size := GetFileSize(mmFileHandle, @SizeHigh);
    FSize := Size;
    FDataSize := Size;
    if Size = $FFFFFFFF then
      { Raise exception. }
      raise EIpBaseException.Create(SysErrorMessage(GetLastError) + SFilename +
                                    mmFileName);
  end
  else
    Size := FSize;

  { Read-only? }
  if FReadOnly then begin
    AccessMode := FILE_MAP_READ;
    ProtectMode := PAGE_READONLY;
  end
  else begin
    AccessMode := FILE_MAP_ALL_ACCESS;
    ProtectMode := PAGE_READWRITE;
  end;

  mmMapHandle := CreateFileMapping(mmFileHandle, nil, ProtectMode,
                                   0, Size, nil);
  if mmMapHandle = 0 then
    { Raise exception. }
    raise EIpBaseException.Create(SysErrorMessage(GetLastError) + SFilename +
                                  mmFileName);

  mmPointer := MapViewOfFile(mmMapHandle, AccessMode, 0, 0, Size);
  if mmPointer = nil then
    raise EIpBaseException.Create(SysErrorMessage(GetLastError) + SFilename +
                                  mmFileName);
  mmPos := 0;
end;
{$ENDIF}

{-----------------------------------------------------------------------------}

procedure TIpMemMapStream.Resize(const NewSize : Longint);
var
  SavPos : Longint;
begin
  { Close the map. }
  if NewSize < FSize then
    SavPos := 0
  else
    SavPos := mmPos;
  CloseMap;

  {$IFDEF IP_LAZARUS}
  writeln('TIpMemMapStream.Resize ToDo');
  {$ELSE}
  { Update the size of the file. }
  if SetFilePointer(mmFileHandle, NewSize, nil, FILE_BEGIN) <> $FFFFFFFF then begin
    if SetEndOfFile(mmFileHandle) = false then
      raise EIpBaseException.Create(SysErrorMessage(GetLastError) + SFilename +
                                    mmFileName);
  end
  else
    raise EIpBaseException.Create(SysErrorMessage(GetLastError) + SFilename +
                                  mmFileName);
  {$ENDIF}

  { Update internal size information. }
  FSize := NewSize;
  if FSize < FDataSize then
    FDataSize := FSize;

  { Re-open the map. }
  mmFileExists := True;
  OpenMap;
  mmPos := SavPos;
end;

{-----------------------------------------------------------------------------}

procedure TIpMemMapStream.SetSize(NewSize : Longint);
begin
  if mmFileHandle <> 0 then
    Resize(NewSize);
  FSize := NewSize;
end;

{-----------------------------------------------------------------------------}

function TIpMemMapStream.Read(var Buffer; Count: Longint): Longint;
begin
  if mmFileHandle = 0 then
    raise EIpBaseException.CreateFmt(SMemMapMustBeOpen, ['Read']);
  if (mmPos + Count) > FDataSize then
    Result := FDataSize - mmPos
  else
    Result := Count;
  Move(PByteArray(mmPointer)[mmPos], Buffer, Result);
  inc(mmPos, Result);
end;

{-----------------------------------------------------------------------------}

function TIpMemMapStream.Write(const Buffer; Count: Longint): Longint;
var
  NewSize : Longint;
begin
  if mmFileHandle = 0 then
    raise EIpBaseException.CreateFmt(SMemMapMustBeOpen, ['Write']);
  if not FReadOnly then begin
    if (mmPos + Count) > FSize then begin
      if FCanGrow then begin
        { Grow the stream. }
        NewSize := FSize + Trunc(FSize * FGrowthFactor);
        if NewSize < FSize + Count then
          NewSize := FSize + Count;
        Resize(NewSize);
        Result := Count;
      end
      else
        Result := FSize - mmPos;
    end
    else
      Result := Count;

    Move(Buffer, PByteArray(mmPointer)[mmPos], Result);
    inc(mmPos, Result);
    if mmPos > FDataSize then
      FDataSize := mmPos + 1;
  end
  else
    Result := 0;
end;

{-----------------------------------------------------------------------------}

function TIpMemMapStream.Seek(Offset: Longint; Origin: Word): Longint;
begin
  if mmFileHandle = 0 then
    raise EIpBaseException.CreateFmt(SMemMapMustBeOpen, ['Seek']);
  case Origin of
    soFromBeginning :
      if Offset < 0 then
        raise EIpBaseException.Create(SOriginFromBegin)
      else
        mmPos := Offset;

    soFromCurrent   :
      mmPos := mmPos + Offset;

    soFromEnd       :
      if Offset > 0 then
        raise EIpBaseException.Create(SOriginFromEnd)
      else
        mmPos := FSize + Offset;
  end;  { case }
  Result := mmPos;
end;

{-----------------------------------------------------------------------------}
{                          TIpBufferedStream                                  }
{-----------------------------------------------------------------------------}

const
  BufferSize = 16384; // higher values for more speed but more memory

constructor TIpBufferedStream.Create(aStream : TStream);
begin
  inherited Create;

  {allocate the buffer}
  FBufSize := BufferSize;

  GetMem(FBuffer, FBufSize);

  {save the stream}
  if (aStream = nil) then
    raise EIpBaseException.Create(SNoStreamErr);
  FStream := aStream;

  bsInitForNewStream;
end;

{-----------------------------------------------------------------------------}

constructor TIpBufferedStream.CreateEmpty;
begin
  inherited Create;

  {allocate the buffer}
  FBufSize := BufferSize;
  GetMem(FBuffer, FBufSize);
  bsInitForNewStream
end;

{-----------------------------------------------------------------------------}

destructor TIpBufferedStream.Destroy;
begin
  if (FBuffer <> nil) and (FStream <> nil) then
    if FDirty then
      bsWriteToStream;
  FreeMem(FBuffer, FBufSize);

  inherited Destroy;
end;

{-----------------------------------------------------------------------------}

procedure TIpBufferedStream.bsInitForNewStream;
begin
  if (FStream <> nil) then
    FSize := FStream.Size
  else
    FSize := 0;
  FBufCount := 0;
  FBufOfs := 0;
  FBufPos := 0;
  FDirty := false;
end;

{-----------------------------------------------------------------------------}

function TIpBufferedStream.ReadChar(var aCh : AnsiChar) : Boolean;
begin
  {is there anything to read?}
  if (FSize = (FBufOfs + FBufPos)) then begin
    Result := false;
    Exit;
  end;
  {if we get here, we'll definitely read a character}
  Result := true;
  {make sure that the buffer has some data in it}
  if (FBufCount = 0) then
    bsReadFromStream
  else if (FBufPos = FBufCount) then begin
    if FDirty then
      bsWriteToStream;
    FBufPos := 0;
    inc(FBufOfs, FBufSize);
    bsReadFromStream;
  end;
  {get the next character}
  aCh := AnsiChar(FBuffer[FBufPos]);
  inc(FBufPos);
end;

{-----------------------------------------------------------------------------}

procedure TIpBufferedStream.bsReadFromStream;
var
  NewPos : Longint;
begin
  {assumptions: FBufOfs is where to read the buffer
                FBufSize is the number of bytes to read
                FBufCount will be the number of bytes read}
  NewPos := FStream.Seek(FBufOfs, soFromBeginning);
  if (NewPos <> FBufOfs) then
    raise EIpBaseException.Create(SNoSeekForRead);
  FBufCount := FStream.Read(FBuffer^, FBufSize);
end;

{-----------------------------------------------------------------------------}

procedure TIpBufferedStream.bsSetStream(aValue : TStream);
begin
  if (aValue <> FStream) then begin
    {if the buffer is dirty, flush it to the current stream}
    if FDirty and (FStream <> nil) then
      bsWriteToStream;
    {remember the stream and initialize all fields}
    FStream := aValue;
    bsInitForNewStream;
  end;
end;

{-----------------------------------------------------------------------------}

procedure TIpBufferedStream.bsWriteToStream;
var
  NewPos       : Longint;
  BytesWritten : Longint;
begin
  {assumptions: FDirty is true
                FBufOfs is where to write the buffer
                FBufCount is the number of bytes to write
                FDirty will be set false afterwards}
  NewPos := FStream.Seek(FBufOfs, soFromBeginning);
  if (NewPos <> FBufOfs) then
    raise EIpBaseException.Create(SNoSeekForWrite);
  BytesWritten := FStream.Write(FBuffer^, FBufCount);
  if (BytesWritten <> FBufCount) then
    raise EIpBaseException.Create(SCannotWriteToStream);
  FDirty := false;
end;
{Begin !!.12}

{-----------------------------------------------------------------------------}

procedure TIpBufferedStream.Flush;
begin
  if FDirty then
    bsWriteToStream;
end;
{End !!.12}

{-----------------------------------------------------------------------------}

procedure TIpBufferedStream.FreeStream ;
begin
  if (FBuffer <> nil) and (FStream <> nil) then begin
    if FDirty then
      bsWriteToStream;
    FStream.Free;
    FStream := nil;
  end;
end;

{-----------------------------------------------------------------------------}

function TIpBufferedStream.Read(var Buffer; Count : Longint) : Longint;
var
  BytesToGo   : Longint;
  BytesToRead : Longint;
  BufAsBytes  : TByteArray absolute Buffer;
  DestPos     : Longint;
begin
  Result := 0;
  if not Assigned(FStream) then
    Exit;
  {calculate the number of bytes we could read if possible}
  BytesToGo := MinLong(Count, FSize - (FBufOfs + FBufPos));
  {we will return this number of bytes or raise an exception}
  Result := BytesToGo;
  {are we going to read some data after all?}
  if (BytesToGo > 0) then begin
    {make sure that the buffer has some data in it}
    if (FBufCount = 0) then
      bsReadFromStream;
    {read as much as we can from the current buffer}
    BytesToRead := MinLong(BytesToGo, FBufCount - FBufPos);
    {transfer that number of bytes}
    Move(FBuffer[FBufPos], BufAsBytes[0], BytesToRead);
    {update our counters}
    inc(FBufPos, BytesToRead);
    dec(BytesToGo, BytesToRead);
    {if we have more bytes to read then we've reached the end of the
     buffer and so we need to read another, and another, etc}
    DestPos := 0;
    while BytesToGo > 0 do begin
      {if the current buffer is dirty, write it out}
      if FDirty then
        bsWriteToStream;
      {position and read the next buffer}
      FBufPos := 0;
      inc(FBufOfs, FBufSize);
      bsReadFromStream;
      {calculate the new destination position, and the number of bytes
       to read from this buffer}
      inc(DestPos, BytesToRead);
      BytesToRead := MinLong(BytesToGo, FBufCount - FBufPos);
      {transfer that number of bytes}
      Move(FBuffer[FBufPos], BufAsBytes[DestPos], BytesToRead);
      {update our counters}
      inc(FBufPos, BytesToRead);
      dec(BytesToGo, BytesToRead);
    end;
  end;
end;

{-----------------------------------------------------------------------------}

function TIpBufferedStream.Seek(Offset : Longint; Origin : word) : Longint;
var
  NewPos : Longint;
  NewOfs : Longint;
begin
  Result := 0;
  if not Assigned(FStream) then
    Exit;
  {optimization: to help code that just wants the current stream
   position (ie, reading the Position property), check for this as a
   special case}
  if (Offset = 0) and (Origin = soFromCurrent) then begin
    Result := FBufOfs + FBufPos;
    Exit;
  end;
  {calculate the desired position}
  case Origin of
    soFromBeginning : NewPos := Offset;
    soFromCurrent   : NewPos := (FBufOfs + FBufPos) + Offset;
    soFromEnd       : NewPos := FSize + Offset;
  else
    raise EIpBaseException.Create(SBadSeekOrigin);
    NewPos := 0; {to fool the compiler's warning--we never get here}
  end;
  {force the new position to be valid}
  if (NewPos < 0) then
    NewPos := 0
  else if (NewPos > FSize) then
    NewPos := FSize;
  {calculate the offset for the buffer}
  NewOfs := (NewPos div FBufSize) * FBufSize;
  {if the offset differs, we have to move the buffer window}
  if (NewOfs <> FBufOfs) then begin
    {check to see whether we have to write the current buffer to the
     original stream first}
    if FDirty then
      bsWriteToStream;
    {mark the buffer as empty}
    FBufOfs := NewOfs;
    FBufCount := 0;
  end;
  {set the position within the buffer}
  FBufPos := NewPos - FBufOfs;
  Result := NewPos;
end;

{-----------------------------------------------------------------------------}

function TIpBufferedStream.Write(const Buffer; Count : Longint) : Longint;
type
  TIpByteArray = array[0..MaxInt-1] of Byte;
var
  BytesToGo   : Longint;
  BytesToWrite: Longint;
  BufAsBytes  : TIpByteArray absolute Buffer;
  DestPos     : Longint;
begin
  Result := 0;
  if not Assigned(FStream) then
    Exit;
  {calculate the number of bytes we should be able to write}
  BytesToGo := Count;
  {we will return this number of bytes or raise an exception}
  Result := BytesToGo;
  {are we going to write some data?}
  if (BytesToGo > 0) then begin
    {try and make sure that the buffer has some data in it}
    if (FBufCount = 0) and ((FBufOfs + FBufPos) < FSize) then
      bsReadFromStream;
    {write as much as we can to the current buffer}
    BytesToWrite := MinLong(BytesToGo, FBufSize - FBufPos);
    {transfer that number of bytes}
    Move(BufAsBytes[0], FBuffer[FBufPos], BytesToWrite);
    FDirty := true;
    {update our counters}
    inc(FBufPos, BytesToWrite);
    if (FBufCount < FBufPos) then begin
      FBufCount := FBufPos;
      FSize := FBufOfs + FBufPos;
    end;
    dec(BytesToGo, BytesToWrite);
    {if we have more bytes to write then we've reached the end of the
     buffer and so we need to write another, and another, etc}
    DestPos := 0;
    while BytesToGo > 0 do begin
      {as the current buffer is dirty, write it out}
      bsWriteToStream;
      {position and read the next buffer, if required}
      FBufPos := 0;
      inc(FBufOfs, FBufSize);
      if (FBufOfs < FSize) then
        bsReadFromStream
      else
        FBufCount := 0;
      {calculate the new destination position, and the number of bytes
       to write to this buffer}
      inc(DestPos, BytesToWrite);
      BytesToWrite := MinLong(BytesToGo, FBufSize - FBufPos);
      {transfer that number of bytes}
      if BytesToWrite > 0 then
        Move(BufAsBytes[DestPos], FBuffer[0], BytesToWrite);
      FDirty := true;
      {update our counters}
      inc(FBufPos, BytesToWrite);
      if (FBufCount < FBufPos) then begin
        FBufCount := FBufPos;
        FSize := FBufOfs + FBufPos;
      end;
      dec(BytesToGo, BytesToWrite);
    end;
  end;
end;

{-----------------------------------------------------------------------------}
{                           TIpAnsiTextStream                                 }
{-----------------------------------------------------------------------------}

constructor TIpAnsiTextStream.Create(aStream : TStream);
begin
  inherited Create(aStream);

  {set up the line index variables}
  atsResetLineIndex;
end;

{-----------------------------------------------------------------------------}

destructor TIpAnsiTextStream.Destroy;
begin
  {if needed, free the fixed line buffer}
  if (FFixedLine <> nil) then
    FreeMem(FFixedLine, FixedLineLength);
  {free the line index}
  FLineIndex.Free;
  inherited Destroy;
end;

{-----------------------------------------------------------------------------}

function TIpAnsiTextStream.AtEndOfStream : Boolean;
begin
  Result := FSize = (FBufOfs + FBufPos);
end;

{-----------------------------------------------------------------------------}

procedure TIpAnsiTextStream.atsGetLine(var aStartPos, aEndPos, aLen : Longint);
var
  Done   : Boolean;
  Ch     : AnsiChar;
  PrevCh : AnsiChar;
  TempLineTerm: Integer;
begin
  if (LineTerminator = ltNone) then begin
    aStartPos := FBufOfs + FBufPos;
    aEndPos := Seek(aStartPos + FixedLineLength, soFromBeginning);
    aLen := aEndPos - aStartPos;
  end
  else begin
    aStartPos := FBufOfs + FBufPos;
    Ch := #0;
    Done := false;

    // use temp as local variable for speed
    case LineTerminator of
      ltCRLF : TempLineTerm := 0;
      ltLF   : TempLineTerm := 1;
      ltCR   : TempLineTerm := 2;
      ltOther: TempLineTerm := 3;
    else
      raise EIpBaseException.Create(SBadLineTerminator);
    end;

    if FDirty then
      bsWriteToStream;

    while not Done do
    begin
      PrevCh := Ch;

      {is there anything to read?}
      if (FSize = (FBufOfs + FBufPos)) then begin
        aEndPos := FBufOfs + FBufPos;
        aLen := aEndPos - aStartPos;
        Done := True;
      end;

      {make sure that the buffer has some data in it}
      if (FBufCount = 0) then
        bsReadFromStream
      else if (FBufPos = FBufCount) then begin
        FBufPos := 0;
        inc(FBufOfs, FBufSize);
        bsReadFromStream;
      end;

      {get the next character}
      Ch := AnsiChar(FBuffer[FBufPos]);
      inc(FBufPos);

      case TempLineTerm of
        0   : if (Ch = #10) then begin
                     Done := true;
                     aEndPos := FBufOfs + FBufPos;
                   if PrevCh = #13 then
                     aLen := aEndPos - aStartPos - 2
                   else
                     aLen := aEndPos - aStartPos - 1;
                   end;
        1   : if (Ch = #10) then begin
                     Done := true;
                     aEndPos := FBufOfs + FBufPos;
                     aLen := aEndPos - aStartPos - 1;
                   end;
        2   : if (Ch = #13) then begin
                     Done := true;
                     aEndPos := FBufOfs + FBufPos;
                       aLen := aEndPos - aStartPos - 1;
                   end;
        3   : if (Ch = LineTermChar) then begin
                     Done := true;
                     aEndPos := FBufOfs + FBufPos;
                     aLen := aEndPos - aStartPos - 1;
                   end;
      end;
    end;
  end;
end;

{-----------------------------------------------------------------------------}

function TIpAnsiTextStream.atsGetLineCount : Longint;
begin
  if FLineCount < 0 then
    Result := MaxLongInt
  else
    Result := FLineCount;
end;

{-----------------------------------------------------------------------------}

procedure TIpAnsiTextStream.atsResetLineIndex;
begin
  {make sure we have a line index}
  if (FLineIndex = nil) then begin
    FLineIndex := TIntegerList.Create;  {create the index: even elements are}
    FLineIndex.Count := LineIndexCount * 2; {linenums, odd are offsets}

    {if we didn't have a line index, set up some reasonable defaults}
    FLineTerm := ltCRLF;  {normal Windows text file terminator}
    FLineEndCh := #10;    {not used straight away}
    FLineLen := 80;       {not used straight away}
  end;
  FLineIndex[0] := 0;     {the first line is line 0 and...}
  FLineIndex[1] := 0;     {...it starts at position 0}
  FLineInxTop := 0;       {the top valid index}
  FLineInxStep := 1;      {step count before add a line to index}
  FLineCount := -1;       {number of lines (-1 = don't know)}
  FLineCurrent := 0;      {current line}
  FLineCurOfs := 0;       {current line offset}
end;

{-----------------------------------------------------------------------------}

procedure TIpAnsiTextStream.atsSetLineTerm(aValue : TIpLineTerminator);
begin
  if (aValue <> LineTerminator) and ((FBufOfs + FBufPos) = 0) then begin
    {if there was no terminator, free the line buffer}
    if (LineTerminator = ltNone) then begin
      FreeMem(FFixedLine, FixedLineLength);
      FFixedLine := nil;
    end;
    {set the new value}
    FLineTerm := aValue;
    {if there is no terminator now, allocate the line buffer}
    if (LineTerminator = ltNone) then begin
      GetMem(FFixedLine, FixedLineLength);
    end;
    atsResetLineIndex;
  end;
end;

{-----------------------------------------------------------------------------}

procedure TIpAnsiTextStream.atsSetLineEndCh(aValue : AnsiChar);
begin
  if ((FBufOfs + FBufPos) = 0) then begin
    FLineEndCh := aValue;
    atsResetLineIndex;
  end;
end;

{-----------------------------------------------------------------------------}

procedure TIpAnsiTextStream.atsSetLineLen(aValue : Integer);
begin
  if (aValue <> FixedLineLength) and ((FBufOfs + FBufPos) = 0) then begin
    {validate the new length first}
    if (aValue < 1) or (aValue > 1024) then
      raise EIpBaseException.Create(SBadLineLength);

    {set the new value; note that if there is no terminator we need to
     free the old line buffer, and then allocate a new one}
    if (LineTerminator = ltNone) then
      FreeMem(FFixedLine, FixedLineLength);
    FLineLen := aValue;
    if (LineTerminator = ltNone) then
      GetMem(FFixedLine, FixedLineLength);
    atsResetLineIndex;
  end;
end;

{-----------------------------------------------------------------------------}

procedure TIpAnsiTextStream.bsInitForNewStream;
begin
  inherited bsInitForNewStream;
  atsResetLineIndex;
end;

{-----------------------------------------------------------------------------}

function TIpAnsiTextStream.ReadLine : string;
var
  CurPos : Longint;
  EndPos : Longint;
  Len    : Longint;
  StLen  : Longint;
begin
  if not Assigned(FStream) then
    Exit;
  atsGetLine(CurPos, EndPos, Len);
  if (LineTerminator = ltNone) then begin
    {at this point, Len will either equal FixedLineLength, or it will
     be less than it because we read the last line of all and it was
     short}
    StLen := FixedLineLength;
    {$IFDEF MSWindows}
    SetLength(Result, StLen);
    {$ELSE}
    {$IFDEF IP_LAZARUS}
    SetLength(Result, StLen);
    {$ELSE}
    if (StLen > 255) then
      StLen := 255;
    Result[0] := char(StLen);
    {$ENDIF}
    {$ENDIF}
    if (Len < StLen) then
      FillChar(Result[Len+1], StLen-Len, ' ');
  end
  else {LineTerminator is not ltNone} begin
    {$IFDEF MSWindows}
    SetLength(Result, Len);
    {$ELSE}
    {$IFDEF IP_LAZARUS}
    SetLength(Result, Len);
    {$ELSE}
    if (Len > 255) then
      Len := 255;
    Result[0] := char(Len);
    {$ENDIF}
    {$ENDIF}
  end;
  {read the line}
  Seek(CurPos, soFromBeginning);
  if Len > 0 then
    Read(Result[1], Len);
  Seek(EndPos, soFromBeginning);
end;

{-----------------------------------------------------------------------------}

function TIpAnsiTextStream.ReadLineArray(aCharArray : PAnsiChar;
                                         aLen       : Longint)
                                                    : Longint;
var
  CurPos : Longint;
  EndPos : Longint;
  Len    : Longint;
  StLen  : Longint;
begin
  Result := 0;
  if not Assigned(FStream) then
    Exit;
  atsGetLine(CurPos, EndPos, Len);
  if (LineTerminator = ltNone) then begin
    {at this point, Len will either equal FixedLineLength, or it will
     be less than it because we read the last line of all and it was
     short}
    StLen := FixedLineLength;
    if (StLen > aLen) then
      StLen := aLen;
    if (Len < StLen) then
      FillChar(aCharArray[Len], StLen-Len, ' ');
    Result := StLen;
  end
  else {LineTerminator is not ltNone} begin
    if (Len > aLen) then
      Len := aLen;
    Result := Len;
  end;
  Seek(CurPos, soFromBeginning);
  Read(aCharArray[0], Len);
  Seek(EndPos, soFromBeginning);
end;

{-----------------------------------------------------------------------------}

function TIpAnsiTextStream.ReadLineZ(aSt : PAnsiChar; aMaxLen : Longint) : PAnsiChar;
var
  CurPos : Longint;
  EndPos : Longint;
  Len    : Longint;
  StLen  : Longint;
begin
  Result := nil;
  if not Assigned(FStream) then
    Exit;
  Result := aSt;
  atsGetLine(CurPos, EndPos, Len);
  if (LineTerminator = ltNone) then begin
    {at this point, Len will either equal FixedLineLength, or it will
     be less than it because we read the last line of all and it was
     short}
    StLen := FixedLineLength;
    if (StLen > aMaxLen) then
      StLen := aMaxLen;
    if (Len < StLen) then
      FillChar(Result[Len], StLen-Len, ' ');
    Result[StLen] := #0;
  end
  else {LineTerminator is not ltNone} begin
    if (Len > aMaxLen) then
      Len := aMaxLen;
    Result[Len] := #0;
  end;
  Seek(CurPos, soFromBeginning);
  Read(Result[0], Len);
  Seek(EndPos, soFromBeginning);
end;

{-----------------------------------------------------------------------------}

function TIpAnsiTextStream.SeekNearestLine(aOffset : Longint) : Longint;
var
  CurLine : Longint;
  CurOfs  : Longint;
  CurPos  : Longint;
  EndPos  : Longint;
  Len     : Longint;
  i       : Longint;
  Done    : Boolean;
  L, R, M : Integer;
begin
  Result := 0;
  if not Assigned(FStream) then
    Exit;
  {if the offset we want is for the current line, reposition at the
   current line offset, return the current line number and exit}
  if (aOffset = FLineCurOfs) then begin
    Seek(FLineCurOfs, soFromBeginning);
    Result := FLineCurrent;
    Exit;
  end;
  {if the offset requested is less than or equal to zero, just
   position at line zero (ie, the start of the stream)}
  if (aOffset <= 0) then begin
    Seek(0, soFromBeginning);
    FLineCurrent := 0;
    FLineCurOfs := 0;
    Result := 0;
    Exit;
  end;
  {if the offset requested is greater than or equal to the size of the
   stream, position at the end of the stream (note that if we don't
   know the number of lines in the stream yet, FLineCount is set to
   -1 and we can't take this shortcut because we need to return the
   true value)}
  if (FLineCount >= 0) and (aOffset >= FSize) then begin
    Seek(0, soFromEnd);
    FLineCurrent := FLineCount;
    FLineCurOfs := FSize;
    Result := FLineCount;
    Exit;
  end;
  {if the offset requested is greater than the top item in the
   line index, we shall have to build up the index until we get to the
   line we require, or just beyond}
  if aOffset > FLineIndex[FLineInxTop+1] then begin
    {position at the last known line offset}
    CurLine := FLineIndex[FLineInxTop];
    CurOfs := FLineIndex[FLineInxTop+1];
    Seek(CurOfs, soFromBeginning);
    Done := false;
    {continue reading lines in chunks of FLineInxStep and add an index
     entry for each chunk}
    while not Done do begin
      for i := 0 to pred(FLineInxStep) do begin
        atsGetLine(CurPos, EndPos, Len);
        inc(CurLine);
        CurOfs := EndPos;
        if (EndPos = FSize) then begin
          Done := true;
          Break;
        end;
      end;
      if Done then
        FLineCount := CurLine
      else begin
        inc(FLineInxTop, 2);
        if (FLineInxTop = (LineIndexCount * 2)) then begin
          {we've exhausted the space in the index: rescale}
          FLineInxTop := FLineInxTop div 2;
          for i := 0 to pred(FLineInxTop) do begin
            if Odd(i) then
              FLineIndex.Exchange((i*2)-1, i)
            else
              FLineIndex.Exchange(i*2, i);
          end;
          FLineInxStep := FLineInxStep * 2;
        end;
        FLineIndex[FLineInxTop] := CurLine;
        FLineIndex[FLineInxTop+1] := CurOfs;
        if (aOffset <= CurOfs) then
          Done := true;
      end;
    end;
  end;
  {we can now work out where the nearest item in the index is to the
   line we require}
  L := 1;
  R := FLineInxTop+1;
  while (L <= R) do begin
    M := (L + R) div 2;
    if not Odd(M) then
      inc(M);
    if aOffset < FLineIndex[M] then
      R := M - 2
    else if aOffset > FLineIndex[M] then
      L := M + 2
    else begin
      FLineCurrent := FLineIndex[M-1];
      FLineCurOfs := FLineIndex[M];
      Seek(FLineCurOfs, soFromBeginning);
      Result := FLineCurrent;
      Exit;
    end;
  end;
  {the item at L-2 will have the nearest smaller offset than the
   one we want, hence the nearest smaller line is at L-3; start here
   and read through the stream forwards}
  CurLine := FLineIndex[L-3];
  Seek(FLineIndex[L-2], soFromBeginning);
  while true do begin
    atsGetLine(CurPos, EndPos, Len);
    inc(CurLine);
    if (EndPos > aOffset) then begin
      FLineCurrent := CurLine - 1;
       FLineCurOfs := CurPos;
      Seek(CurPos, soFromBeginning);
      Result := CurLine - 1;
      Exit;
    end
    else if (CurLine = FLineCount) or (EndPos = aOffset) then begin
      FLineCurrent := CurLine;
      FLineCurOfs := EndPos;
      Seek(EndPos, soFromBeginning);
      Result := CurLine;
      Exit;
    end;
  end;
end;

{-----------------------------------------------------------------------------}

function TIpAnsiTextStream.SeekLine(aLineNum : Longint) : Longint;
var
  CurLine : Longint;
  CurOfs  : Longint;
  CurPos  : Longint;
  EndPos  : Longint;
  Len     : Longint;
  i       : Longint;
  Done    : Boolean;
  L, R, M : Integer;
begin
  Result := 0;
  if not Assigned(FStream) then
    Exit;
  {if the line number we want is the current line, reposition at the
   current line offset, return the current line number and exit}
  if (aLineNum = FLineCurrent) then begin
    Seek(FLineCurOfs, soFromBeginning);
    Result := FLineCurrent;
    Exit;
  end;
  {if the line number requested is less than or equal to zero, just
   position at line zero (ie, the start of the stream)}
  if (aLineNum <= 0) then begin
    Seek(0, soFromBeginning);
    FLineCurrent := 0;
    FLineCurOfs := 0;
    Result := 0;
    Exit;
  end;
  {if the line number requested is greater than or equal to the line
   count, position at the end of the stream (note that if we don't
   know the number of lines in the stream yet, FLineCount is set to
   -1)}
  if (FLineCount >= 0) and (aLineNum > FLineCount) then begin
    Seek(0, soFromEnd);
    FLineCurrent := FLineCount;
    FLineCurOfs := FSize;
    Result := FLineCount;
    Exit;
  end;
  {if the line number requested is greater than the top item in the
   line index, we shall have to build up the index until we get to the
   line we require, or just beyond}
  if aLineNum > FLineIndex[FLineInxTop] then begin
    {position at the last known line offset}
    CurLine := FLineIndex[FLineInxTop];
    CurOfs := FLineIndex[FLineInxTop+1];
    Seek(CurOfs, soFromBeginning);
    Done := false;
    {continue reading lines in chunks of FLineInxStep and add an index
     entry for each chunk}
    while not Done do begin
      for i := 0 to pred(FLineInxStep) do begin
        atsGetLine(CurPos, EndPos, Len);
        inc(CurLine);
        CurOfs := EndPos;
        if (EndPos = FSize) then begin
          Done := true;
          Break;
        end;
      end;
      if Done then
        FLineCount := CurLine
      else begin
        inc(FLineInxTop, 2);
        if (FLineInxTop = (LineIndexCount * 2)) then begin
          {we've exhausted the space in the index: rescale}
          FLineInxTop := FLineInxTop div 2;
          for i := 0 to pred(FLineInxTop) do begin
            if Odd(i) then
              FLineIndex.Exchange((i*2)-1, i)
            else
              FLineIndex.Exchange(i*2, i);
          end;
          FLineInxStep := FLineInxStep * 2;
        end;
        FLineIndex[FLineInxTop] := CurLine;
        FLineIndex[FLineInxTop+1] := CurOfs;
        if (aLineNum <= CurLine) then
          Done := true;
      end;
    end;
  end;
  {we can now work out where the nearest item in the index is to the
   line we require}
  L := 0;
  R := FLineInxTop;
  while (L <= R) do begin
    M := (L + R) div 2;
    if Odd(M) then
      dec(M);
    if aLineNum < FLineIndex[M] then
      R := M - 2
    else if aLineNum > FLineIndex[M] then
      L := M + 2
    else begin
      FLineCurrent := FLineIndex[M];
      FLineCurOfs := FLineIndex[M+1];
      Seek(FLineCurOfs, soFromBeginning);
      Result := FLineCurrent;
      Exit;
    end;
  end;
  {the item at L-2 will have the nearest smaller line number than the
   one we want; start here and read through the stream forwards}
  CurLine := FLineIndex[L-2];
  Seek(FLineIndex[L-1], soFromBeginning);
  while true do begin
    atsGetLine(CurPos, EndPos, Len);
    inc(CurLine);
    if (CurLine = FLineCount) or (CurLine = aLineNum) then begin
      FLineCurrent := CurLine;
      FLineCurOfs := EndPos;
      Seek(EndPos, soFromBeginning);
      Result := CurLine;
      Exit;
    end;
  end;
end;

{-----------------------------------------------------------------------------}

procedure TIpAnsiTextStream.WriteLine(const aSt : string);
{Rewritten !!.15}
begin
  if Length(aSt) > 0 then
    WriteLineArray(@aSt[1], length(aSt))
  else
    WriteLineArray(nil, 0);
end;

{-----------------------------------------------------------------------------}

procedure TIpAnsiTextStream.WriteLineArray(aCharArray : PAnsiChar;
                                           aLen       : Longint);
var
  C : AnsiChar;
begin
  if not Assigned(FStream) then
    Exit;
  if (aCharArray = nil) then
    aLen := 0;
  if (LineTerminator = ltNone) then begin
    if (aLen >= FixedLineLength) then
      Write(aCharArray[0], FixedLineLength)
    else begin
      FillChar(FFixedLine[aLen], FixedLineLength-aLen, ' ');
      if (aLen > 0) then
        Move(aCharArray[0], FFixedLine[0], aLen);
      Write(FFixedLine[0], FixedLineLength);
    end;
  end
  else begin
    if (aLen > 0) then
      Write(aCharArray[0], aLen);
    case LineTerminator of
      ltNone : {this'll never get hit};
      ltCR   : Write(LineTerm[ltCR], 1);
      ltLF   : Write(LineTerm[ltLF], 1);
      ltCRLF : Write(LineTerm[ltCRLF], 2);
      ltOther: begin
                 C := LineTermChar;
                 Write(C, 1);
               end;
    else
      raise EIpBaseException.Create(SBadLineTerminator);
    end;
  end;
end;

{-----------------------------------------------------------------------------}

procedure TIpAnsiTextStream.WriteLineZ(aSt : PAnsiChar);
var
  LenSt : Longint;
begin
  if not Assigned(FStream) then
    Exit;
  if (aSt = nil) then
    LenSt := 0
  else
    LenSt := StrLen(aSt);
  WriteLineArray(aSt, LenSt);
end;


{ TIpDownloadFileStream }

constructor TIpDownloadFileStream.Create(const aPath : string);
begin
  FHandle := IpFileOpenFailed;
  inherited Create;
  dfsMakeTempFile(aPath);

  FHandle := THandle(FileOpen(FFileName, fmShareDenyNone + fmOpenReadWrite));
  if (Handle = IpFileOpenFailed) then
{$IFDEF Version6OrHigher}
    RaiseLastOSError; 
{$ELSE}
    RaiseLastWin32Error;
{$ENDIF}
end;

destructor TIpDownloadFileStream.Destroy;
begin
  {$IFDEF IP_LAZARUS}
  writeln('ToDo: TIpDownloadFileStream.Destroy ');
  {$ELSE}
  FlushFileBuffers(FHandle);
  if (Handle <> INVALID_HANDLE_VALUE) then
    CloseHandle(Handle);
  {$ENDIF}
  inherited Destroy;
end;

procedure TIpDownloadFileStream.dfsMakeTempFile(const aPath : string);
begin
  { Make sure the path has no backslash. }
  if aPath[length(aPath)] = '\' then
    FPath := Copy(aPath, 1, pred(length(aPath)))
  else
    FPath := aPath;

  { Check that it really exists. }
  if not DirExists(aPath) then
    raise EIpBaseException.Create(SBadPath);

  { Create a new uniquely named file in that folder. }
  FFileName := GetTemporaryFile(FPath);                                {!!.12}
end;

function TIpDownloadFileStream.Read(var Buffer; Count : Longint) : Longint;
{$IFDEF IP_LAZARUS}
begin
  writeln('ToDo: TIpDownloadFileStream.Read ');
  Result:=0;
end;
{$ELSE}
var
  ReadOK : Bool;
begin
  ReadOK := ReadFile(Handle, Buffer, Count, DWord(Result), nil);

  if not ReadOK then begin
    raise EIpBaseException.Create(SysErrorMessage(GetLastError) + SFilename + FFileName);
    Result := 0;
  end;
end;
{$ENDIF}

procedure TIpDownloadFileStream.Rename(aNewName : string);
var
  NewFullName : string;
begin
  {$IFDEF IP_LAZARUS}
  writeln('ToDo: TIpDownloadFileStream.Rename ');
  {$ENDIF}
  {close the current handle}
  {$IFNDEF IP_LAZARUS}
  CloseHandle(Handle);
  {$ENDIF}
  FHandle := IpFileOpenFailed;
  {calculate the full new name}
  NewFullName := FPath + '\' + aNewName;
  {rename the file}
{$IFDEF Version6OrHigher}
  {$IFNDEF IP_LAZARUS}
  if not MoveFile(PAnsiChar(FFileName), PAnsiChar(NewFullName)) then
    RaiseLastOSError;
  {$ENDIF}
{$ELSE}
  Win32Check(MoveFile(PAnsiChar(FFileName), PAnsiChar(NewFullName)));
{$ENDIF}
  {open up the same file, but with its new name}
  FFileName := NewFullName;
  try
    FHandle := THandle(FileOpen(FFileName, fmShareDenyNone + fmOpenRead));
  except
    { do nothing }
  end;

  if (Handle = IpFileOpenFailed) then
{$IFDEF Version6OrHigher}
    RaiseLastOSError;
{$ELSE}
    RaiseLastWin32Error;
{$ENDIF}

  FRenamed  := true;
end;

procedure TIpDownloadFileStream.Move(aNewName : string);
begin
  {$IFDEF IP_LAZARUS}
  writeln('ToDo: TIpDownloadFileStream.Move ');
  {$ENDIF}
  {close the current handle}
  {$IFNDEF IP_LAZARUS}
  CloseHandle(Handle);
  {$ENDIF}
  FHandle := IpFileOpenFailed;
  {copy the file}                                                      {!!.01}
{$IFDEF Version6OrHigher}
  {$IFNDEF IP_LAZARUS}
  if not CopyFile(PAnsiChar(FFileName), PAnsiChar(aNewName), False) then
    RaiseLastOSError;
  {$ENDIF}
{$ELSE}
  Win32Check(CopyFile(PAnsiChar(FFileName),                            {!!.01}
    PAnsiChar(aNewName), False));                                      {!!.01}
{$ENDIF}

  {open up the same file, but with its new name}
  FFileName := aNewName;
  try
    FHandle := THandle(FileOpen(FFileName, fmShareDenyNone + fmOpenRead));
  except
    { do nothing }
  end;

  if (Handle = IpFileOpenFailed) then
{$IFDEF Version6OrHigher}
    RaiseLastOSError;
{$ELSE}
    RaiseLastWin32Error;
{$ENDIF}

  FRenamed  := true;
end;

function TIpDownloadFileStream.Seek(Offset : Longint; Origin : Word) : Longint;
begin
  {$IFDEF IP_LAZARUS}
  writeln('ToDo: TIpDownloadFileStream.Seek');
  Result := 0;
  {$ELSE}
  Result := SetFilePointer(Handle, Offset, nil, Origin);
  {$ENDIF}
end;

function TIpDownloadFileStream.Write(const Buffer; Count : Longint) : Longint;
{$IFDEF IP_LAZARUS}
begin
  writeln('ToDo: TIpDownloadFileStream.Write');
  Result:=Count;
end;
{$ELSE}
var
  WriteOK : Bool;
begin
  WriteOK := WriteFile(Handle, Buffer, Count, DWord(Result), nil);

  if not WriteOK then begin
    raise EIpBaseException.Create(SysErrorMessage(GetLastError) + SFilename + FFileName);
    Result := 0
  end;
end;
{$ENDIF}


{ TIpByteStream }
constructor TIpByteStream.Create(aStream : TStream);
begin
  inherited Create;
  FStream := aStream;
end;

destructor TIpByteStream.Destroy;
begin
  inherited Destroy;
end;

function TIpByteStream.Read(var b : Byte) : Boolean;
begin
  Result := True;
  if (BufPos = BufEnd) then begin
    BufPos := 0;
    BufEnd := FStream.Read(Buffer, SizeOf(Buffer));
    if (BufEnd = 0) then begin
      Result := False;
      Exit;
    end;
  end;
  b := Buffer[BufPos];
  Inc(BufPos);
end;

function TIpByteStream.GetPosition : Integer;
begin
  Result := FStream.Position - BufEnd + BufPos;
end;

function TIpByteStream.GetSize : Integer;
begin
  Result := FStream.Size;
end;

end.