Generating a list of shortcuts (denoted by button captions containing the & symbol) from resource files

103 views Asked by At

I have a list of 429 MFC resource files that I have to generate a list of shortcuts for, which would be buttons containing an ampersand symbol (e.g. BUTTON "&Close") indicating that ALT-C is the shortcut for closing that particular dialog.

The problem is that the resource files contain many different dialogs formatted as such:

IDD_VIDEO DIALOG  0, 0, 471, 187
...
BEGIN
    ...
    PUSHBUTTON      "&Close",IDC_CLOSE,89,166,53,14
    ...
END

The format I would like to pull out would be a list of "&Close" (Or ideally "ALT-C &Close") and other labels with shortcuts, sectioned by which dialog they're under (e.g. IDD_VIDEO). Regex seems like the best solution but I haven't been able to figure a working regex out for this yet.

1

There are 1 answers

5
Tim Pietzcker On BEST ANSWER

Thanks for the added specifications. This should work:

^                  # Start of line
(IDD_\w+)          # Alphanumeric identifier, starting with IDD_
\s+DIALOG\b        # followed by "DIALOG"
((?:(?!^END\b).)*) # and any number of characters unless there's an END in-between

This will match the entire section from IDD_whatever until the next END. Then you need to take that string and apply the following regex to it:

"([^"]*&[^"]*)"  # String containing at least one &

Here's a C# example:

Regex sectionRegex = new Regex(
    @"^                 # Start of line
    (IDD_\w+)           # Alphanumeric identifier, starting with IDD_
    \s+DIALOG\b         # followed by ""DIALOG""
    ((?:(?!^END\b).)*)  # and any number of characters unless there's an END in-between",
    RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);

Regex altCRegex = new Regex(
    @"""([^""]*&[^""]*)"" # String containing at least one &", 
    RegexOptions.IgnorePatternWhitespace);

Match matchResults = sectionRegex.Match(subjectString);
while (matchResults.Success) {
    identifier = matchResults.Groups[1].Value;
    section = matchResults.Groups[2].Value;
    Match sectionResults = altCRegex.Match(section);
    while (sectionResults.Success) {
        altCString = sectionResult.Groups[1].Value;
        sectionResults = sectionResults.NextMatch();
    }
    matchResults = matchResults.NextMatch();
} 

Of course this code snippet doesn't do anything with identifier and altCString, but I think you get the idea.