dirty_booleans_0.1.0_d92d5dc5/src/dirty_booleans.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
with Ada.Characters.Conversions;
with Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
with Ada.Strings.Wide_Wide_Fixed;
with Ada.Wide_Wide_Characters.Handling;

package body Dirty_Booleans is

   -----------
   -- Value --
   -----------

   function Value (S : String) return Dirty_Boolean
   is (Value (Ada.Characters.Conversions.To_Wide_Wide_String (S)));

   -----------
   -- Value --
   -----------

   function Value (S : Wide_Wide_String) return Dirty_Boolean
   is
      use Ada.Strings.Wide_Wide_Fixed;
      use Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
      use Ada.Wide_Wide_Characters.Handling;
      N : constant Wide_Wide_String :=
            Trim (To_Lower (S), Ada.Strings.Both);
   begin
      if Enable_No_Yes then
         if N = "no" then
            return False;
         elsif N = "yes" then
            return True;
         end if;
      end if;

      if Enable_N_Y then
         if N = "n" then
            return False;
         elsif N = "y" then
            return True;
         end if;
      end if;

      if Enable_False_True then
         if N = "false" then
            return False;
         elsif N = "true" then
            return True;
         end if;
      end if;

      if Enable_F_T then
         if N = "f" then
            return False;
         elsif N = "t" then
            return True;
         end if;
      end if;

      if Enable_Empty and then N = "" then
         return False;
      end if;

      if Enable_Non_Empty and then N /= "" then
         return True;
      end if;

      if Enable_0_1_Str then
         if N = "0" then
            return False;
         elsif N = "1" then
            return True;
         end if;
      end if;

      begin
         if Enable_Non_Zero_Str then
            if Integer'Wide_Wide_Value (N) /= 0 then
               return True;
            else
               return False;
            end if;
         end if;
      exception
         when others =>
            null; -- Not an int
      end;

      raise Constraint_Error
        with "String doesn't match any recognized value: "
        & Encode (N);
   end Value;

   -----------
   -- Value --
   -----------

   function Value (I : Integer) return Dirty_Boolean
   is (if Enable_Non_Zero
       then Dirty_Boolean (I /= 0)
       elsif Enable_0_1
       then
         (case I is
             when 0 => False,
             when 1 => True,
             when others =>
                raise Constraint_Error with
                  "Value out of range 0/1:" & I'Image)
       else raise Constraint_Error with "Comparisons with numbers disabled");

end Dirty_Booleans;