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