On Sunday, August 5, 2012 3:07:05 PM UTC+3, Eric Sosman wrote:
> On 8/5/2012 7:39 AM, d wrote:
>
> > Hi,
>
> > I am looking for a way to strip tokens using the C preprocessor. I looked in the GNU documentation and found nothing that would help me.
>
> > Specifically, I would like to strip parenthesis "(" and ")". For example, if I would to feed the macro STRIP with "This string contains parenthesis right around here)"; I would like the output to be "This string contains parenthesis right around here".
>
> > Can anyone point me towards the proper documentation and/or a working example?
>
>
>
> If I understand your intent correctly, you're out of luck.
>
> A quote-enclosed literal is a single preprocessing token as far
>
> as the preprocessor is concerned, and there's no way to look
>
> inside; tokens are indivisible. The preprocessor can suppress
>
> tokens, duplicate them, splice them together, and rearrange
>
> them, but it operates at the level of the token, not of the
>
> token's interior parts.
>
>
>
> What's your overall goal? Maybe there's a different approach.
>
>
>
> --
>
> Eric Sosman
>
> d
Thanks,
I inherited an existing (and awkward) implementation where, in order to abstract the underlying implementation of printf; a macro was introduced:
#define PRINTF(message) os_printf message
For each OS, os_printf would be redeclared - 99% of the time as a plain old printf.
The ramification of this implementation is that in order to feed a regular printf, such as: printf ( const char * format, ... ); both format and __VA_ARGS__ would be fed as a single literal (if I understand you properly).
Now, I want to implement this differently without breaking backward compatibility. Towards this, I want to separate the format from the arguments.
Another option that came to mind would be to feed the message into sprintf; however that faces the same problem since I would have to call sprintf(buf, message); where message would be the mangled format/args tuple.
I hope that I am clear.