Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

PRINTING LOTS OF OUTPUT
Dear All,

Apologies if this is OT or answered elsewhere, but I am looking for
some help.

I am using gawk to generate a C file as part of a build process, and
as such, the gawk script
does a lot of printing. Currently, I am using lots of print statements
(1 per line) to actually output the text. The body of my script looks
similar too:

print "int main(void) "
print "{"
...
print " return (0);"
print "}"

... and basically, I was wondering if there is a better way to produce
lots of static text ?

Many thanks,

James

Report this thread to moderator Post Follow-up to this message
Old Post
James Pascoe
11-16-04 11:50 PM


Re: PRINTING LOTS OF OUTPUT
In article <fe6eb44f.0411150757.5f62fd22@posting.google.com>,
James Pascoe <james@james-pascoe.com> wrote:
>Dear All,
>
>Apologies if this is OT or answered elsewhere, but I am looking for
>some help.
>
>I am using gawk to generate a C file as part of a build process, and
>as such, the gawk script
>does a lot of printing. Currently, I am using lots of print statements
>(1 per line) to actually output the text. The body of my script looks
>similar too:
>
>print "int main(void) "
>print "{"
>...
>print " return (0);"
>print "}"
>
>... and basically, I was wondering if there is a better way to produce
>lots of static text ?

The short answer is "not really".  There's nothing really wrong with the
above, but sometimes I bunch them together, like this:

print "int main(void)\n{\n return (0);\n}"

at some cost in readability.  Or, you could try:

OFS="\n"
print "int main(void)",
"{",
" return (0);",
"}"


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
11-16-04 11:50 PM


Re: PRINTING LOTS OF OUTPUT

Kenny McCormack wrote:

> In article <fe6eb44f.0411150757.5f62fd22@posting.google.com>,
> James Pascoe <james@james-pascoe.com> wrote:
> 
>
>
> The short answer is "not really".  There's nothing really wrong with the
> above, but sometimes I bunch them together, like this:
>
>     print "int main(void)\n{\n return (0);\n}"
>
> at some cost in readability.  Or, you could try:
>
>     OFS="\n"
>     print "int main(void)",
> 	"{",
> 	" return (0);",
> 	"}"
>

For that example, just set it up in one or more variables outside the
script, then print those, e.g.:

echo "" | gawk '{printf "%s%s",start,end}' start="void
main(void)
{
" end="	return 0;
}
"

You can escape the newlines between each variable definition if you
prefer for readability, e.g.:

echo "" | gawk '{printf "%s%s",start,end }' \
start="\
void
main(void)
{
" \
end="
return 0;
}
"

If you have other cases where the output gets built dynamically during
the execution of the program, we'd need to see examples to offer advice,
but the above works fine for static output.

By the way, no big deal but "return" is a language builtin in C, not a
function, so it's better style to not surround it in unnecessary brackets.

Regards,

Ed.



Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
11-16-04 11:50 PM


Re: PRINTING LOTS OF OUTPUT
James Pascoe wrote:
> Dear All,
>
> Apologies if this is OT or answered elsewhere, but I am looking for
> some help.
>
> I am using gawk to generate a C file as part of a build process, and
> as such, the gawk script
> does a lot of printing. Currently, I am using lots of print statements
> (1 per line) to actually output the text. The body of my script looks
> similar too:
>
> print "int main(void) "
> print "{"
> ...
> print " return (0);"
> print "}"
>
> ... and basically, I was wondering if there is a better way to produce
> lots of static text ?

My answer may be OT, too :-)  But could it be that you are using an
inappropriate tool? If the main task is to create C templates, even
parameterized templates, you might want to have a look at the "here
document" feature of bourne-like shells.

cat <<- EOT
int main(void)
{
..
return 0;
}
EOT


Janis

Report this thread to moderator Post Follow-up to this message
Old Post
Janis Papanagnou
11-16-04 11:50 PM


Re: PRINTING LOTS OF OUTPUT

Kenny McCormack wrote:

> In article <fe6eb44f.0411150757.5f62fd22@posting.google.com>,
> James Pascoe <james@james-pascoe.com> wrote:
> 
>
>
> The short answer is "not really".  There's nothing really wrong with the
> above, but sometimes I bunch them together, like this:
>
>     print "int main(void)\n{\n return (0);\n}"
>
> at some cost in readability.  Or, you could try:
>
>     OFS="\n"
>     print "int main(void)",
> 	"{",
> 	" return (0);",
> 	"}"
>

For that example, just set it up in one or more variables outside the
script, then print those, e.g.:

echo "" | gawk '{printf "%s%s",start,end}' start="void
main(void)
{
" end="	return 0;
}
"

You can escape the newlines between each variable definition if you
prefer for readability, e.g.:

echo "" | gawk '{printf "%s%s",start,end }' \
start="\
void
main(void)
{
" \
end="
return 0;
}
"

If you have other cases where the output gets built dynamically during
the execution of the program, we'd need to see examples to offer advice,
but the above works fine for static output.

By the way, no big deal but "return" is a language builtin in C, not a
function, so it's better style to not surround it in unnecessary brackets.

Regards,

Ed.



Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
11-18-04 01:55 AM


Re: PRINTING LOTS OF OUTPUT
In article <fe6eb44f.0411150757.5f62fd22@posting.google.com>,
James Pascoe <james@james-pascoe.com> wrote:
>Dear All,
>
>Apologies if this is OT or answered elsewhere, but I am looking for
>some help.
>
>I am using gawk to generate a C file as part of a build process, and
>as such, the gawk script
>does a lot of printing. Currently, I am using lots of print statements
>(1 per line) to actually output the text. The body of my script looks
>similar too:
>
>print "int main(void) "
>print "{"
>...
>print " return (0);"
>print "}"
>
>... and basically, I was wondering if there is a better way to produce
>lots of static text ?

The short answer is "not really".  There's nothing really wrong with the
above, but sometimes I bunch them together, like this:

print "int main(void)\n{\n return (0);\n}"

at some cost in readability.  Or, you could try:

OFS="\n"
print "int main(void)",
"{",
" return (0);",
"}"


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
11-19-04 01:56 AM


Re: PRINTING LOTS OF OUTPUT
James Pascoe wrote:
> Dear All,
>
> Apologies if this is OT or answered elsewhere, but I am looking for
> some help.
>
> I am using gawk to generate a C file as part of a build process, and
> as such, the gawk script
> does a lot of printing. Currently, I am using lots of print statements
> (1 per line) to actually output the text. The body of my script looks
> similar too:
>
> print "int main(void) "
> print "{"
> ...
> print " return (0);"
> print "}"
>
> ... and basically, I was wondering if there is a better way to produce
> lots of static text ?

My answer may be OT, too :-)  But could it be that you are using an
inappropriate tool? If the main task is to create C templates, even
parameterized templates, you might want to have a look at the "here
document" feature of bourne-like shells.

cat <<- EOT
int main(void)
{
..
return 0;
}
EOT


Janis

Report this thread to moderator Post Follow-up to this message
Old Post
Janis Papanagnou
11-21-04 08:57 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:56 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.