home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / xc212os2.zip / SAMPLES / NODES / nodes.ob2 < prev    next >
Text File  |  1994-07-20  |  4KB  |  144 lines

  1. MODULE Nodes;
  2.  
  3. IMPORT  oberonRTS, tio:=STextIO, wio:=SWholeIO, res:=SIOResult;
  4.  
  5. TYPE
  6.   Node* = POINTER TO NodeDesc;
  7.   NodeDesc* = RECORD
  8.     number: INTEGER;
  9.     l,r: Node;
  10.   END;
  11.  
  12. VAR
  13.   cur: Node; (* current node *)
  14.   count: INTEGER;
  15.  
  16. PROCEDURE (n: Node) Handle*(command: ARRAY OF CHAR);
  17. (* Handle input *)
  18. END Handle;
  19.  
  20. PROCEDURE (n: Node) GetName*(VAR s: ARRAY OF CHAR);
  21. (* Returns name of the node *)
  22. BEGIN
  23.   COPY("node",s);
  24. END GetName;
  25.  
  26. PROCEDURE (n: Node) Help*;
  27. (** Print help message *)
  28. BEGIN
  29.   tio.WriteString("*** help not available ***");
  30.   tio.WriteLn;
  31. END Help;
  32.  
  33. PROCEDURE Insert*(n: Node);
  34. (** Inserts new node right to the current node. *)
  35. BEGIN
  36.   IF cur = NIL THEN cur:=n; n.l:=n; n.r:=n
  37.   ELSE
  38.     n.r:=cur.r; n.l:=cur;
  39.     n.r.l:=n; n.l.r:=n;
  40.   END;
  41.   n.number:=count; INC(count);
  42. END Insert;
  43.  
  44. PROCEDURE Delete;
  45. BEGIN
  46.   IF cur.l = cur THEN HALT END;
  47.   cur:=cur.l;
  48.   cur.r:=cur.r.r;
  49.   cur.r.l:=cur;
  50. END Delete;
  51.  
  52. PROCEDURE Info;
  53.   VAR objects, busymem: oberonRTS.CARDINAL;
  54. BEGIN
  55.   oberonRTS.GetInfo(objects,busymem);
  56.   tio.WriteString("#objects = "); wio.WriteCard(objects,5);
  57.   tio.WriteString("   busymem = "); wio.WriteCard(busymem,5);
  58.   tio.WriteLn;
  59. END Info;
  60.  
  61. PROCEDURE WriteNodeName(n: Node);
  62.   VAR s: ARRAY 64 OF CHAR;
  63. BEGIN
  64.   wio.WriteInt(n.number,0); tio.WriteChar(" ");
  65.   n.GetName(s); tio.WriteString(s);
  66. END WriteNodeName;
  67.  
  68. PROCEDURE ShowNodes;
  69.   VAR l: Node;
  70. BEGIN
  71.   l:=cur;
  72.   REPEAT
  73.     tio.WriteString("  ");
  74.     WriteNodeName(l);
  75.     tio.WriteLn;
  76.     l:=l^.l;
  77.   UNTIL l = cur;
  78. END ShowNodes;
  79.  
  80. PROCEDURE Help*;
  81. BEGIN
  82.   tio.WriteLn;
  83.   tio.WriteString("A simple oberon system supports a list of nodes."); tio.WriteLn;
  84.   tio.WriteString("Each node can handle input in its own way."); tio.WriteLn;
  85.   tio.WriteString("When you type some text it is passed to the"); tio.WriteLn;
  86.   tio.WriteString("current node. The following common operations are"); tio.WriteLn;
  87.   tio.WriteString("also available (starting from the character '.'):"); tio.WriteLn;
  88.   tio.WriteString("   .l - show all nodes in the list"); tio.WriteLn;
  89.   tio.WriteString("   .n - switch to the next node"); tio.WriteLn;
  90.   tio.WriteString("   .p - switch to the previous node"); tio.WriteLn;
  91.   tio.WriteString("   .c - call garbage collector"); tio.WriteLn;
  92.   tio.WriteString("   .d - delete current node"); tio.WriteLn;
  93.   tio.WriteString("   .h - print the current node help"); tio.WriteLn;
  94.   tio.WriteString("   .q - exit the system"); tio.WriteLn;
  95.   tio.WriteString("   .? - print this text"); tio.WriteLn;
  96.   tio.WriteString(""); tio.WriteLn;
  97.   tio.WriteLn;
  98. END Help;
  99.  
  100. PROCEDURE ProceedLine(s: ARRAY OF CHAR);
  101.   VAR i: INTEGER;
  102. BEGIN
  103.   s[LEN(s)-1]:=0X; i:=0;
  104.   WHILE s[i]=' ' DO INC(i) END;
  105.   IF s[i]='.' THEN
  106.     CASE s[i+1] OF
  107.       |'h','H': cur.Help;
  108.       |'n','N': cur:=cur.l;
  109.       |'p','P': cur:=cur.r;
  110.       |'c','C': Info; oberonRTS.Collect; Info;
  111.       |'l','L': ShowNodes;
  112.       |'d','D': Delete;
  113.       |'q','Q': HALT(0);
  114.       |'?'    : Help;
  115.     ELSE
  116.       tio.WriteString("#Unknown command");
  117.       tio.WriteLn;
  118.     END;
  119.   ELSE
  120.     cur.Handle(s);
  121.   END;
  122. END ProceedLine;
  123.  
  124. PROCEDURE Loop*;
  125. (** Main loop *)
  126.   VAR s: ARRAY 64 OF CHAR;
  127. BEGIN
  128.   LOOP
  129.     IF cur=NIL THEN EXIT END;
  130.     WriteNodeName(cur); tio.WriteString("> ");
  131.     tio.ReadRestLine(s);
  132.     CASE res.ReadResult() OF
  133.       |res.allRight,res.outOfRange: ProceedLine(s); tio.SkipLine;
  134.       |res.endOfLine: tio.SkipLine;
  135.       |res.endOfInput: HALT;
  136.    END;
  137.   END;
  138. END Loop;
  139.  
  140. BEGIN
  141.   cur:=NIL;
  142.   count:=0;
  143. END Nodes.
  144.