tdf#161411 - UI: Add Better wording for ASCII-only characters
[LibreOffice.git] / sal / osl / all / filepath.cxx
blobafba01739b1bf63e056b959d01681d05539a1346
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>
22 #include <cassert>
24 static sal_uInt32 osl_defCalcTextWidth( rtl_uString *ustrText )
26 return ustrText ? ustrText->length : 0;
29 oslFileError SAL_CALL osl_abbreviateSystemPath( rtl_uString *ustrSystemPath, rtl_uString **pustrCompacted, sal_uInt32 uMaxWidth, oslCalcTextWidthFunc pfnCalcWidth )
31 rtl_uString *ustrPath = nullptr;
32 rtl_uString *ustrFile = nullptr;
33 sal_uInt32 uPathWidth, uFileWidth;
35 if ( !pfnCalcWidth )
36 pfnCalcWidth = osl_defCalcTextWidth;
39 sal_Int32 iLastSlash = rtl_ustr_lastIndexOfChar_WithLength( ustrSystemPath->buffer, ustrSystemPath->length, SAL_PATHDELIMITER );
41 if ( iLastSlash >= 0 )
43 rtl_uString_newFromStr_WithLength( &ustrPath, ustrSystemPath->buffer, iLastSlash );
44 rtl_uString_newFromStr_WithLength( &ustrFile, &ustrSystemPath->buffer[iLastSlash], ustrSystemPath->length - iLastSlash );
46 else
48 rtl_uString_new( &ustrPath );
49 rtl_uString_newFromString( &ustrFile, ustrSystemPath );
53 assert(ustrPath && ustrFile);
55 uPathWidth = pfnCalcWidth( ustrPath );
56 uFileWidth = pfnCalcWidth( ustrFile );
58 /* First abbreviate the directory component of the path */
60 while ( uPathWidth + uFileWidth > uMaxWidth )
62 if ( ustrPath->length > 3 )
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;
70 uPathWidth = pfnCalcWidth( ustrPath );
72 else
73 break;
76 /* Now abbreviate file component */
78 while ( uPathWidth + uFileWidth > uMaxWidth )
80 if ( ustrFile->length > 4 )
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;
88 uFileWidth = pfnCalcWidth( ustrFile );
90 else
91 break;
94 rtl_uString_newConcat( pustrCompacted, ustrPath, ustrFile );
96 /* Event now if path was compacted to ".../..." it can be too large */
98 uPathWidth += uFileWidth;
100 while ( uPathWidth > uMaxWidth )
102 (*pustrCompacted)->length--;
103 (*pustrCompacted)->buffer[(*pustrCompacted)->length] = 0;
104 uPathWidth = pfnCalcWidth( *pustrCompacted );
107 rtl_uString_release(ustrPath);
108 rtl_uString_release(ustrFile);
110 return osl_File_E_None;
113 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */