178
edits
m (Whitespace tweaks) |
(add code samples) |
||
Line 1: | Line 1: | ||
==Super Brief Quick Reference== | ==Super Brief Quick Reference== | ||
* Use File::open to open files (for reading) as before. | * Use File::open to open files (for reading) as before. | ||
* However, do *not* use (absolute) paths with File::open()! | * However, do *not* use (absolute) paths with <code>File::open()</code>! | ||
* an FSNode is kind of a "portable path". If you need to process a path (e.g. coming from the config file), first create an FSNode from it, then use that for whatever you need to (e.g. pass the FSNode to File::open) | * an FSNode is kind of a "portable path". If you need to process a path (e.g. coming from the config file), first create an FSNode from it, then use that for whatever you need to (e.g. pass the FSNode to <code>File::open()</code>) | ||
Line 8: | Line 8: | ||
Here is just a quick guide on the various ways to open a file, without explaining much background. | Here is just a quick guide on the various ways to open a file, without explaining much background. | ||
The basic case: you want to open a file called "datafile.dat": | |||
<syntax type="C++">#include "common/file.h" | |||
using Common::File; | |||
File* f = new File; | |||
if (!f || !f->open("datafile.dat")) { | |||
// handle failure to find/open file | |||
} | |||
// access f</syntax> | |||
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" | |||
using Common::File; | |||
File* f = new File; | |||
< | if (!f || !f->open("data/datafile.dat")) { | ||
// handle failure to find/open file | |||
} | |||
// access f</syntax> | |||
This functions by searching through a default search path managed by the SearchManager (short: SearchMan). | |||
The default search path contains: | |||
* the game directory | |||
* the global "extrapath" from the config file | |||
* the game-specific "extrapath" from the config file | |||
Also, by default it contains a number of system-specific paths, such as: | |||
* on some platforms, a global data dir (e.g., /usr/share/scummvm ) | |||
* on Mac OS X, the Resource directory of the .app bundle | |||
* the current directory [subject to change in the future] | |||
Notes: | |||
*A File is a SeekableReadStream. See the doxygen documentation of that class to see how to access the contents of a file. | |||
*You must not pass absolute paths to <tt>File::open()</tt>! If you must open a file using a path, the correct way is to first create an FSNode from the path, then pass that to File::open. | |||
*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. | |||
Line 52: | Line 58: | ||
**by asking a directory FSNode for a child node | **by asking a directory FSNode for a child node | ||
Finally, you can | Finally, you can create FSNodes from "paths". Caveat: You may not assume anything about the path format, like what the separator char is; in fact, there may not even exist the *concept* of a path separator on the target system. Hence, the only valid way to do that is to feed a "path" created by another FSNode to this FSNode. I.e. you can "serialize" an FSNode to a path, via the FSNode::getPath() method, then write that to a config file. Later, you read it back in, and create a new FSNode from it. That works fine, as long as you stay on the same OS / platform. | ||
Warning: a "path" as returned by FSNode::getPath should *not* be passed to File::open(). If you want to open a file at a specific path, first create an FSNode from it, then use that to open the file. | Warning: a "path" as returned by FSNode::getPath should *not* be passed to File::open(). If you want to open a file at a specific path, first create an FSNode from it, then use that to open the file by calling <code>node->openForReading();</code>. | ||
edits