bump product version to 6.1.0.2
[LibreOffice.git] / sal / osl / all / filepath.cxx
blob7e8022f4f9e81fab378c63a39daa7c09e11c80e8
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 <osl/file.h>
21 #include <rtl/ustring.h>
23 static sal_uInt32 osl_defCalcTextWidth( rtl_uString *ustrText )
25 return ustrText ? ustrText->length : 0;
28 oslFileError SAL_CALL osl_abbreviateSystemPath( rtl_uString *ustrSystemPath, rtl_uString **pustrCompacted, sal_uInt32 uMaxWidth, oslCalcTextWidthFunc pfnCalcWidth )
30 rtl_uString *ustrPath = nullptr;
31 rtl_uString *ustrFile = nullptr;
32 sal_uInt32 uPathWidth, uFileWidth;
34 if ( !pfnCalcWidth )
35 pfnCalcWidth = osl_defCalcTextWidth;
38 sal_Int32 iLastSlash = rtl_ustr_lastIndexOfChar_WithLength( ustrSystemPath->buffer, ustrSystemPath->length, SAL_PATHDELIMITER );
40 if ( iLastSlash >= 0 )
42 rtl_uString_newFromStr_WithLength( &ustrPath, ustrSystemPath->buffer, iLastSlash );
43 rtl_uString_newFromStr_WithLength( &ustrFile, &ustrSystemPath->buffer[iLastSlash], ustrSystemPath->length - iLastSlash );
45 else
47 rtl_uString_new( &ustrPath );
48 rtl_uString_newFromString( &ustrFile, ustrSystemPath );
52 uPathWidth = pfnCalcWidth( ustrPath );
53 uFileWidth = pfnCalcWidth( ustrFile );
55 /* First abbreviate the directory component of the path */
57 while ( uPathWidth + uFileWidth > uMaxWidth )
59 if ( ustrPath->length > 3 )
61 ustrPath->length--;
62 ustrPath->buffer[ustrPath->length-3] = '.';
63 ustrPath->buffer[ustrPath->length-2] = '.';
64 ustrPath->buffer[ustrPath->length-1] = '.';
65 ustrPath->buffer[ustrPath->length] = 0;
67 uPathWidth = pfnCalcWidth( ustrPath );
69 else
70 break;
73 /* Now abbreviate file component */
75 while ( uPathWidth + uFileWidth > uMaxWidth )
77 if ( ustrFile->length > 4 )
79 ustrFile->length--;
80 ustrFile->buffer[ustrFile->length-3] = '.';
81 ustrFile->buffer[ustrFile->length-2] = '.';
82 ustrFile->buffer[ustrFile->length-1] = '.';
83 ustrFile->buffer[ustrFile->length] = 0;
85 uFileWidth = pfnCalcWidth( ustrFile );
87 else
88 break;
91 rtl_uString_newConcat( pustrCompacted, ustrPath, ustrFile );
93 /* Event now if path was compacted to ".../..." it can be to large */
95 uPathWidth += uFileWidth;
97 while ( uPathWidth > uMaxWidth )
99 (*pustrCompacted)->length--;
100 (*pustrCompacted)->buffer[(*pustrCompacted)->length] = 0;
101 uPathWidth = pfnCalcWidth( *pustrCompacted );
104 if ( ustrPath )
105 rtl_uString_release( ustrPath );
107 if ( ustrFile )
108 rtl_uString_release( ustrFile );
110 return osl_File_E_None;
113 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */