Version 7.3.5.2, tag libreoffice-7.3.5.2
[LibreOffice.git] / tools / source / misc / pathutils.cxx
blob706740a320f999765bbe6929434bd8823edd085d
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 #if defined(_WIN32)
24 #define WIN32_LEAN_AND_MEAN
25 #include <windows.h>
27 #include <o3tl/safeint.hxx>
28 #include <sal/types.h>
29 #include <tools/pathutils.hxx>
31 namespace tools {
33 WCHAR * filename(WCHAR * path) {
34 WCHAR * f = path;
35 for (WCHAR * p = path;;) {
36 switch (*p++) {
37 case L'\0':
38 return f;
39 case L'\\':
40 f = p;
41 break;
46 WCHAR * buildPath(
47 WCHAR * path, WCHAR const * frontBegin, WCHAR const * frontEnd,
48 WCHAR const * backBegin, std::size_t backLength)
50 // Remove leading ".." segments in the second path together with matching
51 // segments in the first path that are neither empty nor "." nor ".." nor
52 // end in ":" (which is not foolproof, as it can erroneously erase the start
53 // of a UNC path, but only if the input is bad data):
54 while (backLength >= 2 && backBegin[0] == L'.' && backBegin[1] == L'.' &&
55 (backLength == 2 || backBegin[2] == L'\\'))
57 if (frontEnd - frontBegin < 2 || frontEnd[-1] != L'\\' ||
58 frontEnd[-2] == L'\\' || frontEnd[-2] == L':' ||
59 (frontEnd[-2] == L'.' &&
60 (frontEnd - frontBegin < 3 || frontEnd[-3] == L'\\' ||
61 (frontEnd[-3] == L'.' &&
62 (frontEnd - frontBegin < 4 || frontEnd[-4] == L'\\')))))
64 break;
66 WCHAR const * p = frontEnd - 1;
67 while (p != frontBegin && p[-1] != L'\\') {
68 --p;
70 if (p == frontBegin) {
71 break;
73 frontEnd = p;
74 if (backLength == 2) {
75 backBegin += 2;
76 backLength -= 2;
77 } else {
78 backBegin += 3;
79 backLength -= 3;
82 if (backLength <
83 o3tl::make_unsigned(MAX_PATH - (frontEnd - frontBegin)))
85 WCHAR * p;
86 if (frontBegin == path) {
87 p = const_cast< WCHAR * >(frontEnd);
88 } else {
89 p = path;
90 while (frontBegin != frontEnd) {
91 *p++ = *frontBegin++;
94 for (; backLength > 0; --backLength) {
95 *p++ = *backBegin++;
97 *p = L'\0';
98 return p;
99 } else {
100 SetLastError(ERROR_FILENAME_EXCED_RANGE);
101 return nullptr;
107 #endif
109 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */