Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <osl/file.h>
21 : #include <rtl/ustring.h>
22 :
23 0 : static sal_uInt32 SAL_CALL osl_defCalcTextWidth( rtl_uString *ustrText )
24 : {
25 0 : return ustrText ? ustrText->length : 0;
26 : }
27 :
28 0 : oslFileError SAL_CALL osl_abbreviateSystemPath( rtl_uString *ustrSystemPath, rtl_uString **pustrCompacted, sal_uInt32 uMaxWidth, oslCalcTextWidthFunc pfnCalcWidth )
29 : {
30 0 : oslFileError error = osl_File_E_None;
31 0 : rtl_uString *ustrPath = NULL;
32 0 : rtl_uString *ustrFile = NULL;
33 : sal_uInt32 uPathWidth, uFileWidth;
34 :
35 0 : if ( !pfnCalcWidth )
36 0 : pfnCalcWidth = osl_defCalcTextWidth;
37 :
38 : {
39 0 : sal_Int32 iLastSlash = rtl_ustr_lastIndexOfChar_WithLength( ustrSystemPath->buffer, ustrSystemPath->length, SAL_PATHDELIMITER );
40 :
41 0 : if ( iLastSlash >= 0 )
42 : {
43 0 : rtl_uString_newFromStr_WithLength( &ustrPath, ustrSystemPath->buffer, iLastSlash );
44 0 : rtl_uString_newFromStr_WithLength( &ustrFile, &ustrSystemPath->buffer[iLastSlash], ustrSystemPath->length - iLastSlash );
45 : }
46 : else
47 : {
48 0 : rtl_uString_new( &ustrPath );
49 0 : rtl_uString_newFromString( &ustrFile, ustrSystemPath );
50 : }
51 : }
52 :
53 0 : uPathWidth = pfnCalcWidth( ustrPath );
54 0 : uFileWidth = pfnCalcWidth( ustrFile );
55 :
56 : /* First abbreviate the directory component of the path */
57 :
58 0 : while ( uPathWidth + uFileWidth > uMaxWidth )
59 : {
60 0 : if ( ustrPath->length > 3 )
61 : {
62 0 : ustrPath->length--;
63 0 : ustrPath->buffer[ustrPath->length-3] = '.';
64 0 : ustrPath->buffer[ustrPath->length-2] = '.';
65 0 : ustrPath->buffer[ustrPath->length-1] = '.';
66 0 : ustrPath->buffer[ustrPath->length] = 0;
67 :
68 0 : uPathWidth = pfnCalcWidth( ustrPath );
69 : }
70 : else
71 0 : break;
72 : }
73 :
74 : /* Now abbreviate file component */
75 :
76 0 : while ( uPathWidth + uFileWidth > uMaxWidth )
77 : {
78 0 : if ( ustrFile->length > 4 )
79 : {
80 0 : ustrFile->length--;
81 0 : ustrFile->buffer[ustrFile->length-3] = '.';
82 0 : ustrFile->buffer[ustrFile->length-2] = '.';
83 0 : ustrFile->buffer[ustrFile->length-1] = '.';
84 0 : ustrFile->buffer[ustrFile->length] = 0;
85 :
86 0 : uFileWidth = pfnCalcWidth( ustrFile );
87 : }
88 : else
89 0 : break;
90 : }
91 :
92 0 : rtl_uString_newConcat( pustrCompacted, ustrPath, ustrFile );
93 :
94 : /* Event now if path was compacted to ".../..." it can be to large */
95 :
96 0 : uPathWidth += uFileWidth;
97 :
98 0 : while ( uPathWidth > uMaxWidth )
99 : {
100 0 : (*pustrCompacted)->length--;
101 0 : (*pustrCompacted)->buffer[(*pustrCompacted)->length] = 0;
102 0 : uPathWidth = pfnCalcWidth( *pustrCompacted );
103 : }
104 :
105 0 : if ( ustrPath )
106 0 : rtl_uString_release( ustrPath );
107 :
108 0 : if ( ustrFile )
109 0 : rtl_uString_release( ustrFile );
110 :
111 0 : return error;
112 : }
113 :
114 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|