LibreOffice Module tools (master) 1
pathutils.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice 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 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#if defined(_WIN32)
23
24#define WIN32_LEAN_AND_MEAN
25#include <windows.h>
26
27#include <o3tl/safeint.hxx>
28#include <sal/types.h>
29#include <tools/pathutils.hxx>
30
31namespace tools {
32
33WCHAR * 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;
42 }
43 }
44}
45
46WCHAR * buildPath(
47 WCHAR * path, WCHAR const * frontBegin, WCHAR const * frontEnd,
48 WCHAR const * backBegin, std::size_t backLength)
49{
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'\\'))
56 {
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'\\')))))
63 {
64 break;
65 }
66 WCHAR const * p = frontEnd - 1;
67 while (p != frontBegin && p[-1] != L'\\') {
68 --p;
69 }
70 if (p == frontBegin) {
71 break;
72 }
73 frontEnd = p;
74 if (backLength == 2) {
75 backBegin += 2;
76 backLength -= 2;
77 } else {
78 backBegin += 3;
79 backLength -= 3;
80 }
81 }
82 if (backLength <
83 o3tl::make_unsigned(MAX_PATH - (frontEnd - frontBegin)))
84 {
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++;
92 }
93 }
94 for (; backLength > 0; --backLength) {
95 *p++ = *backBegin++;
96 }
97 *p = L'\0';
98 return p;
99 } else {
100 SetLastError(ERROR_FILENAME_EXCED_RANGE);
101 return nullptr;
102 }
103}
104
105}
106
107#endif
108
109/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void * p
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
Note: this class is a true marvel of engineering: because the author could not decide whether it's be...