0

I am trying to parse a header and create method stubs from the interface/method declarations.

I want to take c++ com method declarations like this:

STDMETHOD(GetCubeMapSurface)(THIS_ D3DCUBEMAP_FACES FaceType,UINT Level,IDirect3DSurface9** ppCubeMapSurface) PURE;

Then modify it to generate a c++ method stub from it like this:

HRESULT __stdcall WrapIDirect3DCubeTexture9::GetCubeMapSurface(D3DCUBEMAP_FACES FaceType, UINT Level, IDirect3DSurface9 * * ppCubeMapSurface) 
{

}

I am a little unsure if I should be using regex for this or using .net string functions, and I am confused on how exactly to implement it either way.

I have quite a few methods to do, so creating a tool seems like the right thing to do.

Can anyone help guide me in the right direction?

EDIT: I should have added that I was looking for some help on how I should be implementing it. I wasn't sure if I should be tokenizing all words/special chars and empty spaces and just go from there, using a regex like this and then just parsing and processing with it broken up.

"(\d[x0-9a-fA-F.UL]*|\w+|\s+|"[^"]*"|.)"

Although now it seems like overkill and that I was over analyzing this whole thing. I ended up quickly creating an implementation with .net string functions, and then seen that Caesay helped me out in the regex direction. So I came up with two implementations.

I have decided I will go with the regex implementation. Since I will be doing some other advanced processing and parsing, and regex would make that easier. The implementations are below.

String based implementation:

                if (line.StartsWith("    STDMETHOD"))
                {
                    string newstr = line.Replace("   STDMETHOD(", "HRESULT __stdcall WrapIDirect3DCubeTexture9::");
                    newstr = StringExtensions.RemoveFirst(newstr, ")");
                    newstr = newstr.Replace("THIS_ ", "");
                    newstr = newstr.Replace(" PURE;", Environment.NewLine + "{ " + Environment.NewLine + Environment.NewLine + "}");
                    textBox2.AppendText(newstr + Environment.NewLine);

                }

String extension class taken from(C# - Simplest way to remove first occurrence of a substring from another string):

public static class StringExtensions
{
    public static string RemoveFirst(this string source, string remove)
    {
        int index = source.IndexOf(remove);
        return (index < 0)
            ? source
            : source.Remove(index, remove.Length);
    }
}

Now for the Regex implementation:

                if (line.StartsWith("    STDMETHOD"))
                {
                    Regex regex = new Regex(@"\(.*?\)");
                    MatchCollection matches = regex.Matches(line);
                    string newstr = String.Format(@"HRESULT __stdcall WrapIDirect3DCubeTexture9::{0}({1})", matches[0].Value.Trim('(', ')'), matches[1].Value.Trim('(', ')'));
                    newstr = newstr.Replace("THIS_ ", "");
                    textBox2.AppendText(newstr + Environment.NewLine + "{" + Environment.NewLine + Environment.NewLine + "}" + Environment.NewLine);
                }
Community
  • 1
  • 1
user1632018
  • 2,485
  • 10
  • 52
  • 87
  • Not quite sure I understand what you want here. You want us to write you a regex? or do you want us to tell you which will work better? They will both work, the devil is in the details. – crthompson Nov 24 '14 at 21:11
  • 3
    This is what you are asking: "Here is an input string, and an output string. Please write me code that will translate this". I would suggest trying to implement this yourself, using either regex - or .Net string manipulation (both will work). After you have tried, then it would be more appropriate to ask for help with what you may be stuck on. – caesay Nov 24 '14 at 21:19
  • @caesay Sorry I wasn't clear before. I decided to implement both, and used your string.format idea for the Regex. I have fixed my question along with my solutions. – user1632018 Nov 25 '14 at 01:04

1 Answers1

1

I will write you some code to help get you started.

If you start with a minimal output string containing the variables, it will be easier to see what needs to be done, so:

String.Format(@"HRESULT __stdcall WrapIDirect3DCubeTexture9::{0}({1}) 
{{

}}", "methodName", "arguments");

Here we can see there are two items we need to extract from the original string, the method name - and the arguments. I would suggest using a regex to match what is in the parenthesis in the original string. This will give you two matches, the method name - and the arguments. You will need to do post-processing on the arguments string but this will give an idea.

caesay
  • 16,932
  • 15
  • 95
  • 160
  • 1
    The braces need to be `{{` and then `}}` when not doing `{0}` and `{1}`. Otherwise you'll get an exception. – David Sherret Nov 24 '14 at 21:41
  • Thank you. I ended up doing both, and used your ideas for the regex implementation. I posted them in the question. I will be using the regex variation. – user1632018 Nov 25 '14 at 01:06