[lug] OT: CPP question

Sexton, George gsexton at mhsoftware.com
Sat Mar 30 18:01:30 MST 2002


Actually I am pre-processing a SQL Script into multiple variants. I.E. SQL
Server, Postgresql, Oracle, etc. Hence the single quotes. I have never tried
to use M4. I will have to look at it.

-----Original Message-----
From: lug-admin at lug.boulder.co.us [mailto:lug-admin at lug.boulder.co.us]On
Behalf Of Tkil
Sent: 29 March, 2002 3:46 PM
To: lug at lug.boulder.co.us
Subject: Re: [lug] OT: CPP question


>>>>> "George" == George Sexton <Sexton> writes:

George> I have a question about CPP, and I was hoping someone could help me.
George> Given:

George> #ifdef THIS
George> #define MY_FUNC(x) SomeFunction(x)
George> #else
George> #define MY_FUNC(x) SomeOtherFunction(x)
George> #endif

George> MY_FUNC(some_literal);

George> If THIS is defined, I would like the output to be:

George>    SomeFunction('some_literal');

George> but if THIS is not defined, I would like the output to be:

George>    SomeOtherFunction(some_literal);

do you really want single quotes?  that's harder.

if you want double-quotes, use the "stringify" preprocessor operator,
a single hash/pound/octothorpe character, something like this:

#ifdef DEBUG
#  define assert(x)                                                     \
    do {                                                                \
        int v = (x);                                                    \
        if (!v) {                                                       \
            fprintf(stderr, "%s:%s: assertion \"%s\" failed.\n",        \
                    __FILE__, __LINE__, #x);                            \
        }                                                               \
    while (0)
#else /* DEBUG */
#  define assert(x)
#endif /* DEBUG */

or similar.

George> I tried MY_FUNC(x) SomeFunction('x') but it gives:

George>   SomeFunction('x');

George> I tried MY_FUNC(x) SomeFunction(''x'') but it gives:

George>   SomeFunction(''some_literal'');

ansi cpp uses token-based macro expansion, so working with strings is
occasionally painful.  they introduced "#" (stringify) and "##" (paste
tokens) to help with this.

or did i totally misunderstand your question?

George> Does anyone have an idea on how I can create the macro so that
George> in one form the output is the literal single quoted, but in
George> another it is just the literal?

single-quoting will be hard, i fear.  hm... double-expansion might do
it.  hm, not that i can find -- getting a preprocessing token defined
to be a single quote would help, i think, but i can't seem to do it
obviously.  the "cpp(Stringification)" info page flatly states:

   There is no way to convert a macro argument into a character
   constant.

(well, at least the a version i found on the web does -- the one on
scrye doesn't seem to have that in it.  peculiar.  probably version
differences.)

what are you doing that you want single-quoted output anyway?  is cpp
the right tool, or should you investigate m4?

t.

_______________________________________________
Web Page:  http://lug.boulder.co.us
Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug




More information about the LUG mailing list