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.
25 #ifndef __STDC_FORMAT_MACROS
26 #define __STDC_FORMAT_MACROS
30 #include "pbd/convert.h"
41 short_version (string orig, string::size_type target_length)
43 /* this tries to create a recognizable abbreviation
44 of "orig" by removing characters until we meet
45 a certain target length.
47 note that we deliberately leave digits in the result
52 string::size_type pos;
54 /* remove white-space and punctuation, starting at end */
56 while (orig.length() > target_length) {
57 if ((pos = orig.find_last_of (_("\"\n\t ,<.>/?:;'[{}]~`!@#$%^&*()_-+="))) == string::npos) {
60 orig.replace (pos, 1, "");
63 /* remove lower-case vowels, starting at end */
65 while (orig.length() > target_length) {
66 if ((pos = orig.find_last_of (_("aeiou"))) == string::npos) {
69 orig.replace (pos, 1, "");
72 /* remove upper-case vowels, starting at end */
74 while (orig.length() > target_length) {
75 if ((pos = orig.find_last_of (_("AEIOU"))) == string::npos) {
78 orig.replace (pos, 1, "");
81 /* remove lower-case consonants, starting at end */
83 while (orig.length() > target_length) {
84 if ((pos = orig.find_last_of (_("bcdfghjklmnpqrtvwxyz"))) == string::npos) {
87 orig.replace (pos, 1, "");
90 /* remove upper-case consonants, starting at end */
92 while (orig.length() > target_length) {
93 if ((pos = orig.find_last_of (_("BCDFGHJKLMNPQRTVWXYZ"))) == string::npos) {
96 orig.replace (pos, 1, "");
99 /* whatever the length is now, use it */
105 atoi (const string& s)
107 return std::atoi (s.c_str());
111 atof (const string& s)
113 return std::atof (s.c_str());
117 internationalize (const char *package_name, const char **array)
121 for (uint32_t i = 0; array[i]; ++i) {
122 v.push_back (dgettext(package_name, array[i]));
129 int_from_hex (char hic, char loc)
131 int hi; /* hi byte */
132 int lo; /* low byte */
136 if( ('0'<=hi) && (hi<='9') ) {
138 } else if( ('a'<= hi) && (hi<= 'f') ) {
140 } else if( ('A'<=hi) && (hi<='F') ) {
146 if( ('0'<=lo) && (lo<='9') ) {
148 } else if( ('a'<=lo) && (lo<='f') ) {
150 } else if( ('A'<=lo) && (lo<='F') ) {
154 return lo + (16 * hi);
158 url_decode (string& url)
160 string::iterator last;
161 string::iterator next;
163 for (string::iterator i = url.begin(); i != url.end(); ++i) {
169 if (url.length() <= 3) {
175 --last; /* points at last char */
176 --last; /* points at last char - 1 */
178 for (string::iterator i = url.begin(); i != last; ) {
189 if (isxdigit (*i) && isxdigit (*next)) {
190 /* replace first digit with char */
191 *i = int_from_hex (*i,*next);
192 ++i; /* points at 2nd of 2 digits */
202 url_decode (ustring& url)
204 ustring::iterator last;
205 ustring::iterator next;
207 for (ustring::iterator i = url.begin(); i != url.end(); ++i) {
211 url.replace (i, next, 1, ' ');
215 if (url.length() <= 3) {
221 --last; /* points at last char */
222 --last; /* points at last char - 1 */
224 for (ustring::iterator i = url.begin(); i != last; ) {
235 if (isxdigit (*i) && isxdigit (*next)) {
236 /* replace first digit with char */
237 url.replace (i, next, 1, (gunichar) int_from_hex (*i,*next));
238 ++i; /* points at 2nd of 2 digits */
249 length2string (const int32_t frames, const float sample_rate)
251 int32_t secs = (int32_t) (frames / sample_rate);
252 int32_t hrs = secs / 3600;
253 secs -= (hrs * 3600);
254 int32_t mins = secs / 60;
257 int32_t total_secs = (hrs * 3600) + (mins * 60) + secs;
258 int32_t frames_remaining = (int) floor (frames - (total_secs * sample_rate));
259 float fractional_secs = (float) frames_remaining / sample_rate;
261 char duration_str[32];
262 sprintf (duration_str, "%02" PRIi32 ":%02" PRIi32 ":%05.2f", hrs, mins, (float) secs + fractional_secs);
269 length2string (const int64_t frames, const double sample_rate)
271 int64_t secs = (int64_t) floor (frames / sample_rate);
272 int64_t hrs = secs / 3600LL;
273 secs -= (hrs * 3600LL);
274 int64_t mins = secs / 60LL;
275 secs -= (mins * 60LL);
277 int64_t total_secs = (hrs * 3600LL) + (mins * 60LL) + secs;
278 int64_t frames_remaining = (int64_t) floor (frames - (total_secs * sample_rate));
279 float fractional_secs = (float) frames_remaining / sample_rate;
281 char duration_str[64];
282 sprintf (duration_str, "%02" PRIi64 ":%02" PRIi64 ":%05.2f", hrs, mins, (float) secs + fractional_secs);
288 chars_equal_ignore_case(char x, char y)
290 static std::locale loc;
291 return toupper(x, loc) == toupper(y, loc);
295 strings_equal_ignore_case (const string& a, const string& b)
297 if (a.length() == b.length()) {
298 return std::equal (a.begin(), a.end(), b.begin(), chars_equal_ignore_case);