1. -- 
  2. --  Copyright (c) 2009, 
  3. --  Reto Buerki, Adrian-Ken Rueegsegger 
  4. -- 
  5. --  This file is part of Alog. 
  6. -- 
  7. --  Alog is free software; you can redistribute it and/or modify 
  8. --  it under the terms of the GNU Lesser General Public License as published 
  9. --  by the Free Software Foundation; either version 2.1 of the License, or 
  10. --  (at your option) any later version. 
  11. -- 
  12. --  Alog is distributed in the hope that it will be useful, 
  13. --  but WITHOUT ANY WARRANTY; without even the implied warranty of 
  14. --  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  15. --  GNU Lesser General Public License for more details. 
  16. -- 
  17. --  You should have received a copy of the GNU Lesser General Public License 
  18. --  along with Alog; if not, write to the Free Software 
  19. --  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 
  20. --  MA  02110-1301  USA 
  21. -- 
  22.  
  23. with Ada.Strings.Unbounded.Hash; 
  24. with Ada.Containers.Hashed_Maps; 
  25.  
  26. --  Alog maps package. Provides map data types. 
  27. package Alog.Maps is 
  28.  
  29.    type Wildcard_Level_Map is tagged private; 
  30.    --  A map of loglevels with string as key type. 
  31.  
  32.    type Cursor is private; 
  33.    --  Index for a map element. 
  34.  
  35.    No_Element : constant Cursor; 
  36.  
  37.    Wildcard   : constant Character := '*'; 
  38.    --  Character used as wildcard indicator in lookups. 
  39.  
  40.    function Element 
  41.      (Map : Wildcard_Level_Map; 
  42.       Key : String) 
  43.       return Log_Level; 
  44.    --  Returns the loglevel element for given key. 
  45.  
  46.    function Element (Position : Cursor) return Log_Level; 
  47.    --  Returns the loglevel element at given position. 
  48.  
  49.    function Find 
  50.      (Map : Wildcard_Level_Map; 
  51.       Key : String) 
  52.       return Cursor; 
  53.    --  Returns the position for an element with specified key. If no element is 
  54.    --  found No_Element is returned. 
  55.  
  56.    function Lookup 
  57.      (Map : Wildcard_Level_Map; 
  58.       Key : String) 
  59.       return Cursor; 
  60.    --  Returns the position of the element with the closest match to given key. 
  61.    --  This function considers wildcards when searching for an element. 
  62.    -- 
  63.    --  Example:      Key   | Element 
  64.    --             ------------------- 
  65.    --              Foo.*   |  Debug 
  66.    --              Foo.Bar |  Alert 
  67.    -- 
  68.    --  A lookup for "Foo.Foo" has no exact match. The next closest match is 
  69.    --  "Foo.*" which will return the Debug element. Looking for "Foo" will 
  70.    --  return Debug since it matches the wildcard "Foo.*". 
  71.    -- 
  72.    --  If no exact and wildcard match is found No_Element is returned. 
  73.  
  74.    procedure Insert 
  75.      (Map  : in out Wildcard_Level_Map; 
  76.       Key  :        String; 
  77.       Item :        Log_Level); 
  78.    --  Insert given key/item pair into map. If given key is already present the 
  79.    --  associated item is replaced. 
  80.  
  81.    procedure Clear (Map : in out Wildcard_Level_Map); 
  82.    --  Clears the wildcard map. 
  83.  
  84.    function Length (Map : Wildcard_Level_Map) return Natural; 
  85.    --  Return the number of elements in the map. 
  86.  
  87. private 
  88.  
  89.    use Ada.Strings.Unbounded; 
  90.  
  91.    package Map_Of_Loglevels_Package is new Ada.Containers.Hashed_Maps 
  92.      (Key_Type        => Unbounded_String, 
  93.       Element_Type    => Log_Level, 
  94.       Hash            => Hash, 
  95.       Equivalent_Keys => "="); 
  96.  
  97.    package MOLP renames Map_Of_Loglevels_Package; 
  98.  
  99.    type Wildcard_Level_Map is tagged record 
  100.       Data : MOLP.Map; 
  101.    end record; 
  102.  
  103.    type Cursor is new MOLP.Cursor; 
  104.  
  105.    No_Element : constant Cursor := Cursor (MOLP.No_Element); 
  106.  
  107. end Alog.Maps;