FileStream.h

00001 /***************************************************************************
00002  *                                                                         *
00003  *   (c) Art Tevs, MPI Informatik Saarbruecken                             *
00004  *       mailto: <tevs@mpi-sb.mpg.de>                                      *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  *                                                                         *
00011  ***************************************************************************/
00012 
00013 
00014 #ifndef _NR_FILE_STREAM_H_
00015 #define _NR_FILE_STREAM_H_
00016 
00017 
00018 //----------------------------------------------------------------------------------
00019 // Includes
00020 //----------------------------------------------------------------------------------
00021 #include "Prerequisities.h"
00022 #include "IStream.h"
00023 #include "Resource.h"
00024 
00025 namespace nrEngine{
00026 
00027         //! Derived class from IStream to provide streaming from files
00028         /**
00029         * FileStream is derived class from IStream and IResource. It provides
00030         * the functionality of streaming the files from the disk. We use the
00031         * std::ifstream class to do this work.
00032         *
00033         * This class is a simple file class which provide the user only
00034         * with nessary methods. You should use plugins to extend the functionality
00035         * for working with compressed, encrypted etc. file archives.
00036         *
00037         * Because each file is also a resource, so you can open the files through
00038         * the resource manager. This will call the appropriate resource loader
00039         * (in our case file loader) to provide you with the file you requested.
00040         * The type of such a resource is "File"
00041         * Plugins can add new types like "ZipFile", "ZipArchive", ...
00042         *
00043         * \ingroup vfs
00044         **/
00045         class _NRExport FileStream : public IStream, public IResource {
00046         public:
00047 
00048                 /**
00049                 * Constructor of the file stream. The loader should
00050                 * specify all neccessary properties of this object to allow
00051                 * opening of the file.
00052                 **/
00053                 FileStream();
00054 
00055                 /**
00056                 * Virtual destructor allows to derive new classes from this interface
00057                 **/
00058                 virtual ~FileStream();
00059 
00060 
00061                 /**
00062                  * Open a file as a stream without using of underlying resource system.
00063                  **/
00064                 virtual Result open (const std::string& fileName);
00065 
00066                 //------------------------------------------------------------
00067                 //              IStream Interface
00068                 //------------------------------------------------------------
00069 
00070                 /**
00071                 * Return the name of the stream. The name of the stream is the same
00072                 * as the name of the underlying resource used for the file storing.
00073                 **/
00074                 const std::string& getName()    const { return getResourceName(); }
00075 
00076                 /**
00077                 * @see IStream::read()
00078                 * @param nmemb Number of element to read in
00079                 * @param buf Buffer to read from
00080                 * @param size Size of one element
00081                 **/
00082                 virtual size_t read(void *buf, size_t size, size_t nmemb);
00083 
00084                 /**
00085                 * @copydoc IStream::readDelim()
00086                 **/
00087                 virtual size_t readDelim(void* buf, size_t count, const std::string& delim = std::string("\n"));
00088 
00089                 /**
00090                 * @copydoc IStream::tell()
00091                 **/
00092                 virtual size_t tell() const;
00093 
00094                 /**
00095                 * @copydoc IStream::eof()
00096                 **/
00097                 virtual bool eof() const;
00098 
00099                 /**
00100                 * @copydoc IStream::getData()
00101                 **/
00102                 virtual byte* getData(size_t& count) const;
00103 
00104                 /**
00105                 * @copydoc IStream::seek()
00106                 **/
00107                 virtual bool seek(int32 offset, int32 whence = IStream::CURRENT);
00108 
00109                 /**
00110                 *  @copydoc IStream::close()
00111                 **/
00112                 virtual void close ();
00113 
00114         private:
00115 
00116                 //! The file stream loader should be able to change the data of this object
00117                 friend class FileStreamLoader;
00118 
00119                 //! Stream used to read the file
00120                 SharedPtr< std::ifstream > mStream;
00121 
00122                 Result unloadResource();
00123                 Result reloadResource(PropertyList* params);
00124         };
00125 
00126 
00127         //! Empty file stream to represent empty files
00128         /**
00129          * This is  file stream which loads an empty file. You will
00130          * need this to get ResourceManagment working corectly. Any
00131          * access to a resource instantiated from this class have
00132          * no effect.
00133          *
00134          * \ingroup vfs
00135          **/
00136         class _NRExport EmptyFileStream : public FileStream {
00137 
00138         public:
00139                 //! Initialize the empty file
00140                 EmptyFileStream();
00141 
00142                 //! Deinitilize the empty file object
00143                 ~EmptyFileStream();
00144 
00145                 //! Read data from the empty file returns nothing
00146                 size_t read(void *buf, size_t size, size_t nmemb);
00147 
00148                 //! Read until we fund a delimeter
00149                 size_t readDelim(void* buf, size_t count, const std::string& delim = "\n");
00150 
00151                 //! What is the position of the empty file reading cursor
00152                 size_t tell() const;
00153 
00154                 //! Are we on the end of file. For empty files it is always true
00155                 bool eof() const;
00156 
00157                 //! Returns no data from the empty file
00158                 byte* getData(size_t& count) const;
00159 
00160                 //! Seek or not to seek? That is the question!
00161                 bool seek(int32 offset, int32 whence = IStream::CURRENT);
00162 
00163                 //! Close the empty file
00164                 void close ();
00165 
00166         };
00167 
00168 
00169 }; // end namespace
00170 
00171 #endif

Generated on Wed Sep 12 23:19:42 2007 for nrEngine by  doxygen 1.5.1