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);
}