411 REFERENCE · Wikibooks
Oberon/A2/Oberon.Sort.Mod
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
(* ETH Oberon, Copyright 2001 ETH Zuerich Institut fuer Computersysteme, ETH Zentrum, CH-8092 Zuerich.
Refer to the "General ETH Oberon System Source License" contract available at: http://www.oberon.ethz.ch/ *)
MODULE Sort IN Oberon; (** portable *) (*SHML 13.11.91 / mf 13.10.94 / tk adapted for System3 1.6.95*)
IMPORT Oberon, Texts, Objects, Gadgets, TextGadgets, Desktops, Documents,
TextFrames, Viewers, MenuViewers;
CONST NofLines = 4000;
#009A00 suffix = ".Srt";
Menu = "System.Close System.Copy System.Grow ET.Move ET.Search ET.Replace ET.StoreAscii ";
TYPE
#009A00 Integer = LONGINT; (* LONGINT in S3 context, INTEGER in V5 context. *)
String = ARRAY 256 OF CHAR;
Array = POINTER TO ARRAY NofLines OF String;
VAR W: Texts.Writer;
suffixArray: ARRAY 32 OF CHAR;
#009A00 (* Add suffix to name without overrunning the new array. *)
#009A00 PROCEDURE AddSuffix(VAR name, suffix, new: ARRAY OF CHAR);
#009A00 VAR i, si: Integer;
#009A00 BEGIN
#009A00 (* Locate upper bound of the first character of suffix. *)
#009A00 i := 0; si := LEN(new) - 1;
#009A00 WHILE (suffix[i] # 0X) & (0 < si) DO INC(i); DEC(si) END;
#009A00 IF suffix[i] # 0X THEN
#009A00 Texts.WriteString(W, "Long suffix truncated to fit in new array."); Texts.WriteLn(W);
#009A00 Texts.Append(Oberon.Log, W.buf)
#009A00 END;
#009A00 (* Retain or copy as much of the original name as suffix allows. *)
#009A00 i := 0; IF name = new THEN
#009A00 WHILE (i < si) & (name[i] # 0X) DO INC(i) END
#009A00 ELSE
#009A00 WHILE (i < si) & (name[i] # 0X) DO new[i] := name[i]; INC(i) END
#009A00 END;
#009A00 IF name[i] # 0X THEN
#009A00 Texts.WriteString(W, "Name truncated to fit suffix."); Texts.WriteLn(W);
#009A00 Texts.Append(Oberon.Log, W.buf)
#009A00 END;
#009A00 (* Add suffix. *)
#009A00 si := 0;
#009A00 WHILE (suffix[si] # 0X) & (i +1 < LEN(new)) DO
#009A00 new[i] := suffix[si]; INC(i); INC(si)
#009A00 END;
#009A00 new[i] := 0X
#009A00 END AddSuffix;
PROCEDURE WriteMsg(n: #FF0000 (* LONGINT *) #009A00 Integer; str: ARRAY OF CHAR);
(* Write number n followed by str followed by a newline to the Log *)
BEGIN
Texts.WriteInt(W, n, 0);
IF n=1 THEN Texts.WriteString(W, " line ")
ELSE Texts.WriteString(W, " lines ")
END;
Texts.WriteString(W, str); Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf)
END WriteMsg;
(* Sort n elements of array in ascending order, HeapSort *)
PROCEDURE HSortArray(array: Array; n: #FF0000 (* INTEGER *) #009A00 Integer);
VAR left, right: #FF0000 (* INTEGER *) #009A00 Integer; a: String;
PROCEDURE Sift(left, right: #FF0000 (* INTEGER *) #009A00 Integer);
VAR i, j: #FF0000 (* INTEGER *) #009A00 Integer; a: String;
BEGIN i:=left; j:=2*left; a:=array[left];
IF (j < right) & (array[j] < array[j+1]) THEN INC(j) END;
WHILE (j <= right) & (a < array[j]) DO
array[i]:=array[j]; i:=j; j:=2*j;
IF (j < right) & (array[j] < array[j+1]) THEN INC(j) END
END;
array[i]:=a
END Sift;
BEGIN left:=n DIV 2+1; right:=n-1;
WHILE left > 0 DO DEC(left); Sift(left, right) END;
WHILE right > 0 DO
a:=array[0]; array[0]:=array[right]; array[right]:=a;
DEC(right); Sift(left, right)
END
END HSortArray;
PROCEDURE FillArray(array: Array; VAR n: #FF0000 (* INTEGER *) #009A00 Integer; text: Texts.Text; emptyLines: BOOLEAN);
(* Fill array with lines from text (including empty lines if requested); return number of lines in n *)
VAR j: #FF0000 (* INTEGER *) #009A00 Integer; len, pos: #FF0000 (* LONGINT *) #009A00 Integer; R: Texts.Reader; ch: CHAR; white: BOOLEAN;
BEGIN
len:=text.len;
#FF0000 (* IF len=0 THEN RETURN END; *)
#009A00 IF len#0 THEN
Texts.OpenReader(R, text, len-1); Texts.Read(R, ch);
IF ch # 0DX THEN Texts.Write(W, 0DX); Texts.Append(text, W.buf) END; (* terminate text with a CR *)
Texts.OpenReader(R, text, 0);
n:=0; pos:=0; len:=text.len;
IF emptyLines THEN (* include empty lines *)
REPEAT j:=0;
REPEAT Texts.Read(R, ch); array[n, j]:=ch; INC(j) UNTIL ch=0DX;
array[n, j]:=0X; INC(pos, #FF0000 (* LONG(j) *) #009A00 j);
INC(n)
UNTIL pos=len
ELSE (* exclude empty lines *)
REPEAT j:=0; white:=TRUE;
REPEAT Texts.Read(R, ch);
IF white & (ch > " ") THEN white:=FALSE END;
array[n, j]:=ch; INC(j)
UNTIL ch=0DX;
array[n, j]:=0X; INC(pos, #FF0000 (* LONG(j) *) #009A00 j);
IF ~white THEN INC(n) END (* keep line if not only white-space *)
UNTIL pos=len
END
#009A00 END
END FillArray;
PROCEDURE FillText(text: Texts.Text; array: Array; n: #FF0000 (* INTEGER *) #009A00 Integer; reverse, unique: BOOLEAN);
(* Fill text with n lines from array; in reverse order if requested *)
VAR i, j, delta: #FF0000 (* INTEGER *) #009A00 Integer; ch: CHAR; last: String;
BEGIN
IF reverse THEN i:=n-1; delta:=-1 ELSE i:=0; delta:=1 END;
IF unique THEN last[0]:=0X;
WHILE n > 0 DO
IF array[i] # last THEN last:=array[i];
ch:=last[0]; j:=0;
WHILE ch # 0X DO Texts.Write(W, ch); INC(j); ch:=last[j] END;
END;
INC(i, delta); DEC(n)
END
ELSE
WHILE n > 0 DO ch:=array[i, 0]; j:=0;
WHILE ch # 0X DO Texts.Write(W, ch); INC(j); ch:=array[i, j] END;
INC(i, delta); DEC(n)
END
END;
Texts.Append(text, W.buf)
END FillText;
PROCEDURE Sort*; (** ("^" | "*" | <name>) ["/" {c}] where c IN {"r", "e", "u"} **)
(** Sort a marked viewer, a selection, or a file. Option /r means in reverse order; /e keep empty lines **)
VAR
S#009A00 , nameS: Texts.Scanner;
n: Integer;
text, sel: Texts.Text;
beg, end, time: #FF0000 (* LONGINT *) #009A00 Integer;
buf: Texts.Buffer;
array: Array;
reverse, empty, unique: BOOLEAN;
TF: TextGadgets.Frame;
D: Objects.Object;
V: Viewers.Viewer;
name: ARRAY 16 OF CHAR;
X, Y: INTEGER;
BEGIN
Texts.OpenScanner(S, Oberon.Par.text, Oberon.Par.pos); Texts.Scan(S);
IF S.class=Texts.Char THEN
IF S.c="*" THEN
text:=Oberon.MarkedText();
#009A00 (* Also get the name of the viewer. *)
#009A00 D := Documents.MarkedDoc();
#009A00 IF D = NIL THEN (* Plain old Text viewer. *)
#009A00 Texts.OpenScanner(nameS, Oberon.MarkedViewer().dsc(TextFrames.Frame).text, 0);
#009A00 Texts.Scan(nameS);
#009A00 AddSuffix(nameS.s, suffixArray, name)
#009A00 ELSE
#009A00 AddSuffix(Documents.MarkedDoc().name, suffixArray, name)
#009A00 END
ELSIF S.c="^" THEN Oberon.GetSelection(sel, beg, end, time);
IF time >= 0 THEN NEW(buf); Texts.OpenBuf(buf); Texts.Save(sel, beg, end, buf);
NEW(text); Texts.Open(text, ""); Texts.Append(text, buf)
END
END
ELSIF S.class=Texts.Name THEN NEW(text); Texts.Open(text, S.s)
END;
Texts.Scan(S);
reverse:=FALSE; empty:=FALSE; unique:=FALSE;
IF (S.class=Texts.Char) & (S.c=Oberon.OptionChar) THEN Texts.Scan(S);
IF S.class=Texts.Name THEN
reverse:=(CAP(S.s[0])="R") OR (CAP(S.s[1])="R") OR (CAP(S.s[2])="R");
empty:=(CAP(S.s[0])="E") OR (CAP(S.s[1])="E") OR (CAP(S.s[2])="E");
unique:=(CAP(S.s[0])="U") OR (CAP(S.s[1])="U") OR (CAP(S.s[2])="U");
END
END;
NEW(array);
FillArray(array, n, text, empty); HSortArray(array, n);
NEW(text); Texts.Open(text, "");
FillText(text, array, n, reverse, unique); WriteMsg(n, "sorted.");
#009A00 IF D = NIL THEN (* Text viewer. *)
#009A00 Oberon.AllocateUserViewer(Oberon.Par.vwr.X, X, Y);
#009A00 V := MenuViewers.New(
#009A00 TextFrames.NewMenu(name, Menu),
#009A00 TextFrames.NewText(text, 0),
#009A00 TextFrames.menuH,
#009A00 X, Y);
#009A00 ELSE
#009A00 D:=Gadgets.CreateObject("TextDocs.NewDoc");
#009A00 NEW(TF); TextGadgets.Init(TF, text, FALSE); D(Documents.Document).W:=300; #FF0000 (* D.name := "Sorted.Text"; *)#009A00
#009A00 COPY(name, D(Documents.Document).name);
#009A00 Documents.Init(D(Documents.Document), TF);
#009A00 Desktops.ShowDoc(D(Documents.Document))
#009A00 END;
array:=NIL;
Oberon.Collect
END Sort;
BEGIN
#009A00 suffixArray := suffix;
Texts.OpenWriter(W)
END Sort.