Package: registry

Dependencies

   with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

Description

This package implements the main abstract interface for writing and reading configuration information. The current implementation can be used under unix and windows. This interface should be easy to extend, so that this configuration informatins can be stored on other locations, for example ldap server or a database.

Under Unix the information is written to a config file. This config file has a human readable format and is stored on /etc/<programmname>.config for system wide access and $HOME/<programmname>.config for user accessible information.

On Windows the information is stored in the registry under HKEY_LOCAL_MACHINE/Software/<programmname> and HKEY_CURRENT_USER/Software/<programmname>.

The above example works on Windows and Unix.

 with registry.factory; use registry.factory, registry;
 with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
 with Ada.Text_IO; use Ada.Text_IO;

 procedure main is
   reg : registry_type'class  := registry.factory.Create(
                                             "registrytest",CLASS_USER);
   val : Unbounded_String;
   section : r_section := To_Section("A_New_Section");
 begin
   Open(reg); -- Opens access to the registry
   Put(reg,To_Key("A_Key"),"A Value"); -- A Key in the default section
   Set_Section(reg,section);
   Put(reg,To_Key("A_Key"),"An other Value"); -- An other key in a new section
   Put(reg,To_key("An_other_Key"),"An other Value on  a new key");
   Set_Section(reg,empty_section);
   Get(reg,To_Key("A_Key"),val);
   Put_Line(To_String(val));
   Set_Section(reg,section);
   Put_Line(To_String(val));
   Close(reg);
 end main;
Also see the file test/test.adb for a larger example.

Header

package registry is
 
pragma elaborate_body;

Exceptions

REGISTRY_NO_KEY
Raised when Get is used with an unkown key.
REGISTRY_NO_SECTION
Raised when Get or Put is used, before a section via Set_Section is set
REGISTRY_SUBSECTION_EXISTS
Raised when Delete_Section is used on a section with subsections.
REGISTRY_SYNTAX_ERROR
Raised when a syntax error occures in the configuration file

Type Summary

keyTable
r_key derived from Unbounded_String
New Operations:  Delete_Key, Get, Get, Put, Put, To_Key
Inherited Operations:  "&", "&", "&", "&", "&", "*", "*", "*", "<", "<", "<", "<=", "<=", "<=", "=", "=", "=", ">", ">", ">", ">=", ">=", ">=", Append, Append, Append, Count, Count, Count, Delete, Delete, Element, Find_Token, Head, Head, Index, Index, Index, Index_Non_Blank, Insert, Insert, Length, Overwrite, Overwrite, Replace_Element, Replace_Slice, Replace_Slice, Slice, Tail, Tail, To_String, To_Unbounded_String, To_Unbounded_String, Translate, Translate, Translate, Translate, Trim, Trim, Trim, Trim
r_section derived from Unbounded_String
New Operations:  Delete_Section, Set_Section, To_Section
Inherited Operations:  "&", "&", "&", "&", "&", "*", "*", "*", "<", "<", "<", "<=", "<=", "<=", "=", "=", "=", ">", ">", ">", ">=", ">=", ">=", Append, Append, Append, Count, Count, Count, Delete, Delete, Element, Find_Token, Head, Head, Index, Index, Index, Index_Non_Blank, Insert, Insert, Length, Overwrite, Overwrite, Replace_Element, Replace_Slice, Replace_Slice, Slice, Tail, Tail, To_String, To_Unbounded_String, To_Unbounded_String, Translate, Translate, Translate, Translate, Trim, Trim, Trim, Trim
registry_type (abstract type)
Primitive Operations:  Close, Create, Delete_Key, Delete_Section, Get, Get, Get_Keys, Get_Sections, Is_Open, Open, Put, Put, Set_Section
registry_type_Access
sectionTable
sys_access

Constants and Named Numbers

empty_section : constant r_section := To_Unbounded_String("");
The default (empty) section
section_separator : constant r_section := To_Unbounded_String("\");
Separator for subsections

Other Items:

type sys_access is (CLASS_USER,CLASS_SYSTEM);
Type of the access. CLASS_USER accesses the user information, CLASS_SYSTEM the system wide configuration information

type r_key is new Unbounded_String;
Type for the key name

type r_section is new Unbounded_String;
Type for the section name

type registry_type is abstract tagged private;
Abstract record which stores the internal data for a registry.

type registry_type_Access is access all registry_type'Class;
Abstract handle for accessing the registry

type sectionTable is array(Integer range <>) of r_section;
Type for returning sections

type keyTable is array(Integer range<>) of r_key;
Type for returning keys

function To_Key(key : String) return r_key;
Convert a String to a key value
key : String String with key name
Returns : r_key Key as type r_key

function To_Section(section : String) return r_section;
Convert a String with a secton name to type r_section
section : String String with section name
Returns : r_Section Section as type r_section

function Create(programm_name : String;s_access :sys_access) return registry_type is abstract;
Create a registry for programm_name with selected access.
programm_name : String Name of the programm or more general the name of the config file on Unix or registry key under PROGRAMM/ on Windows
s_access : sys_access If CLASS_USER the information is stored so that the normal user can access them. CLASS_SYSTEM indicates system wide access. The user can only read but not write this information. Only the administrator can read and write to this.

For opening a registry better use the Create function from the package registry.factory. This function returns a correct initialized instance of one of the subtypes of registry-file or registry-win.


procedure Set_Section(reg : in out registry_type; s : in r_section) is abstract;
Set a setion in the registry. If the section does not exists the section is created.
reg : registry_type a with Create initialized handle of the registry.
s : r_section A section name.

procedure Put(reg : in out registry_type;
              key : in r_key;
              val : in String
             ) is abstract;
Store a key/value pair in the registry. If the key does not exists in the section, the key is created, otherwise the old value of the key is overwritten. The value is from type String.
reg : registry_type a with Create initialized handle of the registry
key : r_key The key name.
val : String The string value for the key.

procedure Put(reg : in out registry_type;
              key : in r_key;
              val : in Integer
             ) is abstract;
Store a key/value pair in the registry. If the key does not exists in the section, the key is created, otherwise the old value of the key is overwritten. The value is from type Integer.
reg : registry_type a with Create initialized handle of the registry
key : r_key The key name.
val : Integer The integer value for the key.

procedure Get(reg : in out registry_type;
              key : in r_key;
              val : out Unbounded_String
             ) is abstract;
Get a value from a key. In case that the key does not exists the exception REGISTRY_NO_KEY is raised;
reg : registry_type a with Create initialized handle of the registry.
key : r_key The key name.
val : Unbounded_String Returns the value from the key.

procedure Get(reg : in out registry_type;
              key : in r_key;
              val : out Integer
             ) is abstract;
Get a value from a key. In case that the key does not exists the exception REGISTRY_NO_KEY is raised;
reg : registry_type a with Create initialized handle of the registry.
key : r_key The key name.
val :Integer Returns the value from the key.

function Get_Sections(reg : in registry_type) 
return sectionTable is abstract;
Get all known sections.
reg : registry_type a with Create initialized handle of the registry.
Returns : secionTable An array of r_section with all sections of this registry.

function Get_Keys(reg : in registry_type) 
return keyTable is abstract;
Get all known keys for the current section. If there is no key, raise REGISTRY_NO_KEY exception.
reg : registry_type a with Create initialized handle of the registry.
Returns : keyTable An array of r_key with all keys of a section, set with Set_Section.

procedure Open(reg : in out registry_type) is abstract;
Open access to the registry
reg : registry_type a with Create initialized handle of the registry.

procedure Close(reg: in out registry_type) is abstract;
Close access to the registry and write back all changes.
reg : registry_type a with Create initialized handle of the registry.

function Is_Open(reg : in registry_type) return Boolean is abstract;
Check if the registry is opened;
reg : registry_type a with Create initialized handle of the registry.
Returns : boolean true if the registry is open.

procedure Delete_Key(reg : in out registry_type; key : in r_key) is abstract;
Delete a key from the current section. In case that the key does not exists the exception REGISTRY_NO_KEY is raised.
reg : registry_type a with Create initialized handle of the registry.
key : r_key The key name to delete.

procedure Delete_Section(reg : in out registry_type; sec : in r_section) is abstract;
Delete a complete section including all keys. The current section is set back to the empty default section. So after this command you must restore your previous default section via the Set_Section function. In case that the section does not exists the exception REGISTRY_NO_SECTION is raised. Also in case that subsections exists the exception REGISTRY_SUBSECTION_EXISTS is raised.
reg : registry_type a with Create initialized handle of the registry.
sec : r_section The section name to delete.

function Get_ErrorMessage(reg : in registry_type'Class) return String;
In case of an exception, Get_ErrorMessage returns a verbose error message
reg : registry_type handle of the registry which caused this exception.
Returns : String Error message string.

function Get_ErrorMessage(reg : in registry_type'Class) return Unbounded_String;
In case of an exception, Get_ErrorMessage returns a verbose error message
reg : registry_type handle of the registry which caused this exception.
Returns : Unbounded_String Error message string.

private

   --  Implementation-defined ...
end registry;