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 :
29 0 : oslFileError SAL_CALL osl_abbreviateSystemPath( rtl_uString *ustrSystemPath, rtl_uString **pustrCompacted, sal_uInt32 uMaxWidth, oslCalcTextWidthFunc pfnCalcWidth )
30 : {
31 0 : oslFileError error = osl_File_E_None;
32 0 : rtl_uString *ustrPath = NULL;
33 0 : rtl_uString *ustrFile = NULL;
34 : sal_uInt32 uPathWidth, uFileWidth;
35 :
36 0 : if ( !pfnCalcWidth )
37 0 : pfnCalcWidth = osl_defCalcTextWidth;
38 :
39 : {
40 0 : sal_Int32 iLastSlash = rtl_ustr_lastIndexOfChar_WithLength( ustrSystemPath->buffer, ustrSystemPath->length, SAL_PATHDELIMITER );
41 :
42 0 : if ( iLastSlash >= 0 )
43 : {
44 0 : rtl_uString_newFromStr_WithLength( &ustrPath, ustrSystemPath->buffer, iLastSlash );
45 0 : rtl_uString_newFromStr_WithLength( &ustrFile, &ustrSystemPath->buffer[iLastSlash], ustrSystemPath->length - iLastSlash );
46 : }
47 : else
48 : {
49 0 : rtl_uString_new( &ustrPath );
50 0 : rtl_uString_newFromString( &ustrFile, ustrSystemPath );
51 : }
52 : }
53 :
54 0 : uPathWidth = pfnCalcWidth( ustrPath );
55 0 : uFileWidth = pfnCalcWidth( ustrFile );
56 :
57 : /* First abbreviate the directory component of the path */
58 :
59 0 : while ( uPathWidth + uFileWidth > uMaxWidth )
60 : {
61 0 : if ( ustrPath->length > 3 )
62 : {
63 0 : ustrPath->length--;
64 0 : ustrPath->buffer[ustrPath->length-3] = '.';
65 0 : ustrPath->buffer[ustrPath->length-2] = '.';
66 0 : ustrPath->buffer[ustrPath->length-1] = '.';
67 0 : ustrPath->buffer[ustrPath->length] = 0;
68 :
69 0 : uPathWidth = pfnCalcWidth( ustrPath );
70 : }
71 : else
72 0 : break;
73 : }
74 :
75 : /* Now abbreviate file component */
76 :
77 0 : while ( uPathWidth + uFileWidth > uMaxWidth )
78 : {
79 0 : if ( ustrFile->length > 4 )
80 : {
81 0 : ustrFile->length--;
82 0 : ustrFile->buffer[ustrFile->length-3] = '.';
83 0 : ustrFile->buffer[ustrFile->length-2] = '.';
84 0 : ustrFile->buffer[ustrFile->length-1] = '.';
85 0 : ustrFile->buffer[ustrFile->length] = 0;
86 :
87 0 : uFileWidth = pfnCalcWidth( ustrFile );
88 : }
89 : else
90 0 : break;
91 : }
92 :
93 0 : rtl_uString_newConcat( pustrCompacted, ustrPath, ustrFile );
94 :
95 : /* Event now if path was compacted to ".../..." it can be to large */
96 :
97 0 : uPathWidth += uFileWidth;
98 :
99 0 : while ( uPathWidth > uMaxWidth )
100 : {
101 0 : (*pustrCompacted)->length--;
102 0 : (*pustrCompacted)->buffer[(*pustrCompacted)->length] = 0;
103 0 : uPathWidth = pfnCalcWidth( *pustrCompacted );
104 : }
105 :
106 0 : if ( ustrPath )
107 0 : rtl_uString_release( ustrPath );
108 :
109 0 : if ( ustrFile )
110 0 : rtl_uString_release( ustrFile );
111 :
112 0 : return error;
113 : }
114 :
115 :
116 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|