1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
25 #define WIN32_LEAN_AND_MEAN
27 #include <sal/types.h>
28 #include <tools/pathutils.hxx>
32 WCHAR
* filename(WCHAR
* path
) {
34 for (WCHAR
* p
= path
;;) {
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
'\\')))))
65 WCHAR
const * p
= frontEnd
- 1;
66 while (p
!= frontBegin
&& p
[-1] != L
'\\') {
69 if (p
== frontBegin
) {
73 if (backLength
== 2) {
82 static_cast< std::size_t >(MAX_PATH
- (frontEnd
- frontBegin
)))
83 // hopefully std::size_t is large enough
86 if (frontBegin
== path
) {
87 p
= const_cast< WCHAR
* >(frontEnd
);
90 while (frontBegin
!= frontEnd
) {
94 for (; backLength
> 0; --backLength
) {
100 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
109 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */