Update ooo320-m1
[ooovba.git] / sal / osl / all / filepath.c
blobb586e1f3c74e7a2ecbbb224732d96f9d13434c26
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: filepath.c,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <osl/file.h>
32 #include <rtl/ustring.h>
34 static sal_uInt32 SAL_CALL osl_defCalcTextWidth( rtl_uString *ustrText )
36 return ustrText ? ustrText->length : 0;
40 oslFileError SAL_CALL osl_abbreviateSystemPath( rtl_uString *ustrSystemPath, rtl_uString **pustrCompacted, sal_uInt32 uMaxWidth, oslCalcTextWidthFunc pfnCalcWidth )
42 oslFileError error = osl_File_E_None;
43 rtl_uString *ustrPath = NULL;
44 rtl_uString *ustrFile = NULL;
45 sal_uInt32 uPathWidth, uFileWidth;
47 if ( !pfnCalcWidth )
48 pfnCalcWidth = osl_defCalcTextWidth;
51 sal_Int32 iLastSlash = rtl_ustr_lastIndexOfChar_WithLength( ustrSystemPath->buffer, ustrSystemPath->length, SAL_PATHDELIMITER );
53 if ( iLastSlash >= 0 )
55 rtl_uString_newFromStr_WithLength( &ustrPath, ustrSystemPath->buffer, iLastSlash );
56 rtl_uString_newFromStr_WithLength( &ustrFile, &ustrSystemPath->buffer[iLastSlash], ustrSystemPath->length - iLastSlash );
58 else
60 rtl_uString_new( &ustrPath );
61 rtl_uString_newFromString( &ustrFile, ustrSystemPath );
65 uPathWidth = pfnCalcWidth( ustrPath );
66 uFileWidth = pfnCalcWidth( ustrFile );
68 /* First abbreviate the directory component of the path */
70 while ( uPathWidth + uFileWidth > uMaxWidth )
72 if ( ustrPath->length > 3 )
74 ustrPath->length--;
75 ustrPath->buffer[ustrPath->length-3] = '.';
76 ustrPath->buffer[ustrPath->length-2] = '.';
77 ustrPath->buffer[ustrPath->length-1] = '.';
78 ustrPath->buffer[ustrPath->length] = 0;
80 uPathWidth = pfnCalcWidth( ustrPath );
82 else
83 break;
86 /* Now abbreviate file component */
88 while ( uPathWidth + uFileWidth > uMaxWidth )
90 if ( ustrFile->length > 4 )
92 ustrFile->length--;
93 ustrFile->buffer[ustrFile->length-3] = '.';
94 ustrFile->buffer[ustrFile->length-2] = '.';
95 ustrFile->buffer[ustrFile->length-1] = '.';
96 ustrFile->buffer[ustrFile->length] = 0;
98 uFileWidth = pfnCalcWidth( ustrFile );
100 else
101 break;
104 rtl_uString_newConcat( pustrCompacted, ustrPath, ustrFile );
106 /* Event now if path was compacted to ".../..." it can be to large */
108 uPathWidth += uFileWidth;
110 while ( uPathWidth > uMaxWidth )
112 (*pustrCompacted)->length--;
113 (*pustrCompacted)->buffer[(*pustrCompacted)->length] = 0;
114 uPathWidth = pfnCalcWidth( *pustrCompacted );
117 if ( ustrPath )
118 rtl_uString_release( ustrPath );
120 if ( ustrFile )
121 rtl_uString_release( ustrFile );
123 return error;