home *** CD-ROM | disk | FTP | other *** search
/ Solo Programadores 22 / SOLO_22.iso / docs / lovelace / directio.adb < prev    next >
Encoding:
Text File  |  1995-10-24  |  1.7 KB  |  67 lines

  1.  
  2. with Ada.Characters.Handling;
  3. use  Ada.Characters.Handling;
  4.  
  5. package body Directions is
  6.  
  7.  Abbreviations : constant String := "nsewud";
  8.  
  9.  procedure To_Direction(Text : in Unbounded_String;
  10.                         Is_Direction : out Boolean;
  11.                         Dir  : out Direction) is
  12.   Lower_Text : String := To_Lower(To_String(Text));
  13.   -- Attempt to turn "Text" into a direction.
  14.   -- If successful, set "Is_Direction" True and "Dir" to the value.
  15.   -- If not successful, set "Is_Direction" False and "Dir" to arbitrary value.
  16.  begin
  17.    if Length(Text) = 1 then
  18.      -- Check if it's a one-letter abbreviation.
  19.      for D in Direction'Range loop
  20.        if Lower_Text(1) = Abbreviations(Direction'Pos(D) + 1) then
  21.          Is_Direction := True;
  22.          Dir := D;
  23.          return;
  24.        end if;
  25.      end loop;
  26.      Is_Direction := False;
  27.      Dir := North;
  28.      return;
  29.  
  30.    else
  31.      -- Not a one-letter abbreviation, try a full name.
  32.      for D in Direction'Range loop
  33.        if Lower_Text = To_Lower(Direction'Image(D)) then
  34.          Is_Direction := True;
  35.          Dir := D;
  36.          return;
  37.        end if;
  38.      end loop;
  39.      Is_Direction := False;
  40.      Dir := North;
  41.      return;
  42.    end if;
  43.  end To_Direction;
  44.  
  45.  function To_Direction(Text : in Unbounded_String) return Direction is
  46.    Is_Direction : Boolean;
  47.    Dir          : Direction;
  48.  begin
  49.    To_Direction(Text, Is_Direction, Dir);
  50.    if Is_Direction then
  51.       return Dir;
  52.    else
  53.       raise Constraint_Error;
  54.    end if;
  55.  end To_Direction;
  56.  
  57.  function Is_Direction(Text : in Unbounded_String) return Boolean is
  58.    Is_Direction : Boolean;
  59.    Dir          : Direction;
  60.  begin
  61.    To_Direction(Text, Is_Direction, Dir);
  62.    return Is_Direction;
  63.  end Is_Direction;
  64.  
  65. end Directions;
  66.  
  67.