Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageIn 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);",
"}"
Post Follow-up to this message
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.
Post Follow-up to this messageJames 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
Post Follow-up to this message
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.
Post Follow-up to this messageIn 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);",
"}"
Post Follow-up to this messageJames 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
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.