summaryrefslogtreecommitdiff
path: root/system/base/1.7.5/src/file
blob: 530dcb3aefbb3568fd452883eea1e8a4c891b4c9 (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
(* ------------------- VERSION 35     02.06.86 ------------------- *)
PACKET file handling DEFINES     (* Autoren: J.Liedtke, D.Martinek *)
       (***********) 
 
       FILE,
       :=,
       sequential file,
       reorganize,
       input,
       output,
       modify,
       close,
       putline,
       getline,
       put,
       get,
       write ,
       line,
       reset,
       down,
       up,
       downety,
       uppety,
       pattern found,
       to first record,
       to line,
       to eof,
       insert record,
       delete record,
       read record,
       write record,
       is first record,
       eof,
       line no,
       FRANGE,
       set range,
       reset range ,
       remove,
       clear removed,
       reinsert,
       max line length,
       edit info,
       line type ,
       copy attributes ,
       headline,
       put tabs,
       get tabs,
       col,
       word,
       at,
       removed lines,
       exec,
       pos ,
       len ,
       subtext ,
       change ,
       lines ,
       segments ,
       mark ,
       mark line no ,
       mark col ,
       set marked range ,
       split line ,
       concatenate line ,
       prefix ,
       sort ,
       lexsort :


(**********************************************************************)
(*                                                                    *)
(*  Terminologie:                                                     *)
(*                                                                    *)
(*                                                                    *)
(*     ATOMROW       Menge aller Atome eines FILEs.                   *)
(*                   Die einzelnen Atome haben zwar eine Position     *)
(*                   im Row, aber in dieser Betrachtung keine         *)
(*                   logische Reihenfolge.                            *)
(*                                                                    *)
(*     ATOM          Basiselement, kann eine Zeile der Datei und die  *)
(*                   zugehoerige Verwaltungsinformation aufnehmen     *)
(*                                                                    *)
(*     CHAIN         Zyklisch geschlossene Kette von Segmenten.       *)
(*                                                                    *)
(*     SEGMENT       Teilbereich des Atomrows, enthaelt 1 oder mehr   *)
(*                   zusammenhaengende Atoms.                         *)
(*                   Jedes Segment hat ein Vorgaenger- und ein        *)
(*                   Nachfolgersegment.                               *)
(*                   Jedes Segment enthaelt einen logisch zumsammen-  *)
(*                   haengenden Teile einer Sequence.                 *)
(*                                                                    *)
(*     SEQUENCE      Logische Folge von Lines.                        *)
(*                   Jede Sequence ist Teil einer Chain oder besteht  *)
(*                   vollstaendig daraus:                             *)
(*                                                                    *)
(*                        SEG1--SEG2--SEG3--SEG4--SEG5                *)
(*                          :----sequence----:                        *)
(*                                                                    *)
(*                   Die 'Reihenfolge' ebenso wie die 'Anzahl' der    *)
(*                   Lines ist eine wesentliche Eigenschaft einer     *)
(*                   Sequence.                                        *)
(*                                                                    *)
(*     LINE          Ein Atom als Element ein Sequence betrachtet.    *)
(*                                                                    *)
(*                                                                    *)
(**********************************************************************)
(*                                                                    *)
(* Eigenschaften:                                                     *)
(*                                                                    *)
(*     Folgende Mengen bilden eine Zerlegung (im math. Sinn) einer    *)
(*     gesamten Datei:                                                *)
(*                        used segment chain                          *)
(*                        scratch segment chain                       *)
(*                        free segment chain                          *)
(*                        unused tail                                 *)
(*                                                                    *)
(*     Fuer jedes X aus (used, scratch, free) gelten:                 *)
(*                                                                    *)
(*     'X sequence' ist echte Teilmenge von 'X segment chain'.        *)
(*                                                                    *)
(*                       (Daraus folgt, es gibt keine leere 'chain'.) *)
(*                                                                    *)
(*     'X segment chain' ist zyklisch gekettet.                       *)
(*                                                                    *)
(*     Alle Atome von 'X segment chain' haben definierten Inhalt.     *)
(*                                                                    *)
(**********************************************************************)
 
 
LET file size    = 4075 ,
    nil          = 0 ,
 
    free root    = 1 ,
    scratch root = 2 ,
    used root    = 3 ,
    first unused = 4 ;
 
 
LET SEQUENCE = STRUCT (INT index, segment begin, segment end,
                       INT line no, lines),
    SEGMENT  = STRUCT (INT succ, pred, end),
    ATOM     = STRUCT (SEGMENT seg, INT type, TEXT line),
    ATOMROW  = ROW filesize ATOM,
 
    LIST     = STRUCT (SEQUENCE used, INT prefix lines, postfix lines,
                       SEQUENCE scratch, free, INT unused tail,
                       INT mode, col, limit, edit info, mark line, mark col,
                       ATOMROW atoms);
 
TYPE FILE = BOUND LIST ;
 
TYPE FRANGE = STRUCT (INT pre, post, BOOL pre was split, post was split);


OP := (FRANGE VAR left, FRANGE CONST right):
  CONCR (left) := CONCR (right)
ENDOP := ;


OP := (FILE VAR left, FILE CONST right):
  EXTERNAL 260
END OP :=;


PROC becomes (INT VAR a, b) :
  INTERNAL 260  ;
  a := b
END PROC becomes;
 
 
PROC initialize (FILE VAR f) :
 
   f.used     := SEQUENCE : (used root, used root, used root, 1, 0);
   f.prefix lines  := 0;
   f.postfix lines := 0;
   f.free     := SEQUENCE : (free root, free root, free root, 1, 0);
   f.scratch  := SEQUENCE : (scratch root, scratch root, scratch root, 1, 0);
   f.unused tail := first unused;

   f.limit := 77;
   f.edit info := 0;
   f.col := 1 ;
   f.mark line := 0 ;
   f.mark col := 0 ;

   INT VAR i;
   FOR i FROM 1 UPTO 3 REP
     root (i).seg  := SEGMENT : (i, i, i);
     root (i).line := ""
   PER;
   put tabs (f, "") .
 
root : f.atoms .
 
END PROC initialize;
 
 
(**********************************************************************)
(*                                                                    *)
(*        Segment Handler         (SEGMENTs & CHAINs)                 *)
(*                                                                    *)
(**********************************************************************)

INT PROC segs (SEQUENCE CONST s, ATOMROW CONST atom) :

  INT VAR number of segments := 0 ,
  actual segment := s.segment begin ;
  REP
    number of segments INCR 1 ;
    actual segment := atom (actual segment).seg.succ
  UNTIL actual segment = s.segment begin PER ;
  number of segments .

ENDPROC segs ;

 
PROC next segment (SEQUENCE VAR s, ATOMROW CONST atom) :
 
  disable stop;
  s.line no INCR (s.segment end - s.index + 1);
  INT CONST new segment index := actual segment.succ;
  s.segment begin := new segment index;
  s.segment end   := new segment.end;
  s.index         := new segment index .

actual segment :  atom (s.segment begin).seg .
new segment :     atom (new segment index).seg .
 
END PROC next segment;
 
 
PROC previous segment (SEQUENCE VAR s, ATOMROW CONST atom) :

  disable stop;
  s.line no DECR (s.index - s.segment begin + 1);
  INT CONST new segment index := actual segment.pred;
  s.segment begin := new segment index;
  s.segment end   := new segment.end;
  s.index         := s.segment end .
 
actual segment :  atom (s.segment begin).seg .
new segment :     atom (new segment index).seg .
 
END PROC previous segment;
 
 
PROC split segment (SEQUENCE VAR s, ATOMROW VAR atom) :

  disable stop;
  IF not at segment top
    THEN split segment at actual position
  FI .

split segment at actual position :
  INT CONST pred index   := s.segment begin,
            actual index := s.index,
            succ index   := pred.succ;

  actual.pred  := pred index;
  actual.succ  := succ index;
  actual.end   := s.segment end;
 
  pred.succ    := actual index;
  pred.end     := actual index - 1;
 
  succ.pred    := actual index;
 
  s.segment begin := actual index .
 
not at segment top : s.index > s.segment begin .

pred   : atom (pred index).seg .
 
actual : atom (actual index).seg .
 
succ   : atom (succ index).seg .
 
END PROC split segment;
 

PROC join segments (ATOMROW VAR atom,
                    INT CONST first index, INT VAR second index) :
 
  disable stop;
  IF first seg.end + 1 = second index
    THEN attach second to first segment
    ELSE link first to second segment
  FI .
 
attach second to first segment :
  first seg.end  := second seg.end;
  INT VAR successor of second := second seg.succ;
  IF successor of second = second index
    THEN first seg.succ := first index
    ELSE join segments (atom, first index, successor of second)
  FI;
  second index := first index .
 
link first to second segment :
  first seg.succ  := second index;
  second seg.pred := first index .
 
first seg :  atom (first index).seg .
second seg : atom (second index).seg .
 
END PROC join segments;
 
 
PROC delete segments (SEQUENCE VAR from, ATOMROW VAR atom,
                      INT CONST first index, last index, lines) :
 
  determine surrounding segments and new atom index;
  join surrounding segments;
  update sequence descriptor .
 
determine surrounding segments and new atom index :
  INT VAR pred index   := first seg.pred,
          actual index := last seg.succ;
  from.index := actual index .
 
join surrounding segments :
  join segments (atom, pred index, actual index) .
 
update sequence descriptor :
  from.segment begin := actual index;
  from.segment end := actual seg.end;
  from.lines DECR lines .
 
actual seg :  atom (actual index).seg .
first seg :   atom (first index).seg .
last seg :    atom (last index).seg .
 
END PROC delete segments;
 
 
PROC insert segments (SEQUENCE VAR into, ATOMROW VAR atom,
                      INT CONST first index, last index, lines) :
 
  join into sequence and new segments;
  update sequence descriptor .
 
join into sequence and new segments :
  INT VAR actual index := into.index,
          pred index   := actual seg.pred;
  join segments (atom, last index, actual index);
  actual index := first index;
  join segments (atom, pred index, actual index) .
 
update sequence descriptor :
  into.index := first index;
  into.segment begin := actual index;
  into.segment end := actual seg.end;
  into.lines INCR lines .
 
actual seg :  atom (actual index).seg .
 
END PROC insert segments;
 
 
PROC next atom (SEQUENCE VAR s, ATOMROW CONST atom) :
 
  IF s.line no <= s.lines
    THEN to next atom
    ELSE errorstop ("'down' nach Dateiende")
  FI .
 
to next atom :
  disable stop;
  IF s.index = s.segment end
    THEN next segment (s, atom)
    ELSE s.index INCR 1;
         s.line no INCR 1
  FI
 
END PROC next atom;
 
 
PROC next atoms (SEQUENCE VAR s, ATOMROW CONST atom, INT CONST times) :
 
  INT CONST destination line := min (s.line no + times, s.lines + 1);
  jump upto destination segment;
  position within destination segment .
 
jump upto destination segment :
  WHILE s.line no + length of actual segments tail < destination line REP
    next segment (s, atom);
  PER .
 
position within destination segment :
  disable stop;
  s.index INCR (destination line - s.line no);
  s.line no := destination line .
 
length of actual segments tail :   s.segment end - s.index .
 
END PROC next atoms;
 
 
PROC previous atom (SEQUENCE VAR s, ATOMROW CONST atom) :
 
  IF s.line no > 1
    THEN to previous atom
    ELSE errorstop ("'up' am Dateianfang")
  FI .
 
to previous atom :
  disable stop;
  IF s.index = s.segment begin
    THEN previous segment (s, atom)
    ELSE s.index DECR 1;
         s.line no DECR 1
  FI
 
END PROC previous atom;
 
 
PROC previous atoms (SEQUENCE VAR s, ATOMROW CONST atom, INT CONST times) :
 
  INT CONST destination line := max (1, s.line no - times);
  jump back to destination segment;
  position within destination segment .
 
jump back to destination segment :
  WHILE s.line no - length of actual segments head > destination line REP
    previous segment (s, atom);
  PER .
 
position within destination segment :
  disable stop;
  s.index DECR (s.line no - destination line);
  s.line no := destination line .
 
length of actual segments head :   s.index - s.segment begin .
 
END PROC previous atoms;


TEXT VAR pre, pat, pattern0;
INT VAR last search line ;

PROC search down (SEQUENCE VAR s, ATOMROW CONST atom, TEXT CONST pattern,
                  INT CONST max lines, INT VAR column) :

  INT CONST start col := column ,
            start line := s.lineno ;
  last search line := min (s.lines, s.lineno + max lines) ;
  pre:= somefix (pattern) ;
  pattern0 := pattern ** 0 ;
  down in atoms (s, atom, pre, column); 
  IF NOT (last search succeeded CAND like pattern)
    THEN try again
  FI; 
  last search succeeded := TRUE ;
  column := matchpos (0) . 
 
try again: 
  WHILE s.line no < last search line
    REP next atom (s, atom) ;
        column := 1 ; 
        down in atoms (s, atom, pre, column); 
        IF last search succeeded CAND like pattern
          THEN LEAVE try again
        FI
    PER; 
    column := 1 + LENGTH record; 
    last search succeeded := FALSE ;
    LEAVE search down. 

like pattern :
  correct position ;
  pat := any (column-1) ;
  pat CAT any ;
  pat CAT pattern0 ;
  pat CAT any ;
  record LIKE pat .

correct position :
  IF s.lineno = start line
    THEN column := start col
    ELSE column := 1
  FI .

record : atom (s.index).line .

ENDPROC search down ;

PROC down in atoms (SEQUENCE VAR s, ATOMROW CONST atom, TEXT CONST pattern,
                    INT VAR column) :
 
  last search succeeded := FALSE ;
  search forwards in actual line ;
  IF NOT found AND s.line no < last search line
    THEN search in following lines
  FI ;
  IF found
    THEN last search succeeded := TRUE
    ELSE set column behind last char
  FI .

set column behind last char :
  column := LENGTH atom (s.index).line + 1 .

search forwards in actual line :
  IF pattern <> ""
    THEN column := pos (atom (s.index).line, pattern, column)
  ELIF column > LENGTH atom (s.index).line
    THEN column := 0
  FI .

search in following lines :
  next atom (s, atom) ;
  IF pattern = ""
    THEN column := 1 ;
         LEAVE search in following lines
  FI ;
  REP
    search forwards through segment ;
    update file position forwards ;
    IF found OR s.line no = last search line
      THEN LEAVE search in following lines
      ELSE next segment (s, atom)
    FI
  PER .

search forwards through segment :
  INT VAR search index := s.index ,
  last index := min (s.segment end, s.index+(last search line-s.line no));
  REP
    column := pos (atom (search index).line, pattern) ;
    IF found OR search index = last index
      THEN LEAVE search forwards through segment
    FI ;
    search index INCR 1
  PER .

update file position forwards :
  disable stop ;
  s.line no INCR (search index - s.index) ;
  s.index := search index ;
  enable stop .

found :  column > 0 .

ENDPROC down in atoms ;

TEXT PROC prefix (TEXT CONST pattern) : 
 
  INT VAR invalid char pos := pos (pattern, ""0"", ""31"", 1) ; 
  SELECT invalid char pos OF  
    CASE 0    : pattern 
    CASE 1    : ""                                              
    OTHERWISE : subtext (pattern, 1, invalid char pos - 1) 
  ENDSELECT . 
 
ENDPROC prefix ; 

PROC search up (SEQUENCE VAR s, ATOMROW CONST atom, TEXT CONST pattern,
                INT CONST max lines, INT VAR column) :

     last search line := max (1, s.lineno - max lines) ;
     pre:= prefix (pattern);
     pattern0 := pattern ** 0;
     remember start point ;
     up in atoms (s, atom, pre, column); 
     IF   NOT (last search succeeded CAND last pattern in line found)
     THEN try again 
     FI; 
     last search succeeded := TRUE ;
     column := matchpos (0) . 
 
  try again: 
     WHILE s.lineno > last search line OR column > 1
     REP   previous atom (s, atom); 
           column := LENGTH record ;
           up in atoms (s, atom, pre, column); 
           IF last search succeeded CAND last pattern in line found
             THEN LEAVE try again
           FI 
     PER; 
     column := 1; 
     last search succeeded := FALSE ;
     LEAVE search up. 

  remember start point :
     INT VAR c:= column, r:= s.lineno;.

  last pattern in line found :
    column := 2 ;
    WHILE like pattern CAND right of start REP
      column := matchpos (0) +1
    PER ;
    column DECR 1 ;
    like pattern CAND right of start .

  like pattern :
     pat := any (column-1) ;
     pat CAT any ;
     pat CAT pattern0 ;
     pat CAT any ;
     record LIKE pat .

  right of start : (r > s.lineno COR c >= matchpos(0)) .
  record         : atom (s.index).line .

ENDPROC search up ;

PROC up in atoms (SEQUENCE VAR s, ATOMROW CONST atom, TEXT CONST pattern,
                  INT VAR column) :

  last search succeeded := FALSE ;
  search backwards in actual line ;
  IF NOT found AND s.line no > last search line
    THEN search in preceeding lines
  FI ;
  IF found
    THEN last search succeeded := TRUE
    ELSE column := 1
  FI .

search backwards in actual line :
  IF pattern = ""
    THEN LEAVE search backwards in actual line
  FI ;
  INT VAR last pos , new pos := 0 ;
  REP
    last pos := new pos ;
    new pos := pos (atom (s.index).line, pattern, last pos+1) ;
  UNTIL new pos = 0 OR new pos > column PER ;
  column := last pos .

search in preceeding lines :
  previous atom (s, atom) ;
  IF pattern = ""
    THEN column := LENGTH atom (s.index).line + 1 ;
         last search succeeded := TRUE ;
         LEAVE search in preceeding lines
  FI ;
  REP
    search backwards through segment ;
    update file position backwards ;
    IF found OR s.line no = last search line
      THEN LEAVE search in preceeding lines
      ELSE previous segment (s, atom)
    FI
  PER .

search backwards through segment :
  INT VAR search index := s.index ,
  last index := max (s.segment begin, s.index-(s.line no-last search line));
  REP
    new pos := 0 ;
    REP
      column := new pos ;
      new pos := pos (atom (search index).line, pattern, column+1) ;
    UNTIL new pos = 0 PER ;
    IF found OR search index = last index
      THEN LEAVE search backwards through segment
    FI ;
    search index DECR 1
  PER .

update file position backwards :
  disable stop ;
  s.line no DECR (s.index - search index) ;
  s.index := search index ;
  enable stop .

found :  column > 0 .

ENDPROC up in atoms ;

BOOL VAR last search succeeded ;

BOOL PROC pattern found :
  last search succeeded
ENDPROC pattern found ;



PROC delete atom (SEQUENCE VAR used, free, ATOMROW VAR atom) :
 
  disable stop;
  IF used.line no <= used.lines
    THEN delete actual atom
    ELSE errorstop ("'delete' am Dateiende")
  FI .
 
delete actual atom :
  position behind actual free segment;
  split segment (used, atom);
  INT VAR actual index := used.index;
  cut off tail of actual used segment;
  delete segments (used, atom, actual index, actual index, 1);
  insert segments (free, atom, actual index, actual index, 1) .
 
position behind actual free segment :
  IF free.line no <= free.lines
    THEN next segment (free, atom)
  FI .
 
cut off tail of actual used segment :
  IF actual index <> used.segment end
    THEN used.index INCR 1;
         split segment (used, atom);
         used.index DECR 1 
  FI .
 
END PROC delete atom;
 
 
PROC insert atom (SEQUENCE VAR used, free,INT VAR unused, ATOMROW VAR atom) :
 
  disable stop;
  split segment (used, atom);
  IF free.lines > 0
    THEN insert new atom from free sequence
  ELIF unused <= file size
    THEN insert new atom from unused tail
  ELSE   errorstop ("FILE-Ueberlauf")
  FI .
 
insert new atom from free sequence :
  get a free segments head;
  make this atom to actual segment;
  transfer from free to used chain .
 
get a free segments head :
  IF actual free segment is root segment
    THEN previous segment (free, atom)
  FI;
  position to actual segments head .
 
position to actual segments head :
  INT VAR actual index := free.segment begin;
  free.line no DECR (free.index - actual index);
  free.index := actual index .
 
make this atom to actual segment :
  IF free.segment end > actual index
    THEN free.index INCR 1;
         split segment (free, atom);
         free.index DECR 1
  FI .
 
transfer from free to used chain :
  delete segments (free, atom, actual index, actual index, 1);
  insert segments (used, atom, actual index, actual index, 1);
  atom (actual index).line := "" .
 
insert new atom from unused tail :
  actual index := unused;
  atom (actual index).seg := 
                      SEGMENT:(actual index, actual index, actual index);
  atom (actual index).line := "";
  insert segments (used, atom, actual index, actual index, 1);
  unused INCR 1 .
 
actual free segment is root segment :    free.segment begin = free root .
 
END PROC insert atom;
 
 
PROC insert next (SEQUENCE VAR used, free, INT VAR unused, ATOMROW VAR atom,
                  TEXT CONST record) :
 
  IF used.line no > used.lines
    THEN insert atom (used, free, unused, atom)
  ELIF actual position before unused nonempty atomrow part
    THEN forward and insert atom by simple extension of used atomrow part
    ELSE next atom (used, atom);
         insert atom (used, free, unused, atom)
  FI;
  atom (used.index).line := record .
 
forward and insert atom by simple extension of used atomrow part :
  used.line no INCR 1;
  used.lines INCR 1;
  used.index INCR 1;
  used.segment end INCR 1;
  atom (used.segment begin).seg.end INCR 1;
  unused INCR 1 .
 
actual position before unused nonempty atomrow part :
  used.index = unused - 1 AND unused part not empty .
 
unused part not empty :  unused <= file size .
 
END PROC insert next;
 
 
PROC transfer subsequence (SEQUENCE VAR source, dest, 
                           ATOMROW VAR atom, INT CONST size) :
 
  IF size > 0
    THEN INT VAR subsequence size := min (size, source.line no);
         mark begin of source part;
         mark end of source part;
         split destination sequence;
         transfer part
  FI .
 
mark begin of source part :
  previous atoms (source, atom, subsequence size - 1);
  split segment (source, atom);
  INT CONST first := source.segment begin .
 
mark end of source part :
  next atoms (source, atom, subsequence size - 1);
  INT CONST last := source.segment begin;
  next atom (source, atom);
  split segment (source, atom) .
 
split destination sequence :
  split segment (dest, atom) .
 
transfer part :
  disable stop;
  delete segments (source, atom, first, last, subsequence size);
  source.line no DECR subsequence size;
  insert segments (dest, atom, first, last, subsequence size);
  next atoms (dest, atom, subsequence size - 1) .
 
END PROC transfer subsequence;
 


(********************************************************************)
(*****                                                          *****)
(*****                  FILE handler                            *****)
(*****                                                          *****)
(********************************************************************)


 
LET file type    = 1003 ,
    file type 16 = 1002 ,

    closed       = 0,
    inp          = 1,
    outp         = 2,
    mod          = 3,
    end          = 4,

    max limit    = 16000,
    super limit  = 16001;

 
TYPE TRANSPUTDIRECTION = INT;
 
 
TRANSPUTDIRECTION PROC input :
  TRANSPUTDIRECTION : (inp)
END PROC input;
 
 
TRANSPUTDIRECTION PROC output :
  TRANSPUTDIRECTION : (outp)
END PROC output;
 
 
TRANSPUTDIRECTION PROC modify :
  TRANSPUTDIRECTION : (mod)
END PROC modify;
 
 
FILE VAR result file;
 
 
FILE PROC sequential file (TRANSPUTDIRECTION CONST mode,
                            DATASPACE CONST ds) :
  IF   type (ds) = file type
  THEN result := ds
  ELIF type (ds) < 0
  THEN result := ds; type (ds, file type); initialize (result file)
  ELSE enable stop; errorstop ("Datenraum hat falschen Typ")
  FI;
  reset (result file, mode);
  result file .
 
result : CONCR (result file) .

END PROC sequential file;


FILE PROC sequential file (TRANSPUTDIRECTION CONST mode, TEXT CONST name) :

  IF   exists (name)
  THEN get dataspace if file
  ELIF CONCR (mode) <> inp
  THEN get new file space
  ELSE errorstop (""""+name+""" gibt es nicht") ; enable stop
  FI;
  update status if necessary;
  reset (result file, mode);
  result file .

get dataspace if file :
  IF type (old (name)) = file type 16
    THEN reorganize (name)
  FI ;
  result := old (name, file type) ;
  IF is 170 file
    THEN result.col := 1 ;
         result.mark line := 0 ;
         result.mark col  := 0
  FI .

is 170 file : result.mark col < 0 .

get new file space :
  result := new (name);
  IF   NOT is error
  THEN type (old (name), file type); initialize (result file)
  FI .

update status if necessary :
  IF   CONCR (mode) <> inp
  THEN status (name, ""); headline (result file, name)
  FI .
 
result : CONCR (result file) .
 
END PROC sequential file;
 
 
PROC reset (FILE VAR f) :

  IF f.mode = end
    THEN reset (f, input)
    ELSE reset (f, TRANSPUTDIRECTION:(f.mode))
  FI .

ENDPROC reset ;

PROC reset (FILE VAR f, TRANSPUTDIRECTION CONST mode) :

  IF f.mode <> mod OR new mode <> mod
    THEN f.mode := new mode ;
         initialize file index 
  FI .

initialize file index :
  IF new mode = outp
    THEN to line without check (f, f.used.lines);
         col := super limit
    ELSE to line without check (f, 1);
         col := 1 ;
         IF new mode = inp AND file is empty
           THEN f.mode := end
         FI
  FI .
 
file is empty : f.used.lines = 0 .
 
new mode  : CONCR (mode) .

col :   CONCR (CONCR (f)).col .
 
END PROC reset;
 
 
PROC input (FILE VAR f) :
 
  reset (f, input) .
 
END PROC input;
 
 
PROC output (FILE VAR f) :
 
  reset (f, output)
 
END PROC output;
 
 
PROC modify (FILE VAR f) :
 
  reset (f, modify)

END PROC modify;
 
 
PROC close (FILE VAR f) :
 
  f.mode := closed .

END PROC close;
 
 
PROC check mode (FILE CONST f, INT CONST mode) :
 
  IF f.mode = mode
    THEN LEAVE check mode
    ELIF f.mode = closed
    THEN errorstop ("Datei zu!")
    ELIF f.mode = mod
    THEN errorstop ("unzulaessiger Zugriff auf modify-FILE")
    ELIF mode = mod
    THEN errorstop ("Zugriff nur auf modify-FILE zulaessig")
    ELIF f.mode = end
    THEN errorstop ("Leseversuch nach Dateiende")
    ELIF mode = inp
    THEN errorstop ("Leseversuch auf output-FILE")
    ELIF mode = outp
    THEN errorstop ("Schreibversuch auf input-FILE")
  FI .
 
END PROC check mode;
 
 
PROC to line without check (FILE VAR f, INT CONST destination line) :
 
  INT CONST distance := destination line - f.used.line no;
  IF   distance > 0
    THEN next atoms (f.used, f.atoms, distance)
  ELIF distance < 0
    THEN previous atoms (f.used, f.atoms, - distance)
  FI .
 
END PROC to line without check;
 
 
PROC to line (FILE VAR f, INT CONST destination line) :
 
  check mode (f, mod);
  to line without check (f, destination line)
 
END PROC to line;
 
 
PROC to first record (FILE VAR f) :
 
  to line (f, 1)
 
END PROC to first record;
 
 
PROC to eof (FILE VAR f) :
 
  to line (f, f.used.lines + 1) .
 
END PROC to eof;
 
 
PROC putline (FILE VAR f, TEXT CONST word) :
 
  write (f, word);
  col := super limit .
 
col : CONCR (CONCR (f)).col .
 
END PROC putline;
 
 
PROC delete record (FILE VAR f) :
 
  check mode (f, mod);
  delete atom (f.used, f.free, f.atoms) .
 
END PROC delete record;
 
 
PROC insert record (FILE VAR f) :
 
  check mode (f, mod);
  insert atom (f.used, f.free, f.unused tail, f.atoms) .
 
END PROC insert record;
 
 
PROC down (FILE VAR f) :
 
  check mode (f, mod);
  next atom (f.used, f.atoms) .
 
END PROC down ;
 
PROC up (FILE VAR f) :
 
  check mode (f, mod);
  previous atom (f.used, f.atoms) .
 
END PROC up ;

PROC down (FILE VAR f, INT CONST n) :

  to line (f, lineno (f) + n)

ENDPROC down ;

PROC up (FILE VAR f, INT CONST n) :

  to line (f, lineno (f) - n)

ENDPROC up ;

 
PROC write record (FILE VAR f, TEXT CONST record) :
 
  check mode (f, mod);
  IF   not at eof
    THEN f.atoms (f.used.index).line := record
    ELSE errorstop ("'write' nach Dateiende")
  FI .
 
not at eof :  f.used.line no <= f.used.lines .
 
END PROC write record;
 
 
PROC read record (FILE CONST f, TEXT VAR record) :
 
  check mode (f, mod);
  record := f.atoms (f.used.index).line .
 
END PROC read record;
 
 
PROC line (FILE VAR f) :
 
  IF   mode = end
    THEN errorstop ("Leseversuch nach Dateiende")
    ELIF mode = inp
    THEN next atom (f.used, f.atoms); col := 1; check eof
    ELIF mode = outp
    THEN IF col <= max limit
           THEN col := super limit
           ELSE append empty line
         FI
  FI .
 
append empty line :
  insert next (f.used, f.free, f.unused tail, f.atoms, "") .
 
col : CONCR (CONCR (f)).col .
 
mode : CONCR (CONCR (f)).mode .
 
check eof :
  IF eof (f) THEN mode := end FI .
 
END PROC line;
 
 
PROC line (FILE VAR f, INT CONST lines) :
 
  INT VAR i; FOR i FROM 1 UPTO lines REP line (f) PER
 
END PROC line;
 

PROC getline (FILE VAR f, TEXT VAR text) :

  check mode (f, inp);
  text := subtext (record, f.col);
  IF f.used.line no >= f.used.lines
    THEN f.mode := end ;
         set end of file
    ELSE to next line ;
         f.col := 1
  FI .

to next line :
  next atom (f.used, f.atoms) .

set end of file :
  f.col := LENGTH record + 1 .

record : f.atoms (f.used.index).line .

END PROC getline;
 
 
BOOL PROC is first record (FILE CONST f) :
 
  check mode (f, mod);
  f.used.line no = 1 .
 
END PROC is first record;
 
 
BOOL PROC eof (FILE CONST f) :
 
  IF   line no < lines THEN FALSE
  ELIF line no = lines THEN col > LENGTH record
  ELSE                      TRUE
  FI .

line no :  f.used.line no .
lines   :  f.used.lines .
col     :  f.col .
record  :  f.atoms (f.used.index).line .
 
END PROC eof;
 
 
INT PROC line no (FILE CONST f) :
 
  f.used.line no .
 
END PROC line no;


PROC line type (FILE VAR f, INT CONST t) :

  f.atoms (f.used.index).type := t .

ENDPROC line type ;

INT PROC line type (FILE CONST f) :

  f.atoms (f.used.index).type .

ENDPROC line type ;
 
 
PROC put (FILE VAR f, TEXT CONST word) :
 
  check mode (f, outp);
  IF col + LENGTH word > f.limit
    THEN append new line
    ELSE record CAT word
  FI;
  record CAT " ";
  col := LENGTH record + 1 .
 
append new line :
  insert next (f.used, f.free, f.unused tail, f.atoms, word) .
 
record : f.atoms (f.used.index).line .
col :    f.col .
 
END PROC put;

 
PROC put (FILE VAR f, INT CONST value) :
 
  put (f, text (value))
 
END PROC put;
 
 
PROC put (FILE VAR f, REAL CONST real) :
 
  put (f, text (real))
 
END PROC put;
 
 
PROC write (FILE VAR f, TEXT CONST word) :
 
  check mode (f, outp);
  IF col + LENGTH word - 1 > f.limit
    THEN append new line
    ELSE record CAT word
  FI;
  col := LENGTH record + 1 .
 
append new line :
  insert next (f.used, f.free, f.unused tail, f.atoms, word) .
 
record : f.atoms (f.used.index).line .
col    : f.col .
 
END PROC write;
 
 
PROC get (FILE VAR f, TEXT VAR word, TEXT CONST separator) :
 
  check mode (f, inp);
  skip separators;
  IF word found
    THEN get word
    ELSE try to find word in next line
  FI .
 
skip separators :
  INT CONST separator length := LENGTH separator;
  WHILE is separator REP col INCR separator length PER .

is separator : 
  subtext (record, col, col + separator length - 1) = separator .
 
word found : col <= LENGTH record .
 
get word :
  INT VAR end of word := pos (record, separator, col) - 1;
  IF separator found
    THEN get text upto separator
    ELSE get rest of record
  FI .
 
separator found : end of word >= 0 .
 
get text upto separator :
  word := subtext (record, col, end of word);
  col := end of word + separator length + 1;
  IF col > LENGTH record THEN line (f) FI .
 
get rest of record :
  word := subtext (record, col); line (f) .
 
record : f.atoms (f.used.index).line .
col    : f.col .
 
try to find word in next line :
  line (f); IF eof (f) THEN word := "" ELSE get (f, word, separator) FI .
 
END PROC get;
 
 
PROC get (FILE VAR f, TEXT VAR word, INT CONST max length) :
 
  check mode (f, inp);
  IF word is only a part of record
    THEN get text of certain length
    ELSE get rest of record
  FI .
 
word is only a part of record :
  col <= LENGTH record - max length .
 
get text of certain length :
  word := text (record, max length, col);
  col INCR max length .

get rest of record :
  word := subtext (record, col); line (f) .
 
record : f.atoms (f.used.index).line .
col    : f.col .
 
END PROC get;
 
 
PROC get (FILE VAR f, TEXT VAR word) :
 
  get (f, word, " ")
 
END PROC get;
 
 
TEXT VAR number word;
 
 
PROC get (FILE VAR f, INT VAR number) :
 
  get (f, number word);
  number := int (number word)
 
END PROC get;
 
 
PROC get (FILE VAR f, REAL VAR number) :
 
  get (f, number word);
  number := real (number word)
 
END PROC get;
 
 
TEXT VAR split record ;
INT VAR indentation ;

PROC split line (FILE VAR f, INT CONST split col) :

  split line (f, split col, TRUE)

ENDPROC split line ;

PROC split line (FILE VAR f, INT CONST split col, BOOL CONST note indentation ) :

  IF note indentation 
    THEN get indentation 
    ELSE indentation := 0
  FI ;
  get split record ;
  insert split record and indentation ;
  cut off old record .

get indentation :
  indentation := pos (actual record,""33"",""254"",1) - 1 ;
  IF indentation < 0 OR indentation >= split col
    THEN indentation := split col - 1
  FI .

get split record :
  split record := subtext (actual record, split col, max limit) .

insert split record and indentation :
  down (f) ;
  insert record (f) ;
  INT VAR i ;
  FOR i FROM 1 UPTO indentation REP
    actual record CAT " "
  PER ; 
  actual record CAT split record ;
  up (f) .

cut off old record :
  actual record := subtext (actual record, 1, split col-1) .

actual record : f.atoms (f.used.index).line .

ENDPROC split line ;

PROC concatenate line (FILE VAR f, BOOL CONST delete blanks) :

  down (f) ;
  split record := actual record ;
  IF delete blanks
    THEN delete leading blanks
  FI ;
  delete record (f) ;
  up (f) ;
  actual record CAT split record .

delete leading blanks :
  INT CONST non blank col := pos (split record, ""33"", ""254"", 1) ;
  IF non blank col > 0
    THEN split record := subtext (split record, non blank col)
  FI .

actual record :  f.atoms (f.used.index).line .

ENDPROC concatenate line ;

PROC concatenate line (FILE VAR f) :
  concatenate line (f, TRUE)
ENDPROC concatenate line ;

PROC reorganize :
 
  reorganize (last param)
 
END PROC reorganize;
 
 
TEXT VAR file record ;

PROC reorganize (TEXT CONST file name) :
 
  enable stop ;
  FILE VAR input file, output file;
  DATASPACE VAR scratch space;
  INT CONST type of dataspace := type (old (file name)) ;
  INT VAR counter;
 
  last param (file name);
  IF type of dataspace = file type
    THEN reorganize new to new
  ELIF type of dataspace = file type 16
    THEN reorganize old to new
    ELSE errorstop ("Datenraum hat falschen Typ")
  FI;
  replace file space by scratch space .
 
reorganize new to new :
  input  file := sequential file (input, file name);
  disable stop ;
  scratch space := nilspace ;
  output file := sequential file (output, scratch space);
  copy attributes (input file, output file) ;

  FOR counter FROM 1 UPTO 9999
  WHILE NOT eof (input file) REP
    cout (counter);
    getline (input file, file record);
    putline (output file, file record);
    check for interrupt
  PER .
 
reorganize old to new :
  LET OLDRECORD = STRUCT (INT succ, pred, x, y, TEXT record);
  LET OLDFILE = BOUND ROW 4075 OLDRECORD;
  LET dateianker = 2, freianker = 1;
  INT VAR index := dateianker;
 
  OLDFILE VAR old file := old (file name);
  disable stop;
  scratch space := nilspace;
  output file := sequential file (output, scratch space);
  get old attributes ;
 
  say ("Datei wird in 1.7-Format gewandelt: ") ;

  FOR counter FROM 1 UPTO 9999
  WHILE NOT end of old file REP
    cout (counter);
    index := next record;
    file record := record of old file ;
    IF pos (file record, ""128"", ""250"", 1) > 0
      THEN change special chars
    FI ;
    putline (output file, file record);
    check for interrupt
  PER .
 
get old attributes :
  get old headline ;
  get old limit and tabs .

get old headline :
  headline (output file, old file (dateianker).record) .

get old limit and tabs :
  file record := old file (freianker).record ;
  max line length (output file, int (subtext (file record, 11, 15))) ;
  put tabs (output file, subtext (file record, 16)) .

change special chars :
  change all (file record, ""193"", ""214"") (* Ae *) ;
  change all (file record, ""207"", ""215"") (* Oe *) ;
  change all (file record, ""213"", ""216"") (* Ue *) ;
  change all (file record, ""225"", ""217"") (* ae *) ;
  change all (file record, ""239"", ""218"") (* oe *) ;
  change all (file record, ""245"", ""219"") (* ue *) ;
  change all (file record, ""235"", ""220"") (* k  *) ;
  change all (file record, ""173"", ""221"") (* -  *) ;
  change all (file record, ""163"", ""222"") (* fis   *) ;
  change all (file record, ""160"", ""223"") (* blank *) ;
  change all (file record, ""194"", ""251"") (* eszet *) .

end of old file :      next record = dateianker .

next record :          old file (index).succ .
 
record of old file :   old file (index).record .
 
check for interrupt :
  INT VAR size, used ;
  storage (size, used) ;
  IF used > size
    THEN errorstop ("Speicherengpass")
  FI ;
  IF is error
    THEN forget (scratch space) ; LEAVE reorganize
  FI .
 
replace file space by scratch space :
  headline (output file, file name);
  forget (file name, quiet) ;
  type (scratch space, file type);
  copy (scratch space, file name);
  forget (scratch space) .

END PROC reorganize;


PROC set range (FILE VAR f, INT CONST start line, start col,
                FRANGE VAR old range) :

  check mode (f, mod);
  IF valid restriction parameters
    THEN prepare last line ;
         prepare first line ;
         save old range ;
         set new range
    ELSE errorstop ("FRANGE ungueltig")
  FI .

valid restriction parameters :
  start line > 0 AND start col > 0 AND start before or at actual point .

start before or at actual point :
  start line < line no (f)  OR
  start line = line no (f) AND start col <= col (f) .

prepare last line :
  INT VAR last line ;
  IF col (f) > 1
    THEN split line (f, col(f), FALSE)
  FI .

prepare first line :
  IF start col > 1
    THEN split start line ;
  FI .

split start line :
  INT VAR old line no := line no (f) ;
  to line (f, start line) ;
  split line (f, start col, FALSE) ;
  to line (f, old line no + 1) .

save old range :
  old range.pre := f.prefix lines ;
  old range.post:= f.postfix lines .

set new range :
  get pre lines ;
  get post lines ;
  disable stop ;
  f.prefix lines INCR pre lines ;
  f.postfix lines INCR post lines ;
  f.used.lines DECR (post lines + pre lines) ;
  f.used.line no DECR pre lines .

get pre lines :
  INT VAR pre lines ;
  IF start col = 1
    THEN old range.pre was split := FALSE ;
         pre lines := start line - 1
    ELSE old range.pre was split := TRUE ;
         pre lines := start line
  FI .

get post lines :
  INT VAR post lines ;
  IF col (f) = 1
    THEN old range.post was split := FALSE ;
         post lines := lines (f) - line no (f) + 1
    ELSE old range.post was split := TRUE ;
         post lines := lines (f) - line no (f)
  FI .

END PROC set range;


PROC set range (FILE VAR f, FRANGE VAR new range) :

  check mode (f, mod);
  INT CONST pre add  := prefix - new range.pre,
            post add := postfix - new range.post;
  IF pre add < 0 OR post add < 0
    THEN errorstop ("FRANGE ungueltig")
    ELSE set new range;
         undo splitting if necessary ;
         make range var invalid
  FI .
 
set new range :
  disable stop;
  prefix DECR pre add;
  postfix DECR post add;
  used.line no INCR pre add;
  used.lines INCR (pre add + post add) .

undo splitting if necessary :
  IF new range.pre was split
    THEN concatenate first line
  FI ;
  IF new range.post was split
    THEN concatenate last line
  FI .

concatenate first line :
  INT VAR old line := line no (f) ;
  to line (f, pre add) ;
  concatenate line (f, FALSE) ;
  to line (f, old line - 1) .

concatenate last line :
  old line := line no (f) ;
  to line (f, lines (f) - post add) ;
  concatenate line (f, FALSE) ;
  to line (f, old line) .

make range var invalid :
  new range.pre := maxint .
 
used :    f.used .
prefix :  f.prefix lines .
postfix : f.postfix lines .
 
END PROC set range;
 
PROC reset range (FILE VAR f) :

  FRANGE VAR complete ;
  complete.pre := 0 ;
  complete.post:= 0 ;
  complete.pre was split := FALSE ;
  complete.post was split:= FALSE ;
  set range (f, complete)

ENDPROC reset range ;

PROC remove (FILE VAR f, INT CONST size) :
 
  check mode (f, mod);
  transfer subsequence (f.used, f.scratch, f.atoms, size) .
 
END PROC remove;
 
 
PROC clear removed (FILE VAR f) :
 
  check mode (f, mod);
  transfer subsequence (f.scratch, f.free, f.atoms, f.scratch.lines) .
 
END PROC clear removed;
 
 
PROC reinsert (FILE VAR f) :
 
  check mode (f, mod);
  transfer subsequence (f.scratch, f.used, f.atoms, f.scratch.lines) .
 
END PROC reinsert;


PROC copy attributes (FILE CONST source file, FILE VAR dest file) :

  dest.limit                     := source.limit ;
  dest.atoms (free root).line    := source.atoms (free root).line ;
  dest.atoms (scratch root).line := source.atoms (scratch root).line ;
  dest.edit info                 := source.edit info .

dest   : CONCR (CONCR (dest file)) .
source : CONCR (CONCR (source file)) .

ENDPROC copy attributes ;
 
 
INT PROC max line length (FILE CONST f) :
 
  f.limit .
 
END PROC max line length;
 
 
PROC max line length (FILE VAR f, INT CONST new limit) :
 
  IF new limit > 0 AND new limit <= max limit
    THEN f.limit := new limit
  FI .
 
END PROC max line length;
 
 
TEXT PROC headline (FILE CONST f) :
 
  f.atoms (free root).line .

END PROC headline;
 
 
PROC headline (FILE VAR f, TEXT CONST head) :
 
  f.atoms (free root).line := head .
 
END PROC headline;
 
 
PROC get tabs (FILE CONST f, TEXT VAR tabs) :
 
  tabs := f.atoms (scratch root).line .
 
END PROC get tabs;
 
 
PROC put tabs (FILE VAR f, TEXT CONST tabs) :
 
  f.atoms (scratch root).line := tabs .
 
END PROC put tabs;

 
INT PROC edit info (FILE CONST f) :
 
  f.edit info .
 
END PROC edit info;
 
 
PROC edit info (FILE VAR f, INT CONST info) : 
 
  f.edit info := info .
 
END PROC edit info;
 
 
INT PROC lines (FILE CONST f) :
 
  f.used.lines .
 
END PROC lines;
 
 
INT PROC removed lines (FILE CONST f) :
 
  f.scratch.lines .
 
END PROC removed lines;


INT PROC segments (FILE CONST f) :

  segs(f.used,f.atoms) + segs(f.scratch,f.atoms) + segs(f.free,f.atoms) - 2 .

ENDPROC segments ;


INT PROC col (FILE CONST f) :

  f.col

ENDPROC col ;

PROC col (FILE VAR f, INT CONST new column) :

  IF new column > 0
    THEN f.col := new column
  FI

ENDPROC col ;

TEXT PROC word (FILE CONST f) :

  word (f, " ")

ENDPROC word ;

TEXT PROC word (FILE CONST f, TEXT CONST delimiter) :

  INT VAR del pos := pos (f, delimiter, col (f)) ;
  IF del pos = 0
    THEN del pos := len (f) + 1
  FI ;
  subtext (f, col (f), del pos - 1)

ENDPROC word ;

TEXT PROC word (FILE CONST f, INT CONST max length) :

  subtext (f, col (f), col (f) + max length - 1)

ENDPROC word ;

BOOL PROC at (FILE CONST f, TEXT CONST word) :

  pat := any (column-1) ;
  pat CAT word ;
  pat CAT any ;
  record LIKE pat .

column : f.col .
record : f.atoms (f.used.index).line .

ENDPROC at ;

 
PROC exec (PROC (TEXT VAR, TEXT CONST) proc, FILE VAR f, TEXT CONST t) :
 
  proc (record, t) .
 
record : f.atoms (f.used.index).line .
 
END PROC exec;
 
 
PROC exec (PROC (TEXT VAR, INT CONST) proc, FILE VAR f, INT CONST i) :
 
  proc (record, i) .

record : f.atoms (f.used.index).line .
 
END PROC exec;
 
INT PROC pos (FILE CONST f, TEXT CONST pattern, INT CONST i) :
 
  pos (record, pattern, i) .
 
record : f.atoms (f.used.index).line .
 
END PROC pos ;

PROC down (FILE VAR f, TEXT CONST pattern) :

  down (f, pattern, file size)

ENDPROC down ;

PROC down (FILE VAR f, TEXT CONST pattern, INT CONST max line) :

  check mode (f,mod) ;
  INT VAR pattern pos := f.col + 1 ;
  search down (f.used, f.atoms, pattern, max line, pattern pos) ;
  f.col := pattern pos

ENDPROC down ;

PROC downety (FILE VAR f, TEXT CONST pattern) :
 
  downety (f, pattern, file size)

ENDPROC downety ;

PROC downety (FILE VAR f, TEXT CONST pattern, INT CONST max line) :

  check mode (f,mod) ;
  INT VAR pattern pos := f.col ;
  search down (f.used, f.atoms, pattern, max line, pattern pos) ;
  f.col := pattern pos

ENDPROC downety ;

PROC up (FILE VAR f, TEXT CONST pattern) :

  up (f, pattern, file size)

ENDPROC up ;

PROC up (FILE VAR f, TEXT CONST pattern, INT CONST max line) :

  check mode (f,mod) ;
  INT VAR pattern pos := f.col - 1 ;
  search up (f.used, f.atoms, pattern, max line, pattern pos) ;
  f.col := pattern pos

ENDPROC up ;

PROC uppety (FILE VAR f, TEXT CONST pattern) :

  uppety (f, pattern, file size)

ENDPROC uppety ;

PROC uppety (FILE VAR f, TEXT CONST pattern, INT CONST max line) :

  check mode (f,mod) ;
  INT VAR pattern pos := f.col ;
  search up (f.used, f.atoms, pattern, max line, pattern pos) ;
  f.col := pattern pos

ENDPROC uppety ;


INT PROC len (FILE CONST f) :

  length (record) .

record : f.atoms (f.used.index).line .

ENDPROC len ;

TEXT PROC subtext (FILE CONST f, INT CONST from, to) :

  subtext (record, from, to) .

record : f.atoms (f.used.index).line .

ENDPROC subtext ;

PROC change (FILE VAR f, INT CONST from, to, TEXT CONST new) :

  check mode (f, mod) ;
  change (record, from, to, new) .

record : f.atoms (f.used.index).line .

ENDPROC change ;

 
BOOL PROC mark (FILE CONST f) :

  f.mark line > 0

ENDPROC mark ;

PROC mark (FILE VAR f, INT CONST line no, col) :

  IF line no > 0
    THEN f.mark line := line no + f.prefix lines ;
         f.mark col  := col
    ELSE f.mark line := 0 ;
         f.mark col  := 0
  FI 

ENDPROC mark ;

INT PROC mark line no (FILE CONST f) :

  IF f.mark line = 0
    THEN 0
    ELSE max (1, f.mark line - f.prefix lines)
  FI

ENDPROC mark line no ;

INT PROC mark col (FILE CONST f) :

  IF f.mark line = 0
    THEN 0
  ELIF f.mark line <= f.prefix lines
    THEN 1
  ELSE f.mark col
  FI

ENDPROC mark col ;

PROC set marked range (FILE VAR f, FRANGE VAR old range) :

  IF mark (f)
    THEN set range (f, mark line no (f), mark col (f), old range)
    ELSE old range := previous range of file
  FI .

previous range of file :
  FRANGE : (f.prefix lines, f.postfix lines, FALSE, FALSE) .

ENDPROC set marked range ;


(*****************************************************************)

                                                  (* Autor: P.Heyderhoff *) 
                                                  (* Stand: 11.10.83     *) 
 
BOUND LIST VAR datei; 
INT  VAR sortierstelle, sortanker;
BOOL VAR ascii sort;
TEXT VAR median, tausch , links, rechts;
 
PROC sort (TEXT CONST dateiname) : 
     sort (dateiname, 1)
END PROC sort; 
 
PROC sort (TEXT CONST dateiname, INT CONST sortieranfang) : 
     ascii sort := TRUE ;
     sortierstelle := sortieranfang; sortiere (dateiname)
END PROC sort; 
 
PROC lex sort (TEXT CONST dateiname) :
     lex sort (dateiname, 1)
ENDPROC lex sort ;

PROC lex sort (TEXT CONST dateiname, INT CONST sortieranfang) :
     ascii sort := FALSE ;
     sortierstelle := sortieranfang; sortiere (dateiname)
ENDPROC lex sort ;

PROC sortiere (TEXT CONST dateiname) :

  reorganize file if necessary ;
  sort file .

reorganize file if necessary :
  FILE VAR f := sequential file (modify, dateiname) ;
  IF segments (f) > 1
    THEN reorganize (dateiname)
  FI .

sort file :
  f := sequential file (modify, dateiname) ;
  INT CONST sortende := lines (f) + 3 ;
  sortanker := 1 + 3 ;
  datei := old (dateiname) ;
  quicksort(sortanker, sortende) .

END PROC sortiere; 
 
PROC quicksort ( INT CONST anfang, ende ) : 
     IF   anfang < ende 
     THEN INT VAR p,q; 
          spalte    (anfang, ende, p, q); 
          quicksort (anfang, q); 
          quicksort (p, ende)    FI 
END PROC quicksort; 
 
PROC spalte (INT CONST anfang, ende, INT VAR p, q): 
     fange an der seite an und waehle den median; 
     ruecke p und q so dicht wie moeglich zusammen; 
     hole ggf median in die mitte . 
 
   fange an der seite an und waehle den median : 
     p := anfang; q := ende ; 
     INT CONST m :: (p + q) DIV 2 ; 
     median := subtext(datei m, sortierstelle) . 
 
   ruecke p und q so dicht wie moeglich zusammen : 
     REP schiebe p und q so weit wie moeglich auf bzw ab; 
         IF p < q THEN vertausche die beiden FI 
     UNTIL p > q END REP . 
 
   vertausche die beiden : 
     tausch := datei p; datei p := datei q; datei q := tausch; 
     p INCR 1; q DECR 1 . 
 
   schiebe p und q so weit wie moeglich auf bzw ab : 
     WHILE p kann groesser werden REP p INCR 1 END REP; 
     WHILE q kann kleiner  werden REP q DECR 1 END REP . 
 
   p kann groesser werden : 
     IF p <= ende 
       THEN links := subtext (datei p, sortierstelle) ;
            IF ascii sort
              THEN median >= links
              ELSE median LEXGREATEREQUAL links
            FI
       ELSE FALSE 
     FI . 
 
   q kann kleiner werden : 
     IF q >= anfang 
       THEN rechts := subtext(datei q, sortierstelle) ;
            IF ascii sort
              THEN rechts >= median 
              ELSE rechts LEXGREATEREQUAL median
            FI
       ELSE FALSE 
     FI . 
 
   hole ggf median in die mitte : 
     IF   m < q THEN vertausche m und q 
     ELIF m > p THEN vertausche m und p FI . 
 
   vertausche m und q : 
     tausch := datei m; datei m := datei q; datei q := tausch; q DECR 1 . 
 
   vertausche m und p : 
     tausch := datei m; datei m := datei p; datei p := tausch; p INCR 1 . 
 
   datei m :  datei.atoms (m).line . 
   datei p :  datei.atoms (p).line . 
   datei q :  datei.atoms (q).line . 
 
END PROC spalte; 

END PACKET file handling;