xref: /trunk/main/sal/osl/all/filepath.c (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <osl/file.h>
29 #include <rtl/ustring.h>
30 
31 static sal_uInt32 SAL_CALL osl_defCalcTextWidth( rtl_uString *ustrText )
32 {
33     return ustrText ? ustrText->length : 0;
34 }
35 
36 
37 oslFileError SAL_CALL osl_abbreviateSystemPath( rtl_uString *ustrSystemPath, rtl_uString **pustrCompacted, sal_uInt32 uMaxWidth, oslCalcTextWidthFunc pfnCalcWidth )
38 {
39     oslFileError    error = osl_File_E_None;
40     rtl_uString     *ustrPath = NULL;
41     rtl_uString     *ustrFile = NULL;
42     sal_uInt32      uPathWidth, uFileWidth;
43 
44     if ( !pfnCalcWidth )
45         pfnCalcWidth = osl_defCalcTextWidth;
46 
47     {
48         sal_Int32   iLastSlash = rtl_ustr_lastIndexOfChar_WithLength( ustrSystemPath->buffer, ustrSystemPath->length, SAL_PATHDELIMITER );
49 
50         if ( iLastSlash >= 0 )
51         {
52             rtl_uString_newFromStr_WithLength( &ustrPath, ustrSystemPath->buffer, iLastSlash );
53             rtl_uString_newFromStr_WithLength( &ustrFile, &ustrSystemPath->buffer[iLastSlash], ustrSystemPath->length - iLastSlash );
54         }
55         else
56         {
57             rtl_uString_new( &ustrPath );
58             rtl_uString_newFromString( &ustrFile, ustrSystemPath );
59         }
60     }
61 
62     uPathWidth = pfnCalcWidth( ustrPath );
63     uFileWidth = pfnCalcWidth( ustrFile );
64 
65     /* First abbreviate the directory component of the path */
66 
67     while ( uPathWidth + uFileWidth > uMaxWidth )
68     {
69         if ( ustrPath->length > 3 )
70         {
71             ustrPath->length--;
72             ustrPath->buffer[ustrPath->length-3] = '.';
73             ustrPath->buffer[ustrPath->length-2] = '.';
74             ustrPath->buffer[ustrPath->length-1] = '.';
75             ustrPath->buffer[ustrPath->length] = 0;
76 
77             uPathWidth = pfnCalcWidth( ustrPath );
78         }
79         else
80             break;
81     }
82 
83     /* Now abbreviate file component */
84 
85     while ( uPathWidth + uFileWidth > uMaxWidth )
86     {
87         if ( ustrFile->length > 4 )
88         {
89             ustrFile->length--;
90             ustrFile->buffer[ustrFile->length-3] = '.';
91             ustrFile->buffer[ustrFile->length-2] = '.';
92             ustrFile->buffer[ustrFile->length-1] = '.';
93             ustrFile->buffer[ustrFile->length] = 0;
94 
95             uFileWidth = pfnCalcWidth( ustrFile );
96         }
97         else
98             break;
99     }
100 
101     rtl_uString_newConcat( pustrCompacted, ustrPath, ustrFile );
102 
103     /* Event now if path was compacted to ".../..." it can be to large */
104 
105     uPathWidth += uFileWidth;
106 
107     while ( uPathWidth > uMaxWidth )
108     {
109         (*pustrCompacted)->length--;
110         (*pustrCompacted)->buffer[(*pustrCompacted)->length] = 0;
111         uPathWidth = pfnCalcWidth( *pustrCompacted );
112     }
113 
114     if ( ustrPath )
115         rtl_uString_release( ustrPath );
116 
117     if ( ustrFile )
118         rtl_uString_release( ustrFile );
119 
120     return error;
121 }
122 
123 
124