From: hlf@nic.cerf.net (Howard Ferguson)
Newsgroups: comp.lang.c++,comp.sys.next.misc
Subject: Re: Looking for C++ Pretty-Printer/Formatter
Date: 26 Jan 1993 16:48:22 GMT
Organization: CERFnet Dial n' CERF Customer Group
NNTP-Posting-Host: nic.cerf.net

In article <1993Jan26.022814.27915@netcom.com> dx@netcom.com (dx) writes:
>Can anyone point me to a C++ formatter?  What we're looking for should
>simply rearrange the C++ code, rather than generate TeX or PostScript.
>We'd like to find a formatter that is highly tailorable, as we'd like
>to use it when checking code in and out of a central store, formating
>the code to each programmer's liking on check-out, and reformating it
>to a company-wide standard on check-in.
>

I asked this exact question a few weeks ago and the best I got was the following script
which hacks at the existing cb ( on most unix systems) which is desgned for C. 


From: jdelsign@star-trek.bbn.com (John Delsignore)             \-(1)--(2)--[2]
[2] Re: beautify for C++
Date: Tue Jan 19 06:27:15 PST 1993
Organization: Bolt Beranek and Newman Inc., Cambridge MA
NNTP-Posting-Host: archive.bbn.com

In article <1jfhptINNg2g@news.cerf.net> hlf@nic.cerf.net (Howard Ferguson) writes:
>
>I am looking for a filter that I can run my C++ code through
>that will apply some standard formatting rules to it.
>
>cb does this for C code, but is there a C++ specific version.

I pulled the following out of UnixWorld over a year ago, I forget who
the original author was.  It uses sed to convert the '::' token into a
string that's not likely to appear in your code, and '// comment'
style comments into '/*C++ comment C++*/' comments.  It then pipes the
result to cb and then through another sed filter to undo the
conversion.

====================cut here====================
:
# Use cb and sed to beautify C++ code
#
symbol=MAGIC_C_PLUS_PLUS_COLON_COLON_TOKEN
sed -e 's,\(//.*\),/*C++ \1 C++*/,' \
    -e "s,::,$symbol,g" $* |
cb |
sed -e 's,/\*C++ \(.*\) C++\*/,\1,' \
    -e "s,$symbol,::,g"
----------------------------------------------------------------------
John V. DelSignore, Jr.                 jdelsign@bbn.com
BBN Systems and Technologies
10 Moulton Street                       Phone: (617) 873-2970
Cambridge, MA 02138                     FAX:   (617) 873-3315
----------------------------------------------------------------------

Iplayed with this script and found indent ( again on most unix -- gnu copy on the net
)
was much more satisfactory. I also added a couple of lines to make class declarations
a bit neater. 

However I refuse to believe that nobody has written a proper copy of a C++
beautifier. I would still like to find one as this hack still has limitations

The following in my version of the above script. If you find any more improvements
please let me know. In particular I would like to enforce the format

int*	x;
instead of
int	*x;

I managed to do this in standalone cases but it is more difficult to hack
it when it is in the prototype of a function, or the top of a for loop :
for (int *x; *x>10 .....


# Use indent and sed to beautify C++ code
#
#options to indent are as follows
# -bl   -- puts { on its own line
#       -- following for, while class etc
# -di4  -- lines up text following keyword
#       -- 4 spaces after start of keyword
# -nce  -- forces else keyeword onto a line
#       -- on its own
symbol=MAGIC_C_PLUS_PLUS_COLON_COLON_TOKEN
sed -e 's,\(//.*\),/*C++ \1 C++*/,' \
    -e "s,::,$symbol,g" $* > String.cb.tmp
indent String.cb.tmp -bl -di4 -nce
cat String.cb.tmp |
sed -e 's,/\* C++ \(.*\) C++ \*/,\1,' \
    -e "s,$symbol,::,g" \
    -e "s/[\     ]*private:$/private:/" \
    -e "s/[\     ]*public:$/private:/" \
    -e "s/[\     ]*protected:$/private:/" \
    -e "s/:public/: public/"


	Have fun
		hlf
Newsgroups: comp.lang.c++