A trivial tool to allow a single file to contain both the header file and its implementation so that the number of files in a source tree can be minimized.
I haven't changed this tool in a while despite using it for several projects. It was simple enough to get right.
The successor tool I'm working will be part of my omni-c project. This version will simply automatically generate a header file from a C file. It's smart enough to extract all of the function prototypes, inlined functions, structure, typedefs, macros, etc. Although I've had success putting almost 5K lines of code through it's parser, it's under enough flux that I can't recommend it right now. It may be ready for a demo release in mid-september though I'll need an extensive test suite and I'll want to try it out on code in the while before feeling like it's ready to demo.
Writing header files is a pain - one which we don't change (we only change where the stuff you would write lives). On the other hand, having almost twice as many files in a directory can be avoided. This is much friendlier for editing as you don't need to switch to the header file during editing, it's easier to search and replace by having just one file, etc.
If a C file contains a string like this:
#ifndef _FOOBAR_H_
then everything between that line a line like (and including those two lines):
#endif /* _FOOBAR_H_ */
is extracted to a file named the same as the C file with the suffix ".h".
One key to understanding why this works is that the C file itself should be including it's own header file anyways so having a copy of the header at the top of the C file is that same as including it (and even if you include it again, it should not have an effect).
The source file is never modified by this tool. Additionally, the tool will not overwrite a file that doesn't appear to be generated by this tool so it should be safe to use automatically from a Makefile (or other build system of your choice). Lastly, if the generated header file does exist and wouldn't change, we don't modify the previously generated header file so that modification times are preserved.
- should generated header files be checked in?
Probably not. This partially defeats the purpose of having fewer source files. See https://github.com/jasonaaronwilson/armyknife-scheme for an example of the usage of this tool. (In the future we may have another tool to help with this).
- add command line argument parsing so we can add features in the future (a target directory option would be nice)
- cleanup *.h.tmp and generate tmp files to /tmp/
- add some automated tests