Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function Override 1

Status
Not open for further replies.

naveenprakashj

Technical User
May 2, 2002
14
US
Hi all,

I want to redeclare/redefine a few of the library functions. For. Ex. localtime() of time.h

I have heaps of code wherein localtime() is used. I want to write a wrapper function for localtime as part of the local library which will override the standard localtime(). I dont want to visit each sourcecode and knock off the time.h declaration.

I would like to how know how this can be achieved. If you are not clear with my question, pls. let me know.

Thanks for your time.
 
Well, try using the old #define trick...

Just replace the localtime definition with your own:
#define localtime my_localtime.

You can safely use localtime throughout your code, but the calls will go to your function. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Hey thnx for the solution. But the problem is that I want to use the localtime() within my_localtime again!!! :)
This will result in an infintie recursive call to my_localtime and I'll b not able to invoke the actual localtime()

Can u pls. come up with any other ideas??

Any feedback is well appreciated.
 
Hey nosferatu!!

I kinda liked ur idea and have implemented it. But with a bit of re-engineering!!

Thankx for ur suggestion. It has been very helpful. :)
 
Going with the #define idea .... I didnt test this completely but worked for my small example. This was done on a solaris machine. I'm sure someone probably has a better way of doing this but I thought I'd give ya something to think about. ;-)

you can try (example)

#include <stdio.h>
#include <time.h>

#define localtime(a) mylocaltime(a)

main()
{
time_t ts;
stuct tm *gmt;

struct tm * mylocaltime();

time(&ts);
gmt = localtime(&ts);

printf(&quot;Year is: %d\n&quot;, gmt->tm_year + 1900);

return 0;
}

struct tm * mylocaltime(time_t *ts)
{
#undef localtime(a)

struct tm *gmt;

gmt = localtime(ts);

#define localtime(a) mylocaltime(a)

return gmt;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top