fix lack of return value in tell_about_jack_death(), and fixed up indentation for...
[ardour.git] / libs / pbd / shortpath.cc
1 /*
2     Copyright (C) 2000-2007 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <stdint.h>
21 #include <pbd/shortpath.h>
22
23 #include <stdint.h>
24
25 using namespace Glib;
26 using namespace std;
27
28 ustring
29 short_path (const Glib::ustring& path, ustring::size_type target_characters)
30 {
31         ustring::size_type last_sep;
32         ustring::size_type len = path.length();
33         const char separator = '/';
34
35         if (len <= target_characters) {
36                 return path;
37         }
38
39         if ((last_sep = path.find_last_of (separator)) == ustring::npos) {
40
41                 /* just a filename, but its too long anyway */
42
43                 if (target_characters > 3) {
44                         return path.substr (0, target_characters - 3) + ustring ("...");
45                 } else {
46                         /* stupid caller, just hand back the whole thing */
47                         return path;
48                 }
49         }
50
51         if (len - last_sep >= target_characters) {
52
53                 /* even the filename itself is too long */
54
55                 if (target_characters > 3) {
56                         return path.substr (last_sep+1, target_characters - 3) + ustring ("...");
57                 } else {
58                         /* stupid caller, just hand back the whole thing */
59                         return path;
60                 }
61         }
62         
63         uint32_t so_far = (len - last_sep);
64         uint32_t space_for = target_characters - so_far;
65
66         if (space_for >= 3) {
67                 ustring res = "...";
68                 res += path.substr (last_sep - space_for);
69                 return res;
70         } else {
71                 /* remove part of the end */
72                 ustring res = "...";
73                 res += path.substr (last_sep - space_for, len - last_sep + space_for - 3);
74                 res += "...";
75                 return res;
76                 
77         }
78 }