4

I'm starting out a program in SDL which obviously needs to load resources for the filesystem. I'd like file calls within the program to be platform-independent. My initial idea is to define a macro (lets call it PTH for path) that is defined in the preprocessor based on system type and and then make file calls in the program using it. For example

SDL_LoadBMP(PTH("data","images","filename"));

would simply translate to something filesystem-relevant.

If macros are the accepted way of doing this, what would such macros look like (how can I check for which system is in use, concatenate strings in the macro?)

If not, what is the accepted way of doing this?

Scribble Master
  • 1,100
  • 3
  • 11
  • 17
  • Are you looking for independent of filesystem type (e.g. ext3, ReiserFS, btrfs), or independent of platform (e.g. Windows and Mac and Linux) – Rafe Kettler Jun 28 '11 at 16:15
  • Unless there are some caveats that I don't know of in calling files from exotic filesystems instead of just using a standard Unix path, I'd say platform independent. – Scribble Master Jun 28 '11 at 16:18
  • A macro would work on both, I'd think, as would a preprocessor-determined slash-swapping function. That's ugly, but quite clearly not impossible, so I'd rather have a solution for both. – Scribble Master Jun 28 '11 at 16:25
  • @Scribble: They are two different languages. Available solutions will vary hugely between them, regardless of what solutions you've pre-assumed. Pick one please. – Lightness Races in Orbit Jun 28 '11 at 16:33
  • Relevant solutions have been given for both in the answers, so I believe there is no reason left to choose. Thanks for your input though. – Scribble Master Jun 28 '11 at 16:35
  • You're in to a lot, and I mean **A LOT**, of work trying to write multi-language source files. I suggest you do not mix C and C++ (and Pascal and FORTRAN and COBOL and Lisp and ...) – pmg Jun 28 '11 at 18:07

5 Answers5

5

The Boost Filesystem module is probably your best bet. It has override for the "/" operator on paths so you can do stuff like...

ifstream file2( arg_path / "foo" / "bar" );
Andrew White
  • 52,720
  • 19
  • 113
  • 137
2

GLib has a number of portable path-manipulation functions. If you prefer C++, there's also boost::filesystem.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
  • Thanks, I actually knew of glib, but it slipped my mind. I do prefer C++ to be supported as well, though. – Scribble Master Jun 28 '11 at 16:20
  • glib is supported in C++, in that you can use it. boost::filesystem is indeed nicer to use though, if you don't need C support – bdonlan Jun 28 '11 at 16:22
  • +1 since your answer is helpful and relevant, and addresses both c and c++ but Andrew White's is more detailed in its pitch and relevant to most coders (C++), so I'll have to give him the check, sorry. – Scribble Master Jun 28 '11 at 16:33
1

There's no need to have this as a macro.

One common approach is to abstract paths to use the forward slash as a separator, since that (almost accidentally!) maps very well to a large proportion of actual platforms. For those where it doesn't, you simply translate inside your file system implementation layer.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

Looking at the python implementation for OS9 os.path.join (macpath)

def join(s, *p):
  path = s
  for t in p:
    if (not s) or isabs(t):
        path = t
        continue
    if t[:1] == ':':
        t = t[1:]
    if ':' not in path:
        path = ':' + path
    if path[-1:] != ':':
        path = path + ':'
    path = path + t
  return path

I'm not familiar with developing under SDL on older Macs. Another alternative in game resources is to use a package file format, and load the resources into memory directly (such as a map < string, SDL_Surface > )

Thereby you would load one file (perhaps even a zip, unzipped at load time)

dwerner
  • 6,462
  • 4
  • 30
  • 44
0

I would simply do the platform-equivalent version of chdir(data_base_dir); in your program's startup code, then use relative unix-style paths of the form "images/filename". The last systems where this would not work were MacOS 9, which is completely irrelevant now.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711