tdf#154546 skip dispatch when presenter controller is not set
[LibreOffice.git] / sal / osl / unx / file_path_helper.cxx
bloba8ee5238f95a26d1d62648902e9890b1fde11aa4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <cassert>
23 #include <utility>
25 #include "file_path_helper.hxx"
26 #include "uunxapi.hxx"
28 #include <osl/diagnose.h>
29 #include <rtl/ustring.hxx>
30 #include <sal/log.hxx>
32 const sal_Unicode FPH_CHAR_PATH_SEPARATOR = '/';
33 const sal_Unicode FPH_CHAR_DOT = '.';
34 const sal_Unicode FPH_CHAR_COLON = ':';
36 void osl_systemPathRemoveSeparator(rtl_String* pstrPath)
38 OSL_PRECOND(nullptr != pstrPath, "osl_systemPathRemoveSeparator: Invalid parameter");
39 if (pstrPath == nullptr)
40 return;
42 // maybe there are more than one separator at end
43 // so we run in a loop
44 while ((pstrPath->length > 1) && (pstrPath->buffer[pstrPath->length - 1] == FPH_CHAR_PATH_SEPARATOR))
46 pstrPath->length--;
47 pstrPath->buffer[pstrPath->length] = '\0';
50 SAL_WARN_IF( !((0 == pstrPath->length) || (1 == pstrPath->length) ||
51 (pstrPath->length > 1 && pstrPath->buffer[pstrPath->length - 1] != FPH_CHAR_PATH_SEPARATOR)),
52 "sal.osl",
53 "osl_systemPathRemoveSeparator: Post condition failed");
56 namespace {
58 template<typename T> void systemPathEnsureSeparator(T* ppstrPath)
60 assert(nullptr != ppstrPath);
61 sal_Int32 lp = ppstrPath->getLength();
62 sal_Int32 i = ppstrPath->lastIndexOf(FPH_CHAR_PATH_SEPARATOR);
64 if ((lp > 1 && i != (lp - 1)) || ((lp < 2) && i < 0))
66 *ppstrPath += "/";
69 SAL_WARN_IF( !ppstrPath->endsWith("/"),
70 "sal.osl",
71 "systemPathEnsureSeparator: Post condition failed");
76 bool osl_systemPathIsRelativePath(const rtl_uString* pustrPath)
78 OSL_PRECOND(nullptr != pustrPath, "osl_systemPathIsRelativePath: Invalid parameter");
79 return ((pustrPath == nullptr) || (pustrPath->length == 0) || (pustrPath->buffer[0] != FPH_CHAR_PATH_SEPARATOR));
82 namespace {
84 template<typename T> T systemPathMakeAbsolutePath_(
85 const T& BasePath,
86 const T& RelPath)
88 T base(BasePath);
90 if (!base.isEmpty())
91 systemPathEnsureSeparator(&base);
93 return base + RelPath;
98 OString osl::systemPathMakeAbsolutePath(
99 const OString& BasePath,
100 const OString& RelPath)
102 return systemPathMakeAbsolutePath_(BasePath, RelPath);
105 OUString osl::systemPathMakeAbsolutePath(
106 const OUString& BasePath,
107 const OUString& RelPath)
109 return systemPathMakeAbsolutePath_(BasePath, RelPath);
112 void osl_systemPathGetFileNameOrLastDirectoryPart(
113 const rtl_String* pstrPath,
114 rtl_String** ppstrFileNameOrLastDirPart)
116 OSL_PRECOND(pstrPath && ppstrFileNameOrLastDirPart,
117 "osl_systemPathGetFileNameOrLastDirectoryPart: Invalid parameter");
119 OString path(const_cast<rtl_String*>(pstrPath));
121 osl_systemPathRemoveSeparator(path.pData);
123 OString last_part;
125 if (path.getLength() > 1 || (path.getLength() == 1 && path[0] != FPH_CHAR_PATH_SEPARATOR))
127 sal_Int32 idx_ps = path.lastIndexOf(FPH_CHAR_PATH_SEPARATOR);
128 idx_ps++; // always right to increment by one even if idx_ps == -1!
129 last_part = path.copy(idx_ps);
131 rtl_string_assign(ppstrFileNameOrLastDirPart, last_part.pData);
134 bool osl_systemPathIsHiddenFileOrDirectoryEntry(
135 const rtl_String* pstrPath)
137 OSL_PRECOND(nullptr != pstrPath, "osl_systemPathIsHiddenFileOrDirectoryEntry: Invalid parameter");
138 if ((pstrPath == nullptr) || (pstrPath->length == 0))
139 return false;
141 OString fdp;
142 osl_systemPathGetFileNameOrLastDirectoryPart(pstrPath, &fdp.pData);
144 return ((fdp.pData->length > 0) &&
145 (fdp.pData->buffer[0] == FPH_CHAR_DOT) &&
146 !osl_systemPathIsLocalOrParentDirectoryEntry(fdp.pData));
149 bool osl_systemPathIsLocalOrParentDirectoryEntry(
150 const rtl_String* pstrPath)
152 OSL_PRECOND(pstrPath, "osl_systemPathIsLocalOrParentDirectoryEntry: Invalid parameter");
154 OString dirent;
156 osl_systemPathGetFileNameOrLastDirectoryPart(pstrPath, &dirent.pData);
158 return (dirent == "." ||
159 dirent == "..");
162 namespace {
164 /** Simple iterator for a path list separated by the specified character
166 class path_list_iterator
168 public:
170 /* after construction get_current_item
171 returns the first path in list, no need
172 to call reset first
174 path_list_iterator(OUString path_list, sal_Unicode list_separator = FPH_CHAR_COLON) :
175 m_path_list(std::move(path_list)),
176 m_end(m_path_list.getStr() + m_path_list.getLength() + 1),
177 m_separator(list_separator)
179 reset();
182 path_list_iterator(const path_list_iterator&) = delete;
183 path_list_iterator& operator=(const path_list_iterator&) = delete;
185 void reset()
187 m_path_segment_begin = m_path_segment_end = m_path_list.getStr();
188 advance();
191 void next()
193 OSL_PRECOND(!done(), "path_list_iterator: Already done!");
195 m_path_segment_begin = ++m_path_segment_end;
196 advance();
199 bool done() const
201 return (m_path_segment_end >= m_end);
204 OUString get_current_item() const
206 return OUString(
207 m_path_segment_begin,
208 (m_path_segment_end - m_path_segment_begin));
211 private:
212 /* move m_path_end to the next separator or
213 to the end of the string
215 void advance()
217 while (!done() && *m_path_segment_end && (*m_path_segment_end != m_separator))
218 ++m_path_segment_end;
220 OSL_ASSERT(m_path_segment_end <= m_end);
223 private:
224 OUString m_path_list;
225 const sal_Unicode* m_end;
226 const sal_Unicode m_separator;
227 const sal_Unicode* m_path_segment_begin;
228 const sal_Unicode* m_path_segment_end;
233 bool osl_searchPath(
234 const rtl_uString* pustrFilePath,
235 const rtl_uString* pustrSearchPathList,
236 rtl_uString** ppustrPathFound)
238 OSL_PRECOND(pustrFilePath && pustrSearchPathList && ppustrPathFound, "osl_searchPath: Invalid parameter");
240 bool bfound = false;
241 OUString fp(const_cast<rtl_uString*>(pustrFilePath));
242 OUString pl(const_cast<rtl_uString*>(pustrSearchPathList));
243 path_list_iterator pli(pl);
245 while (!pli.done())
247 OUString p = pli.get_current_item();
248 systemPathEnsureSeparator(&p);
249 p += fp;
251 if (osl::access(osl::OUStringToOString(p), F_OK) > -1)
253 bfound = true;
254 rtl_uString_assign(ppustrPathFound, p.pData);
255 break;
257 pli.next();
259 return bfound;
262 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */