Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hey All!

I made a code generator that works like this:

1) Read from a header file and produce an intermediate file (named File A) that has tags(inserted by me) that facilitates sorting out the information that is read from the header by me for:

-Function declaration

-Actual code for functions with arguments/params etc everything.

2) Read this intermediate file i.e File A and process the information for making another intermediate file (named File B) that I use for another purpose later on. :P File B uses File A and the result is an intermediate code file that has the following:

-Class Declaration

-Function Definitions i.e Actual code for functions with arguments/params etc everything (whatever is dictated by the requirements is output to this intermediate file B) along with some information that I need later, ;)

3) Read File B and use a config file named Input.config and use the information from the config file to alter the contents of the File B to produce the final processed file named File C(quite appropriate, isn¡¦t it? ;))

The input config file has stuff that I need to replace in File B for example Class Name etc.

What is the problem:

-Formatting is handled as desired by me in File B to get the results I¡¦m looking for and HERE IS THE PROBLEM:

1)What I need in File C is formatted code that wraps after 78 characters and the length of lines SHOULD NOT exceed 78 characters

2)Indented code inside if/for loops etc etc etc etc

For Example: File B

class Class_Name:public BaseClass { blah blah blah public: blah blah blah }

I CAN in File B control the formatting to some degree. ļ What I want is this in File C: File C

class ClassMyClassNameNoMatterHowLongItIsName:public BaseClass { blah blah blah public: blah blah blah }

MyClassNameNoMatterHowLongItIs is read from the config file and right now I use pattern matching and whenever I encounter Class_Name, I replace it with the information read from the input config file , Here in lies the problem, if I just use pattern matching and just *replace* the stuff , then , I have no control on the wrapping etc and in all likelihood, I lose most of the formatting information that I preserved in File B.

My questions are:

1) How and what do I do in order to preserve some sort of formatting in File C (my FINAL processed output file) after all the tags etc have been replaced?

2) Should I use a C++ program instead of a PERL script to generate the code in File C instead?

I must add here:

1) I¡¦m a C++ Programmer

2) I don¡¦t know PERL and all that I have managed to accomplish here has been rather painstakingly after debugging with print statements, so please reserve any comments on my illiteracy , I do know that I DON¡¦T know PERL.:p

3) I don¡¦t have nay other experience in PERL or scripting

Thanks in advance for your time and my apologies for the length of the post.

edited: Wed Oct 22 12:51:33 2003 by jeffa - readmore tag

Replies are listed 'Best First'.
Re: Text Wrapping
by tachyon (Chancellor) on Oct 22, 2003 at 04:25 UTC

    You can certainly do it in Perl but why bother? Google for 'pretty print C++' to find all sort of ready made solutions. Like http://www.softpanorama.org/Tools/beautifiers.shtml Here is one in C++ ASBeautifier.cpp

    Perl has all sorts of great pre rolled libraies with amazing coverage at the CPAN (Comprehensive Perl Archive Nework) http://cpan.org For example there is Text::Wrap but I would go with a C++ pre-coded beutifier unless you want to have a fun and useful Perl project to tool around with. You may like stuff like Inline:CPP which will let you embed C++ into your Perl so you can mix and match.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Thanks Tachyon!

      I explored the option (as suggested by you) to use a C++ pre coded beautifier and I discovered the following:

      1) I am unable to get the ENTIRE source code for ASBeautifier and get some obvious compiler errors because of missing source code.

      2) From the link here: http://www.softpanorama.org/Tools/beautifiers.shtml , I checked out the program namely: Artistic Style 1.13.8 but from the options provided, I ascertain that this program is only for formatting and indentation and these do meet my requirements as well BUT I dont see how I could use this for a specific objective that I have in mind:

      "The length of each line of code (that can be compiled by a standard C++ compiler) must NOT exceed 78 characters" Maybe I need to play around a bit with this program in order to get the desired results or maybe add my own code to get this feature going.

      Have you used the program?

        It is on source forge, now called astyle which contains ASBeautifier plus all the headers and docs. http://sourceforge.net/projects/astyle Try compiling it and running it on some of your code, you may be pleasantly surprised (aka it manages line lengths automatically).....

        Release Notes In Artistic Style 0.9.2 (24 November 1998) Fixed a serious bug which led to a maximal supported source code line +size of 128 characters!!!

        :-)

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      ASBeautifier.cpp is just _one_ sourcefile of a package. Of course you cannot compile it without the whole AStyle class. Go to the download section of the site and get the whole thing.

        Yes, some people! aka No shit Sherlock. You give some pople as Silver spoon and they expect you to feed them too!

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Text Wrapping
by tachyon (Chancellor) on Oct 22, 2003 at 05:04 UTC

    While I strongly recommend a robust and tested pretty printer solution here is an example of how you could do it in a few lines of Perl. Not really very robust but you could do:

    Update. Opps, wrong lang. Changed from trailing \ to \t indent continuation

    my $wrap = 76; my $tab = 4; # spaces occupied by tab while(<DATA>) { do{ print; next } if length($_) < $wrap; do{ print "\n"; next } if m/^\s*$/; s/^(\s*)(?=\S)// or die "Choked on $_\n"; my $indent = $1; my $l_wrap = $wrap - length($indent) - $tab; my @bits = $_ =~ m/\G(.{1,$l_wrap})\s+/gc; for ( my $i = 0; $i <@bits; $i++ ) { print $i == 0 ? "$indent$bits[$i]\n" : "$indent\t$bits[$i]\n"; } } __DATA__ class ClassMyClassNameNoMatterHowLongItIsName:public BaseClass { blah blah blah blah blah blah blah blah blah blah blah blah blah b +lah blah blah blah blah blah blah blah blah blah blah blah blah blah +blah blah blah blah blah blah blah blah blah blah blah blah blah blah + blah public: blah blah blah blah blah blah blah blah blah blah blah blah blah b +lah blah blah blah blah blah blah blah } __END__ class ClassMyClassNameNoMatterHowLongItIsName:public BaseClass { blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah bl +ah blah blah blah blah blah blah blah blah blah blah blah blah bl +ah blah blah blah public: blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Text Wrapping
by Anonymous Monk on Oct 22, 2003 at 03:43 UTC
    How do I post from my login name? I am logged in , yet the OP apprears as anonymous monk and not me. :(

      You post from your login name by actually logging in :-) The fact that your OP appears as AM indicates that you didn't login first.

      -- vek --