Re: alternatives to indent for c++
From: zentara (zentara_at_highstream.net)
Date: 11/26/04
- Next message: Chris Schumacher: "Broken copy constructor."
- Previous message: Richard Seguin: "A little disappointed"
- In reply to: Philipp Brandl: "Re: alternatives to indent for c++"
- Next in thread: B. v Ingen Schenau: "Re: alternatives to indent for c++"
- Reply: B. v Ingen Schenau: "Re: alternatives to indent for c++"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 26 Nov 2004 13:38:34 -0500
On Fri, 26 Nov 2004 15:59:34 +0100, Philipp Brandl <brandl@irt.de>
wrote:
>zentara wrote:
>> I'm coming from a Perl background, and am starting to learn
>> basic c++ . I've run into a problem with using indent on c++
>> source code. (You know, I'm cut'n'pasting numbered line code
>> from html files, and running it thru a perl script to strip the line
>> numbers and leading whitespace, then thru indent.)
>Hi and welcome to the wonderful world of C++ programming :)
>
>There's a useful FAQ on code beautification which lists some
>beautification programs and describes how to make sure you can trust
>them. Look here:
>http://www.faqs.org/docs/Linux-HOWTO/C-C++Beautifier-HOWTO.html
>For myself I use BCPP and I haven't encountered any problems yet.
Yeah, I got the latest version and it works fine. I thought I had tried
it, but it must have been an early version off of sunsite.
>A little suggestion: Maybe after all it's a good idea not just to copy
>paste the source code but to actually type it by yourself. This is the
>best way of starting to learn a programming language. >
>Greetings, Phil
I wish I were a good typist...but no, just an agile 2 fingers. :-)
Cut'n'pasting is my thing.
If anyone is interested, here is the Perl script I use to transform
cut'n'pasted code from books like "C++ in 21 Days".
Here is the script, followed by sample input and output.
I've noticed a few typos in the listings,
like semi-colons instead of colons,
or a missing semicolon, and my regex accounts for that.
########################################################
#!/usr/bin/perl
#need a recent version of Perl > 5.8 for
#writing FH to a variable
$newfile = $ARGV[0] or die "need a file.cpp\n";
$outfile = 0;
open (FH, $newfile);
open(OUT, ">> $outfile");
while(<FH>) {
$_ =~ s/^\s*[\d]+(?:\:|;)*\s+//g;
# gets any leading spaces, numbers, 0 or more colon,semicolon(typos)
# and whitespace in front of code strings and leaves \n
$_ =~ s/^\s+//g;
#gets tabs and ws in front of strings and leaves \n
print OUT $_; # in case there is no numbered file
}
print OUT "\n"; #end code with a blankline to avoid warning
close FH;
close OUT;
rename($outfile, $newfile);
chmod 0644,$newfile;
#the default bcpp.cfg file must be in path with bcpp
system("bcpp -fi $newfile -no");
unlink "$newfile.orig"; #comment out if you don't want the backup file
exit;
__END__
########################################################
Sample input, with exaggerated typos in the numbers
1: // Begin rect.cpp
2: #include "rect.hpp"
3: Rectangle::Rectangle(int top, int left, int bottom, int right)
4 {
5: itsTop = top;
6: itsLeft = left;
7: itsBottom = bottom;
8; itsRight = right;
9:
10: itsUpperLeft.SetX(left);
11: itsUpperLeft.SetY(top);
12:
13: itsUpperRight.SetX(right);
14: itsUpperRight.SetY(top);
15:
16: itsLowerLeft.SetX(left);
17: itsLowerLeft.SetY(bottom);
18:
19: itsLowerRight.SetX(right);
20: itsLowerRight.SetY(bottom);
21: }
22:
23:
24: // compute area of the rectangle by finding corners,
25: // establish width and height and then multiply
26: int Rectangle::GetArea() const
27: {
28: int Width = itsRight-itsLeft;
29: int Height = itsTop - itsBottom;
30: return (Width * Height);
31: }
32:
33: int main()
34: {
35: //initialize a local Rectangle variable
36: Rectangle MyRectangle (100, 20, 50, 80 );
37:
38 int Area = MyRectangle.GetArea();
39:
40: cout << "Area: " << Area << "\n";
41; cout << "Upper Left X Coordinate: ";
42: cout << MyRectangle.GetUpperLeft().GetX();
43; return 0;
44: }
#########################################################
Sample ouput:
// Begin rect.cpp
#include "rect.hpp"
Rectangle::Rectangle(int top, int left, int bottom, int right)
{
itsTop = top;
itsLeft = left;
itsBottom = bottom;
itsRight = right;
itsUpperLeft.SetX(left);
itsUpperLeft.SetY(top);
itsUpperRight.SetX(right);
itsUpperRight.SetY(top);
itsLowerLeft.SetX(left);
itsLowerLeft.SetY(bottom);
itsLowerRight.SetX(right);
itsLowerRight.SetY(bottom);
}
// compute area of the rectangle by finding corners,
// establish width and height and then multiply
int Rectangle::GetArea() const
{
int Width = itsRight-itsLeft;
int Height = itsTop - itsBottom;
return (Width * Height);
}
int main()
{
//initialize a local Rectangle variable
Rectangle MyRectangle (100, 20, 50, 80 );
int Area = MyRectangle.GetArea();
cout << "Area: " << Area << "\n";
cout << "Upper Left X Coordinate: ";
cout << MyRectangle.GetUpperLeft().GetX();
return 0;
}
#################################################
-- I'm not really a human, but I play one on earth. http://zentara.net/japh.html
- Next message: Chris Schumacher: "Broken copy constructor."
- Previous message: Richard Seguin: "A little disappointed"
- In reply to: Philipp Brandl: "Re: alternatives to indent for c++"
- Next in thread: B. v Ingen Schenau: "Re: alternatives to indent for c++"
- Reply: B. v Ingen Schenau: "Re: alternatives to indent for c++"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|