.h to .d advice needed

nobody nobody at mailinator.com
Mon Aug 7 01:32:14 PDT 2006


Emp wrote:
...

> Is there some program which can do these things automated?
> Thx for any advice/help. 

htod seems to be the tool you are looking for.

   http://www.digitalmars.com/d/htod.html

   While D is binary compatible with C code, it cannot compile C 
code nor C header files. In order for D to link with C code, the 
C declarations residing in C header files need to be converted 
to a D module. htod is a migration tool to aid in convering C 
header files.

For people reading this thread who are not familiar with RegExps 
changing

   #define FOO 1

to

   const int FOO = 1;

is easy if you take the time to understand how RegExp can 
generalize many patterns. Regular and replacement expressions 
should be available in any basic code editor. They are also 
included in Phobos in std.regex. The docs contain a link to this 
page which documents D's variation of regexp:

   http://www.digitalmars.com/ctg/regular.html


Decimal constants
   [0-9]+

Octal Constants
   0[0-9]+

Hexadecimal Constants
   0[xX][0-9A-F]

Variable Names start alpha or under and allow digits, '-' and '_'
   [a-z_][0-9a-z_-]+

They are seperated by one or more spaces or tabs
   [ \t]+

Putting it all together
   #DEFINE[ \t]+([a-z_][0-9a-z_-]+)[ 
\t]+(([0-9]+)|(0[0-9]+)|(0[x][0-9A-F]))


Now you can search (ignoring case) and replace all those 
patterns with
   const int $1 = $2;


So

   #DEFINE     FOO    1
   #DEFINE FOO-bar  010
   #DEFINE FOO_baz 0x10

becomes

   const int FOO = 1;
   const int foo-bar = 010;
   const int __fOo_bAz = 0x10;




More information about the Digitalmars-d-learn mailing list