Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 25 Aug 2006 01:00:12 +0300
From:      "Matti J. Karki" <mjk@iki.fi>
To:        "=?ISO-8859-1?Q?Kyrre_Nyg=E5rd?=" <kyrreny@broadpark.no>
Cc:        questions@freebsd.org
Subject:   Re: Code beautifiers, anyone?
Message-ID:  <1b15366e0608241500j5f1b4662p9a0d9cd811de841f@mail.gmail.com>
In-Reply-To: <7.0.1.0.2.20060824192439.02386de8@broadpark.no>
References:  <7.0.1.0.2.20060824145822.0194fc10@broadpark.no> <1b15366e0608240618j62d41ad3j537f095b2e566ed5@mail.gmail.com> <7.0.1.0.2.20060824192439.02386de8@broadpark.no>

next in thread | previous in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]
On 8/24/06, Kyrre Nygård <kyrreny@broadpark.no> wrote:
>
> Perhaps you could share with us whatever scripts you've written?
>

Totally forgot to include the actual intendation script.

There should be a Python script attached to this mail. Please note,
that the script is not a silver bullet! It was designed to clean up
some pretty messed up C code. Usually I study the coding style before
creating this kind of clean-up-scripts. Also, the code is not very
clean itself (pretty ironic, I guess) :) It's just a hack to take care
of one step of the cleaning process.


        -Matti

[-- Attachment #2 --]
import sys
import re
import os

INDENTSTR = "    "

f = open(sys.argv[1], "r")
inbuffer = f.read()
f.close()

outbuffer = ""
indent = 0
indentnext = 0

inbuffer = re.sub('\n +', '\n', inbuffer)
inbuffer = re.sub('\t+', '', inbuffer)
inbuffer = re.sub('\) *?\n\{', ') {', inbuffer)
inbuffer = re.sub('\) *?{', ') {', inbuffer)
inbuffer = re.sub('else *?\n{', 'else {\n', inbuffer)
inbuffer = re.sub('{ *?(.+?\n)', '{\n\g<1>', inbuffer)
inbuffer = re.sub('(\n.+?)}', '\g<1>\n}', inbuffer)
inbuffer = re.sub('\n +', '\n', inbuffer)

for chr in inbuffer:
	if chr == "{":
		indent += 1
		outbuffer += "{"
		continue
	if chr == "}":
		indent -= 1
		outbuffer += indent * INDENTSTR + "}"
		indentnext = 0
		continue
	if chr == "\n":
		outbuffer += "\n"
		indentnext = 1
		continue
	if indentnext == 1:
		outbuffer +=  indent * INDENTSTR + chr
		indentnext = 0
	else:
		outbuffer += chr

outfilename = sys.argv[1]
oldfilename = outfilename + ".bak"

os.rename(outfilename, oldfilename)

f = open(outfilename, "w")
f.write(outbuffer)
f.close()

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1b15366e0608241500j5f1b4662p9a0d9cd811de841f>