File indexing completed on 2024-04-28 04:40:02

0001 /* POLE - Portable C++ library to access OLE Storage 
0002    Copyright (C) 2002-2005 Ariya Hidayat <ariya@kde.org>
0003 
0004    Redistribution and use in source and binary forms, with or without 
0005    modification, are permitted provided that the following conditions 
0006    are met:
0007    * Redistributions of source code must retain the above copyright notice, 
0008      this list of conditions and the following disclaimer.
0009    * Redistributions in binary form must reproduce the above copyright notice, 
0010      this list of conditions and the following disclaimer in the documentation 
0011      and/or other materials provided with the distribution.
0012    * Neither the name of the authors nor the names of its contributors may be 
0013      used to endorse or promote products derived from this software without 
0014      specific prior written permission.
0015 
0016    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
0017    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
0018    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
0019    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
0020    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
0021    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
0022    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
0023    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
0024    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
0025    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
0026    THE POSSIBILITY OF SUCH DAMAGE.
0027 */
0028 
0029 #ifndef POLE_H
0030 #define POLE_H
0031 
0032 #include <string>
0033 #include <list>
0034 
0035 namespace POLE
0036 {
0037 
0038 class StorageIO;
0039 class Stream;
0040 class StreamIO;
0041 
0042 class Storage
0043 {
0044   friend class Stream;
0045   friend class StreamOut;
0046 
0047 public:
0048 
0049   // for Storage::result()
0050   enum { Ok, OpenFailed, NotOLE, BadOLE, UnknownError };
0051   
0052   /**
0053    * Constructs a storage with name filename.
0054    **/
0055   explicit Storage( const char* filename );
0056 
0057   /**
0058    * Destroys the storage.
0059    **/
0060   ~Storage();
0061   
0062   /**
0063    * Opens the storage. Returns true if no error occurs.
0064    **/
0065   bool open();
0066 
0067   /**
0068    * Closes the storage.
0069    **/
0070   void close();
0071   
0072   /**
0073    * Returns the error code of last operation.
0074    **/
0075   int result();
0076 
0077   /**
0078    * Finds all stream and directories in given path.
0079    **/
0080   std::list<std::string> entries( const std::string& path = "/" );
0081   
0082   /**
0083    * Returns true if specified entry name is a directory.
0084    */
0085   bool isDirectory( const std::string& name ); 
0086 
0087   /**
0088    * Finds and returns a stream with the specified name.
0089    * If reuse is true, this function returns the already created stream
0090    * (if any). Otherwise it will create the stream.
0091    *
0092    * When errors occur, this function returns NULL.
0093    *
0094    * You do not need to delete the created stream, it will be handled
0095    * automatically.
0096    **/
0097   Stream* stream( const std::string& name, bool reuse = true );
0098   //Stream* stream( const std::string& name, int mode = Stream::ReadOnly, bool reuse = true );
0099   
0100 private:
0101   StorageIO* io;
0102   
0103   // no copy or assign
0104   Storage( const Storage& );
0105   Storage& operator=( const Storage& );
0106 
0107 };
0108 
0109 class Stream
0110 {
0111   friend class Storage;
0112   friend class StorageIO;
0113   
0114 public:
0115 
0116   /**
0117    * Creates a new stream.
0118    */
0119   // name must be absolute, e.g "/Workbook"
0120   Stream( Storage* storage, const std::string& name );
0121 
0122   /**
0123    * Destroys the stream.
0124    */
0125   ~Stream();
0126 
0127   /**
0128    * Returns the full stream name.
0129    */
0130   std::string fullName(); 
0131   
0132   /**
0133    * Returns the stream size.
0134    **/
0135   unsigned long size();
0136 
0137   /**
0138    * Returns the current read/write position.
0139    **/
0140   unsigned long tell();
0141 
0142   /**
0143    * Sets the read/write position.
0144    **/
0145   void seek( unsigned long pos ); 
0146 
0147   /**
0148    * Reads a byte.
0149    **/
0150   int getch();
0151 
0152   /**
0153    * Reads a block of data.
0154    **/
0155   unsigned long read( unsigned char* data, unsigned long maxlen );
0156   
0157   /**
0158    * Returns true if the read/write position is past the file.
0159    **/
0160   bool eof();
0161   
0162   /**
0163    * Returns true whenever error occurs.
0164    **/
0165   bool fail();
0166 
0167 private:
0168   StreamIO* io;
0169 
0170   // no copy or assign
0171   Stream( const Stream& );
0172   Stream& operator=( const Stream& );    
0173 };
0174 
0175 }
0176 
0177 #endif // POLE_H