Open main menu

Difference between revisions of "HOWTO-Open Files"

667 bytes removed ,  11:13, 30 November 2008
add code samples
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.


*Most of you can simply keep using class Common::File as before. The main changes in it:
The basic case: you want to open a file called "datafile.dat":
**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.
<syntax type="C++">#include "common/file.h"
**You can pass relative paths in a limited fashion; you must use the "/" character as separator. I am too lazy to provide details on this right now; but check out the doxygen docs of class FSDirectory.
using Common::File;
**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 new file SearchManager (short: SearchMan) is such an Archive subclass, and can wrap arbitrary paths, ZIP archives, etc. By providing Archive subclasses, you can extend this arbitrarily.


If you have an FSNode and want to read from the corresponding file, you can use File::open(FSNode). Internally, this calls FSNode::openForReading(). Alternatively, you can use this method directly (but then you also must delete the ReadStream returned by it later on). This can be useful if you need to keep a pointer to the file stream anyway. E.g. assume you used to do this:
File* f = new File;
<syntax type="C++">  Common::File *f = new Common::File;
if (!f || !f->open("datafile.dat")) {
  if (f && f->open(node))
   // handle failure to find/open file
    return f;
}
  else {
// access f</syntax>
    delete f;
    return 0;
   }</syntax>
you can now do this instead:
<syntax type="C++">  return node.openForReading();</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;


You can also invoke the SearchManager directly to open a file with a specific name, but looking for it in various places (the game path, extrapath, DATADIR, current dir, etc.): SearchMan::openFile(filename). This is almost exactly what File::open(String) does, only that the later also tries to open the filename with a dot appened. I.e. if you do
File* f = new File;
<syntax type="C++"> file.open("foo")</syntax>
if (!f || !f->open("data/datafile.dat")) {
then if it can't find "foo", it also tries to open "foo." (this is to workaround problems on some systems, where an old DOS file without an extension incorrectly may get a dot appended when copying it). If you don't need this extra lookup, and if you may want to use the Stream directly, instead of wrapping it in a Common::File instance, you can use the SearchMan directly. To continue my example from above, instead of
  // handle failure to find/open file
<syntax type="C++">  Common::File *f = new Common::File;
}
  if (f && f->open(filename))
// access f</syntax>
    return f;
 
  else {
This functions by searching through a default search path managed by the SearchManager (short: SearchMan).
    delete f;
The default search path contains:
    return 0;
* the game directory
  }</syntax>
* the global "extrapath" from the config file
you could now do this instead (with almost identical meaning, except for the "trailing dot" hack):
* the game-specific "extrapath" from the config file
<syntax type="C++">  return SearchMan.openFile(filename);</syntax>
 
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 creat 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.
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>.




178

edits