Thread: Platform independent paths

  1. #1
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459

    Platform independent paths

    G'day!

    Okay, I'm writting a little portable application and I want to separate the data from the executable. What's the best way to generate/use these paths at runtime? So far I'm using the pre-processor but that's a bit ugly.

    ie,

    Code:
    fp = fopen(BASE_PATH "fonts" PATH_SEP "test.fnt", "rb");
    And BASE_PATH is generated by my makefile. Ie, on Windows it might be "C:\Program Files\xxx\" or "/usr/share/xxx/" on Linux.

    Or generating them at runtime, but then I need to free them if I use the heap!

    Thanks!
    Zac

    PS: This is the board of best fit, General didn't make sense. Nor did the Linux or Windows boards :-)
    Last edited by zacs7; 11-21-2008 at 09:10 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    How about...?
    Code:
    fp = fopen(BASE_PATH "fonts/test.fnt", "rb");
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You don't need to. The Windows API handles '/' and converts it to '\' for you. Give it a try.
    But of course the command line doesn't handle '/', so if you're using the system() call you'd need to make the paths correct.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Hmm thanks :-)

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The other solution (should you be in a situation where / isn't translated in windows, or you are moving a Windows app to Linux), is to form the path as usual, and then replace fopen() with a macro like this:
    Code:
    // 0myfopen.h or osdep.h (as you are bound to have other functions that are specific to a partciular system)
    #define fopen(x, y) myfopen(x, y)
    FILE *myfopen(const char *fname, const char *mode);
    
    
    .... 
    
    // myfopen.c or linux/win/macosdep.c
    #undef fopen()
    FILE *myfopen(const char *fname, const char *mode)
    {
         FILE *f;
         char *tmpname = convert(fname);
         if (tmpname)
         {
             f = fopen(tmpname, mode);
             free(tmpname);
             return f;
         }
         // Error message??
         return NULL;
    }
    Convert would allocate a new copy of the string and translate as appropriate.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Thanks matsp . I'll have to use that anyway when I do runtime detection of a writeable path ie a home directory or "My Documents".

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I believe Windows API translates '/' to '\' due to backwards compatibility, but I don't think Microsoft will rip that out anytime soon, so I think you're safe
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 12-19-2006, 04:23 PM
  2. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  3. Platform independent C code
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 03-04-2005, 01:32 PM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. Platform Independent
    By mikedelo in forum C Programming
    Replies: 8
    Last Post: 10-22-2001, 05:08 AM