linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* macro to print filename
@ 2011-06-01 16:07 ratheesh kannoth
  2011-06-01 17:38 ` Michal Nazarewicz
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: ratheesh kannoth @ 2011-06-01 16:07 UTC (permalink / raw)
  To: linux-c-programming

__FILE__ macro prints relative path name of the file. I need only
filename. How to extract this ?

I dont want to use any string library - since it includes more
Processing and needs to store the character array in executable. IS
there any method at preprocessing level to do this .

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: macro to print filename
  2011-06-01 16:07 macro to print filename ratheesh kannoth
@ 2011-06-01 17:38 ` Michal Nazarewicz
  2011-06-01 18:45   ` Zhongye Jia
  2011-06-01 18:08 ` Zhongye Jia
  2011-06-02  2:10 ` Libin Yang
  2 siblings, 1 reply; 12+ messages in thread
From: Michal Nazarewicz @ 2011-06-01 17:38 UTC (permalink / raw)
  To: linux-c-programming, ratheesh kannoth

On Wed, 01 Jun 2011 18:07:42 +0200, ratheesh kannoth  
<ratheesh.ksz@gmail.com> wrote:
> __FILE__ macro prints relative path name of the file.

Not prints but expands to but we know what you mean. ;)

> I need only filename. How to extract this?
>
> I don't want to use any string library - since it includes more
> Processing and needs to store the character array in executable.
> Is there any method at preprocessing level to do this.

I can say with 99,9% confidence that there is no such thing in C
standard and 99% that there is no such thing in GCC.

You probably can do that in C++ with templates if that's any
constellation.

-- 
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michal "mina86" Nazarewicz    (o o)
ooo +-----<email/xmpp: mnazarewicz@google.com>-----ooO--(_)--Ooo--

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: macro to print filename
  2011-06-01 16:07 macro to print filename ratheesh kannoth
  2011-06-01 17:38 ` Michal Nazarewicz
@ 2011-06-01 18:08 ` Zhongye Jia
  2011-06-01 19:11   ` Michal Nazarewicz
  2011-06-02  2:10 ` Libin Yang
  2 siblings, 1 reply; 12+ messages in thread
From: Zhongye Jia @ 2011-06-01 18:08 UTC (permalink / raw)
  To: ratheesh kannoth; +Cc: linux-c-programming

no need for an array, just a pointer is enough.

#include <stdio.h>

int main()
{
        char filename[] = __FILE__, *basename;
        basename = filename + sizeof(filename) - 2;
        while( basename+1 != filename && *basename != '/' ) {
                basename--;
        }
        basename++;
        printf("%s\n%s\n", filename, basename);
        return 0;
}

On Thu, Jun 2, 2011 at 00:07, ratheesh kannoth <ratheesh.ksz@gmail.com> wrote:
> __FILE__ macro prints relative path name of the file. I need only
> filename. How to extract this ?
>
> I dont want to use any string library - since it includes more
> Processing and needs to store the character array in executable. IS
> there any method at preprocessing level to do this .
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: macro to print filename
  2011-06-01 17:38 ` Michal Nazarewicz
@ 2011-06-01 18:45   ` Zhongye Jia
  2011-06-01 19:08     ` Michal Nazarewicz
  0 siblings, 1 reply; 12+ messages in thread
From: Zhongye Jia @ 2011-06-01 18:45 UTC (permalink / raw)
  To: Michal Nazarewicz; +Cc: linux-c-programming

I do not think this can be done in *compiling* time, neither macro in
c nor template in c++, so would you be so kind to offer the solution
by using template or just give  some tips? Since I'm not that good at
c++ :)

On Thu, Jun 2, 2011 at 01:38, Michal Nazarewicz <mina86@mina86.com> wrote:
> On Wed, 01 Jun 2011 18:07:42 +0200, ratheesh kannoth
> <ratheesh.ksz@gmail.com> wrote:
>>
>> __FILE__ macro prints relative path name of the file.
>
> Not prints but expands to but we know what you mean. ;)
>
>> I need only filename. How to extract this?
>>
>> I don't want to use any string library - since it includes more
>> Processing and needs to store the character array in executable.
>> Is there any method at preprocessing level to do this.
>
> I can say with 99,9% confidence that there is no such thing in C
> standard and 99% that there is no such thing in GCC.
>
> You probably can do that in C++ with templates if that's any
> constellation.
>
> --
> Best regards,                                         _     _
> .o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
> ..o | Computer Science,  Michal "mina86" Nazarewicz    (o o)
> ooo +-----<email/xmpp: mnazarewicz@google.com>-----ooO--(_)--Ooo--
> --
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: macro to print filename
  2011-06-01 18:45   ` Zhongye Jia
@ 2011-06-01 19:08     ` Michal Nazarewicz
  2011-06-02 13:39       ` Zhongye Jia
  0 siblings, 1 reply; 12+ messages in thread
From: Michal Nazarewicz @ 2011-06-01 19:08 UTC (permalink / raw)
  To: Zhongye Jia; +Cc: linux-c-programming

On Wed, 01 Jun 2011 20:45:06 +0200, Zhongye Jia <jia.zhongye@gmail.com>  
wrote:
> I do not think this can be done in *compiling* time, neither macro in
> c nor template in c++, so would you be so kind to offer the solution
> by using template or just give  some tips? Since I'm not that good at
> c++ :)

I was thinking along the lines of:

template<unsigned offset>
struct _filename {
	static const char *const val = __FILE__[offset] == '/'
		? __FILE__ + offset + 1
		: _filename<offset-1>::val;
};

template<>
struct _filename<0> {
	static const char *const val = __FILE__;
};

static const char *const filename = _filename<sizeof(__FILE__) - 1>;

I'm a bit rusty so I dunno if that'll work in this format.

-- 
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michal "mina86" Nazarewicz    (o o)
ooo +-----<email/xmpp: mnazarewicz@google.com>-----ooO--(_)--Ooo--

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: macro to print filename
  2011-06-01 18:08 ` Zhongye Jia
@ 2011-06-01 19:11   ` Michal Nazarewicz
  2011-06-01 19:24     ` Aniruddha Bhattacharyya
  0 siblings, 1 reply; 12+ messages in thread
From: Michal Nazarewicz @ 2011-06-01 19:11 UTC (permalink / raw)
  To: ratheesh kannoth, Zhongye Jia; +Cc: linux-c-programming

On Wed, 01 Jun 2011 20:08:50 +0200, Zhongye Jia <jia.zhongye@gmail.com>  
wrote:
> #include <stdio.h>
>
> int main()
> {
>         char filename[] = __FILE__, *basename;
>         basename = filename + sizeof(filename) - 2;
>         while( basename+1 != filename && *basename != '/' ) {
>                 basename--;
>         }
>         basename++;

And you are not using strchr() why?  I know OP said about not using
string libraries but I don't think he meant that he's fine with
reimplementing the wheel.

>         printf("%s\n%s\n", filename, basename);
>         return 0;
> }

-- 
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michal "mina86" Nazarewicz    (o o)
ooo +-----<email/xmpp: mnazarewicz@google.com>-----ooO--(_)--Ooo--

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: macro to print filename
  2011-06-01 19:11   ` Michal Nazarewicz
@ 2011-06-01 19:24     ` Aniruddha Bhattacharyya
  2011-06-01 19:27       ` Michal Nazarewicz
  0 siblings, 1 reply; 12+ messages in thread
From: Aniruddha Bhattacharyya @ 2011-06-01 19:24 UTC (permalink / raw)
  To: Michal Nazarewicz; +Cc: ratheesh kannoth, Zhongye Jia, linux-c-programming

From what I understood, Ratheesh wanted a compile time solution.

My workaround will be to go for  a Makefile based compilation.
1)  "gcc -E yourfile.c"  will preprocess only. so __FILE__ will be expanded .

output file will be yourfile.c (with macro expanded)

2) then run your custom tool/bash script to trim the filepath to filename.
use awk tool maybe.

3) Do the final compile using Gcc


On Thu, Jun 2, 2011 at 12:41 AM, Michal Nazarewicz <mina86@mina86.com> wrote:
>
> On Wed, 01 Jun 2011 20:08:50 +0200, Zhongye Jia <jia.zhongye@gmail.com> wrote:
>>
>> #include <stdio.h>
>>
>> int main()
>> {
>>        char filename[] = __FILE__, *basename;
>>        basename = filename + sizeof(filename) - 2;
>>        while( basename+1 != filename && *basename != '/' ) {
>>                basename--;
>>        }
>>        basename++;
>
> And you are not using strchr() why?  I know OP said about not using
> string libraries but I don't think he meant that he's fine with
> reimplementing the wheel.
>
>>        printf("%s\n%s\n", filename, basename);
>>        return 0;
>> }
>
> --
> Best regards,                                         _     _
> .o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
> ..o | Computer Science,  Michal "mina86" Nazarewicz    (o o)
> ooo +-----<email/xmpp: mnazarewicz@google.com>-----ooO--(_)--Ooo--
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: macro to print filename
  2011-06-01 19:24     ` Aniruddha Bhattacharyya
@ 2011-06-01 19:27       ` Michal Nazarewicz
  0 siblings, 0 replies; 12+ messages in thread
From: Michal Nazarewicz @ 2011-06-01 19:27 UTC (permalink / raw)
  To: Aniruddha Bhattacharyya
  Cc: ratheesh kannoth, Zhongye Jia, linux-c-programming

On Wed, 01 Jun 2011 21:24:59 +0200, Aniruddha Bhattacharyya  
<aniruddha.aot@gmail.com> wrote:
> From what I understood, Ratheesh wanted a compile time solution.
>
> My workaround will be to go for  a Makefile based compilation.
> 1)  "gcc -E yourfile.c"  will preprocess only. so __FILE__ will be  
> expanded .
>
> output file will be yourfile.c (with macro expanded)
>
> 2) then run your custom tool/bash script to trim the filepath to  
> filename.
> use awk tool maybe.
>
> 3) Do the final compile using Gcc

That sounds too complicated.  How about:

foo.o: foo.c
	gcc -DMY_FILE=$(basename $<) -o $@ $<

(Syntax might be wrong.)

-- 
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michal "mina86" Nazarewicz    (o o)
ooo +-----<email/xmpp: mnazarewicz@google.com>-----ooO--(_)--Ooo--

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: macro to print filename
  2011-06-01 16:07 macro to print filename ratheesh kannoth
  2011-06-01 17:38 ` Michal Nazarewicz
  2011-06-01 18:08 ` Zhongye Jia
@ 2011-06-02  2:10 ` Libin Yang
  2 siblings, 0 replies; 12+ messages in thread
From: Libin Yang @ 2011-06-02  2:10 UTC (permalink / raw)
  To: ratheesh kannoth, linux-c-programming@vger.kernel.org

man 3 basename

Regards,
Libin 

> -----Original Message-----
> From: linux-c-programming-owner@vger.kernel.org
> [mailto:linux-c-programming-owner@vger.kernel.org] On Behalf Of ratheesh kannoth
> Sent: Thursday, June 02, 2011 12:08 AM
> To: linux-c-programming@vger.kernel.org
> Subject: macro to print filename
> 
> __FILE__ macro prints relative path name of the file. I need only
> filename. How to extract this ?
> 
> I dont want to use any string library - since it includes more
> Processing and needs to store the character array in executable. IS
> there any method at preprocessing level to do this .
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: macro to print filename
  2011-06-01 19:08     ` Michal Nazarewicz
@ 2011-06-02 13:39       ` Zhongye Jia
  2011-06-02 18:11         ` ratheesh kannoth
  0 siblings, 1 reply; 12+ messages in thread
From: Zhongye Jia @ 2011-06-02 13:39 UTC (permalink / raw)
  To: Michal Nazarewicz; +Cc: linux-c-programming

thanks very much! it works very well only except that the
initialization should be placed out of the struct. the compiling time
computation in c++ is really amazing.

2011/6/2 Michal Nazarewicz <mina86@mina86.com>:
> On Wed, 01 Jun 2011 20:45:06 +0200, Zhongye Jia <jia.zhongye@gmail.com>
> wrote:
>>
>> I do not think this can be done in *compiling* time, neither macro in
>> c nor template in c++, so would you be so kind to offer the solution
>> by using template or just give  some tips? Since I'm not that good at
>> c++ :)
>
> I was thinking along the lines of:
>
> template<unsigned offset>
> struct _filename {
>        static const char *const val = __FILE__[offset] == '/'
>                ? __FILE__ + offset + 1
>                : _filename<offset-1>::val;
> };
>
> template<>
> struct _filename<0> {
>        static const char *const val = __FILE__;
> };
>
> static const char *const filename = _filename<sizeof(__FILE__) - 1>;
>
> I'm a bit rusty so I dunno if that'll work in this format.
>
> --
> Best regards,                                         _     _
> .o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
> ..o | Computer Science,  Michal "mina86" Nazarewicz    (o o)
> ooo +-----<email/xmpp: mnazarewicz@google.com>-----ooO--(_)--Ooo--
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: macro to print filename
  2011-06-02 13:39       ` Zhongye Jia
@ 2011-06-02 18:11         ` ratheesh kannoth
  2011-06-02 19:11           ` Michal Nazarewicz
  0 siblings, 1 reply; 12+ messages in thread
From: ratheesh kannoth @ 2011-06-02 18:11 UTC (permalink / raw)
  To: Zhongye Jia; +Cc: Michal Nazarewicz, linux-c-programming

On Thu, Jun 2, 2011 at 7:09 PM, Zhongye Jia <jia.zhongye@gmail.com> wrote:
> thanks very much! it works very well only except that the
> initialization should be placed out of the struct. the compiling time
> computation in c++ is really amazing.
>
> 2011/6/2 Michal Nazarewicz <mina86@mina86.com>:
>> On Wed, 01 Jun 2011 20:45:06 +0200, Zhongye Jia <jia.zhongye@gmail.com>
>> wrote:
>>>
>>> I do not think this can be done in *compiling* time, neither macro in
>>> c nor template in c++, so would you be so kind to offer the solution
>>> by using template or just give  some tips? Since I'm not that good at
>>> c++ :)
>>
>> I was thinking along the lines of:
>>
>> template<unsigned offset>
>> struct _filename {
>>        static const char *const val = __FILE__[offset] == '/'
>>                ? __FILE__ + offset + 1
>>                : _filename<offset-1>::val;
>> };
>>
>> template<>
>> struct _filename<0> {
>>        static const char *const val = __FILE__;
>> };
>>
>> static const char *const filename = _filename<sizeof(__FILE__) - 1>;
>>
>> I'm a bit rusty so I dunno if that'll work in this format.
>>
>> --
>> Best regards,                                         _     _
>> .o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
>> ..o | Computer Science,  Michal "mina86" Nazarewicz    (o o)
>> ooo +-----<email/xmpp: mnazarewicz@google.com>-----ooO--(_)--Ooo--
>>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

This is nice one.

so we could use __FILE__ with Makefile to solve this problem. We could
 use __FILE__  in our program and define Makefile for each directory.
From toplevel directory, call using - make -C subdir/ . This will give
help __FILE__ macro to exapand to just filename since "make" is
executing inside the directory.

-Ratheesh
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: macro to print filename
  2011-06-02 18:11         ` ratheesh kannoth
@ 2011-06-02 19:11           ` Michal Nazarewicz
  0 siblings, 0 replies; 12+ messages in thread
From: Michal Nazarewicz @ 2011-06-02 19:11 UTC (permalink / raw)
  To: Zhongye Jia, ratheesh kannoth; +Cc: linux-c-programming

On Thu, 02 Jun 2011 20:11:05 +0200, ratheesh kannoth wrote:
> so we could use __FILE__ with Makefile to solve this problem. We could
>  use __FILE__  in our program and define Makefile for each directory.
> From toplevel directory, call using - make -C subdir/ . This will give
> help __FILE__ macro to exapand to just filename since "make" is
> executing inside the directory.

This might not work in included files though.  Also, I'm not sure if
it's guaranteed that __FILE__ is always a relative path.

-- 
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michal "mina86" Nazarewicz    (o o)
ooo +-----<email/xmpp: mnazarewicz@google.com>-----ooO--(_)--Ooo--

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2011-06-02 19:11 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-06-01 16:07 macro to print filename ratheesh kannoth
2011-06-01 17:38 ` Michal Nazarewicz
2011-06-01 18:45   ` Zhongye Jia
2011-06-01 19:08     ` Michal Nazarewicz
2011-06-02 13:39       ` Zhongye Jia
2011-06-02 18:11         ` ratheesh kannoth
2011-06-02 19:11           ` Michal Nazarewicz
2011-06-01 18:08 ` Zhongye Jia
2011-06-01 19:11   ` Michal Nazarewicz
2011-06-01 19:24     ` Aniruddha Bhattacharyya
2011-06-01 19:27       ` Michal Nazarewicz
2011-06-02  2:10 ` Libin Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).