crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianDebugLib/PluginAPI/KeyBindings/DbgEngineKeyBindings.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 * 
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 using System;
       
    18 using System.Collections.Generic;
       
    19 using System.Text;
       
    20 using System.Text.RegularExpressions;
       
    21 using System.IO;
       
    22 using SymbianUtils.PluginManager;
       
    23 using SymbianStructuresLib.Arm;
       
    24 using SymbianStructuresLib.Arm.Instructions;
       
    25 using SymbianDebugLib.Engine;
       
    26 using SymbianDebugLib.PluginAPI.Types;
       
    27 
       
    28 namespace SymbianDebugLib.PluginAPI.Types.KeyBindings
       
    29 {
       
    30     public abstract class DbgEngineKeyBindings : DbgPluginEngine
       
    31     {
       
    32         #region Factory function
       
    33         public static DbgEngineKeyBindings New( DbgEngine aEngine )
       
    34         {
       
    35             PluginManager<DbgEngineKeyBindings> loader = new PluginManager<DbgEngineKeyBindings>( 1 );
       
    36             loader.Load( new object[] { aEngine } );
       
    37             //
       
    38             DbgEngineKeyBindings ret = null;
       
    39             foreach ( DbgEngineKeyBindings engine in loader )
       
    40             {
       
    41                 if ( engine is DbgEngineKeyBindingsStub && loader.Count > 1 )
       
    42                 {
       
    43                     continue;
       
    44                 }
       
    45                 else
       
    46                 {
       
    47                     ret = engine;
       
    48                     break;
       
    49                 }
       
    50             }
       
    51             //
       
    52             return ret;
       
    53         }
       
    54         #endregion
       
    55 
       
    56         #region Constructors
       
    57         protected DbgEngineKeyBindings( DbgEngine aEngine )
       
    58             : base( aEngine )
       
    59         {
       
    60         }
       
    61         #endregion
       
    62 
       
    63         #region API
       
    64         public void LoadFromFile( string aFileName )
       
    65         {
       
    66             using ( StreamReader reader = new StreamReader( new FileStream( aFileName, FileMode.Open, FileAccess.Read ) ) )
       
    67             {
       
    68                 string line = reader.ReadLine();
       
    69                 while ( line != null )
       
    70                 {
       
    71                     Match m = KRegEx.Match( line );
       
    72                     if ( m.Success )
       
    73                     {
       
    74                         int scanCode = int.Parse( m.Groups[ "ScanCode" ].Value, System.Globalization.NumberStyles.HexNumber );
       
    75                         string value = m.Groups[ "Interpretation" ].Value;
       
    76                         //
       
    77                         if ( iTable.ContainsKey( scanCode ) == false )
       
    78                         {
       
    79                             iTable.Add( scanCode, value );
       
    80                         }
       
    81                     }
       
    82                     line = reader.ReadLine();
       
    83                 }
       
    84             }
       
    85         }
       
    86         #endregion
       
    87 
       
    88         #region From DbgPluginEngine
       
    89         protected override void DoClear()
       
    90         {
       
    91             iTable.Clear();
       
    92         }
       
    93         #endregion
       
    94 
       
    95         #region Properties
       
    96         public bool IsKeyBindingTableAvailable
       
    97         {
       
    98             get { return iTable.Count > 0; }
       
    99         }
       
   100 
       
   101         public string this[ int aScanCode ]
       
   102         {
       
   103             get
       
   104             {
       
   105                 string ret = KUnknownKey;
       
   106                 //
       
   107                 if ( iTable.TryGetValue( aScanCode, out ret ) == false )
       
   108                 {
       
   109                     ret = KUnknownKey;
       
   110                 }
       
   111                 //
       
   112                 return ret;
       
   113             }
       
   114         }
       
   115         #endregion
       
   116 
       
   117         #region Internal methods
       
   118         #endregion
       
   119 
       
   120         #region Internal constants
       
   121         private const string KUnknownKey = "Unknown Key";
       
   122         private static readonly Regex KRegEx = new Regex(
       
   123               "0x(?<ScanCode>[0-9a-fA-F]{4})\\=(?<Interpretation>.+)\r\n",
       
   124             RegexOptions.Singleline
       
   125             | RegexOptions.CultureInvariant
       
   126             | RegexOptions.IgnorePatternWhitespace
       
   127             | RegexOptions.Compiled
       
   128             );
       
   129         #endregion
       
   130 
       
   131         #region Data members
       
   132         private Dictionary<int, string> iTable = new Dictionary<int, string>();
       
   133         #endregion
       
   134     }
       
   135 }