Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sal/osl/all/filepath.cxx
Line
Count
Source
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 <osl/file.h>
21
#include <rtl/ustring.h>
22
#include <cassert>
23
24
static sal_uInt32 osl_defCalcTextWidth( rtl_uString *ustrText )
25
95.3k
{
26
95.3k
    return ustrText ? ustrText->length : 0;
27
95.3k
}
28
29
oslFileError SAL_CALL osl_abbreviateSystemPath( rtl_uString *ustrSystemPath, rtl_uString **pustrCompacted, sal_uInt32 uMaxWidth, oslCalcTextWidthFunc pfnCalcWidth )
30
46
{
31
46
    rtl_uString     *ustrPath = nullptr;
32
46
    rtl_uString     *ustrFile = nullptr;
33
46
    sal_uInt32      uPathWidth, uFileWidth;
34
35
46
    if ( !pfnCalcWidth )
36
46
        pfnCalcWidth = osl_defCalcTextWidth;
37
38
46
    {
39
46
        sal_Int32   iLastSlash = rtl_ustr_lastIndexOfChar_WithLength( ustrSystemPath->buffer, ustrSystemPath->length, SAL_PATHDELIMITER );
40
41
46
        if ( iLastSlash >= 0 )
42
16
        {
43
16
            rtl_uString_newFromStr_WithLength( &ustrPath, ustrSystemPath->buffer, iLastSlash );
44
16
            rtl_uString_newFromStr_WithLength( &ustrFile, &ustrSystemPath->buffer[iLastSlash], ustrSystemPath->length - iLastSlash );
45
16
        }
46
30
        else
47
30
        {
48
30
            rtl_uString_new( &ustrPath );
49
30
            rtl_uString_newFromString( &ustrFile, ustrSystemPath );
50
30
        }
51
46
    }
52
53
46
    assert(ustrPath && ustrFile);
54
55
46
    uPathWidth = pfnCalcWidth( ustrPath );
56
46
    uFileWidth = pfnCalcWidth( ustrFile );
57
58
    /* First abbreviate the directory component of the path */
59
60
26.6k
    while ( uPathWidth + uFileWidth > uMaxWidth )
61
26.6k
    {
62
26.6k
        if ( ustrPath->length > 3 )
63
26.6k
        {
64
26.6k
            ustrPath->length--;
65
26.6k
            ustrPath->buffer[ustrPath->length-3] = '.';
66
26.6k
            ustrPath->buffer[ustrPath->length-2] = '.';
67
26.6k
            ustrPath->buffer[ustrPath->length-1] = '.';
68
26.6k
            ustrPath->buffer[ustrPath->length] = 0;
69
70
26.6k
            uPathWidth = pfnCalcWidth( ustrPath );
71
26.6k
        }
72
39
        else
73
39
            break;
74
26.6k
    }
75
76
    /* Now abbreviate file component */
77
78
68.5k
    while ( uPathWidth + uFileWidth > uMaxWidth )
79
68.5k
    {
80
68.5k
        if ( ustrFile->length > 4 )
81
68.5k
        {
82
68.5k
            ustrFile->length--;
83
68.5k
            ustrFile->buffer[ustrFile->length-3] = '.';
84
68.5k
            ustrFile->buffer[ustrFile->length-2] = '.';
85
68.5k
            ustrFile->buffer[ustrFile->length-1] = '.';
86
68.5k
            ustrFile->buffer[ustrFile->length] = 0;
87
88
68.5k
            uFileWidth = pfnCalcWidth( ustrFile );
89
68.5k
        }
90
12
        else
91
12
            break;
92
68.5k
    }
93
94
46
    rtl_uString_newConcat( pustrCompacted, ustrPath, ustrFile );
95
96
    /* Event now if path was compacted to ".../..." it can be too large */
97
98
46
    uPathWidth += uFileWidth;
99
100
94
    while ( uPathWidth > uMaxWidth )
101
48
    {
102
48
        (*pustrCompacted)->length--;
103
48
        (*pustrCompacted)->buffer[(*pustrCompacted)->length] = 0;
104
48
        uPathWidth = pfnCalcWidth( *pustrCompacted );
105
48
    }
106
107
46
    rtl_uString_release(ustrPath);
108
46
    rtl_uString_release(ustrFile);
109
110
46
    return osl_File_E_None;
111
46
}
112
113
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */