Branch libreoffice-5-0-4
[LibreOffice.git] / tools / source / misc / pathutils.cxx
blob0b3f172ee3ed1f654573f9d74bdb2b78bff5ecae
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 WNT
24 #include <cstddef>
25 #define WIN32_LEAN_AND_MEAN
26 #include <windows.h>
27 #include <sal/types.h>
28 #include <tools/pathutils.hxx>
30 namespace tools {
32 WCHAR * filename(WCHAR * path) {
33 WCHAR * f = path;
34 for (WCHAR * p = path;;) {
35 switch (*p++) {
36 case L'\0':
37 return f;
38 case L'\\':
39 f = p;
40 break;
45 WCHAR * buildPath(
46 WCHAR * path, WCHAR const * frontBegin, WCHAR const * frontEnd,
47 WCHAR const * backBegin, std::size_t backLength)
49 // Remove leading ".." segments in the second path together with matching
50 // segments in the first path that are neither empty nor "." nor ".." nor
51 // end in ":" (which is not foolprove, as it can erroneously erase the start
52 // of a UNC path, but only if the input is bad data):
53 while (backLength >= 2 && backBegin[0] == L'.' && backBegin[1] == L'.' &&
54 (backLength == 2 || backBegin[2] == L'\\'))
56 if (frontEnd - frontBegin < 2 || frontEnd[-1] != L'\\' ||
57 frontEnd[-2] == L'\\' || frontEnd[-2] == L':' ||
58 (frontEnd[-2] == L'.' &&
59 (frontEnd - frontBegin < 3 || frontEnd[-3] == L'\\' ||
60 (frontEnd[-3] == L'.' &&
61 (frontEnd - frontBegin < 4 || frontEnd[-4] == L'\\')))))
63 break;
65 WCHAR const * p = frontEnd - 1;
66 while (p != frontBegin && p[-1] != L'\\') {
67 --p;
69 if (p == frontBegin) {
70 break;
72 frontEnd = p;
73 if (backLength == 2) {
74 backBegin += 2;
75 backLength -= 2;
76 } else {
77 backBegin += 3;
78 backLength -= 3;
81 if (backLength <
82 static_cast< std::size_t >(MAX_PATH - (frontEnd - frontBegin)))
83 // hopefully std::size_t is large enough
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 NULL;
107 #endif
109 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */