Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Line break in preprocessor output

Reply
Thread Tools

Line break in preprocessor output

 
 
John Speth
Guest
Posts: n/a
 
      05-29-2007
Hi Group-

I want to use the C preprocessor to generate expanded text as a text
processor for software test script generation. The preprocessor output will
never be compiled. I need to insert newlines in the preprocessed output
file but I can't figure out how to do it.

Here's a stab at what I'm trying to do but failing:

#define CMD(s) text s \
enter \
sleep 500

CMD(mmem:mdir "a1")
CMD(mmem:mdir "a2")

I'd like to output a literal newline for every macro line continuation
character ('\'). I know my example won't work but it's the closest I've
been able to get to actually get the output that I'd like to get, which is:

text mmem:mdir "a1"
enter
sleep 500
text mmem:mdir "a2"
enter
sleep 500

What I get instead is:

text mmem:mdir "a1" enter sleep 500
text mmem:mdir "a2" enter sleep 500

The desired newlines are missing.

Does anyone know how to get the C proeprocessor to output a literal newline
using macros?

Thanks, John.


 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      05-29-2007
"John Speth" <(E-Mail Removed)> writes:

> Does anyone know how to get the C proeprocessor to output a literal newline
> using macros?


I don't think that there is a way to do it. You might consider
designating a character sequence to represent a new-line
character and then postprocess the C preprocessor's output with
another program that can replace that sequence with a new-line.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
 
Reply With Quote
 
 
 
 
ais523
Guest
Posts: n/a
 
      05-29-2007
On May 29, 5:23 pm, "John Speth" <(E-Mail Removed)> wrote:
> Hi Group-
>
> I want to use the C preprocessor to generate expanded text as a text
> processor for software test script generation. The preprocessor output will
> never be compiled. I need to insert newlines in the preprocessed output
> file but I can't figure out how to do it.
>
> Here's a stab at what I'm trying to do but failing:
>
> #define CMD(s) text s \
> enter \
> sleep 500
>
> CMD(mmem:mdir "a1")
> CMD(mmem:mdir "a2")
>
> I'd like to output a literal newline for every macro line continuation
> character ('\'). I know my example won't work but it's the closest I've
> been able to get to actually get the output that I'd like to get, which is:
>
> text mmem:mdir "a1"
> enter
> sleep 500
> text mmem:mdir "a2"
> enter
> sleep 500
>
> What I get instead is:
>
> text mmem:mdir "a1" enter sleep 500
> text mmem:mdir "a2" enter sleep 500
>
> The desired newlines are missing.
>
> Does anyone know how to get the C proeprocessor to output a literal newline
> using macros?
>
> Thanks, John.


With some preprocessors, it's possible that the sequence of characters
\u000A will expand into a newline during preprocessing (this is almost
legal C99 syntax, but the standard specifically says that this sort of
thing isn't allowed (6.4.3:2 in N1124.pdf)). (I haven't tested, but it
seems reasonable that at least one preprocessor will get confused into
emitting a newline in this situation; the #defines would be
substituted in stage 4, before the universal character names are even
considered by the compiler.) One point of interest is that universal
character names are one C99 feature that I've actually used in
practice.
--
ais523

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      05-29-2007
John Speth wrote:
> Hi Group-
>
> I want to use the C preprocessor to generate expanded text as a text
> processor for software test script generation. The preprocessor output will
> never be compiled. I need to insert newlines in the preprocessed output
> file but I can't figure out how to do it.


The C preprocessor is probably the wrong tool. You might look into a
program designed as a stand-alone macro language, like the old, I know,
but trusty M4.
 
Reply With Quote
 
David Tiktin
Guest
Posts: n/a
 
      05-29-2007
On 29 May 2007, "John Speth" <(E-Mail Removed)> wrote:

> I want to use the C preprocessor to generate expanded text as a
> text processor for software test script generation. The
> preprocessor output will never be compiled. I need to insert
> newlines in the preprocessed output file but I can't figure out
> how to do it.


Like others who have responded, I don't think there's a way to do this
portably with the C preprocessor. But could you explain why you chose
that particular tool? Why not use a "real" macro processor like m4?

http://en.wikipedia.org/wiki/M4_(computer_language)

Since it's usually written in C, you should be able to build it for
just about any platform.

Dave

--
D.a.v.i.d T.i.k.t.i.n
t.i.k.t.i.n [at] a.d.v.a.n.c.e.d.r.e.l.a.y [dot] c.o.m
 
Reply With Quote
 
John Speth
Guest
Posts: n/a
 
      05-29-2007

"David Tiktin" <(E-Mail Removed)-bogus.com> wrote in message
news:Xns993F77B3CA75Adtiktinnospambogusco@216.196. 97.136...
> On 29 May 2007, "John Speth" <(E-Mail Removed)> wrote:
>
>> I want to use the C preprocessor to generate expanded text as a
>> text processor for software test script generation. The
>> preprocessor output will never be compiled. I need to insert
>> newlines in the preprocessed output file but I can't figure out
>> how to do it.

>
> Like others who have responded, I don't think there's a way to do this
> portably with the C preprocessor. But could you explain why you chose
> that particular tool? Why not use a "real" macro processor like m4?
>
> http://en.wikipedia.org/wiki/M4_(computer_language)
>
> Since it's usually written in C, you should be able to build it for
> just about any platform.


(I'm the OP) I chose the C preprocessor because it seemed like a capable
tool for the job AND it was at my fingertips. I'm looking for instant
gratification. M4 looks like it'll work but now I have to spend an hour or
two learning it.

It appears simply outputting a newline is just plain not possible using the
C proeprocessor. It's such a simple thing too

Thanks to everyone for your help and suggestions.

JJS


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
`if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ? lovecreatesbea...@gmail.com C Programming 12 04-14-2008 07:59 AM
Is the output of the preprocessor deterministic ? spibou@gmail.com C Programming 32 06-28-2006 07:03 AM
Compiler error occurred when try to use a flexible template expression in preprocessor definesCompiler error occurred when try to use a flexible template expression in preprocessor defines snnn C++ 6 03-14-2005 04:09 PM
preprocessor, token concatenation, no valid preprocessor token Cronus C++ 1 07-14-2004 11:10 PM
How reliable preprocessor output is? Vittal C Programming 5 07-19-2003 04:34 PM



Advertisments