xref: /trunk/main/vos/inc/vos/stream.hxx (revision 67e470dafe1997e73f56ff7ff4878983707e3e07)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _VOS_STREAM_HXX_
25 #define _VOS_STREAM_HXX_
26 
27 #   include <vos/types.hxx>
28 #   include <vos/object.hxx>
29 #   include <vos/istream.hxx>
30 
31 namespace vos
32 {
33 
34 /** Adds seeking capabilities to IStream
35 */
36 class IPositionableStream : public vos::IStream
37 {
38 
39 public:
40 
41     typedef sal_Int32 Offset;
42 
43 public:
44 
45     ///
46     virtual sal_Bool SAL_CALL seekTo(Offset position) const = 0;
47 
48     ///
49     virtual sal_Bool SAL_CALL seekRelative(Offset change) const = 0;
50 
51     ///
52     virtual sal_Bool SAL_CALL seekToEnd() const = 0;
53 
54     ///
55     virtual sal_Bool SAL_CALL changeSize(sal_uInt32 new_size) = 0;
56 
57     ///
58     virtual sal_uInt32 SAL_CALL getSize() const = 0;
59 
60 
61     ///
62     virtual Offset SAL_CALL getOffset() const = 0;
63 
64 
65 protected:
66     IPositionableStream() { }
67     virtual ~IPositionableStream() { }
68 
69 };
70 
71 
72 /** Implements IPositionableStream
73 */
74 class OStream : public vos::OObject,
75                 public vos::IPositionableStream
76 {
77     VOS_DECLARE_CLASSINFO(VOS_NAMESPACE(OStream, vos));
78 
79 public:
80 
81     ///
82     OStream(IPositionableStream& rStream);
83 
84     ///
85     virtual ~OStream ();
86 
87     // ----------------- Read operations -------------------------
88 
89     ///
90     virtual sal_Int32 SAL_CALL read(void* pbuffer, sal_uInt32 n) const;
91 
92     ///
93     sal_Int32 SAL_CALL read(IPositionableStream::Offset offset,
94                  void* pbuffer,
95                  sal_uInt32 n) const;
96 
97     ///
98     inline sal_Bool SAL_CALL read(sal_Int32& value) const;
99 
100     ///
101     inline sal_Bool SAL_CALL read(sal_Int16& value) const;
102 
103     ///
104     inline sal_Bool SAL_CALL read(sal_Char& value) const;
105 
106     ///
107     inline sal_Bool SAL_CALL read(sal_uInt8& value) const;
108 
109     // ----------------- Write operations ------------------------
110 
111     ///
112     virtual sal_Int32 SAL_CALL write(const void* pbuffer, sal_uInt32 n);
113 
114     ///
115     sal_Int32 SAL_CALL write(IPositionableStream::Offset offset,
116                   const void* pbuffer,
117                   sal_uInt32 n);
118     ///
119     inline sal_Bool SAL_CALL write(sal_Int32 value);
120 
121     ///
122     inline sal_Bool SAL_CALL write(sal_Int16 value);
123 
124     ///
125     inline sal_Bool SAL_CALL write(sal_Char value);
126 
127     ///
128     inline sal_Bool SAL_CALL write(sal_uInt8 value);
129 
130     ///
131     sal_Bool SAL_CALL append(void* pbuffer, sal_uInt32 n); // Write at the end of the Stream.
132 
133     // ------------- Positioning and sizing operations ----------
134 
135     ///
136     inline sal_Bool SAL_CALL seekToBegin() const;
137 
138     // ----------------- Stream interface ------------------------
139 
140     ///
141     virtual sal_Bool SAL_CALL seekTo(IPositionableStream::Offset pos) const;
142 
143     ///
144     virtual sal_Bool SAL_CALL seekToEnd() const;
145 
146     ///
147     virtual sal_Bool SAL_CALL seekRelative(IPositionableStream::Offset change) const;
148 
149     ///
150     virtual sal_Bool SAL_CALL changeSize(sal_uInt32 new_size);
151 
152     ///
153     virtual sal_uInt32 SAL_CALL getSize() const;
154 
155     ///
156     virtual sal_Bool SAL_CALL isEof() const;
157 
158     ///
159     virtual IPositionableStream::Offset SAL_CALL getOffset() const;
160 
161 protected:
162     IPositionableStream& m_rStream;
163 
164     // ----------------- Stream operators ------------------------
165 
166     friend const OStream& operator>> (OStream& rStream, sal_Int32& value);
167     friend const OStream& operator>> (OStream& rStream, sal_Int16& value);
168     friend const OStream& operator>> (OStream& rStream, sal_uInt8& value);
169     friend const OStream& operator>> (OStream& rStream, sal_Char& value);
170     friend OStream& operator<< (OStream& rStream, sal_Int32 value);
171     friend OStream& operator<< (OStream& rStream, sal_Int16 value);
172     friend OStream& operator<< (OStream& rStream, sal_uInt8 value);
173     friend OStream& operator<< (OStream& rStream, sal_Char value);
174 };
175 
176 inline sal_Bool OStream::read(sal_Int32& value) const
177 {
178     return (read(&value, sizeof(value)) == sizeof(value));
179 }
180 
181 inline sal_Bool OStream::read(sal_Int16& value) const
182 {
183     return (read(&value, sizeof(value)) == sizeof(value));
184 }
185 
186 inline sal_Bool OStream::read(sal_Char& value) const
187 {
188     return (read(&value, sizeof(value)) == sizeof(value));
189 }
190 
191 inline sal_Bool OStream::read(sal_uInt8& value) const
192 {
193     return (read(&value, sizeof(value)) == sizeof(value));
194 }
195 
196 inline sal_Bool OStream::write(sal_Int32 value)
197 {
198     return (write(&value, sizeof(value)) == sizeof(value));
199 }
200 
201 inline sal_Bool OStream::write(sal_Int16 value)
202 {
203     return (write(&value, sizeof(value)) == sizeof(value));
204 }
205 
206 inline sal_Bool OStream::write(sal_Char value)
207 {
208     return (write(&value, sizeof(value)) == sizeof(value));
209 }
210 
211 inline sal_Bool OStream::write(sal_uInt8 value)
212 {
213     return (write((sal_uInt8*)&value, sizeof(value)) == sizeof(value));
214 }
215 
216 inline sal_Bool OStream::seekToBegin() const
217 {
218     return (seekTo(0L));
219 }
220 
221 inline const OStream& operator>> (OStream& rStream, sal_Int32& value)
222 {
223     rStream.read(value);
224 
225     return (rStream);
226 }
227 
228 inline const OStream& operator>> (OStream& rStream, sal_Int16& value)
229 {
230     rStream.read(value);
231 
232     return (rStream);
233 }
234 
235 inline const OStream& operator>> (OStream& rStream, sal_uInt8& value)
236 {
237     rStream.read(value);
238 
239     return (rStream);
240 }
241 
242 inline const OStream& operator>> (OStream& rStream, sal_Char& value)
243 {
244     rStream.read(value);
245 
246     return (rStream);
247 }
248 
249 inline OStream& operator<< (OStream& rStream, sal_Int32 value)
250 {
251     rStream.write(value);
252 
253     return (rStream);
254 }
255 
256 inline OStream& operator<< (OStream& rStream, sal_Int16 value)
257 {
258     rStream.write(value);
259 
260     return (rStream);
261 }
262 
263 inline OStream& operator<< (OStream& rStream, sal_uInt8 value)
264 {
265     rStream.write(value);
266 
267     return (rStream);
268 }
269 
270 inline OStream& operator<< (OStream& rStream, sal_Char value)
271 {
272     rStream.write(value);
273 
274     return (rStream);
275 }
276 
277 }
278 
279 #endif // _VOS_STREAM_HXX_
280 
281 
282