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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2024 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using KeePassLib.Utility;
namespace KeePass.UI
{
internal sealed class AccessKeyManagerEx
{
private readonly bool[] m_vUsedLetters = new bool[26];
private readonly bool[] m_vUsedDigits = new bool[10];
#if DEBUG
private static readonly char[] g_vKeybDep = new char[] {
'\u00C4', '\u00D6', '\u00DC', '\u00E4', '\u00F6', '\u00FC'
};
#endif
private bool IsFree(char ch, out bool[] vUsed, out int iUsed)
{
if((ch >= 'A') && (ch <= 'Z'))
{
vUsed = m_vUsedLetters;
iUsed = ch - 'A';
}
else if((ch >= 'a') && (ch <= 'z'))
{
vUsed = m_vUsedLetters;
iUsed = ch - 'a';
}
else if((ch >= '0') && (ch <= '9'))
{
vUsed = m_vUsedDigits;
iUsed = ch - '0';
}
else
{
vUsed = null;
iUsed = -1;
return false;
}
return !vUsed[iUsed];
}
public string CreateText(string strText, bool bEncode)
{
return CreateText(strText, bEncode, 0);
}
private string CreateText(string strText, bool bEncode, int iPrefStart)
{
if(string.IsNullOrEmpty(strText)) { Debug.Assert(false); return string.Empty; }
if(bEncode) strText = StrUtil.EncodeMenuText(strText);
else { Debug.Assert(strText.Replace("&&", string.Empty).IndexOf('&') < 0); }
bool[] vUsed;
int iUsed, i = iPrefStart, n = strText.Length;
if((i < 0) || (i >= n)) { Debug.Assert(false); i = 0; iPrefStart = 0; }
if(n == 0) { Debug.Assert(false); return string.Empty; }
do
{
if(IsFree(strText[i], out vUsed, out iUsed))
{
vUsed[iUsed] = true;
return strText.Insert(i, "&");
}
if(++i == n) i = 0;
}
while(i != iPrefStart);
return strText;
}
public string CreateText(string strText, bool bEncode, string strParamPlh,
string strParamValue)
{
if(string.IsNullOrEmpty(strText)) { Debug.Assert(false); return string.Empty; }
if(string.IsNullOrEmpty(strParamPlh)) strParamPlh = "{PARAM}";
if(strParamValue == null) { Debug.Assert(false); strParamValue = string.Empty; }
int iParam = strText.IndexOf(strParamPlh);
string str = strText.Replace(strParamPlh, strParamValue);
return CreateText(str, bEncode, iParam);
}
public string RegisterText(string strText)
{
if(string.IsNullOrEmpty(strText)) { Debug.Assert(false); return string.Empty; }
Debug.Assert(StrUtil.Count(strText.Replace("&&", string.Empty), "&") == 1);
bool[] vUsed;
int iUsed, iOffset = 0, n = strText.Length;
do
{
int i = strText.IndexOf('&', iOffset);
if(i < iOffset) { Debug.Assert(false); break; } // No access key
int iNext = i + 1;
if(iNext == n) { Debug.Assert(false); break; } // Lonely '&' at end
char chNext = strText[iNext];
if(chNext != '&')
{
if(IsFree(chNext, out vUsed, out iUsed))
vUsed[iUsed] = true;
#if DEBUG
else if(Array.IndexOf(g_vKeybDep, chNext) >= 0) { }
else { Debug.Assert(false); } // Unknown or duplicate char.
#endif
break;
}
iOffset = iNext + 1;
}
while(iOffset < n);
return strText;
}
}
}
|