summary refs log tree commit diff stats
path: root/tools/cmerge.nim
blob: e461b00b0dc804644c227244125a91da4c5160ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Simple tool to merge C projects into a single C file

import os, strtabs, pegs

proc process(dir, infile: string, outfile: TFile, processed: PStringTable) =
  if processed.hasKey(infile): return
  processed[infile] = "True"
  echo "adding: ", infile
  for line in lines(dir / infile):
    if line =~ peg"""s <- ig '#include' ig '"' {[^"]+} '"' ig
                     comment <- '/*' !'*/'* '*/' / '//' .*
                     ig <- (comment / \s+)* """:
      # follow the include file:
      process(dir, matches[0], outfile, processed)
    else:
      writeln(outfile, line)

proc main(dir, outfile: string) =
  var o: TFile
  if open(o, outfile, fmWrite):
    var processed = newStringTable([outfile, "True"])
    for infile in walkfiles(dir / "*.c"):
      process(dir, extractFilename(infile), o, processed)
    close(o)
  else:
    quit("Cannot open for writing: " & outfile)

if ParamCount() != 2:
  quit "Usage: cmerge directory outfile"
else:
  main(ParamStr(1), addFileExt(ParamStr(2), "c"))