PageRenderTime 49ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/pole/pole.h

https://bitbucket.org/sbaum/writeabless
C Header | 239 lines | 71 code | 43 blank | 125 comment | 4 complexity | 963a51313160ec298e282c48d8f4ca69 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /* POLE - Portable C++ library to access OLE Storage
  2. Copyright (C) 2002-2005 Ariya Hidayat <ariya@kde.org>
  3. Performance optimization: Dmitry Fedorov
  4. Copyright 2009 <www.bioimage.ucsb.edu> <www.dimin.net>
  5. Fix for more than 236 mbat block entries : Michel Boudinot
  6. Copyright 2010 <Michel.Boudinot@inaf.cnrs-gif.fr>
  7. Considerable rework to allow for creation and updating of structured storage: Stephen Baum
  8. Copyright 2013 <srbaum@gmail.com>
  9. Added GetAllStreams, reworked datatypes
  10. Copyright 2013 Felix Gorny from Bitplane
  11. More datatype changes to allow for 32 and 64 bit code, some fixes involving incremental updates, flushing
  12. Copyright 2013 <srbaum@gmail.com>
  13. Version: 0.5.2
  14. Redistribution and use in source and binary forms, with or without
  15. modification, are permitted provided that the following conditions
  16. are met:
  17. * Redistributions of source code must retain the above copyright notice,
  18. this list of conditions and the following disclaimer.
  19. * Redistributions in binary form must reproduce the above copyright notice,
  20. this list of conditions and the following disclaimer in the documentation
  21. and/or other materials provided with the distribution.
  22. * Neither the name of the authors nor the names of its contributors may be
  23. used to endorse or promote products derived from this software without
  24. specific prior written permission.
  25. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #ifndef POLE_H
  38. #define POLE_H
  39. #include <cstdio>
  40. #include <string>
  41. #include <list>
  42. namespace POLE
  43. {
  44. #if defined WIN32 || defined WIN64 || defined _WIN32 || defined _WIN64 || defined _MSVC
  45. typedef __int32 int32;
  46. typedef __int64 int64;
  47. typedef unsigned __int32 uint32;
  48. typedef unsigned __int64 uint64;
  49. #else
  50. typedef int int32;
  51. typedef long long int64;
  52. typedef unsigned int uint32;
  53. typedef unsigned long long uint64;
  54. #endif
  55. class StorageIO;
  56. class Stream;
  57. class StreamIO;
  58. class Storage
  59. {
  60. friend class Stream;
  61. friend class StreamOut;
  62. public:
  63. // for Storage::result()
  64. enum { Ok, OpenFailed, NotOLE, BadOLE, UnknownError };
  65. /**
  66. * Constructs a storage with name filename.
  67. **/
  68. Storage( const char* filename );
  69. /**
  70. * Destroys the storage.
  71. **/
  72. ~Storage();
  73. /**
  74. * Opens the storage. Returns true if no error occurs.
  75. **/
  76. bool open(bool bWriteAccess = false, bool bCreate = false);
  77. /**
  78. * Closes the storage.
  79. **/
  80. void close();
  81. /**
  82. * Returns the error code of last operation.
  83. **/
  84. int result();
  85. /**
  86. * Finds all stream and directories in given path.
  87. **/
  88. std::list<std::string> entries( const std::string& path = "/" );
  89. /**
  90. * Returns true if specified entry name is a directory.
  91. */
  92. bool isDirectory( const std::string& name );
  93. /**
  94. * Returns true if specified entry name exists.
  95. */
  96. bool exists( const std::string& name );
  97. /**
  98. * Returns true if storage can be modified.
  99. */
  100. bool isWriteable();
  101. /**
  102. * Deletes a specified stream or directory. If directory, it will
  103. * recursively delete everything underneath said directory.
  104. * returns true for success
  105. */
  106. bool deleteByName( const std::string& name );
  107. /**
  108. * Returns an accumulation of information, hopefully useful for determining if the storage
  109. * should be defragmented.
  110. */
  111. void GetStats(size_t *pEntries, size_t *pUnusedEntries,
  112. size_t *pBigBlocks, size_t *pUnusedBigBlocks,
  113. size_t *pSmallBlocks, size_t *pUnusedSmallBlocks);
  114. std::list<std::string> GetAllStreams( const std::string& storageName );
  115. private:
  116. StorageIO* io;
  117. // no copy or assign
  118. Storage( const Storage& );
  119. Storage& operator=( const Storage& );
  120. };
  121. class Stream
  122. {
  123. friend class Storage;
  124. friend class StorageIO;
  125. public:
  126. /**
  127. * Creates a new stream.
  128. */
  129. // name must be absolute, e.g "/Workbook"
  130. Stream( Storage* storage, const std::string& name, bool bCreate = false, size_t streamSize = 0);
  131. /**
  132. * Destroys the stream.
  133. */
  134. ~Stream();
  135. /**
  136. * Returns the full stream name.
  137. */
  138. std::string fullName();
  139. /**
  140. * Returns the stream size.
  141. **/
  142. size_t size();
  143. /**
  144. * Changes the stream size (note this is done automatically if you write beyond the old size.
  145. * Use this primarily as a preamble to rewriting a stream that is already open. Of course, you
  146. * could simply delete the stream first).
  147. **/
  148. void setSize(size_t newSize);
  149. /**
  150. * Returns the current read/write position.
  151. **/
  152. size_t tell();
  153. /**
  154. * Sets the read/write position.
  155. **/
  156. void seek( size_t pos );
  157. /**
  158. * Reads a byte.
  159. **/
  160. int32 getch();
  161. /**
  162. * Reads a block of data.
  163. **/
  164. size_t read( unsigned char* data, size_t maxlen );
  165. /**
  166. * Writes a block of data.
  167. **/
  168. size_t write( unsigned char* data, size_t len );
  169. /**
  170. * Makes sure that any changes for the stream (and the structured storage) have been written to disk.
  171. **/
  172. void flush();
  173. /**
  174. * Returns true if the read/write position is past the file.
  175. **/
  176. bool eof();
  177. /**
  178. * Returns true whenever error occurs.
  179. **/
  180. bool fail();
  181. private:
  182. StreamIO* io;
  183. // no copy or assign
  184. Stream( const Stream& );
  185. Stream& operator=( const Stream& );
  186. };
  187. }
  188. #endif // POLE_H