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