2 Copyright (C) 2000-2007 Paul Davis
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.
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.
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.
20 #include <pbd/shortpath.h>
26 short_path (const Glib::ustring& path, ustring::size_type target_characters)
28 ustring::size_type last_sep;
29 ustring::size_type len = path.length();
30 const char separator = '/';
32 if (len <= target_characters) {
36 if ((last_sep = path.find_last_of (separator)) == ustring::npos) {
38 /* just a filename, but its too long anyway */
40 if (target_characters > 3) {
41 return path.substr (0, target_characters - 3) + ustring ("...");
43 /* stupid caller, just hand back the whole thing */
48 if (len - last_sep >= target_characters) {
50 /* even the filename itself is too long */
52 if (target_characters > 3) {
53 return path.substr (last_sep+1, target_characters - 3) + ustring ("...");
55 /* stupid caller, just hand back the whole thing */
60 uint32_t so_far = (len - last_sep);
61 uint32_t space_for = target_characters - so_far;
65 res += path.substr (last_sep - space_for);
68 /* remove part of the end */
70 res += path.substr (last_sep - space_for, len - last_sep + space_for - 3);