1 /* POLE - Portable C++ library to access OLE Storage
2    Copyright (C) 2002-2005 Ariya Hidayat <ariya@kde.org>
3 
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions
6    are met:
7    * Redistributions of source code must retain the above copyright notice,
8      this list of conditions and the following disclaimer.
9    * Redistributions in binary form must reproduce the above copyright notice,
10      this list of conditions and the following disclaimer in the documentation
11      and/or other materials provided with the distribution.
12    * Neither the name of the authors nor the names of its contributors may be
13      used to endorse or promote products derived from this software without
14      specific prior written permission.
15 
16    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26    THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef OOXML_POLE_H
30 #define OOXML_POLE_H
31 
32 #include "komsooxml_export.h"
33 
34 class QIODevice;
35 
36 #include <string>
37 #include <list>
38 
39 namespace OOXML_POLE
40 {
41 
42 class StorageIO;
43 class Stream;
44 class StreamIO;
45 
46 class KOMSOOXML_EXPORT Storage
47 {
48     friend class Stream;
49     friend class StreamOut;
50 
51 public:
52 
53     // for Storage::result()
54     enum { Ok, OpenFailed, NotOLE, BadOLE, UnknownError };
55 
56     /**
57      * Constructs a storage with name filename.
58      **/
59     explicit Storage(QIODevice* file);
60 
61     /**
62      * Destroys the storage.
63      **/
64     ~Storage();
65 
66     /**
67      * Opens the storage. Returns true if no error occurs.
68      **/
69     bool open();
70 
71     /**
72      * Closes the storage.
73      **/
74     void close();
75 
76     /**
77      * Returns the error code of last operation.
78      **/
79     int result();
80 
81     /**
82      * Finds all stream and directories in given path.
83      **/
84     std::list<std::string> entries(const std::string& path = "/");
85 
86     /**
87      * Returns true if specified entry name is a directory.
88      */
89     bool isDirectory(const std::string& name);
90 
91     /**
92      * Finds and returns a stream with the specified name.
93      * If reuse is true, this function returns the already created stream
94      * (if any). Otherwise it will create the stream.
95      *
96      * When errors occur, this function returns nullptr.
97      *
98      * You do not need to delete the created stream, it will be handled
99      * automatically.
100      **/
101     Stream* stream(const std::string& name, bool reuse = true);
102     //Stream* stream( const std::string& name, int mode = Stream::ReadOnly, bool reuse = true );
103 
104 private:
105     StorageIO* io;
106 
107     // no copy or assign
108     Storage(const Storage&);
109     Storage& operator=(const Storage&);
110 
111 };
112 
113 class KOMSOOXML_EXPORT Stream
114 {
115     friend class Storage;
116     friend class StorageIO;
117 
118 public:
119 
120     /**
121      * Creates a new stream.
122      */
123     // name must be absolute, e.g "/Workbook"
124     Stream(Storage* storage, const std::string& name);
125 
126     /**
127      * Destroys the stream.
128      */
129     ~Stream();
130 
131     /**
132      * Returns the full stream name.
133      */
134     std::string fullName();
135 
136     /**
137      * Returns the stream size.
138      **/
139     unsigned long size();
140 
141     /**
142      * Returns the current read/write position.
143      **/
144     unsigned long tell();
145 
146     /**
147      * Sets the read/write position.
148      **/
149     void seek(unsigned long pos);
150 
151     /**
152      * Reads a byte.
153      **/
154     int getch();
155 
156     /**
157      * Reads a block of data.
158      **/
159     unsigned long read(unsigned char* data, unsigned long maxlen);
160 
161     /**
162      * Returns true if the read/write position is past the file.
163      **/
164     bool eof();
165 
166     /**
167      * Returns true whenever error occurs.
168      **/
169     bool fail();
170 
171 private:
172     StreamIO* io;
173 
174     // no copy or assign
175     Stream(const Stream&);
176     Stream& operator=(const Stream&);
177 };
178 
179 }
180 
181 #endif // POLE_H
182