Re: alternatives to indent for c++

From: zentara (zentara_at_highstream.net)
Date: 11/26/04


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


Relevant Pages

  • Re: Why is java considered a language for "web" or "internet" programming?
    ... I don't remember reading exact things about Perl interpreter, ... load and parse bytecodes. ... I think, integer in Perl or Python or other scripting languages is some structure, with at the least field for type and value, and pointer to this structure you hold in 'i' variable. ... const int NNUM = 1000000; ...
    (comp.lang.java.help)
  • Writing a module, Segmentation fault
    ... For the access to IGT+, I am writing a Module in Perl. ... banking::igtplus - Perl extension for blah blah blah ... int igt_ack( ... dXSARGS; ...
    (comp.lang.perl.modules)
  • Re: reading a few bytes into a file
    ... This is my first adventure into perl, ... so I am at a loss for a lot of data on the format of the file. ... and return that as an int to me. ... I just read the docs on unpack, and I do not follow what the purpose ...
    (perl.beginners)
  • Re: Using References to Formats, Examining Scalars With Devel::Peek
    ... the format to be able to refer to it by another name didn't occur to me. ... Seems most of the fields are the same as for the rest of the Perl ... from integer to double and how frequently the string portions are updated. ... Using an operator or a built-in with a double and an int usually converts ...
    (comp.lang.perl.misc)
  • Re: Curses-perl that doesnt live up to its name?
    ... int wrap_baudrate{ ... transparent to a perl n00b like me. ... perl Curses, so perl Curses must be doing something to resolve the ... compiler warning about the redeclaration. ...
    (comp.lang.perl.modules)