Difference between revisions of "HOWTO-Open Files"

Jump to navigation Jump to search
12 bytes removed ,  18:58, 4 November 2008
m
Whitespace tweaks
(add howto open files page (based on Fingolfin's e-mail))
 
m (Whitespace tweaks)
Line 15: Line 15:


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:
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:
<syntax type="C++">
<syntax type="C++"> Common::File *f = new Common::File;
  Common::File *f = new Common::File;
   if (f && f->open(node))
   if (f && f->open(node))
     return f;
     return f;
Line 22: Line 21:
     delete f;
     delete f;
     return 0;
     return 0;
   }
   }</syntax>
</syntax>
you can now do this instead:
you can now do this instead:
<syntax type="C++">
<syntax type="C++"> return node.openForReading();</syntax>
  return node.openForReading();
</syntax>




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
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
<syntax type="C++">
<syntax type="C++"> file.open("foo")</syntax>
  file.open("foo")
</syntax>
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
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
<syntax type="C++">
<syntax type="C++"> Common::File *f = new Common::File;
  Common::File *f = new Common::File;
   if (f && f->open(filename))
   if (f && f->open(filename))
     return f;
     return f;
Line 42: Line 35:
     delete f;
     delete f;
     return 0;
     return 0;
   }
   }</syntax>
</syntax>
you could now do this instead (with almost identical meaning, except for the "trailing dot" hack):
you could now do this instead (with almost identical meaning, except for the "trailing dot" hack):
<syntax type="C++">
<syntax type="C++"> return SearchMan.openFile(filename);</syntax>
  return SearchMan.openFile(filename);
</syntax>
 




Line 85: Line 74:


Also, if you a file simple occurs multiple times (say, an engine-data file, like "kyra.dat", may live in the current dir, extrapath and gamepath, in multiple versions), then the basic API of Archive will just give you any of these. If you need to distinguish multiple files with the same name (e.g. because you want to pick the "kyra.dat" with the correct version), there is also an advanced API which lets you deal with that.
Also, if you a file simple occurs multiple times (say, an engine-data file, like "kyra.dat", may live in the current dir, extrapath and gamepath, in multiple versions), then the basic API of Archive will just give you any of these. If you need to distinguish multiple files with the same name (e.g. because you want to pick the "kyra.dat" with the correct version), there is also an advanced API which lets you deal with that.




1,079

edits

Navigation menu