| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* librevenge |
| 3 | * Version: MPL 2.0 / LGPLv2.1+ |
| 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 | * Major Contributor(s): |
| 10 | * Copyright (C) 2002-2005 William Lachance ([email protected]) |
| 11 | * Copyright (C) 2002 Marc Maurer ([email protected]) |
| 12 | * Copyright (C) 2006 Fridrich Strba ([email protected]) |
| 13 | * |
| 14 | * For minor contributions see the git repository. |
| 15 | * |
| 16 | * Alternatively, the contents of this file may be used under the terms |
| 17 | * of the GNU Lesser General Public License Version 2.1 or later |
| 18 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 19 | * applicable instead of those above. |
| 20 | */ |
| 21 | |
| 22 | #include <librevenge-stream/librevenge-stream.h> |
| 23 | |
| 24 | #include <sys/stat.h> |
| 25 | #include <memory> |
| 26 | #include <string> |
| 27 | #include <vector> |
| 28 | #include <boost/algorithm/string.hpp> |
| 29 | |
| 30 | namespace librevenge |
| 31 | { |
| 32 | |
| 33 | namespace |
| 34 | { |
| 35 | |
| 36 | static void sanitizePath(std::string &str) |
| 37 | { |
| 38 | boost::algorithm::replace_all(str, "\\", "/"); |
| 39 | boost::algorithm::trim_right_if(str, boost::is_any_of("/ ")); |
| 40 | } |
| 41 | |
| 42 | static std::string composePath(const std::vector<std::string> &splitPath, std::vector<std::string>::size_type elementCount) |
| 43 | { |
| 44 | std::string path; |
| 45 | for (std::vector<std::string>::size_type i=0; i < splitPath.size() && i < elementCount; ++i) |
| 46 | { |
| 47 | path.append(splitPath[i]); |
| 48 | path.append("/"); |
| 49 | } |
| 50 | sanitizePath(path); |
| 51 | return path; |
| 52 | } |
| 53 | |
| 54 | static bool isReg(const char *const path) |
| 55 | { |
| 56 | struct stat statBuf; |
| 57 | if (stat(path, &statBuf)) |
| 58 | return false; |
| 59 | if (S_ISREG(statBuf.st_mode)) |
| 60 | return true; |
| 61 | #ifndef _WIN32 |
| 62 | else if (S_ISLNK(statBuf.st_mode)) |
| 63 | { |
| 64 | if (lstat(path, &statBuf)) |
| 65 | return false; |
| 66 | if (S_ISREG(statBuf.st_mode)) |
| 67 | return true; |
| 68 | } |
| 69 | #endif |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | static bool isDir(const char *const path) |
| 74 | { |
| 75 | struct stat statBuf; |
| 76 | if (stat(path, &statBuf)) |
| 77 | return false; |
| 78 | if (S_ISDIR(statBuf.st_mode)) |
| 79 | return true; |
| 80 | #ifndef _WIN32 |
| 81 | else if (S_ISLNK(statBuf.st_mode)) |
| 82 | { |
| 83 | if (lstat(path, &statBuf)) |
| 84 | return false; |
| 85 | if (S_ISDIR(statBuf.st_mode)) |
| 86 | return true; |
| 87 | } |
| 88 | #endif |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | } |
| 93 | |
| 94 | struct RVNGDirectoryStreamImpl |
| 95 | { |
| 96 | std::vector<std::string> m_splitPath; |
| 97 | |
| 98 | RVNGDirectoryStreamImpl(const char *path); |
| 99 | }; |
| 100 | |
| 101 | RVNGDirectoryStreamImpl::RVNGDirectoryStreamImpl(const char *const path) |
| 102 | : m_splitPath() |
| 103 | { |
| 104 | std::string pathName(path); |
| 105 | sanitizePath(pathName); |
| 106 | boost::algorithm::split(m_splitPath, pathName, boost::is_any_of("/")); |
| 107 | } |
| 108 | |
| 109 | RVNGDirectoryStream::RVNGDirectoryStream(const char *const path) |
| 110 | : m_impl(nullptr) |
| 111 | { |
| 112 | if (isDir(path)) |
| 113 | m_impl = new RVNGDirectoryStreamImpl(path); |
| 114 | } |
| 115 | |
| 116 | RVNGDirectoryStream::~RVNGDirectoryStream() |
| 117 | { |
| 118 | delete m_impl; |
| 119 | } |
| 120 | |
| 121 | RVNGDirectoryStream *RVNGDirectoryStream::createForParent(const char *const path) |
| 122 | { |
| 123 | std::string parent(path); |
| 124 | sanitizePath(parent); |
| 125 | std::vector<std::string> splitParent; |
| 126 | boost::algorithm::split(splitParent, parent, boost::is_any_of("/")); |
| 127 | parent = composePath(splitParent, splitParent.size() ? splitParent.size() - 1 : 0); |
| 128 | |
| 129 | std::unique_ptr<RVNGDirectoryStream> strm{new RVNGDirectoryStream(parent.c_str())}; |
| 130 | if (strm->isStructured()) // only if parent is a dir |
| 131 | return strm.release(); |
| 132 | |
| 133 | return nullptr; |
| 134 | } |
| 135 | |
| 136 | bool RVNGDirectoryStream::isDirectory(const char *const path) |
| 137 | { |
| 138 | return isDir(path); |
| 139 | } |
| 140 | |
| 141 | bool RVNGDirectoryStream::isStructured() |
| 142 | { |
| 143 | if (!m_impl) |
| 144 | return false; |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | unsigned RVNGDirectoryStream::subStreamCount() |
| 149 | { |
| 150 | if (!m_impl) |
| 151 | return 0; |
| 152 | // TODO: implement me |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | const char *RVNGDirectoryStream::subStreamName(unsigned id) |
| 157 | { |
| 158 | // TODO: implement me |
| 159 | (void) id; |
| 160 | return nullptr; |
| 161 | } |
| 162 | |
| 163 | bool RVNGDirectoryStream::existsSubStream(const char *name) |
| 164 | { |
| 165 | if (!m_impl) |
| 166 | return false; |
| 167 | std::string path(name); |
| 168 | sanitizePath(path); |
| 169 | std::vector<std::string> splitPath; |
| 170 | boost::algorithm::split(splitPath, path, boost::is_any_of("/")); |
| 171 | splitPath.insert(splitPath.begin(), m_impl->m_splitPath.begin(), m_impl->m_splitPath.end()); |
| 172 | path = composePath(splitPath, splitPath.size()); |
| 173 | return isReg(path.c_str()); |
| 174 | |
| 175 | } |
| 176 | |
| 177 | RVNGInputStream *RVNGDirectoryStream::getSubStreamByName(const char *const name) |
| 178 | { |
| 179 | if (!m_impl) |
| 180 | return nullptr; |
| 181 | std::string path(name); |
| 182 | sanitizePath(path); |
| 183 | std::vector<std::string> splitPath; |
| 184 | boost::algorithm::split(splitPath, path, boost::is_any_of("/")); |
| 185 | splitPath.insert(splitPath.begin(), m_impl->m_splitPath.begin(), m_impl->m_splitPath.end()); |
| 186 | path = composePath(splitPath, splitPath.size()); |
| 187 | |
| 188 | if (isReg(path.c_str())) |
| 189 | return new RVNGFileStream(path.c_str()); |
| 190 | else if (isDir(path.c_str())) |
| 191 | return new RVNGDirectoryStream(path.c_str()); |
| 192 | |
| 193 | return nullptr; |
| 194 | } |
| 195 | |
| 196 | RVNGInputStream *RVNGDirectoryStream::getSubStreamById(unsigned id) |
| 197 | { |
| 198 | // TODO: implement me |
| 199 | (void) id; |
| 200 | return nullptr; |
| 201 | } |
| 202 | |
| 203 | const unsigned char *RVNGDirectoryStream::read(const unsigned long, unsigned long &numBytesRead) |
| 204 | { |
| 205 | numBytesRead = 0; |
| 206 | return nullptr; |
| 207 | } |
| 208 | |
| 209 | int RVNGDirectoryStream::seek(const long, const RVNG_SEEK_TYPE) |
| 210 | { |
| 211 | return -1; |
| 212 | } |
| 213 | |
| 214 | long RVNGDirectoryStream::tell() |
| 215 | { |
| 216 | if (!m_impl) |
| 217 | return -1L; |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | bool RVNGDirectoryStream::isEnd() |
| 222 | { |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | } |
| 227 | |
| 228 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |