Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading hash keys from file #332

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions SharpKeys/Dialog_Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Windows.Forms;
using System.ComponentModel;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.Win32;

namespace SharpKeys
Expand Down Expand Up @@ -853,11 +854,68 @@ private void btnSaveKeys_Click(object sender, EventArgs e)
}
}

private string UnquoteString(string s)
{
// remove the leading & trailing '"'
if (s.StartsWith("\"") && s.EndsWith("\""))
{
s = s.Substring(1, s.Length - 2);
}
return s;
}

private bool ReadHashKeysFromFile(string pathToKeysFile)
{
if (!File.Exists(pathToKeysFile))
return false; // the file does not exist

string[] lines = File.ReadAllLines(pathToKeysFile, System.Text.Encoding.Unicode);
if (lines.Length == 0)
return false; // the file is empty

m_hashKeys = new Hashtable();

foreach (string line in lines)
{
if (line.TrimStart().StartsWith("//"))
continue; // skipping a comment

string[] items = line.Split(new char[]{','}, 2);
if (items.GetLength(0) == 2)
{
string param1 = items[0].Trim(); // w/o leading & trailing spaces
string param2 = items[1].Trim(); // w/o leading & trailing spaces

int n = param2.LastIndexOf('"');
if (n != -1)
{
// removing the trailing '//' after the last '"', if any
n = param2.IndexOf("//", n);
if (n != -1)
{
param2 = param2.Remove(n).TrimEnd();
}
}

param2 = Regex.Unescape(UnquoteString(param2));
param1 = Regex.Unescape(UnquoteString(param1));

m_hashKeys.Add(param1, param2);
}
}
return true;
}

private void BuildParseTables()
{
if (m_hashKeys != null)
return;

string pathToExeDir = Path.GetDirectoryName(Application.ExecutablePath);
string pathToKeysFile = Path.Combine(pathToExeDir, "SharpKeys.keys");
if (ReadHashKeysFromFile(pathToKeysFile))
return;

// the hash table uses a string in the form of Hi_Lo scan code (in Hex values)
// that most sources say are scan codes. The 00_00 will disable a key - everything else
// is pretty obvious. There is a bit of a reverse lookup however, so labels changed here
Expand Down
Binary file added SharpKeys/bin/Debug/SharpKeys.keys
Binary file not shown.
Binary file added SharpKeys/bin/Release/SharpKeys.keys
Binary file not shown.