Recursively Include C headers

Difference between revision 15 and current revision

Summary: Rollback to 2011-11-28 16:58 UTC

No diff available.

Problem Specification

i want to generate one big .hpp file out of multiple .hpp files and want to include the text of all #include "foo.hpp" file recursively

Code

#!/usr/bin/awk -f
# by goedel
BEGIN {
  include_path = "."
  iplen = split(include_path, ip, /:/)
}
{ print_line($0) }

function print_file(file,   line)
{
  while (getline line < file > 0)
    print_line(line)
  close(file)
}
function print_line(line,   f, file)
{
  if (match(line, /^[ \t]*#include[ \	]*"[^"]+"/)) {
    f = substr(line, RSTART, RLENGTH)
    sub(/^[ \t]*#include[ \t]*"/, "", f)
    sub(/"/, "", f)
    file = complete_path(f)
    if (!(file in list_of_paths)) {
      list_of_paths[file] = 1
      print_file(file)
    }
  } else
    print line
}
function complete_path(file, p)
{
  if (file ~ /^[\/.]/)
    return file
  for (i = 1; i <= iplen; ++i) {
    p = ip[i]
    if (p == "")
      p = "."
    if (p !~ /\/$/)
      p = p "/"
    p = p file
    if (system(sprintf("/usr/bin/test -f \"%s\"", p)) == 0)
      return p
  }
  printf("error:%s: file not found in include_path\n", f) | "/bin/cat 1>&2"
  exit(1)
}