C file system abstraction method

I am running a program in SDL, which obviously should load resources for the file system. I would like the file calls within the program to be platform independent. My initial idea is to define a macro (allows you to call it PTH for the path), which is defined in the preprocessor based on the type of system, and then makes file calls in the program using it. for instance

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

will just translate to something related to the file system.

If macros are an accepted way to do this, such macros would look (how to check which system is used, combine lines in a macro?)

If not, what is an acceptable way to do this?

+4
source share
5 answers

The Boost Fileystem module is probably the best choice. It has an override for the / operator on tracks, so you can do things like ...

 ifstream file2( arg_path / "foo" / "bar" ); 
+5
source

GLib has a number of portable trace management features. If you prefer C ++, there is also a boost :: file system .

+2
source

There is no need to have this as a macro.

One general approach is to use abstract paths to use a forward slash as a separator, since this (almost by accident!) Displays a very good portion of real platforms. For those where this is not the case, you simply translate the implementation level of your file system.

+1
source

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 am not familiar with SDL development on older Macs. Another alternative in game resources is to use the package file format and load resources directly into memory (for example, a <string, SDL_Surface> card)

This way you upload a single file (maybe even a ZIP unpacked at boot time)

0
source

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

0
source

All Articles