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