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.
21 #include <pbd/shortpath.h>
27 short_path (const Glib::ustring& path, ustring::size_type target_characters)
29 ustring::size_type last_sep;
30 ustring::size_type len = path.length();
31 const char separator = '/';
33 if (len <= target_characters) {
37 if ((last_sep = path.find_last_of (separator)) == ustring::npos) {
39 /* just a filename, but its too long anyway */
41 if (target_characters > 3) {
42 return path.substr (0, target_characters - 3) + ustring ("...");
44 /* stupid caller, just hand back the whole thing */
49 if (len - last_sep >= target_characters) {
51 /* even the filename itself is too long */
53 if (target_characters > 3) {
54 return path.substr (last_sep+1, target_characters - 3) + ustring ("...");
56 /* stupid caller, just hand back the whole thing */
61 uint32_t so_far = (len - last_sep);
62 uint32_t space_for = target_characters - so_far;
66 res += path.substr (last_sep - space_for);
69 /* remove part of the end */
71 res += path.substr (last_sep - space_for, len - last_sep + space_for - 3);