ardour
shortpath.cc
Go to the documentation of this file.
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 using namespace Glib;
24 using namespace std;
25 
26 ustring
27 short_path (const Glib::ustring& path, ustring::size_type target_characters)
28 {
29  ustring::size_type last_sep;
30  ustring::size_type len = path.length();
31  const char separator = '/';
32 
33  if (len <= target_characters) {
34  return path;
35  }
36 
37  if ((last_sep = path.find_last_of (separator)) == ustring::npos) {
38 
39  /* just a filename, but its too long anyway */
40 
41  if (target_characters > 3) {
42  return path.substr (0, target_characters - 3) + ustring ("...");
43  } else {
44  /* stupid caller, just hand back the whole thing */
45  return path;
46  }
47  }
48 
49  if (len - last_sep >= target_characters) {
50 
51  /* even the filename itself is too long */
52 
53  if (target_characters > 3) {
54  return path.substr (last_sep+1, target_characters - 3) + ustring ("...");
55  } else {
56  /* stupid caller, just hand back the whole thing */
57  return path;
58  }
59  }
60 
61  uint32_t so_far = (len - last_sep);
62  uint32_t space_for = target_characters - so_far;
63 
64  if (space_for >= 3) {
65  ustring res = "...";
66  res += path.substr (last_sep - space_for);
67  return res;
68  } else {
69  /* remove part of the end */
70  ustring res = "...";
71  res += path.substr (last_sep - space_for, len - last_sep + space_for - 3);
72  res += "...";
73  return res;
74 
75  }
76 }
Definition: Beats.hpp:239
ustring short_path(const Glib::ustring &path, ustring::size_type target_characters)
Definition: shortpath.cc:27