2 Copyright (C) 2006 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.
22 #ifndef __STDC_FORMAT_MACROS
23 #define __STDC_FORMAT_MACROS
27 #include "pbd/convert.h"
38 short_version (string orig, string::size_type target_length)
40 /* this tries to create a recognizable abbreviation
41 of "orig" by removing characters until we meet
42 a certain target length.
44 note that we deliberately leave digits in the result
49 string::size_type pos;
51 /* remove white-space and punctuation, starting at end */
53 while (orig.length() > target_length) {
54 if ((pos = orig.find_last_of (_("\"\n\t ,<.>/?:;'[{}]~`!@#$%^&*()_-+="))) == string::npos) {
57 orig.replace (pos, 1, "");
60 /* remove lower-case vowels, starting at end */
62 while (orig.length() > target_length) {
63 if ((pos = orig.find_last_of (_("aeiou"))) == string::npos) {
66 orig.replace (pos, 1, "");
69 /* remove upper-case vowels, starting at end */
71 while (orig.length() > target_length) {
72 if ((pos = orig.find_last_of (_("AEIOU"))) == string::npos) {
75 orig.replace (pos, 1, "");
78 /* remove lower-case consonants, starting at end */
80 while (orig.length() > target_length) {
81 if ((pos = orig.find_last_of (_("bcdfghjklmnpqrtvwxyz"))) == string::npos) {
84 orig.replace (pos, 1, "");
87 /* remove upper-case consonants, starting at end */
89 while (orig.length() > target_length) {
90 if ((pos = orig.find_last_of (_("BCDFGHJKLMNPQRTVWXYZ"))) == string::npos) {
93 orig.replace (pos, 1, "");
96 /* whatever the length is now, use it */
102 atoi (const string& s)
104 return std::atoi (s.c_str());
108 atof (const string& s)
110 return std::atof (s.c_str());
114 internationalize (const char *package_name, const char **array)
118 for (uint32_t i = 0; array[i]; ++i) {
119 v.push_back (dgettext(package_name, array[i]));
126 int_from_hex (char hic, char loc)
128 int hi; /* hi byte */
129 int lo; /* low byte */
133 if( ('0'<=hi) && (hi<='9') ) {
135 } else if( ('a'<= hi) && (hi<= 'f') ) {
137 } else if( ('A'<=hi) && (hi<='F') ) {
143 if( ('0'<=lo) && (lo<='9') ) {
145 } else if( ('a'<=lo) && (lo<='f') ) {
147 } else if( ('A'<=lo) && (lo<='F') ) {
151 return lo + (16 * hi);
155 url_decode (string& url)
157 string::iterator last;
158 string::iterator next;
160 for (string::iterator i = url.begin(); i != url.end(); ++i) {
166 if (url.length() <= 3) {
172 --last; /* points at last char */
173 --last; /* points at last char - 1 */
175 for (string::iterator i = url.begin(); i != last; ) {
186 if (isxdigit (*i) && isxdigit (*next)) {
187 /* replace first digit with char */
188 *i = int_from_hex (*i,*next);
189 ++i; /* points at 2nd of 2 digits */
199 url_decode (ustring& url)
201 ustring::iterator last;
202 ustring::iterator next;
204 for (ustring::iterator i = url.begin(); i != url.end(); ++i) {
208 url.replace (i, next, 1, ' ');
212 if (url.length() <= 3) {
218 --last; /* points at last char */
219 --last; /* points at last char - 1 */
221 for (ustring::iterator i = url.begin(); i != last; ) {
232 if (isxdigit (*i) && isxdigit (*next)) {
233 /* replace first digit with char */
234 url.replace (i, next, 1, (gunichar) int_from_hex (*i,*next));
235 ++i; /* points at 2nd of 2 digits */
246 length2string (const int32_t frames, const float sample_rate)
248 int32_t secs = (int32_t) (frames / sample_rate);
249 int32_t hrs = secs / 3600;
250 secs -= (hrs * 3600);
251 int32_t mins = secs / 60;
254 int32_t total_secs = (hrs * 3600) + (mins * 60) + secs;
255 int32_t frames_remaining = (int) floor (frames - (total_secs * sample_rate));
256 float fractional_secs = (float) frames_remaining / sample_rate;
258 char duration_str[32];
259 sprintf (duration_str, "%02" PRIi32 ":%02" PRIi32 ":%05.2f", hrs, mins, (float) secs + fractional_secs);
266 length2string (const int64_t frames, const double sample_rate)
268 int64_t secs = (int64_t) floor (frames / sample_rate);
269 int64_t hrs = secs / 3600LL;
270 secs -= (hrs * 3600LL);
271 int64_t mins = secs / 60LL;
272 secs -= (mins * 60LL);
274 int64_t total_secs = (hrs * 3600LL) + (mins * 60LL) + secs;
275 int64_t frames_remaining = (int64_t) floor (frames - (total_secs * sample_rate));
276 float fractional_secs = (float) frames_remaining / sample_rate;
278 char duration_str[64];
279 sprintf (duration_str, "%02" PRIi64 ":%02" PRIi64 ":%05.2f", hrs, mins, (float) secs + fractional_secs);
284 chars_equal_ignore_case(char x, char y)
286 static std::locale loc;
287 return toupper(x, loc) == toupper(y, loc);
291 strings_equal_ignore_case (const string& a, const string& b)
293 if (a.length() == b.length()) {
294 return std::equal (a.begin(), a.end(), b.begin(), chars_equal_ignore_case);