summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 8db9b6a)
raw | patch | inline | side by side (parent: 8db9b6a)
author | DeaDDooMER <deaddoomer@deadsoftware.ru> | |
Fri, 15 Sep 2017 11:09:46 +0000 (14:09 +0300) | ||
committer | DeaDDooMER <deaddoomer@deadsoftware.ru> | |
Fri, 15 Sep 2017 11:09:46 +0000 (14:09 +0300) |
rtl/Reals.obn | [new file with mode: 0644] | patch | blob |
rtl/Texts.obn | [new file with mode: 0644] | patch | blob |
diff --git a/rtl/Reals.obn b/rtl/Reals.obn
--- /dev/null
+++ b/rtl/Reals.obn
@@ -0,0 +1 @@
+MODULE Reals; (* interface based on Oberon V4 *)\r\r IMPORT SYSTEM, CPStrings, CPMath;\r\r PROCEDURE Expo*(x: REAL): INTEGER;\r BEGIN\r RETURN SHORT(CPMath.Exponent(x));\r END Expo;\r\r PROCEDURE ExpoL*(x: LONGREAL): INTEGER;\r BEGIN\r RETURN SHORT(CPMath.Exponent(x));\r END ExpoL;\r\r PROCEDURE SetExpo*(e: INTEGER; VAR x: REAL);\r BEGIN\r x := SHORT(CPMath.Real(CPMath.Mantissa(x), e));\r END SetExpo;\r\r PROCEDURE SetExpoL*(e: INTEGER; VAR x: LONGREAL);\r BEGIN\r x := CPMath.Real(CPMath.Mantissa(x), e);\r END SetExpoL;\r\r PROCEDURE Ten*(e: INTEGER): REAL;\r BEGIN\r RETURN SHORT(CPMath.IntPower(10, e));\r END Ten;\r\r PROCEDURE TenL*(e: INTEGER): LONGREAL;\r BEGIN\r RETURN CPMath.IntPower(10, e);\r END TenL;\r\r PROCEDURE Convert*(x: REAL; n: INTEGER; VAR d: ARRAY OF CHAR);\r BEGIN\r CPStrings.RealToStringForm(x, n, 0, 3, "0", d);\r END Convert;\r\r PROCEDURE ConvertL*(x: LONGREAL; n: INTEGER; VAR d: ARRAY OF CHAR);\r BEGIN\r CPStrings.RealToStringForm(x, n, 0, 3, "0", d);\r END ConvertL;\r\r PROCEDURE ConvertH*(x: REAL; VAR d: ARRAY OF CHAR);\r BEGIN\r CPStrings.IntToStringForm(SYSTEM.VAL(LONGINT, x), CPStrings.hexadecimal, 0, "0", FALSE, d);\r END ConvertH;\r\r PROCEDURE ConvertHL*(x: LONGREAL; VAR d: ARRAY OF CHAR);\r BEGIN\r CPStrings.IntToStringForm(SYSTEM.VAL(HUGEINT, x), CPStrings.hexadecimal, 0, "0", FALSE, d);\r END ConvertHL;\r\rEND Reals.\r
\ No newline at end of file
diff --git a/rtl/Texts.obn b/rtl/Texts.obn
--- /dev/null
+++ b/rtl/Texts.obn
@@ -0,0 +1 @@
+MODULE Texts; (** CAS/HM 7.10.93 -- interface based on Texts by JG/NW 6.12.91**)\r (* Ported from Oberon V4 -- this version not depended on graphics *)\r\r IMPORT\r Files, Reals;\r\r (*--- insert field e: Elem into Texts.Scanner and change Texts.Scan to set it in case of class=6 *)\r\r\r CONST\r ElemChar* = 1CX;\r TAB = 9X; CR = 0DX; maxD = 9;\r (**FileMsg.id**)\r load* = 0; store* = 1;\r (**Notifier op**)\r replace* = 0; insert* = 1; delete* = 2;\r (**Scanner.class**)\r Inval* = 0; Name* = 1; String* = 2; Int* = 3; Real* = 4; LongReal* = 5; Char* = 6;\r\r textTag = 0F0X; version = 01X;\r\r TYPE\r FontsFont = POINTER TO FontDesc;\r FontDesc = RECORD\r name: ARRAY 32 OF CHAR;\r END;\r\r Run = POINTER TO RunDesc;\r RunDesc = RECORD\r prev, next: Run;\r len: LONGINT;\r fnt: FontsFont;\r col, voff: SHORTINT\r END;\r\r Piece = POINTER TO PieceDesc;\r PieceDesc = RECORD (RunDesc)\r file: Files.File;\r org: LONGINT\r END;\r\r Elem* = POINTER TO ElemDesc;\r Buffer* = POINTER TO BufDesc;\r Text* = POINTER TO TextDesc;\r\r ElemMsg* = RECORD END;\r Handler* = PROCEDURE (e: Elem; VAR msg: ElemMsg);\r\r ElemDesc* = RECORD (RunDesc)\r W*, H*: LONGINT;\r handle*: Handler;\r base: Text\r END;\r\r FileMsg* = RECORD (ElemMsg)\r id*: INTEGER;\r pos*: LONGINT;\r r*: Files.Rider\r END;\r\r CopyMsg* = RECORD (ElemMsg)\r e*: Elem\r END;\r\r IdentifyMsg* = RECORD (ElemMsg)\r mod*, proc*: ARRAY 32 OF CHAR\r END;\r\r\r BufDesc* = RECORD\r len*: LONGINT;\r head: Run\r END;\r\r Notifier* = PROCEDURE (T: Text; op: INTEGER; beg, end: LONGINT);\r\r TextDesc* = RECORD\r len*: LONGINT;\r notify*: Notifier;\r head, cache: Run;\r corg: LONGINT\r END;\r\r Reader* = RECORD\r eot*: BOOLEAN;\r fnt*: FontsFont;\r col*, voff*: SHORTINT;\r elem*: Elem;\r rider: Files.Rider;\r run: Run;\r org, off: LONGINT\r END;\r\r Scanner* = RECORD (Reader)\r nextCh*: CHAR;\r line*, class*: INTEGER;\r i*: LONGINT;\r x*: REAL;\r y*: LONGREAL;\r c*: CHAR;\r len*: SHORTINT;\r s*: ARRAY 32 OF CHAR\r END;\r\r Writer* = RECORD\r buf*: Buffer;\r fnt*: FontsFont;\r col*, voff*: SHORTINT;\r rider: Files.Rider;\r file: Files.File\r END;\r\r Alien = POINTER TO RECORD (ElemDesc)\r file: Files.File;\r org, span: LONGINT;\r mod, proc: ARRAY 32 OF CHAR\r END;\r\r\r VAR\r new*: Elem;\r del: Buffer;\r\r\r (* run primitives *)\r\r PROCEDURE Find (T: Text; VAR pos: LONGINT; VAR u: Run; VAR org, off: LONGINT);\r VAR v: Run; m: LONGINT;\r BEGIN\r IF pos >= T.len THEN pos := T.len; u := T.head; org := T.len; off := 0; T.cache := T.head; T.corg := 0\r ELSE v := T.cache.next; m := pos - T.corg;\r IF pos >= T.corg THEN\r WHILE m >= v.len DO DEC(m, v.len); v := v.next END\r ELSE\r WHILE m < 0 DO v := v.prev; INC(m, v.len) END;\r END;\r u := v; org := pos - m; off := m; T.cache := v.prev; T.corg := org\r END\r END Find;\r\r PROCEDURE Split (off: LONGINT; VAR u, un: Run);\r VAR p, U: Piece;\r BEGIN\r IF off = 0 THEN un := u; u := un.prev\r ELSIF off >= u.len THEN un := u.next\r ELSE NEW(p); un := p; U := u(Piece);\r p^ := U^; INC(p.org, off); DEC(p.len, off); DEC(U.len, p.len);\r p.prev := U; p.next := U.next; p.next.prev := p; U.next := p\r END\r END Split;\r\r PROCEDURE Merge (T: Text; u: Run; VAR v: Run);\r VAR p, q: Piece;\r BEGIN\r IF (u IS Piece) & (v IS Piece) & (u.fnt.name = v.fnt.name) & (u.col = v.col) & (u.voff = v.voff) THEN\r p := u(Piece); q := v(Piece);\r IF (p.file = q.file) & (p.org + p.len = q.org) THEN\r IF T.cache = u THEN INC(T.corg, q.len)\r ELSIF T.cache = v THEN T.cache := T.head; T.corg := 0\r END;\r INC(p.len, q.len); v := v.next\r END\r END\r END Merge;\r\r PROCEDURE Splice (un, v, w: Run; base: Text); (* (u, un) -> (u, v, w, un) *)\r VAR u: Run;\r BEGIN\r IF v # w.next THEN u := un.prev;\r u.next := v; v.prev := u; un.prev := w; w.next := un;\r REPEAT\r IF v IS Elem THEN v(Elem).base := base END;\r v := v.next\r UNTIL v = un\r END\r END Splice;\r\r PROCEDURE ClonePiece (p: Piece): Piece;\r VAR q: Piece;\r BEGIN NEW(q); q^ := p^; RETURN q\r END ClonePiece;\r\r PROCEDURE CloneElem (e: Elem): Elem;\r VAR msg: CopyMsg;\r BEGIN msg.e := NIL; e.handle(e, msg); RETURN msg.e\r END CloneElem;\r\r\r (** Elements **)\r\r PROCEDURE CopyElem* (SE, DE: Elem);\r BEGIN DE.len := SE.len; DE.fnt := SE.fnt; DE.col := SE.col; DE.voff := SE.voff;\r DE.W := SE.W; DE.H := SE.H; DE.handle := SE.handle\r END CopyElem;\r\r PROCEDURE ElemBase* (E: Elem): Text;\r BEGIN RETURN E.base\r END ElemBase;\r \r PROCEDURE ElemPos* (E: Elem): LONGINT;\r VAR u: Run; pos: LONGINT;\r BEGIN u := E.base.head.next; pos := 0;\r WHILE u # E DO pos := pos + u.len; u := u.next END;\r RETURN pos\r END ElemPos;\r\r\r PROCEDURE HandleAlien (E: Elem; VAR msg: ElemMsg);\r VAR e: Alien; r: Files.Rider; i: LONGINT; ch: CHAR;\r BEGIN\r WITH E: Alien DO\r IF msg IS CopyMsg THEN\r (*\r WITH msg: CopyMsg DO NEW(e); CopyElem(E, e);\r e.file := E.file; e.org := E.org; e.span := E.span; e.mod := E.mod; e.proc := E.proc;\r msg.e := e\r END\r *)\r ELSIF msg IS IdentifyMsg THEN\r WITH msg: IdentifyMsg DO\r COPY(E.mod, msg.mod); COPY(E.proc, msg.proc); msg.mod[31] := 1X (*alien*)\r END\r ELSIF msg IS FileMsg THEN\r WITH msg: FileMsg DO\r IF msg.id = store THEN Files.Set(r, E.file, E.org); i := E.span;\r WHILE i > 0 DO Files.Read(r, ch); Files.Write(msg.r, ch); DEC(i) END\r END\r END\r END\r END\r END HandleAlien;\r\r\r (** Buffers **)\r\r PROCEDURE OpenBuf* (B: Buffer);\r VAR u: Run;\r BEGIN NEW(u); u.next := u; u.prev := u; B.head := u; B.len := 0\r END OpenBuf;\r\r PROCEDURE Copy* (SB, DB: Buffer);\r VAR u, v, vn: Run;\r BEGIN u := SB.head.next; v := DB.head.prev;\r WHILE u # SB.head DO\r IF u IS Piece THEN vn := ClonePiece(u(Piece)) ELSE vn := CloneElem(u(Elem)) END;\r v.next := vn; vn.prev := v; v := vn; u := u.next\r END;\r v.next := DB.head; DB.head.prev := v;\r INC(DB.len, SB.len)\r END Copy;\r\r PROCEDURE Recall* (VAR B: Buffer);\r BEGIN B := del; del := NIL\r END Recall;\r\r\r (** Texts **)\r\r PROCEDURE Save* (T: Text; beg, end: LONGINT; B: Buffer);\r VAR u, v, w, wn: Run; uo, ud, vo, vd: LONGINT;\r BEGIN Find(T, beg, u, uo, ud); Find(T, end, v, vo, vd);\r w := B.head.prev;\r WHILE u # v DO\r IF u IS Piece THEN wn := ClonePiece(u(Piece)); DEC(wn.len, ud); INC(wn(Piece).org, ud)\r ELSE wn := CloneElem(u(Elem))\r END;\r w.next := wn; wn.prev := w; w := wn; u := u.next; ud := 0\r END;\r IF vd > 0 THEN (*v IS Piece*) wn := ClonePiece(v(Piece)); wn.len := vd - ud; INC(wn(Piece).org, ud);\r w.next := wn; wn.prev := w; w := wn\r END;\r w.next := B.head; B.head.prev := w;\r INC(B.len, end - beg)\r END Save;\r\r PROCEDURE Insert* (T: Text; pos: LONGINT; B: Buffer);\r VAR u, un, v: Run; p, q: Piece; uo, ud, len: LONGINT;\r BEGIN Find(T, pos, u, uo, ud); Split(ud, u, un);\r len := B.len; v := B.head.next;\r Merge(T, u, v); Splice(un, v, B.head.prev, T);\r INC(T.len, len); B.head.next := B.head; B.head.prev := B.head; B.len := 0;\r T.notify(T, insert, pos, pos + len)\r END Insert;\r\r PROCEDURE Append* (T: Text; B: Buffer);\r VAR v: Run; pos, len: LONGINT;\r BEGIN pos := T.len; len := B.len; v := B.head.next;\r Merge(T, T.head.prev, v); Splice(T.head, v, B.head.prev, T);\r INC(T.len, len); B.head.next := B.head; B.head.prev := B.head; B.len := 0;\r T.notify(T, insert, pos, pos + len)\r END Append;\r\r PROCEDURE Delete* (T: Text; beg, end: LONGINT);\r VAR c, u, un, v, vn: Run; co, uo, ud, vo, vd: LONGINT;\r BEGIN\r Find(T, beg, u, uo, ud); Split(ud, u, un); c := T.cache; co := T.corg;\r Find(T, end, v, vo, vd); Split(vd, v, vn); T.cache := c; T.corg := co;\r NEW(del); OpenBuf(del); del.len := end - beg;\r Splice(del.head, un, v, NIL);\r Merge(T, u, vn); u.next := vn; vn.prev := u;\r DEC(T.len, end - beg);\r T.notify(T, delete, beg, end)\r END Delete;\r\r PROCEDURE ChangeLooks* (T: Text; beg, end: LONGINT; sel: SET; fnt: FontsFont; col, voff: SHORTINT);\r VAR c, u, un, v, vn: Run; co, uo, ud, vo, vd: LONGINT;\r BEGIN Find(T, beg, u, uo, ud); Split(ud, u, un); c := T.cache; co := T.corg;\r Find(T, end, v, vo, vd); Split(vd, v, vn); T.cache := c; T.corg := co;\r WHILE un # vn DO\r IF (0 IN sel) & (fnt # NIL) THEN un.fnt := fnt END;\r IF 1 IN sel THEN un.col := col END;\r IF 2 IN sel THEN un.voff := voff END;\r Merge(T, u, un);\r IF u.next = un THEN u := un; un := un.next ELSE u.next := un; un.prev := u END\r END;\r Merge(T, u, un); u.next := un; un.prev := u;\r T.notify(T, replace, beg, end)\r END ChangeLooks;\r\r\r (** Readers **)\r\r PROCEDURE OpenReader* (VAR R: Reader; T: Text; pos: LONGINT);\r VAR u: Run;\r BEGIN\r IF pos >= T.len THEN pos := T.len END;\r Find(T, pos, u, R.org, R.off); R.run := u; R.eot := FALSE;\r IF u IS Piece THEN\r Files.Set(R.rider, u(Piece).file, u(Piece).org + R.off)\r END\r END OpenReader;\r\r PROCEDURE Read* (VAR R: Reader; VAR ch: CHAR);\r VAR u: Run;\r BEGIN u := R.run; R.fnt := u.fnt; R.col := u.col; R.voff := u.voff; INC(R.off);\r IF u IS Piece THEN Files.Read(R.rider, ch); R.elem := NIL\r ELSIF u IS Elem THEN ch := ElemChar; R.elem := u(Elem)\r ELSE ch := 0X; R.elem := NIL; R.eot := TRUE\r END;\r IF R.off = u.len THEN INC(R.org, u.len); u := u.next;\r IF u IS Piece THEN\r WITH u: Piece DO Files.Set(R.rider, u.file, u.org) END\r END;\r R.run := u; R.off := 0\r END\r END Read;\r\r PROCEDURE ReadElem* (VAR R: Reader);\r VAR u, un: Run;\r BEGIN u := R.run;\r WHILE u IS Piece DO INC(R.org, u.len); u := u.next END;\r IF u IS Elem THEN un := u.next; R.run := un; INC(R.org); R.off := 0;\r R.fnt := u.fnt; R.col := u.col; R.voff := u.voff; R.elem := u(Elem);\r IF un IS Piece THEN\r WITH un: Piece DO Files.Set(R.rider, un.file, un.org) END\r END\r ELSE R.eot := TRUE; R.elem := NIL\r END\r END ReadElem;\r\r PROCEDURE ReadPrevElem* (VAR R: Reader);\r VAR u: Run;\r BEGIN u := R.run.prev;\r WHILE u IS Piece DO DEC(R.org, u.len); u := u.prev END;\r IF u IS Elem THEN R.run := u; DEC(R.org); R.off := 0;\r R.fnt := u.fnt; R.col := u.col; R.voff := u.voff; R.elem := u(Elem)\r ELSE R.eot := TRUE; R.elem := NIL\r END\r END ReadPrevElem;\r\r PROCEDURE Pos* (VAR R: Reader): LONGINT;\r BEGIN RETURN R.org + R.off\r END Pos;\r\r\r (** Scanners --------------- NW --------------- **)\r\r PROCEDURE OpenScanner* (VAR S: Scanner; T: Text; pos: LONGINT);\r BEGIN OpenReader(S, T, pos); S.line := 0; S.nextCh := " "\r END OpenScanner;\r\r (*IEEE floating point formats:\r x = 2^(e-127) * 1.m bit 0: sign, bits 1- 8: e, bits 9-31: m\r x = 2^(e-1023) * 1.m bit 0: sign, bits 1-11: e, bits 12-63: m *)\r\r PROCEDURE Scan* (VAR S: Scanner);\r CONST maxD = 32;\r VAR ch, term: CHAR;\r neg, negE, hex: BOOLEAN;\r i, j, h: SHORTINT;\r e: INTEGER; k: LONGINT;\r x, f: REAL; y, g: LONGREAL;\r d: ARRAY maxD OF CHAR;\r\r PROCEDURE ReadScaleFactor;\r BEGIN Read(S, ch);\r IF ch = "-" THEN negE := TRUE; Read(S, ch)\r ELSE negE := FALSE;\r IF ch = "+" THEN Read(S, ch) END\r END;\r WHILE ("0" <= ch) & (ch <= "9") DO\r e := e*10 + ORD(ch) - 30H; Read(S, ch)\r END\r END ReadScaleFactor;\r\r BEGIN ch := S.nextCh; i := 0;\r LOOP\r IF ch = CR THEN INC(S.line)\r ELSIF (ch # " ") & (ch # TAB) THEN EXIT\r END ;\r Read(S, ch)\r END;\r IF ("A" <= CAP(ch)) & (CAP(ch) <= "Z") THEN (*name*)\r REPEAT S.s[i] := ch; INC(i); Read(S, ch)\r UNTIL (CAP(ch) > "Z")\r OR ("A" > CAP(ch)) & (ch > "9")\r OR ("0" > ch) & (ch # ".")\r OR (i = 31);\r S.s[i] := 0X; S.len := i; S.class := 1\r ELSIF ch = 22X THEN (*literal string*)\r Read(S, ch);\r WHILE (ch # 22X) & (ch >= " ") & (i # 31) DO\r S.s[i] := ch; INC(i); Read(S, ch)\r END;\r S.s[i] := 0X; S.len := i; Read(S, ch); S.class := 2\r ELSE\r IF ch = "-" THEN neg := TRUE; Read(S, ch) ELSE neg := FALSE END ;\r IF ("0" <= ch) & (ch <= "9") THEN (*number*)\r hex := FALSE; j := 0;\r LOOP d[i] := ch; INC(i); Read(S, ch);\r IF ch < "0" THEN EXIT END;\r IF "9" < ch THEN\r IF ("A" <= ch) & (ch <= "F") THEN hex := TRUE; ch := CHR(ORD(ch)-7)\r ELSIF ("a" <= ch) & (ch <= "f") THEN hex := TRUE; ch := CHR(ORD(ch)-27H)\r ELSE EXIT\r END\r END\r END;\r IF ch = "H" THEN (*hex number*)\r Read(S, ch); S.class := 3;\r IF i-j > 8 THEN j := i-8 END ;\r k := ORD(d[j]) - 30H; INC(j);\r IF (i-j = 7) & (k >= 8) THEN DEC(k, 16) END ;\r WHILE j < i DO k := k*10H + (ORD(d[j]) - 30H); INC(j) END ;\r IF neg THEN S.i := -k ELSE S.i := k END \r ELSIF ch = "." THEN (*read real*)\r Read(S, ch); h := i;\r WHILE ("0" <= ch) & (ch <= "9") DO d[i] := ch; INC(i); Read(S, ch) END ;\r IF ch = "D" THEN\r e := 0; y := 0; g := 1;\r REPEAT y := y*10 + (ORD(d[j]) - 30H); INC(j) UNTIL j = h;\r WHILE j < i DO g := g/10; y := (ORD(d[j]) - 30H)*g + y; INC(j) END ;\r ReadScaleFactor;\r IF negE THEN\r IF e <= 308 THEN y := y / Reals.TenL(e) ELSE y := 0 END\r ELSIF e > 0 THEN\r IF e <= 308 THEN y := Reals.TenL(e) * y ELSE HALT(40) END \r END ;\r IF neg THEN y := -y END ;\r S.class := 5; S.y := y\r ELSE e := 0; x := 0; f := 1;\r REPEAT x := x*10 + (ORD(d[j]) - 30H); INC(j) UNTIL j = h;\r WHILE j < i DO f := f/10; x := (ORD(d[j])-30H)*f + x; INC(j) END;\r IF ch = "E" THEN ReadScaleFactor END ;\r IF negE THEN\r IF e <= 38 THEN x := x / Reals.Ten(e) ELSE x := 0 END\r ELSIF e > 0 THEN\r IF e <= 38 THEN x := Reals.Ten(e) * x ELSE HALT(40) END\r END ;\r IF neg THEN x := -x END ;\r S.class := 4; S.x := x\r END ;\r IF hex THEN S.class := 0 END\r ELSE (*decimal integer*)\r S.class := 3; k := 0;\r REPEAT k := k*10 + (ORD(d[j]) - 30H); INC(j) UNTIL j = i;\r IF neg THEN S.i := -k ELSE S.i := k END;\r IF hex THEN S.class := 0 ELSE S.class := 3 END\r END\r ELSE S.class := 6;\r IF neg THEN S.c := "-" ELSE S.c := ch; Read(S, ch) END\r END\r END;\r S.nextCh := ch\r END Scan;\r\r\r (** Writers **)\r\r PROCEDURE OpenWriter* (VAR W: Writer);\r BEGIN NEW(W.buf); OpenBuf(W.buf);\r (* W.fnt := Fonts.Default; W.col := Display.white; *)\r W.fnt := NIL; W.col := 0; W.voff := 0;\r W.file := Files.New(""); Files.Set(W.rider, W.file, 0)\r END OpenWriter;\r\r PROCEDURE SetFont* (VAR W: Writer; fnt: FontsFont);\r BEGIN W.fnt := fnt\r END SetFont;\r\r PROCEDURE SetColor* (VAR W: Writer; col: SHORTINT);\r BEGIN W.col := col\r END SetColor;\r\r PROCEDURE SetOffset* (VAR W: Writer; voff: SHORTINT);\r BEGIN W.voff := voff\r END SetOffset;\r\r\r PROCEDURE Write* (VAR W: Writer; ch: CHAR);\r VAR u, un: Run; p: Piece;\r BEGIN Files.Write(W.rider, ch); INC(W.buf.len); un := W.buf.head; u := un.prev;\r IF (u IS Piece) & (u(Piece).file = W.file) & (u.fnt.name = W.fnt.name) & (u.col = W.col) & (u.voff = W.voff) THEN\r INC(u.len)\r ELSE NEW(p); u.next := p; p.prev := u; p.next := un; un.prev := p;\r p.len := 1; p.fnt := W.fnt; p.col := W.col; p.voff := W.voff;\r p.file := W.file; p.org := Files.Length(W.file) - 1\r END\r END Write;\r\r PROCEDURE WriteElem* (VAR W: Writer; e: Elem);\r VAR u, un: Run;\r BEGIN\r IF e.base # NIL THEN HALT(99) END;\r INC(W.buf.len); e.len := 1; e.fnt := W.fnt; e.col := W.col; e.voff := W.voff;\r un := W.buf.head; u := un.prev; u.next := e; e.prev := u; e.next := un; un.prev := e\r END WriteElem;\r\r PROCEDURE WriteLn* (VAR W: Writer);\r BEGIN Write(W, CR)\r END WriteLn;\r\r PROCEDURE WriteString* (VAR W: Writer; s: ARRAY OF CHAR);\r VAR i: INTEGER;\r BEGIN i := 0;\r WHILE s[i] >= " " DO Write(W, s[i]); INC(i) END\r END WriteString;\r\r PROCEDURE WriteInt* (VAR W: Writer; x, n: LONGINT);\r VAR i: INTEGER; x0: LONGINT;\r a: ARRAY 11 OF CHAR;\r BEGIN i := 0;\r IF x < 0 THEN\r IF x = MIN(LONGINT) THEN WriteString(W, " -2147483648"); RETURN\r ELSE DEC(n); x0 := -x\r END\r ELSE x0 := x\r END;\r REPEAT\r a[i] := CHR(x0 MOD 10 + 30H); x0 := x0 DIV 10; INC(i)\r UNTIL x0 = 0;\r WHILE n > i DO Write(W, " "); DEC(n) END;\r IF x < 0 THEN Write(W, "-") END;\r REPEAT DEC(i); Write(W, a[i]) UNTIL i = 0\r END WriteInt;\r\r PROCEDURE WriteHex* (VAR W: Writer; x: LONGINT);\r VAR i: INTEGER; y: LONGINT;\r a: ARRAY 10 OF CHAR;\r BEGIN i := 0; Write(W, " ");\r REPEAT y := x MOD 10H;\r IF y < 10 THEN a[i] := CHR(y + 30H) ELSE a[i] := CHR(y + 37H) END;\r x := x DIV 10H; INC(i)\r UNTIL i = 8;\r REPEAT DEC(i); Write(W, a[i]) UNTIL i = 0\r END WriteHex;\r\r PROCEDURE WriteReal* (VAR W: Writer; x: REAL; n: INTEGER);\r VAR e: INTEGER; x0: REAL;\r d: ARRAY maxD OF CHAR;\r BEGIN e := Reals.Expo(x);\r IF e = 0 THEN\r WriteString(W, " 0");\r REPEAT Write(W, " "); DEC(n) UNTIL n <= 3\r ELSIF e = 255 THEN\r WriteString(W, " NaN");\r WHILE n > 4 DO Write(W, " "); DEC(n) END\r ELSE\r IF n <= 9 THEN n := 3 ELSE DEC(n, 6) END;\r REPEAT Write(W, " "); DEC(n) UNTIL n <= 8;\r (*there are 2 < n <= 8 digits to be written*)\r IF x < 0.0 THEN Write(W, "-"); x := -x ELSE Write(W, " ") END;\r e := (e - 127) * 77 DIV 256;\r IF e >= 0 THEN x := x / Reals.Ten(e) ELSE x := Reals.Ten(-e) * x END;\r IF x >= 10.0 THEN x := 0.1*x; INC(e) END;\r x0 := Reals.Ten(n-1); x := x0*x + 0.5;\r IF x >= 10.0*x0 THEN x := x*0.1; INC(e) END;\r Reals.Convert(x, n, d);\r DEC(n); Write(W, d[n]); Write(W, ".");\r REPEAT DEC(n); Write(W, d[n]) UNTIL n = 0;\r Write(W, "E");\r IF e < 0 THEN Write(W, "-"); e := -e ELSE Write(W, "+") END;\r Write(W, CHR(e DIV 10 + 30H)); Write(W, CHR(e MOD 10 + 30H))\r END\r END WriteReal;\r\r PROCEDURE WriteRealFix* (VAR W: Writer; x: REAL; n, k: INTEGER);\r VAR e, i: INTEGER; sign: CHAR; x0: REAL;\r d: ARRAY maxD OF CHAR;\r\r PROCEDURE seq(ch: CHAR; n: INTEGER);\r BEGIN WHILE n > 0 DO Write(W, ch); DEC(n) END\r END seq;\r\r PROCEDURE dig(n: INTEGER);\r BEGIN\r WHILE n > 0 DO\r DEC(i); Write(W, d[i]); DEC(n)\r END\r END dig;\r\r BEGIN e := Reals.Expo(x);\r IF k < 0 THEN k := 0 END;\r IF e = 0 THEN seq(" ", n-k-2); Write(W, "0"); seq(" ", k+1)\r ELSIF e = 255 THEN WriteString(W, " NaN"); seq(" ", n-4)\r ELSE e := (e - 127) * 77 DIV 256;\r IF x < 0 THEN sign := "-"; x := -x ELSE sign := " " END;\r IF e >= 0 THEN (*x >= 1.0, 77/256 = log 2*) x := x/Reals.Ten(e)\r ELSE (*x < 1.0*) x := Reals.Ten(-e) * x\r END;\r IF x >= 10.0 THEN x := 0.1*x; INC(e) END;\r (* 1 <= x < 10 *)\r IF k+e >= maxD-1 THEN k := maxD-1-e\r ELSIF k+e < 0 THEN k := -e; x := 0.0\r END;\r x0 := Reals.Ten(k+e); x := x0*x + 0.5;\r IF x >= 10.0*x0 THEN INC(e) END;\r (*e = no. of digits before decimal point*)\r INC(e); i := k+e; Reals.Convert(x, i, d);\r IF e > 0 THEN\r seq(" ", n-e-k-2); Write(W, sign); dig(e);\r Write(W, "."); dig(k)\r ELSE seq(" ", n-k-3);\r Write(W, sign); Write(W, "0"); Write(W, ".");\r seq("0", -e); dig(k+e)\r END\r END\r END WriteRealFix;\r\r PROCEDURE WriteRealHex* (VAR W: Writer; x: REAL);\r VAR i: INTEGER;\r d: ARRAY 8 OF CHAR;\r BEGIN Reals.ConvertH(x, d); i := 0;\r REPEAT Write(W, d[i]); INC(i) UNTIL i = 8\r END WriteRealHex;\r\r PROCEDURE WriteLongReal* (VAR W: Writer; x: LONGREAL; n: INTEGER);\r CONST maxD = 16;\r VAR e: INTEGER; x0: LONGREAL;\r d: ARRAY maxD OF CHAR;\r BEGIN e := Reals.ExpoL(x);\r IF e = 0 THEN\r WriteString(W, " 0");\r REPEAT Write(W, " "); DEC(n) UNTIL n <= 3\r ELSIF e = 2047 THEN\r WriteString(W, " NaN");\r WHILE n > 4 DO Write(W, " "); DEC(n) END\r ELSE\r IF n <= 10 THEN n := 3 ELSE DEC(n, 7) END;\r REPEAT Write(W, " "); DEC(n) UNTIL n <= maxD;\r (*there are 2 <= n <= maxD digits to be written*)\r IF x < 0 THEN Write(W, "-"); x := -x ELSE Write(W, " ") END;\r e := SHORT(LONG(e - 1023) * 77 DIV 256);\r IF e >= 0 THEN x := x / Reals.TenL(e) ELSE x := Reals.TenL(-e) * x END ;\r IF x >= 10.0D0 THEN x := 0.1D0 * x; INC(e) END ;\r x0 := Reals.TenL(n-1); x := x0*x + 0.5D0;\r IF x >= 10.0D0*x0 THEN x := 0.1D0 * x; INC(e) END ;\r Reals.ConvertL(x, n, d);\r DEC(n); Write(W, d[n]); Write(W, ".");\r REPEAT DEC(n); Write(W, d[n]) UNTIL n = 0;\r Write(W, "D");\r IF e < 0 THEN Write(W, "-"); e := -e ELSE Write(W, "+") END;\r Write(W, CHR(e DIV 100 + 30H)); e := e MOD 100;\r Write(W, CHR(e DIV 10 + 30H));\r Write(W, CHR(e MOD 10 + 30H))\r END\r END WriteLongReal;\r\r PROCEDURE WriteLongRealHex* (VAR W: Writer; x: LONGREAL);\r VAR i: INTEGER;\r d: ARRAY 16 OF CHAR;\r BEGIN Reals.ConvertHL(x, d); i := 0;\r REPEAT Write(W, d[i]); INC(i) UNTIL i = 16\r END WriteLongRealHex;\r\r PROCEDURE WriteDate* (VAR W: Writer; t, d: LONGINT);\r\r PROCEDURE WritePair(ch: CHAR; x: LONGINT);\r BEGIN Write(W, ch);\r Write(W, CHR(x DIV 10 + 30H)); Write(W, CHR(x MOD 10 + 30H))\r END WritePair;\r\r BEGIN\r WritePair(" ", d MOD 32); WritePair(".", d DIV 32 MOD 16); WritePair(".", d DIV 512 MOD 128);\r WritePair(" ", t DIV 4096 MOD 32); WritePair(":", t DIV 64 MOD 64); WritePair(":", t MOD 64)\r END WriteDate;\r\r\r (** Text Filing **)\r\r PROCEDURE Load0 (VAR r: Files.Rider; T: Text);\r VAR u, un: Run; p: Piece; e: Elem;\r org, pos, hlen, plen: LONGINT; ecnt, fno, fcnt, col, voff: SHORTINT;\r f: Files.File;\r msg: FileMsg;\r mods, procs: ARRAY 64, 32 OF CHAR;\r name: ARRAY 32 OF CHAR;\r fnts: ARRAY 32 OF FontsFont;\r\r PROCEDURE LoadElem (VAR r: Files.Rider; pos, span: LONGINT; VAR e: Elem);\r VAR (* M: Modules.Module; Cmd: Modules.Command; *) a: Alien;\r org, ew, eh: LONGINT; eno: SHORTINT;\r BEGIN new := NIL;\r Files.ReadLInt(r, ew); Files.ReadLInt(r, eh); Files.Read(r, eno);\r IF eno > ecnt THEN ecnt := eno; Files.ReadString(r, mods[eno]); Files.ReadString(r, procs[eno]) END;\r org := Files.Pos(r); (* M := Modules.ThisMod(mods[eno]);\r IF M # NIL THEN Cmd := Modules.ThisCommand(M, procs[eno]);\r IF Cmd # NIL THEN Cmd END\r END; *)\r e := new;\r IF e # NIL THEN e.W := ew; e.H := eh; e.base := T;\r msg.pos := pos; e.handle(e, msg);\r IF Files.Pos(r) # org + span THEN e := NIL END\r END;\r IF e = NIL THEN Files.Set(r, f, org + span);\r NEW(a); a.W := ew; a.H := eh; a.handle := HandleAlien; a.base := T;\r a.file := f; a.org := org; a.span := span;\r COPY(mods[eno], a.mod); COPY(procs[eno], a.proc);\r e := a\r END\r END LoadElem;\r\r BEGIN pos := Files.Pos(r); f := Files.Base(r);\r NEW(u); u.len := MAX(LONGINT); (*u.fnt := Fonts.Default;*)u.fnt := NIL; (*u.col := Display.white;*) u.col := 0;\r T.head := u; ecnt := 0; fcnt := 0;\r msg.id := load; msg.r := r;\r Files.ReadLInt(msg.r, hlen); (*!!!org := pos + hlen;*) org := pos -2 + hlen; pos := org; Files.Read(msg.r, fno);\r WHILE fno # 0 DO\r IF fno > fcnt THEN fcnt := fno; Files.ReadString(msg.r, name); (* fnts[fno] := Fonts.This(name) *) fnts[fno] := NIL END;\r Files.Read(msg.r, col); Files.Read(msg.r, voff); Files.ReadLInt(msg.r, plen);\r IF plen > 0 THEN NEW(p); p.file := f; p.org := pos; un := p; un.len := plen\r ELSE LoadElem(msg.r, pos - org, -plen, e); un := e; un.len := 1\r END;\r un.fnt := fnts[fno]; un.col := col; un.voff := voff;\r INC(pos, un.len); u.next := un; un.prev := u; u := un; Files.Read(msg.r, fno)\r END;\r u.next := T.head; T.head.prev := u; T.cache := T.head; T.corg := 0;\r Files.ReadLInt(msg.r, T.len); Files.Set(r, f, Files.Pos(msg.r) + T.len)\r END Load0;\r \r PROCEDURE Load* (VAR r: Files.Rider; T: Text);\r CONST oldTag = -4095;\r VAR tag: INTEGER;\r BEGIN\r (* for compatibility inner text tags are checked and skipped; remove this in a later version *)\r Files.ReadInt(r, tag); IF tag # oldTag THEN Files.Set(r, Files.Base(r), Files.Pos(r)-2) END;\r Load0(r, T)\r END Load;\r\r PROCEDURE Open* (T: Text; name: ARRAY OF CHAR);\r VAR f: Files.File; r: Files.Rider; u: Run; p: Piece; tag, version: CHAR;\r BEGIN f := Files.Old(name);\r IF f = NIL THEN f := Files.New("") END;\r Files.Set(r, f, 0); Files.Read(r, tag); Files.Read(r, version);\r IF (tag = textTag) OR (tag = 01X) & (version = textTag) THEN Load0(r, T)\r ELSE NEW(u); u.len := MAX(LONGINT); u.fnt := NIL; (* u.col := Display.white; *) u.col := 0;\r T.len := Files.Length(f);\r IF T.len > 0 THEN NEW(p); p.len := T.len; (* p.fnt := Fonts.Default; *) p.fnt := NIL;\r (* p.col := Display.white; *) p.col := 0; p.voff := 0; p.file := f; p.org := 0;\r u.next := p; u.prev := p; p.next := u; p.prev := u\r ELSE u.next := u; u.prev := u\r END;\r T.head := u; T.cache := T.head; T.corg := 0\r END\r END Open;\r\r PROCEDURE Store* (VAR r: Files.Rider; T: Text);\r VAR r1: Files.Rider; u, un: Run; e: Elem; org, pos, delta, hlen, rlen: LONGINT; ecnt, fno, fcnt: SHORTINT;\r msg: FileMsg; iden: IdentifyMsg;\r mods, procs: ARRAY 64, 32 OF CHAR;\r fnts: ARRAY 32 OF FontsFont;\r block: ARRAY 1024 OF CHAR;\r\r PROCEDURE StoreElem (VAR r: Files.Rider; pos: LONGINT; e: Elem);\r VAR r1: Files.Rider; org, span: LONGINT; eno: SHORTINT;\r BEGIN COPY(iden.mod, mods[ecnt]); COPY(iden.proc, procs[ecnt]); eno := 1;\r WHILE (mods[eno] # iden.mod) OR (procs[eno] # iden.proc) DO INC(eno) END;\r Files.Set(r1, Files.Base(r), Files.Pos(r));\r Files.WriteLInt(r, 0); Files.WriteLInt(r, 0); Files.WriteLInt(r, 0); (*fixup slot*)\r Files.Write(r, eno);\r IF eno = ecnt THEN INC(ecnt); Files.WriteString(r, iden.mod); Files.WriteString(r, iden.proc) END;\r msg.pos := pos; org := Files.Pos(r); e.handle(e, msg); span := Files.Pos(r) - org;\r Files.WriteLInt(r1, -span); Files.WriteLInt(r1, e.W); Files.WriteLInt(r1, e.H) (*fixup*)\r END StoreElem;\r\r BEGIN\r org := Files.Pos(r); msg.id := store; msg.r := r; Files.WriteLInt(msg.r, 0); (*fixup slot*)\r u := T.head.next; pos := 0; delta := 0; fcnt := 1; ecnt := 1;\r WHILE u # T.head DO\r IF u IS Elem THEN iden.mod[0] := 0X; u(Elem).handle(u(Elem), iden) ELSE iden.mod[0] := 1X END;\r IF iden.mod[0] # 0X THEN\r fnts[fcnt] := u.fnt; fno := 1;\r WHILE fnts[fno].name # u.fnt.name DO INC(fno) END;\r Files.Write(msg.r, fno);\r IF fno = fcnt THEN INC(fcnt); Files.WriteString(msg.r, u.fnt.name) END;\r Files.Write(msg.r, u.col); Files.Write(msg.r, u.voff)\r END;\r IF u IS Piece THEN rlen := u.len; un := u.next;\r WHILE (un IS Piece) & (un.fnt = u.fnt) & (un.col = u.col) & (un.voff = u.voff) DO\r INC(rlen, un.len); un := un.next\r END;\r Files.WriteLInt(msg.r, rlen); INC(pos, rlen); u := un\r ELSIF iden.mod[0] # 0X THEN StoreElem(msg.r, pos, u(Elem)); INC(pos); u := u.next\r ELSE INC(delta); u := u.next\r END\r END;\r Files.Write(msg.r, 0); Files.WriteLInt(msg.r, T.len - delta);\r (*!!!hlen := Files.Pos(msg.r) - org;*) hlen := Files.Pos(msg.r) - org + 2;\r Files.Set(r1, Files.Base(msg.r), org); Files.WriteLInt(r1, hlen); (*fixup*)\r u := T.head.next;\r WHILE u # T.head DO\r IF u IS Piece THEN\r WITH u: Piece DO Files.Set(r1, u.file, u.org); delta := u.len;\r WHILE delta > LEN(block) DO Files.ReadBytes(r1, block, LEN(block));\r Files.WriteBytes(msg.r, block, LEN(block)); DEC(delta, LEN(block))\r END;\r Files.ReadBytes(r1, block, delta); Files.WriteBytes(msg.r, block, delta)\r END\r ELSE iden.mod[0] := 0X; u(Elem).handle(u(Elem), iden);\r IF iden.mod[0] # 0X THEN Files.Write(msg.r, ElemChar) END\r END;\r u := u.next\r END;\r r := msg.r;\r END Store;\r \r PROCEDURE Close* (T: Text; name: ARRAY OF CHAR);\r VAR f: Files.File; r: Files.Rider; i, res: INTEGER; bak: ARRAY 64 OF CHAR;\r BEGIN\r f := Files.New(name); Files.Set(r, f, 0); Files.Write(r, textTag); Files.Write(r, version); Store(r, T);\r i := 0; WHILE name[i] # 0X DO INC(i) END;\r COPY(name, bak); bak[i] := "."; bak[i+1] := "B"; bak[i+2] := "a"; bak[i+3] := "k"; bak[i+4] := 0X;\r Files.Rename(name, bak, res); Files.Register(f)\r END Close;\r\rBEGIN del := NIL\rEND Texts.\r
\ No newline at end of file