File: class/Extras/Code/Text/cheader.py

#! /usr/local/bin/python
import sys, re
from string import strip

pattDefine = re.compile(                               # compile to pattobj
    '^#[\t ]*define[\t ]+([a-zA-Z0-9_]+)[\t ]*(.*)')   # "# define xxx yyy..."

pattInclude = re.compile(
    '^#[\t ]*include[\t ]+[<"]([a-zA-Z0-9_/\.]+)')     # "# include <xxx>..."

def scan(file):
    count = 0
    for line in file:                                # scan line-by-line
        count += 1
        matchobj = pattDefine.match(line)            # None if match fails
        if matchobj:
            name = matchobj.group(1)                 # substrings for (...) parts
            body = matchobj.group(2) 
            print count, 'defined', name, '=', strip(body)
        else:
            matchobj = pattInclude.match(line)
            if matchobj:
                start, stop = matchobj.span(1)       # start/stop indexes of (...) 
                filename = line[start:stop]          # slice out of line
                print count, 'include', filename     # same as matchobj.group(1)

if len(sys.argv) == 1:
    scan(sys.stdin)                    # no args: read stdin
else:
    scan(open(sys.argv[1], 'r'))       # arg: input file name



[Home page] Books Code Blog Python Author Train Find ©M.Lutz