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

  1. (* Module to handle name type nodes on the general tree structure
  2.    (Oberon Example)  (c) Copyright E. R. Videki 1991 *)
  3.  
  4. MODULE OENames ;
  5.     IMPORT OETree , OEIO  ;
  6.  
  7.  
  8. CONST    MaxName * = 25 ;    (* maximum size of a name we will remember *)
  9.     PutName * = 0 ;        (* the command we recognize in our handler *)
  10.  
  11.  
  12.  
  13. TYPE    NameString *  = ARRAY MaxName OF CHAR ;
  14.     NamePtr * = POINTER TO Name ;
  15.  
  16.     Name * =     
  17.         RECORD (OETree.Apple)    (* an extension of the basic node type *)
  18.             name * : NameString
  19.         END ;
  20.  
  21.  
  22. VAR NameTree *  : NamePtr ;
  23.     refnode : NamePtr ;
  24.  
  25.  
  26. PROCEDURE * Handler ( p : OETree.ApplePtr ;  cmd : INTEGER ) ;
  27. (* handles commands sent to an object that is a name type.  At the moment, the only 
  28. command we process is to pass the name to an I/O routine.  Note the compiler
  29. hint "*" which indicates that this procedure is placed into a variable (field in the
  30. name record) *)
  31. BEGIN
  32.     IF cmd = PutName THEN
  33.             OEIO.WriteString(  p(NamePtr).name  )
  34.         (* note how the type of p is expanded to the extended type we deal with
  35.         in this module.  Also, this type guard checks that p is in fact pointing to
  36.         a name record we are assuming was passed to us. *)
  37.     END (*ignore all unrecognized commands *)
  38. END Handler;
  39.  
  40.  
  41. PROCEDURE * NameSearch( p , ref : OETree.ApplePtr ;   VAR result : INTEGER );    
  42.             (*see OETree's SearchProc type *)
  43. (* (note the compiler hint "*" that indicates this procedure is called via a 
  44. procedural-type variable
  45. when it is the parameter to one of the OETree routines ) *)
  46. VAR i : INTEGER ;
  47. BEGIN
  48.     result := 1 ;
  49.     IF p IS NamePtr THEN    
  50.     (* guard against some other kind of tree elements (generic safety checking) *)
  51.         WITH p : NamePtr DO (* expand the scope to include our extended record type *)
  52.             WITH ref : NamePtr DO     
  53.             (* this makes one efficient check for correct extended type of ref *)
  54.                 i := 0 ;
  55.                 WHILE ( i < MaxName) & ( p.name[i] # 0X) & 
  56.                    (p.name[i] = ref.name[i]) DO INC(i) END;
  57.                 result :=  ORD(ref.name[i]) - ORD(p.name[i])
  58.             END
  59.         END
  60.     ELSE result := -1 (* searching  for names on wrong kind of tree *)
  61.     END
  62. END NameSearch ;
  63.  
  64.  
  65.  
  66. PROCEDURE NewName * ( p : NamePtr ;  VAR result : INTEGER ) ;    
  67. (* add new name element p^ to tree *)
  68. BEGIN
  69.     p.method := Handler ;    
  70.         (* indicate our handler procedure for this type of extended record *)
  71.     OETree.AddNew( NameTree, p , result, NameSearch) 
  72. END NewName ;
  73.  
  74.  
  75. PROCEDURE FindName * (VAR nm : NameString ; VAR result : INTEGER ; VAR p : NamePtr );    
  76. (* search for existing name *)
  77. VAR p1 : OETree.ApplePtr ;
  78. BEGIN
  79.     refnode.name := nm ;
  80.     OETree.Search( NameTree, refnode , p1 , result, NameSearch) ;
  81.     IF result = 0 THEN p := p1(NamePtr)    
  82.         (* expand scope of pointer type for caller; it is name record node *)
  83.     ELSE p := NIL
  84.     END
  85. END FindName ;
  86.  
  87.  
  88. PROCEDURE Init ;
  89. VAR result : INTEGER;
  90. BEGIN
  91.     NEW(refnode) ;    (* for reference of searches *)
  92.     refnode.name[0] := 0X ;
  93.     NEW(NameTree) ;    (* put a dummy head-of-tree element *)
  94.     NameTree.name[0] := 0X ;
  95.     NameTree.refptr := NIL;  NameTree.method := Handler  ;
  96.     OETree.AddNew( NIL , NameTree , result, NameSearch)     
  97.             (*this inits tree links controlled by OETree*)
  98. END Init;
  99.  
  100.  
  101.  
  102.  
  103.  
  104. BEGIN    Init 
  105. END OENames .
  106.  
  107.