Avoid potential negative array index access to cached text.
[LibreOffice.git] / writerperfect / source / common / DirectoryStream.cxx
blob9664fbee97502ded97d39a6cb90f59eeded48ecd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* writerperfect
3 * Version: MPL 2.0 / LGPLv2.1+
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/.
9 * Major Contributor(s):
10 * Copyright (C) 2007 Fridrich Strba (fridrich.strba@bluewin.ch)
12 * For minor contributions see the git repository.
14 * Alternatively, the contents of this file may be used under the terms
15 * of the GNU Lesser General Public License Version 2.1 or later
16 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
17 * applicable instead of those above.
19 * For further information visit http://libwpd.sourceforge.net
22 #include <memory>
23 #include <com/sun/star/container/XChild.hpp>
24 #include <com/sun/star/io/XInputStream.hpp>
25 #include <com/sun/star/sdbc/XResultSet.hpp>
26 #include <com/sun/star/sdbc/XRow.hpp>
27 #include <com/sun/star/ucb/XContent.hpp>
28 #include <com/sun/star/ucb/XContentAccess.hpp>
29 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
31 #include <com/sun/star/uno/Reference.hxx>
32 #include <com/sun/star/uno/Sequence.hxx>
34 #include <comphelper/processfactory.hxx>
36 #include <rtl/ustring.hxx>
38 #include <ucbhelper/content.hxx>
40 #include <DirectoryStream.hxx>
41 #include <WPXSvInputStream.hxx>
42 #include <utility>
44 namespace io = com::sun::star::io;
45 namespace sdbc = com::sun::star::sdbc;
46 namespace ucb = com::sun::star::ucb;
47 namespace uno = com::sun::star::uno;
49 namespace writerperfect
51 namespace
53 uno::Reference<io::XInputStream> findStream(ucbhelper::Content& rContent, std::u16string_view rName)
55 uno::Reference<io::XInputStream> xInputStream;
57 uno::Sequence<OUString> lPropNames{ "Title" };
58 try
60 const uno::Reference<sdbc::XResultSet> xResultSet(
61 rContent.createCursor(lPropNames, ucbhelper::INCLUDE_DOCUMENTS_ONLY));
62 if (xResultSet->first())
64 const uno::Reference<ucb::XContentAccess> xContentAccess(xResultSet,
65 uno::UNO_QUERY_THROW);
66 const uno::Reference<sdbc::XRow> xRow(xResultSet, uno::UNO_QUERY_THROW);
69 const OUString aTitle(xRow->getString(1));
70 if (aTitle == rName)
72 const uno::Reference<ucb::XContent> xSubContent(xContentAccess->queryContent());
73 ucbhelper::Content aSubContent(xSubContent,
74 uno::Reference<ucb::XCommandEnvironment>(),
75 comphelper::getProcessComponentContext());
76 xInputStream = aSubContent.openStream();
77 break;
79 } while (xResultSet->next());
82 catch (const uno::RuntimeException&)
84 // ignore
86 catch (const uno::Exception&)
88 // ignore
91 return xInputStream;
95 struct DirectoryStream::Impl
97 explicit Impl(uno::Reference<ucb::XContent> xContent);
99 uno::Reference<ucb::XContent> xContent;
102 DirectoryStream::Impl::Impl(uno::Reference<ucb::XContent> _xContent)
103 : xContent(std::move(_xContent))
107 DirectoryStream::DirectoryStream(const css::uno::Reference<css::ucb::XContent>& xContent)
108 : m_pImpl(isDirectory(xContent) ? new Impl(xContent) : nullptr)
112 DirectoryStream::~DirectoryStream() {}
114 bool DirectoryStream::isDirectory(const css::uno::Reference<css::ucb::XContent>& xContent)
118 if (!xContent.is())
119 return false;
121 ucbhelper::Content aContent(xContent, uno::Reference<ucb::XCommandEnvironment>(),
122 comphelper::getProcessComponentContext());
123 return aContent.isFolder();
125 catch (...)
127 return false;
131 std::unique_ptr<DirectoryStream>
132 DirectoryStream::createForParent(const css::uno::Reference<css::ucb::XContent>& xContent)
136 if (!xContent.is())
137 return nullptr;
139 std::unique_ptr<DirectoryStream> pDir;
141 const uno::Reference<css::container::XChild> xChild(xContent, uno::UNO_QUERY);
142 if (xChild.is())
144 const uno::Reference<ucb::XContent> xDirContent(xChild->getParent(), uno::UNO_QUERY);
145 if (xDirContent.is())
147 pDir = std::make_unique<DirectoryStream>(xDirContent);
148 if (!pDir->isStructured())
149 pDir.reset();
153 return pDir;
155 catch (...)
157 return nullptr;
161 css::uno::Reference<css::ucb::XContent> DirectoryStream::getContent() const
163 if (!m_pImpl)
164 return css::uno::Reference<css::ucb::XContent>();
165 return m_pImpl->xContent;
168 bool DirectoryStream::isStructured() { return m_pImpl != nullptr; }
170 unsigned DirectoryStream::subStreamCount()
172 // TODO: implement me
173 return 0;
176 const char* DirectoryStream::subStreamName(unsigned /* id */)
178 // TODO: implement me
179 return nullptr;
182 bool DirectoryStream::existsSubStream(const char* /* name */)
184 // TODO: implement me
185 return false;
188 librevenge::RVNGInputStream* DirectoryStream::getSubStreamByName(const char* const pName)
190 if (!m_pImpl)
191 return nullptr;
193 ucbhelper::Content aContent(m_pImpl->xContent, uno::Reference<ucb::XCommandEnvironment>(),
194 comphelper::getProcessComponentContext());
195 const uno::Reference<io::XInputStream> xInputStream(
196 findStream(aContent, OUString::createFromAscii(pName)));
197 if (xInputStream.is())
198 return new WPXSvInputStream(xInputStream);
200 return nullptr;
203 librevenge::RVNGInputStream* DirectoryStream::getSubStreamById(unsigned /* id */)
205 // TODO: implement me
206 return nullptr;
209 const unsigned char* DirectoryStream::read(unsigned long, unsigned long& nNumBytesRead)
211 nNumBytesRead = 0;
212 return nullptr;
215 int DirectoryStream::seek(long, librevenge::RVNG_SEEK_TYPE) { return -1; }
217 long DirectoryStream::tell() { return 0; }
219 bool DirectoryStream::isEnd() { return true; }
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */