For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > November 2006 > how to change this? (a regexp problem maybe)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to change this? (a regexp problem maybe)
Leo.Hou

2006-11-20, 9:59 pm

Dear experts,

I ran into a silly situation where I need to change a macro definition
and a lot of lines of code need to be changed accordingly.

Current code is: #define DEBUG printf
Usage: DEBUG("a=%d", a)

Now I have to add my application name in front of every log.

Some trick I got from FAQ is
#define DEBUG(args) (printf("My_APP: "), printf args)

but I need to call DEBUG(("n is %d\n", n)); in my code.

Now the problem is how to replace thousands of lines of code. I tried
to run replace_regexp DEBUG(.*) with DEBUG((.*)) but it doesn't work.
Could anyone suggest any tools or scripts that could help me out? Any
comment is welcome!

Thanks a lot

Leo

loic-dev@gmx.net

2006-11-21, 4:00 am

Hello Leo,

> I ran into a silly situation where I need to change a macro definition
> and a lot of lines of code need to be changed accordingly.
>
> Current code is: #define DEBUG printf
> Usage: DEBUG("a=%d", a)
>
> Now I have to add my application name in front of every log.
>
> Some trick I got from FAQ is
> #define DEBUG(args) (printf("My_APP: "), printf args)


If your compiler is ISO C99 conform, then you could perhaps use
variadic macro to solve your problem. See for instance:
http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html

The advantage would be that you just have to change the DEBUG macro
definition. I put an example below.

Cheers,
Loic.
--
#include <stdio.h>

#define DEBUG(...) { printf("My App:"); printf(__VA_ARGS__); }

int
main()
{
int n = 42;
DEBUG("n is %d", n);
return 0;
}

Rainer Weikusat

2006-11-21, 4:00 am

"Leo.Hou" <leo.hou@gmail.com> writes:

[...]

> Now the problem is how to replace thousands of lines of code. I tried
> to run replace_regexp DEBUG(.*) with DEBUG((.*)) but it doesn't
> work.


You may need to quote the brackets, because (depending on the
regexp-implementation you are using) they may be interpreted as
grouping/ subexpression capturing construct.
Leo.Hou

2006-11-21, 4:00 am


loic-dev@gmx.net wrote:
> Hello Leo,
>
>
> If your compiler is ISO C99 conform, then you could perhaps use
> variadic macro to solve your problem. See for instance:
> http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html
>
> The advantage would be that you just have to change the DEBUG macro
> definition. I put an example below.
>
> Cheers,
> Loic.
> --
> #include <stdio.h>
>
> #define DEBUG(...) { printf("My App:"); printf(__VA_ARGS__); }
>
> int
> main()
> {
> int n = 42;
> DEBUG("n is %d", n);
> return 0;
> }


Brilliant! Thanks a lot!

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com