home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / oberon / oe.mod < prev    next >
Text File  |  1991-02-24  |  9KB  |  233 lines

  1. (*  "Main" module of names/phone organizer
  2.     (Oberon Example)    (c) Copyright E.R.Videki 1991 *)
  3.  
  4. MODULE OE ;
  5.  
  6.         IMPORT OEIO ,   OETree ,   OENames ,   OEPhone  ;
  7.  
  8.  
  9.  
  10. PROCEDURE MakeName(VAR nm : OENames.NameString  ;   phoneref : OEPhone.PhonePtr ;
  11.                         VAR p : OENames.NamePtr ) ;
  12. VAR  i : INTEGER ;
  13. BEGIN
  14.         p := NIL ;
  15.         IF nm[0] = 0X THEN RETURN END;  (* strange name... *)
  16.         NEW(p) ;
  17.         p.refptr := phoneref ;  (*may be NIL, but that's ok *)
  18.         p.name := nm ;
  19.         OENames.NewName( p ,  i );
  20.         IF i # 0 THEN OEIO.WriteString("*** Error: can't add that name");
  21.                 OEIO.WriteLn; p := NIL
  22.         END
  23. END MakeName ;
  24.  
  25.  
  26. PROCEDURE MakePhone(VAR ph : OEPhone.PhoneData ;  nameref : OENames.NamePtr ;
  27.                         VAR p : OEPhone.PhonePtr ) ;
  28. VAR i : INTEGER ;
  29. BEGIN
  30.         p := NIL ;
  31.         IF ph[0] = 0X THEN RETURN END ; (* ? *)
  32.         NEW(p);
  33.         p.refptr := nameref ;
  34.         p.phone := ph ;
  35.         OEPhone.NewPhone(p, i );
  36.         IF i # 0 THEN OEIO.WriteString("*** Error: can't add that phone number");
  37.                 OEIO.WriteLn; p := NIL
  38.         END
  39. END MakePhone ;
  40.  
  41.  
  42. PROCEDURE FindName(VAR nm : OENames.NameString) ;
  43. VAR p : OENames.NamePtr ;  i : INTEGER ;
  44. BEGIN
  45.         OENames.FindName( nm ,  i ,  p) ;       (* see if name is on the tree somewhere*)
  46.         IF i = 0 THEN   (* name was found... display it *)
  47.                         OEIO.WriteString("Name: ");
  48.                         p.method( p , OENames.PutName ) ;
  49.                                 (* the node handler takes care of showing it *)
  50.                         OEIO.WriteString("    Phone number: ");
  51.                         IF p.refptr = NIL THEN OEIO.WriteString(" <unknown>")
  52.                         ELSE p.refptr.method( p.refptr , OEPhone.PrtPhone )
  53.                                 (* node handler for phone number display*)
  54.                         END ;
  55.                         OEIO.WriteLn
  56.         ELSE OEIO.WriteString("<not found>"); OEIO.WriteLn
  57.         END
  58. END FindName ;
  59.  
  60.  
  61. PROCEDURE FindPhone(VAR ph : OEPhone.PhoneData ) ;
  62. VAR p : OEPhone.PhonePtr ;  i : INTEGER;
  63. BEGIN
  64.         OEPhone.FindPhone( ph ,  i ,  p );
  65.         IF i = 0 THEN   (* phone was found... *)
  66.                         OEIO.WriteString("Phone: ");
  67.                         p.method( p , OEPhone.PrtPhone ) ;
  68.                                 (* method displays phone number for us*)
  69.                         OEIO.WriteString("   Name : ");
  70.                         IF p.refptr = NIL THEN OEIO.WriteString("<nobody>")
  71.                         ELSE  p.refptr.method( p.refptr , OENames.PutName )
  72.                         END ;
  73.                         OEIO.WriteLn
  74.         ELSE  OEIO.WriteString("<not found>"); OEIO.WriteLn
  75.         END
  76. END FindPhone ;
  77.  
  78.  
  79.  
  80. (*  *** This section contains "commands" used when this application runs under the
  81.         ETH Oberon System instead of MSDOS
  82.         (and OEIO has been changed to use Texts) ***
  83.  
  84.  
  85.  
  86. PROCEDURE AddName *  ;
  87. VAR  nm : OENames.NameString ;  pnm : OENames.NamePtr ;
  88.         ph : OEPhone.PhoneData ; pph : OEPhone.PhonePtr ;
  89. BEGIN
  90.         OEIO.Init ;
  91.         OEIO.GetInput(nm) ;
  92.         IF nm[0] # 0X THEN (*something there! *)
  93.                 MakeName(nm, NIL, pnm ) ;
  94.                 IF pnm # NIL THEN
  95.                         OEIO.WriteString(" Name entered ") ;
  96.                         OEIO.GetInput(ph);
  97.                         IF ph[0] # 0X THEN      (* phone number also supplied *)
  98.                                 MakePhone(ph, pnm, pph) ;       (* phone references name node*)
  99.                                 IF pph # NIL THEN
  100.                                         OEIO.WriteString(" - and number entered");
  101.                                         pnm.refptr :=  pph
  102.                                                 (* now name references the phone node too*)
  103.                                 ELSE OEIO.WriteString(" Could not add the phone number")
  104.                                 END
  105.                         ELSE OEIO.WriteString(" No phone number added")
  106.                         END
  107.                 END
  108.         END ;
  109.         OEIO.WriteLn
  110. END AddName ;
  111.  
  112.  
  113.  
  114. PROCEDURE NameSearch *  ;
  115. VAR nm : OENames.NameString ;
  116. BEGIN
  117.         OEIO.Init ;
  118.         OEIO.GetInput(nm) ;
  119.         IF nm[0] # 0X THEN FindName(nm) END
  120. END NameSearch ;
  121.  
  122.  
  123.  
  124. PROCEDURE PhoneSearch * ;
  125. VAR ph : OEPhone.PhoneData ;
  126. BEGIN
  127.         OEIO.Init ;
  128.         OEIO.GetInput(ph);
  129.         IF ph[0] # 0X THEN FindPhone(ph) END
  130. END PhoneSearch ;
  131.  
  132.  
  133.        *** End of ETH Oberon System Commands area *** *)
  134.  
  135. (* *** beginning of common routines shared among Oberon-M (MSDOS) and Oberon System from ETH *** *)
  136. (*      (note that ShowAll is an Oberon System command also) *)
  137.  
  138.  
  139. PROCEDURE * DisplayNode ( p : OETree.ApplePtr ) ;       (* used in "ShowAll" proc below*)
  140. (* this procedure shows how to handle various kinds of type-extended records in one routine*)
  141. VAR cmd, cmd1 : INTEGER ;
  142. BEGIN
  143.         IF p # NIL THEN
  144.                 OEIO.WriteString("-->");
  145.                 IF p IS OENames.NamePtr THEN cmd := OENames.PutName ;
  146.                         cmd1 := OEPhone.PrtPhone
  147.                 ELSE cmd := OEPhone.PrtPhone ;  cmd1 := OENames.PutName
  148.                 END;
  149.                 p.method( p , cmd ) ;
  150.                         (* record-dependent "method" for printing that kind of record *)
  151.                 IF p.refptr # NIL THEN
  152.                         OEIO.WriteString("    linked to: ");
  153.                         p.refptr.method( p.refptr , cmd1 ) ;
  154.                 END ;
  155.                 OEIO.WriteString("<--");  OEIO.WriteLn
  156.         END
  157. END DisplayNode ;
  158.  
  159.  
  160.    (* NOTE: for use under Oberon System, make the export mark below uncommented! *)
  161.  
  162. PROCEDURE ShowAll (* * *);      (* A database dump of all names and phone numbers*)
  163. BEGIN
  164.         OEIO.WriteString("Names tree: "); OEIO.WriteLn ;
  165.         IF OENames.NameTree = NIL THEN
  166.                 OEIO.WriteString("Names tree is empty") ; OEIO.WriteLn
  167.         ELSE OETree.TraverseTree( DisplayNode , OENames.NameTree )
  168.         END ;
  169.  
  170.         OEIO.WriteLn ; OEIO.WriteString("Phone tree: "); OEIO.WriteLn;
  171.         IF OEPhone.PhoneTree = NIL THEN
  172.                 OEIO.WriteString("Phone tree is empty"); OEIO.WriteLn
  173.         ELSE OETree.TraverseTree( DisplayNode , OEPhone.PhoneTree )
  174.         END
  175. END ShowAll ;
  176.  
  177. (* *** End of common routines between Oberon-M MSDOS and ETH Oberon System *** *)
  178.  
  179.  
  180.  
  181. (* *** Below are MSDOS specific routines.
  182.         Note that the module initialization must invoke MainLoop *** *)
  183.  
  184. (*  *** Oberon-M main-loop for use under MSDOS ***  *)
  185. PROCEDURE MainLoop ;
  186. VAR nm : OENames.NameString ;   ph : OEPhone.PhoneData ;  i : INTEGER ;  ch:CHAR ;
  187.     pnm : OENames.NamePtr ;   pph : OEPhone.PhonePtr ;
  188. BEGIN
  189. LOOP
  190.         OEIO.WriteString("***Enter a number to select a function: "); OEIO.WriteLn ;
  191.         OEIO.WriteString("    0   (Exit from the program) " ); OEIO.WriteLn;
  192.         OEIO.WriteString("    1   (Enters a new name and phone number) " ); OEIO.WriteLn;
  193.         OEIO.WriteString("    2   (Using a name, search for a phone number)" ); OEIO.WriteLn ;
  194.         OEIO.WriteString("    3   (Using a phone number, search for a name)" ); OEIO.WriteLn ;
  195.         OEIO.WriteString("    4   (List out the name and phone databases)" ); OEIO.WriteLn ;
  196.         OEIO.GetInput(nm) ;  OEIO.WriteLn;
  197.         (* the following is a very lazy way to handle the choices, but after all,
  198.            this is just an example! *)
  199.         i := 0 ;  WHILE (nm[i]  = " ") OR (nm[i] = 09X (*horizontal tab*)) DO INC(i) END;
  200.         ch := nm[i] ;
  201.         IF ch = "0" THEN EXIT
  202.         ELSIF ch = "1" THEN
  203.                         OEIO.WriteString("Enter name: ");  OEIO.GetInput(nm); OEIO.WriteLn;
  204.                         OEIO.WriteString("Enter phone number: ");
  205.                         OEIO.GetInput(ph) ;  OEIO.WriteLn;
  206.                         IF nm[0] # 0X THEN  (* check for empty name *)
  207.                                 MakeName( nm, NIL, pnm) ;
  208.                                 IF (pnm # NIL) & (ph[0] # 0X) THEN MakePhone(ph, pnm, pph) ;
  209.                                                 IF pph # NIL THEN pnm.refptr := pph END
  210.                                                 (*name points to phone too*)
  211.                                 END
  212.                         END
  213.         ELSIF ch = "2" THEN
  214.                         OEIO.WriteString("Enter name: "); OEIO.GetInput(nm);  OEIO.WriteLn;
  215.                         FindName(nm)
  216.         ELSIF ch = "3" THEN
  217.                         OEIO.WriteString("Enter phone number: "); OEIO.GetInput(ph);
  218.                         OEIO.WriteLn;
  219.                         FindPhone(ph)
  220.         ELSIF ch = "4" THEN ShowAll
  221.         ELSE OEIO.WriteString("** ? Unknown command. Please enter one of the numbers shown.");
  222.                 OEIO.WriteLn
  223.         END
  224. END (*LOOP*)
  225. END MainLoop ;
  226.  
  227. (* ETH Oberon System: put comments around the following call to MainLoop  *)
  228. BEGIN MainLoop
  229. END OE.
  230.  
  231.  
  232.  
  233.