xref: /aoo41x/main/sal/osl/all/filepath.c (revision 647f063d)
1*647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*647f063dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*647f063dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*647f063dSAndrew Rist  * distributed with this work for additional information
6*647f063dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*647f063dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*647f063dSAndrew Rist  * "License"); you may not use this file except in compliance
9*647f063dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*647f063dSAndrew Rist  *
11*647f063dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*647f063dSAndrew Rist  *
13*647f063dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*647f063dSAndrew Rist  * software distributed under the License is distributed on an
15*647f063dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*647f063dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*647f063dSAndrew Rist  * specific language governing permissions and limitations
18*647f063dSAndrew Rist  * under the License.
19*647f063dSAndrew Rist  *
20*647f063dSAndrew Rist  *************************************************************/
21*647f063dSAndrew Rist 
22*647f063dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <osl/file.h>
25cdf0e10cSrcweir #include <rtl/ustring.h>
26cdf0e10cSrcweir 
osl_defCalcTextWidth(rtl_uString * ustrText)27cdf0e10cSrcweir static sal_uInt32 SAL_CALL osl_defCalcTextWidth( rtl_uString *ustrText )
28cdf0e10cSrcweir {
29cdf0e10cSrcweir 	return ustrText ? ustrText->length : 0;
30cdf0e10cSrcweir }
31cdf0e10cSrcweir 
32cdf0e10cSrcweir 
osl_abbreviateSystemPath(rtl_uString * ustrSystemPath,rtl_uString ** pustrCompacted,sal_uInt32 uMaxWidth,oslCalcTextWidthFunc pfnCalcWidth)33cdf0e10cSrcweir oslFileError SAL_CALL osl_abbreviateSystemPath( rtl_uString *ustrSystemPath, rtl_uString **pustrCompacted, sal_uInt32 uMaxWidth, oslCalcTextWidthFunc pfnCalcWidth )
34cdf0e10cSrcweir {
35cdf0e10cSrcweir 	oslFileError	error = osl_File_E_None;
36cdf0e10cSrcweir 	rtl_uString		*ustrPath = NULL;
37cdf0e10cSrcweir 	rtl_uString		*ustrFile = NULL;
38cdf0e10cSrcweir 	sal_uInt32		uPathWidth, uFileWidth;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir 	if ( !pfnCalcWidth )
41cdf0e10cSrcweir 		pfnCalcWidth = osl_defCalcTextWidth;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir 	{
44cdf0e10cSrcweir 		sal_Int32	iLastSlash = rtl_ustr_lastIndexOfChar_WithLength( ustrSystemPath->buffer, ustrSystemPath->length, SAL_PATHDELIMITER );
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 		if ( iLastSlash >= 0 )
47cdf0e10cSrcweir 		{
48cdf0e10cSrcweir 			rtl_uString_newFromStr_WithLength( &ustrPath, ustrSystemPath->buffer, iLastSlash );
49cdf0e10cSrcweir 			rtl_uString_newFromStr_WithLength( &ustrFile, &ustrSystemPath->buffer[iLastSlash], ustrSystemPath->length - iLastSlash );
50cdf0e10cSrcweir 		}
51cdf0e10cSrcweir 		else
52cdf0e10cSrcweir 		{
53cdf0e10cSrcweir 			rtl_uString_new( &ustrPath );
54cdf0e10cSrcweir 			rtl_uString_newFromString( &ustrFile, ustrSystemPath );
55cdf0e10cSrcweir 		}
56cdf0e10cSrcweir 	}
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 	uPathWidth = pfnCalcWidth( ustrPath );
59cdf0e10cSrcweir 	uFileWidth = pfnCalcWidth( ustrFile );
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 	/* First abbreviate the directory component of the path */
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 	while ( uPathWidth + uFileWidth > uMaxWidth )
64cdf0e10cSrcweir 	{
65cdf0e10cSrcweir 		if ( ustrPath->length > 3 )
66cdf0e10cSrcweir 		{
67cdf0e10cSrcweir 			ustrPath->length--;
68cdf0e10cSrcweir 			ustrPath->buffer[ustrPath->length-3] = '.';
69cdf0e10cSrcweir 			ustrPath->buffer[ustrPath->length-2] = '.';
70cdf0e10cSrcweir 			ustrPath->buffer[ustrPath->length-1] = '.';
71cdf0e10cSrcweir 			ustrPath->buffer[ustrPath->length] = 0;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 			uPathWidth = pfnCalcWidth( ustrPath );
74cdf0e10cSrcweir 		}
75cdf0e10cSrcweir 		else
76cdf0e10cSrcweir 			break;
77cdf0e10cSrcweir 	}
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 	/* Now abbreviate file component */
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 	while ( uPathWidth + uFileWidth > uMaxWidth )
82cdf0e10cSrcweir 	{
83cdf0e10cSrcweir 		if ( ustrFile->length > 4 )
84cdf0e10cSrcweir 		{
85cdf0e10cSrcweir 			ustrFile->length--;
86cdf0e10cSrcweir 			ustrFile->buffer[ustrFile->length-3] = '.';
87cdf0e10cSrcweir 			ustrFile->buffer[ustrFile->length-2] = '.';
88cdf0e10cSrcweir 			ustrFile->buffer[ustrFile->length-1] = '.';
89cdf0e10cSrcweir 			ustrFile->buffer[ustrFile->length] = 0;
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 			uFileWidth = pfnCalcWidth( ustrFile );
92cdf0e10cSrcweir 		}
93cdf0e10cSrcweir 		else
94cdf0e10cSrcweir 			break;
95cdf0e10cSrcweir 	}
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 	rtl_uString_newConcat( pustrCompacted, ustrPath, ustrFile );
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 	/* Event now if path was compacted to ".../..." it can be to large */
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 	uPathWidth += uFileWidth;
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 	while ( uPathWidth > uMaxWidth )
104cdf0e10cSrcweir 	{
105cdf0e10cSrcweir 		(*pustrCompacted)->length--;
106cdf0e10cSrcweir 		(*pustrCompacted)->buffer[(*pustrCompacted)->length] = 0;
107cdf0e10cSrcweir 		uPathWidth = pfnCalcWidth( *pustrCompacted );
108cdf0e10cSrcweir 	}
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 	if ( ustrPath )
111cdf0e10cSrcweir 		rtl_uString_release( ustrPath );
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 	if ( ustrFile )
114cdf0e10cSrcweir 		rtl_uString_release( ustrFile );
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 	return error;
117cdf0e10cSrcweir }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 
120