Last update September 18, 2005

H to D with sed

Converting C header files

This is how I converted the header files for libgii.

1. Figure out what files you need to convert. Start with the top level library include (in my case gii.h).

You can use cpp to extract the dependencies (sub include files needed).

#cpp -M gii.h
 - or to extract only local dependancies - 
#cpp -MM gii.h

I just walked through them by hand. I determined that I need:

gii.h gii-defs.h events.h system.h keyboard.h

2. Copy all these files to a working directory

#cp gii.h gii-defs.h events.h system.h keyboard.h /home/th/ggi_to_d
#cd ~/ggi_to_d

3. Modify includes to be local and ignore system includes. (I learned how to use regular expressions and sed during the course of this project. Wow, where have you been all my life sweet little thing.)

My sed script looks like: gii.sed

#Make all ggi headers local
s/#include\s+<ggi\/([[:alnum:]._-]+)>\s*/#include "\1"/
s/#include\s+<([[:alnum:]._\/-]+)>\s*/#include <dummy\/\1>/

It converts #include <ggi/filename.h> to #include "filename.h"

#sed -r -ibak -f gii.sed *.h

Now when I run

#cpp -MM gii.h
it lists only the files I am working with.

4. Convert #defines to const expressions


# ulong definitions
s/#define\s+(\w+)\s+([[:alnum:][:space:][:punct:]()=<>|_,+'\*\/-]+L)\s*$/const ulong \1 = \2; /

# uint definitions
s/#define\s+(\w+)\s+([[:alnum:][:space:][:punct:]()=<>|_,+'\*\/-]+)\s*/const uint \1 = \2; /

# Macro function definitions
s/#(define\s+\w+\s*\(.*)(\\*\s*)$/\/*!Make a function out of this!*\/ \/\/\1 \2/

# Imports:
s/\s*#\s*include\s+<stdio\.h>.*/import std.c.stdio;/
s/\s*#\s*include\s+<stdlib\.h>.*/import std.c.stdlib;/
s/#(include\s*<[[:alnum:][:space:]_\/.]+>)/\/*! We don't want global includes*\/\/\/\1/

#sed -r -ibak2 -f h2d.sed *.h

5. Repair file by hand

open your .h files and fix it. In my case there were a lot of #defines for version control. I didn't need any of them so I removed them. I also had to convert some define functions. These you can safely wait to do untill after using deeify in step 6.

6. Use h2d to convert typedefs and structs

Run deeify (from

I changed the gcc call in the script to keep comments (-C).

#./deeify gii.h

7. Fix the .d file Convert the define functions. If it is a define for use in user code, I convert it to a regular function. If it is a define for readibility in the include file, we use a template.


Hope this helps.

For this library, 3000 lines of include file defines that I did not have to fix line by line. (And shame on the developers of ggi for being so silly with their include files)


FrontPage | News | TestPage | MessageBoard | Search | Contributors | Folders | Index | Help | Preferences | Edit

Edit text of this page (date of last change: September 18, 2005 3:07 (diff))