I have a auto-generated .c file containing const’s and enum’s I need to access from .Net. I would like to run a pre-build script that would convert the .c file (with C syntax) to a .cs file (with C# syntax). The files are pretty simple so it should be possible to script my way out of this problem.
Here is a section of a .c file
#define Bitfile "Test.bit"
static const char* const BitfileSignature = "A6A316954DA417A2F886F3B0035019B1";
typedef enum
{
ControlBool_TestBool_Address = 0x815E,
ControlBool_IrqHandshakeWriteToDma_Address = 0x8112,
ControlBool_WriteToDma_Address = 0x810E,
ControlBool_stop_Address = 0x813A,
} ControlBool;
And I would like to convert that into something like this
public static class MyConstants
{
public static string Bitfile = "Test.bit";
public static string BitfileSignature = "A6A316954DA417A2F886F3B0035019B1";
}
public enum MyEnum
{
ControlBool_TestBool_Address = 0x815E,
ControlBool_IrqHandshakeWriteToDma_Address = 0x8112,
ControlBool_WriteToDma_Address = 0x810E,
ControlBool_stop_Address = 0x813A,
}
Does anyone know a good tool I could use for this conversion? Preferably a tool that comes with VS2010.
You can try T4 Text Templates to generate your code files from. It’s nicely integrated into Visual Studio and once you get the hang of it a fairly easy to use and powerful text templating engine. You’ll need to define 2 separate templates (one for C# and one for C) of course.
[EDIT] : It appears i did not read the whole question. The OP problem is converting from C to C#. I still stand by my advice to use T4 in this case but for reading the C source you’ll need to write some custom parsing code. Considering that it involves only enum type definitions the parsing code should be fairly trivial. On the other hand if the C defs are already generated from some external definition (like an xml file somewhere) then you could treat that as a data source