C Preprocessor -- processing file name

Discussion in 'Programmer's Corner' started by WBahn, Nov 4, 2015.

  1. WBahn

    Thread Starter Moderator

    Mar 31, 2012
    17,716
    4,788
    If I use the __FILE__ macro it expands to the name of the current file as a string constant.

    What I want to do is define a macro to be the bare file name.

    For instance, I might have a file FRED.c in the directory C:\projects. If I have

    #define FILENAME __FILE__

    I get FILENAME defined as the string constant "FRED.c" (I think some compilers return the path information as well, mine doesn't (at least by default)).

    What I need is to be able to set FILENAME to just FRED (without the extension and without the quotes).

    Any ideas on how this might be done? It needs to be done with the preprocessor, so writing a function isn't an option. Nor is writing a separate program to read this file and modify the code before compiling it.
     
  2. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,907
    2,165
    Sub String? That would be a neat trick. I don't know if you can abuse the C preprocessor to do that but it's possible to run another preprocessor step after with something like 'perl/M4' using Make to modify the __FILE__ constants (with a token sequence) to something else in the C source file.

    This is what you normally get.

    file lo.c RPI2 gcc
    root@rpimedia:~# make lo
    cc lo.c -o lo

    Code (C):
    1.  
    2. #include <stdio.h>
    3.  
    4. int main ()
    5. {
    6.   printf ("This is line %d of file \"%s\".\n",
    7.   __LINE__, __FILE__);
    8.   return 0;
    9. }
    10.  
    11. root@rpimedia:~# ./lo
    12. This is line 6 of file "lo.c".
    13.  
     
    Last edited: Nov 4, 2015
  3. WBahn

    Thread Starter Moderator

    Mar 31, 2012
    17,716
    4,788
    I'm definitely of the opinion that what I want to do can't be done. Which is a shame, but not the end of the world.

    The reason I would love to be able to do it is because I write "pseudo classes" in C and use macros to autogenerate much of the code. The code is keyed to the class name via a macro

    #define CLASS FRED

    and the file is named FRED.c

    So if I could get at the base name of the file I could rename a file and, in doing so, rename the class throughout the entire file. As it is, I have to go into the file and change that one #define at the top of the file, so it isn't a big hassle.
     
  4. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    I just use a stream editor.
     
    nsaspook likes this.
  5. WBahn

    Thread Starter Moderator

    Mar 31, 2012
    17,716
    4,788
    Hello... "It needs to be done with the preprocessor, so writing a function isn't an option. Nor is writing a separate program to read this file and modify the code before compiling it."

    That rules out using sed or any other stream editor. Besides, I'm not writing code that someone that doesn't know what a stream editor is, or how to use one, can't use. That completely defeats the purpose.
     
  6. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    Still an improvement over the process you described. And you've already said you can't do it the way you wanted.
     
    Last edited: Nov 8, 2015
  7. WBahn

    Thread Starter Moderator

    Mar 31, 2012
    17,716
    4,788
    How is it an improvement over the process I am currently using?

    Current process: If you want to change the name of the class you have to do two things:
    1) Change the name of the file.
    2) Change the value of a single #define statement at the top of the file.

    What I was hoping to do was eliminate that second step, but that step is definitely not a big hassle. It is comparable to what you would need to do to change the name of a class in Java.

    Actually, unlike Java, the class name and the file name do not have to be the same, so all you really have to do is step two. I just strongly recommend that they be named the same from an organizational perspective.

    How is adding the complication of having to write and debug a stream editor script and then ensure that anyone that uses my code has that same stream editor and jumps through the hoops of running it whenever they change the name of the class an improvement over opening the file and changing one word in it?
     
  8. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    Stream editors can be used directly from the command line. No need to write and debug a script. Actually, there are many ways to eliminate the 2nd step automatically, which are even more elegant. They require a simple script to be written once and used many times,typically a single line of code. That's an investment I have no problem making.
     
  9. WBahn

    Thread Starter Moderator

    Mar 31, 2012
    17,716
    4,788
    I know stream editors can be used from the command line. That doesn't change the fact that not everyone that uses my macros are going to even have a stream editor, let alone the particular stream editor that I've supplied a script for. The very fact that they might think that they even need a stream editor will scare some folks off.
     
  10. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    I consider this an improvement whether your users are sacred or not. When I made the suggestion, you were talking about how YOU made the changes:

    If you want something so that someone else can change the definitions, then that's something else.
     
  11. NorthGuy

    Active Member

    Jun 28, 2014
    603
    121
    For many compilers it wouldn't matter if you renamed "fred.c" to "fred". So, if you're not afraid of loosing c at the end of your filename, it'll be just what you wanted.
     
  12. WBahn

    Thread Starter Moderator

    Mar 31, 2012
    17,716
    4,788
    It's the "many compilers" that is the issue. I want to stay standards compliant. Which, if I understand things now, the __FILE__ macro itself has some implementation-defined behavior (namely whether path information is included in the file name). I haven't verified that but if that's the case then I can't use this approach unless I can write a macro that can strip all the path information off.

    I think the way it is being done now is just fine. But what I can do (low priority) is describe how to automate this procedure, as well as a few other procedures that would be nice to automate but that are beyond the capabilities of the preprocessor.
     
Loading...