Bump for 3.6-28
[LibreOffice.git] / sal / osl / all / filepath.c
blob2139a1803fa24074b06f0de9f8062c793d79a107
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <osl/file.h>
30 #include <rtl/ustring.h>
32 static sal_uInt32 SAL_CALL osl_defCalcTextWidth( rtl_uString *ustrText )
34 return ustrText ? ustrText->length : 0;
38 oslFileError SAL_CALL osl_abbreviateSystemPath( rtl_uString *ustrSystemPath, rtl_uString **pustrCompacted, sal_uInt32 uMaxWidth, oslCalcTextWidthFunc pfnCalcWidth )
40 oslFileError error = osl_File_E_None;
41 rtl_uString *ustrPath = NULL;
42 rtl_uString *ustrFile = NULL;
43 sal_uInt32 uPathWidth, uFileWidth;
45 if ( !pfnCalcWidth )
46 pfnCalcWidth = osl_defCalcTextWidth;
49 sal_Int32 iLastSlash = rtl_ustr_lastIndexOfChar_WithLength( ustrSystemPath->buffer, ustrSystemPath->length, SAL_PATHDELIMITER );
51 if ( iLastSlash >= 0 )
53 rtl_uString_newFromStr_WithLength( &ustrPath, ustrSystemPath->buffer, iLastSlash );
54 rtl_uString_newFromStr_WithLength( &ustrFile, &ustrSystemPath->buffer[iLastSlash], ustrSystemPath->length - iLastSlash );
56 else
58 rtl_uString_new( &ustrPath );
59 rtl_uString_newFromString( &ustrFile, ustrSystemPath );
63 uPathWidth = pfnCalcWidth( ustrPath );
64 uFileWidth = pfnCalcWidth( ustrFile );
66 /* First abbreviate the directory component of the path */
68 while ( uPathWidth + uFileWidth > uMaxWidth )
70 if ( ustrPath->length > 3 )
72 ustrPath->length--;
73 ustrPath->buffer[ustrPath->length-3] = '.';
74 ustrPath->buffer[ustrPath->length-2] = '.';
75 ustrPath->buffer[ustrPath->length-1] = '.';
76 ustrPath->buffer[ustrPath->length] = 0;
78 uPathWidth = pfnCalcWidth( ustrPath );
80 else
81 break;
84 /* Now abbreviate file component */
86 while ( uPathWidth + uFileWidth > uMaxWidth )
88 if ( ustrFile->length > 4 )
90 ustrFile->length--;
91 ustrFile->buffer[ustrFile->length-3] = '.';
92 ustrFile->buffer[ustrFile->length-2] = '.';
93 ustrFile->buffer[ustrFile->length-1] = '.';
94 ustrFile->buffer[ustrFile->length] = 0;
96 uFileWidth = pfnCalcWidth( ustrFile );
98 else
99 break;
102 rtl_uString_newConcat( pustrCompacted, ustrPath, ustrFile );
104 /* Event now if path was compacted to ".../..." it can be to large */
106 uPathWidth += uFileWidth;
108 while ( uPathWidth > uMaxWidth )
110 (*pustrCompacted)->length--;
111 (*pustrCompacted)->buffer[(*pustrCompacted)->length] = 0;
112 uPathWidth = pfnCalcWidth( *pustrCompacted );
115 if ( ustrPath )
116 rtl_uString_release( ustrPath );
118 if ( ustrFile )
119 rtl_uString_release( ustrFile );
121 return error;
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */