Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Stripping tokens in the C preprocessor

Reply
Thread Tools

Stripping tokens in the C preprocessor

 
 
dov.levenglick@gmail.com
Guest
Posts: n/a
 
      08-05-2012
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?

Thanks
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      08-05-2012
On 8/5/2012 7:39 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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
(E-Mail Removed)d
 
Reply With Quote
 
 
 
 
dov.levenglick@gmail.com
Guest
Posts: n/a
 
      08-05-2012
On Sunday, August 5, 2012 3:07:05 PM UTC+3, Eric Sosman wrote:
> On 8/5/2012 7:39 AM, (E-Mail Removed)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
>
> (E-Mail Removed)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.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-05-2012
(E-Mail Removed) writes:
<snip>
> 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.


Ah. That's not what I got from your original post but no matter...
Is this the sort of thing you want to do:

#define PRINTF(m) PRINTF2 m
#define PRINTF2(...) fprintf(stderr, __VA_ARGS__)

so that an old invocation like: PRINTF(("x=%d\n", 42)); expands to
fprintf(stderr, "x=%d\n", 42); ?

If this is not along the right lines, you need to say exactly what "I
want to implement this differently" means.

<snip>
--
Ben.
 
Reply With Quote
 
dov.levenglick@gmail.com
Guest
Posts: n/a
 
      08-05-2012
On Sunday, August 5, 2012 3:33:32 PM UTC+3, Ben Bacarisse wrote:
> (E-Mail Removed) writes:
>
> <snip>
>
> > 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.

>
>
>
> Ah. That's not what I got from your original post but no matter...
>
> Is this the sort of thing you want to do:
>
>
>
> #define PRINTF(m) PRINTF2 m
>
> #define PRINTF2(...) fprintf(stderr, __VA_ARGS__)
>
>
>
> so that an old invocation like: PRINTF(("x=%d\n", 42)); expands to
>
> fprintf(stderr, "x=%d\n", 42); ?
>
>
>
> If this is not along the right lines, you need to say exactly what "I
>
> want to implement this differently" means.
>
>
>
> <snip>
>
> --
>
> Ben.


My existing functions are as follows:

<file_os.h>
#define os_printf printf
<file.h>
#define PRINTF(msg) os_printf msg

<file.c>
PRINTF(("Var equals %d", var));

I am looking for a way, without changing the macro signature in file.h; to have PRINTF call in to a new function:
int global_printf(const char * fmt, ...);
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      08-05-2012
On 8/5/2012 9:40 AM, (E-Mail Removed) wrote:
>
> My existing functions are as follows:
>
> <file_os.h>
> #define os_printf printf
> <file.h>
> #define PRINTF(msg) os_printf msg
>
> <file.c>
> PRINTF(("Var equals %d", var));
>
> I am looking for a way, without changing the macro signature in file.h; to have PRINTF call in to a new function:
> int global_printf(const char * fmt, ...);


What's wrong with changing <file_os.h> to say

#define os_printf global_printf

? Isn't that precisely why the os_printf macro was introduced
in the first place? What am I missing ?

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-05-2012
(E-Mail Removed) writes:

<snip>
> My existing functions are as follows:
>
> <file_os.h>
> #define os_printf printf
> <file.h>
> #define PRINTF(msg) os_printf msg
>
> <file.c>
> PRINTF(("Var equals %d", var));
>
> I am looking for a way, without changing the macro signature in
> file.h; to have PRINTF call in to a new function: int
> global_printf(const char * fmt, ...);


From my reading of your original post, you seem to know how the above
works, so there must be some unstated reason why the obvious

#define os_printf global_printf

is not suitable.

--
Ben.
 
Reply With Quote
 
dov.levenglick@gmail.com
Guest
Posts: n/a
 
      08-05-2012
On Sunday, August 5, 2012 5:04:41 PM UTC+3, Ben Bacarisse wrote:
> <dov> writes:
>
>
>
> <snip>
>
> > My existing functions are as follows:

>
> >

>
> > <file_os.h>

>
> > #define os_printf printf

>
> > <file.h>

>
> > #define PRINTF(msg) os_printf msg

>
> >

>
> > <file.c>

>
> > PRINTF(("Var equals %d", var));

>
> >

>
> > I am looking for a way, without changing the macro signature in

>
> > file.h; to have PRINTF call in to a new function: int

>
> > global_printf(const char * fmt, ...);

>
>
>
> From my reading of your original post, you seem to know how the above
>
> works, so there must be some unstated reason why the obvious
>
>
>
> #define os_printf global_printf
>
>
>
> is not suitable.
>
>
>
> --
>
> Ben.


I mistyped the API for global_printf - sorry.
I want to implement global_printf such that it can take a FILE* handle:
global_printf(FILE* file, const char * fmt, ...);
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-05-2012
(E-Mail Removed) writes:

> On Sunday, August 5, 2012 5:04:41 PM UTC+3, Ben Bacarisse wrote:
>> <dov> writes:
>>
>>
>>
>> <snip>
>>
>> > My existing functions are as follows:

>>
>> >

>>
>> > <file_os.h>

>>
>> > #define os_printf printf

>>
>> > <file.h>

>>
>> > #define PRINTF(msg) os_printf msg

>>
>> >

>>
>> > <file.c>

>>
>> > PRINTF(("Var equals %d", var));

>>
>> >

>>
>> > I am looking for a way, without changing the macro signature in

>>
>> > file.h; to have PRINTF call in to a new function: int

>>
>> > global_printf(const char * fmt, ...);

>>
>>
>>
>> From my reading of your original post, you seem to know how the above
>>
>> works, so there must be some unstated reason why the obvious
>>
>>
>>
>> #define os_printf global_printf
>>
>>
>>
>> is not suitable.
>>
>>
>>
>> --
>>
>> Ben.


Can you do anything to avoid all these extra blanks lines? If not,
please switch back to Google's old interface which is less broken than
the new one.

> I mistyped the API for global_printf - sorry.
> I want to implement global_printf such that it can take a FILE* handle:
> global_printf(FILE* file, const char * fmt, ...);


Now all I need to know is why the example I posted one reply previously
is not suitable. Sure, it used the name "PRINTF2" rather than
"os_printf" but the structure is not changed by using another name. To
recap, what's wrong with:

#define os_printf(...) global_printf(stderr, __VA_ARGS__)

?

--
Ben.
 
Reply With Quote
 
dov.levenglick@gmail.com
Guest
Posts: n/a
 
      08-05-2012
<snip>
> >>

>
> >> Ben.

>
> Can you do anything to avoid all these extra blanks lines? If not,
> please switch back to Google's old interface which is less broken than
> the new one.
>
>
>
> > I mistyped the API for global_printf - sorry.
> > I want to implement global_printf such that it can take a FILE* handle:
> > global_printf(FILE* file, const char * fmt, ...);

>
>
>
> Now all I need to know is why the example I posted one reply previously
> is not suitable. Sure, it used the name "PRINTF2" rather than
> "os_printf" but the structure is not changed by using another name. To
> recap, what's wrong with:
>
> #define os_printf(...) global_printf(stderr, __VA_ARGS__)
>
> --
>
> Ben.


Existing API:
#define PRINTF printf
#define LOG(logpoint, severity, msg) PRINTF msg

LOG(WARN, ("this is a warning, this is a var %d\n", var) );

I want to convert LOG to somehow call into a function:
int new_printf(int logpoint, int severity, const char* fmt, ...)
{
va_list ap;

va_start(ap,fmt);
os_printf("Logpoint %d, severity %d reporting this: ", logpoint, severity);
os_printf(fmp, ap);
va_end(ap);
}

I tried changing
#define LOG(logpoint, severity, ...) new_printf(logpoint, severity, __VA_ARGS__)

However, I am getting the following warning from the compiler:
'function' : 'const char *' differs in levels of indirection from 'unsigned long'

The reason is because ("this is a warning, this is a var %d\n", var) is being parsed as an unsigned long rather than the {format, args} tuple.
 
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
Finding and replacing Invalid Tokens in an XML document Ben Holness Perl 0 01-06-2006 12:11 PM
string into tokens =?Utf-8?B?TFc=?= ASP .Net 1 10-13-2005 06:53 PM
RE: string into tokens =?Utf-8?B?RWx0b24gVw==?= ASP .Net 0 10-13-2005 06:06 PM
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



Advertisments