Open main menu

Difference between revisions of "HOWTO-Open Files"

60 bytes removed ,  22:27, 23 February 2010
→‎How to Open a File: Tweak examples (they didn't comply to code formatting conventions, and also needless used 'new')
(remove outdated comment)
(→‎How to Open a File: Tweak examples (they didn't comply to code formatting conventions, and also needless used 'new'))
Line 9: Line 9:


The basic case: you want to open a file called "datafile.dat":
The basic case: you want to open a file called "datafile.dat":
<syntax type="C++">#include "common/file.h"
<syntax type="C++">
using Common::File;
#include "common/file.h"


File* f = new File;
Common::File f;
if (!f || !f->open("datafile.dat")) {
if (!f.open("datafile.dat")) {
   // handle failure to find/open file
   // handle failure to find/open file
}
}
// access f</syntax>
// access f
</syntax>


Or, you want to open a file called "datafile.dat" in a subdirectory "data" of the game directory:
Or, you want to open a file called "datafile.dat" in a subdirectory "data" of the game directory:
<syntax type="C++">#include "common/file.h"
<syntax type="C++">
using Common::File;
#include "common/file.h"


File* f = new File;
Common::File f;
if (!f || !f->open("data/datafile.dat")) {
if (f.open("data/datafile.dat")) {
   // handle failure to find/open file
   // handle failure to find/open file
}
}
// access f</syntax>
// access f
</syntax>


This functions by searching through a default search path managed by the SearchManager (short: SearchMan).
This functions by searching through a default search path managed by the SearchManager (short: SearchMan).
Line 44: Line 46:
*You can pass relative paths in a limited fashion; you must use the "/" character as separator. See the doxygen docs of class FSDirectory for details.
*You can pass relative paths in a limited fashion; you must use the "/" character as separator. See the doxygen docs of class FSDirectory for details.
*There are new File::open methods: You can pass an "Archive" subclass (e.g. a ZIPArchive) to it, and it will search for that file in the Archive). In particular, the SearchMan is such an Archive subclass, and can wrap arbitrary paths, ZIP archives, etc. By providing Archive subclasses, you can extend this arbitrarily.
*There are new File::open methods: You can pass an "Archive" subclass (e.g. a ZIPArchive) to it, and it will search for that file in the Archive). In particular, the SearchMan is such an Archive subclass, and can wrap arbitrary paths, ZIP archives, etc. By providing Archive subclasses, you can extend this arbitrarily.


==The Parts of the System==
==The Parts of the System==
1,079

edits