Debian Codegraph @

src/lib/QXPMemoryStream.cpp

raw
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libqxp project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #include "QXPMemoryStream.h"
11
12 #include <algorithm>
13
14 namespace libqxp
15 {
16
17 QXPMemoryStream::QXPMemoryStream(const unsigned char *data, unsigned length)
18 : m_data()
19 , m_length((long) length)
20 , m_pos(0)
21 {
22 if (length > 0)
23 {
24 m_data.reset(new unsigned char[length]);
25 std::copy(data, data + length, m_data.get());
26 }
27 }
28
29 QXPMemoryStream::~QXPMemoryStream()
30 {
31 }
32
33 bool QXPMemoryStream::isStructured()
34 {
35 return false;
36 }
37
38 unsigned QXPMemoryStream::subStreamCount()
39 {
40 return 0;
41 }
42
43 const char *QXPMemoryStream::subStreamName(unsigned)
44 {
45 return 0;
46 }
47
48 bool QXPMemoryStream::existsSubStream(const char *)
49 {
50 return false;
51 }
52
53 librevenge::RVNGInputStream *QXPMemoryStream::getSubStreamByName(const char *)
54 {
55 return 0;
56 }
57
58 librevenge::RVNGInputStream *QXPMemoryStream::getSubStreamById(unsigned)
59 {
60 return 0;
61 }
62
63 const unsigned char *QXPMemoryStream::read(unsigned long numBytes, unsigned long &numBytesRead) try
64 {
65 numBytesRead = 0;
66
67 if ((0 == numBytes) || (0 == m_length))
68 return 0;
69
70 if (m_pos < 0)
71 return 0;
72
73 const unsigned long length = static_cast<unsigned long>(m_length);
74 const unsigned long pos = static_cast<unsigned long>(m_pos);
75 const unsigned long remaining = pos < length ? length - pos : 0;
76 const unsigned long numBytesToRead = numBytes < remaining ? numBytes : remaining;
77
78 if (numBytesToRead == 0)
79 return 0;
80
81 const long oldPos = m_pos;
82 m_pos += numBytesToRead;
83
84 numBytesRead = numBytesToRead;
85 return m_data.get() + oldPos;
86 }
87 catch (...)
88 {
89 return 0;
90 }
91
92 int QXPMemoryStream::seek(const long offset, librevenge::RVNG_SEEK_TYPE seekType) try
93 {
94 long pos = 0;
95 switch (seekType)
96 {
97 case librevenge::RVNG_SEEK_SET :
98 pos = offset;
99 break;
100 case librevenge::RVNG_SEEK_CUR :
101 pos = offset + m_pos;
102 break;
103 case librevenge::RVNG_SEEK_END :
104 pos = offset + m_length;
105 break;
106 default :
107 return -1;
108 }
109
110 if ((pos < 0) || (pos > m_length))
111 return 1;
112
113 m_pos = pos;
114 return 0;
115 }
116 catch (...)
117 {
118 return -1;
119 }
120
121 long QXPMemoryStream::tell()
122 {
123 return m_pos;
124 }
125
126 bool QXPMemoryStream::isEnd()
127 {
128 return m_length == m_pos;
129 }
130
131 }
132
133 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */