home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / ADA / LOVELACE / directio.adb < prev    next >
Text File  |  1996-03-01  |  2KB  |  82 lines

  1. --
  2. -- Copyright (C) 1996 Ada Resource Association (ARA), Columbus, Ohio.
  3. -- Author: David A. Wheeler
  4. --
  5.  
  6. with Ada.Characters.Handling;
  7. use  Ada.Characters.Handling;
  8.  
  9. package body Directions is
  10.  
  11.  Abbreviations : constant String := "nsewud";
  12.  
  13.  procedure To_Direction(Text : in Unbounded_String;
  14.                         Is_Direction : out Boolean;
  15.                         Dir  : out Direction) is
  16.   Lower_Text : String := To_Lower(To_String(Text));
  17.   -- Attempt to turn "Text" into a direction.
  18.   -- If successful, set "Is_Direction" True and "Dir" to the value.
  19.   -- If not successful, set "Is_Direction" False and "Dir" to arbitrary value.
  20.  begin
  21.    if Length(Text) = 1 then
  22.      -- Check if it's a one-letter abbreviation.
  23.      for D in Direction'Range loop
  24.        if Lower_Text(1) = Abbreviations(Direction'Pos(D) + 1) then
  25.          Is_Direction := True;
  26.          Dir := D;
  27.          return;
  28.        end if;
  29.      end loop;
  30.      Is_Direction := False;
  31.      Dir := North;
  32.      return;
  33.  
  34.    else
  35.      -- Not a one-letter abbreviation, try a full name.
  36.      for D in Direction'Range loop
  37.        if Lower_Text = To_Lower(Direction'Image(D)) then
  38.          Is_Direction := True;
  39.          Dir := D;
  40.          return;
  41.        end if;
  42.      end loop;
  43.      Is_Direction := False;
  44.      Dir := North;
  45.      return;
  46.    end if;
  47.  end To_Direction;
  48.  
  49.  function To_Direction(Text : in Unbounded_String) return Direction is
  50.    Is_Direction : Boolean;
  51.    Dir          : Direction;
  52.  begin
  53.    To_Direction(Text, Is_Direction, Dir);
  54.    if Is_Direction then
  55.       return Dir;
  56.    else
  57.       raise Constraint_Error;
  58.    end if;
  59.  end To_Direction;
  60.  
  61.  function Is_Direction(Text : in Unbounded_String) return Boolean is
  62.    Is_Direction : Boolean;
  63.    Dir          : Direction;
  64.  begin
  65.    To_Direction(Text, Is_Direction, Dir);
  66.    return Is_Direction;
  67.  end Is_Direction;
  68.  
  69. end Directions;
  70.  
  71. --
  72. -- Permission to use, copy, modify, and distribute this software and its
  73. -- documentation for any purpose and without fee is hereby granted,
  74. -- provided that the above copyright and authorship notice appear in all
  75. -- copies and that both that copyright notice and this permission notice
  76. -- appear in supporting documentation.
  77. -- 
  78. -- The ARA makes no representations about the suitability of this software
  79. -- for any purpose.  It is provided "as is" without express
  80. -- or implied warranty.
  81. -- 
  82.