merged with 1697 revision of trunk (which is post-rc1 but pre-rc2
[ardour.git] / libs / pbd / shortpath.cc
1 #include <pbd/shortpath.h>
2
3 using namespace Glib;
4 using namespace std;
5
6 ustring
7 short_path (const Glib::ustring& path, ustring::size_type target_characters)
8 {
9         ustring::size_type last_sep;
10         ustring::size_type len = path.length();
11         const char separator = '/';
12
13         if (len <= target_characters) {
14                 return path;
15         }
16
17         if ((last_sep = path.find_last_of (separator)) == ustring::npos) {
18
19                 /* just a filename, but its too long anyway */
20
21                 if (target_characters > 3) {
22                         return path.substr (0, target_characters - 3) + ustring ("...");
23                 } else {
24                         /* stupid caller, just hand back the whole thing */
25                         return path;
26                 }
27         }
28
29         if (len - last_sep >= target_characters) {
30
31                 /* even the filename itself is too long */
32
33                 if (target_characters > 3) {
34                         return path.substr (last_sep+1, target_characters - 3) + ustring ("...");
35                 } else {
36                         /* stupid caller, just hand back the whole thing */
37                         return path;
38                 }
39         }
40         
41         uint32_t so_far = (len - last_sep);
42         uint32_t space_for = target_characters - so_far;
43
44         if (space_for >= 3) {
45                 ustring res = "...";
46                 res += path.substr (last_sep - space_for);
47                 return res;
48         } else {
49                 /* remove part of the end */
50                 ustring res = "...";
51                 res += path.substr (last_sep - space_for, len - last_sep + space_for - 3);
52                 res += "...";
53                 return res;
54                 
55         }
56 }