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.
23 #ifndef __STDC_FORMAT_MACROS
24 #define __STDC_FORMAT_MACROS
28 #include "pbd/convert.h"
39 short_version (string orig, string::size_type target_length)
41 /* this tries to create a recognizable abbreviation
42 of "orig" by removing characters until we meet
43 a certain target length.
45 note that we deliberately leave digits in the result
50 string::size_type pos;
52 /* remove white-space and punctuation, starting at end */
54 while (orig.length() > target_length) {
55 if ((pos = orig.find_last_of (_("\"\n\t ,<.>/?:;'[{}]~`!@#$%^&*()_-+="))) == string::npos) {
58 orig.replace (pos, 1, "");
61 /* remove lower-case vowels, starting at end */
63 while (orig.length() > target_length) {
64 if ((pos = orig.find_last_of (_("aeiou"))) == string::npos) {
67 orig.replace (pos, 1, "");
70 /* remove upper-case vowels, starting at end */
72 while (orig.length() > target_length) {
73 if ((pos = orig.find_last_of (_("AEIOU"))) == string::npos) {
76 orig.replace (pos, 1, "");
79 /* remove lower-case consonants, starting at end */
81 while (orig.length() > target_length) {
82 if ((pos = orig.find_last_of (_("bcdfghjklmnpqrtvwxyz"))) == string::npos) {
85 orig.replace (pos, 1, "");
88 /* remove upper-case consonants, starting at end */
90 while (orig.length() > target_length) {
91 if ((pos = orig.find_last_of (_("BCDFGHJKLMNPQRTVWXYZ"))) == string::npos) {
94 orig.replace (pos, 1, "");
97 /* whatever the length is now, use it */
103 atoi (const string& s)
105 return ::atoi (s.c_str());
109 atof (const string& s)
111 return ::atof (s.c_str());
115 internationalize (const char *package_name, const char **array)
119 for (uint32_t i = 0; array[i]; ++i) {
120 v.push_back (dgettext(package_name, array[i]));
127 int_from_hex (char hic, char loc)
129 int hi; /* hi byte */
130 int lo; /* low byte */
134 if( ('0'<=hi) && (hi<='9') ) {
136 } else if( ('a'<= hi) && (hi<= 'f') ) {
138 } else if( ('A'<=hi) && (hi<='F') ) {
144 if( ('0'<=lo) && (lo<='9') ) {
146 } else if( ('a'<=lo) && (lo<='f') ) {
148 } else if( ('A'<=lo) && (lo<='F') ) {
152 return lo + (16 * hi);
156 url_decode (string& url)
158 string::iterator last;
159 string::iterator next;
161 for (string::iterator i = url.begin(); i != url.end(); ++i) {
167 if (url.length() <= 3) {
173 --last; /* points at last char */
174 --last; /* points at last char - 1 */
176 for (string::iterator i = url.begin(); i != last; ) {
187 if (isxdigit (*i) && isxdigit (*next)) {
188 /* replace first digit with char */
189 *i = int_from_hex (*i,*next);
190 ++i; /* points at 2nd of 2 digits */
200 url_decode (ustring& url)
202 ustring::iterator last;
203 ustring::iterator next;
205 for (ustring::iterator i = url.begin(); i != url.end(); ++i) {
209 url.replace (i, next, 1, ' ');
213 if (url.length() <= 3) {
219 --last; /* points at last char */
220 --last; /* points at last char - 1 */
222 for (ustring::iterator i = url.begin(); i != last; ) {
233 if (isxdigit (*i) && isxdigit (*next)) {
234 /* replace first digit with char */
235 url.replace (i, next, 1, (gunichar) int_from_hex (*i,*next));
236 ++i; /* points at 2nd of 2 digits */
247 length2string (const int32_t frames, const float sample_rate)
249 int32_t secs = (int32_t) (frames / sample_rate);
250 int32_t hrs = secs / 3600;
251 secs -= (hrs * 3600);
252 int32_t mins = secs / 60;
255 int32_t total_secs = (hrs * 3600) + (mins * 60) + secs;
256 int32_t frames_remaining = (int) floor (frames - (total_secs * sample_rate));
257 float fractional_secs = (float) frames_remaining / sample_rate;
259 char duration_str[32];
260 sprintf (duration_str, "%02" PRIi32 ":%02" PRIi32 ":%05.2f", hrs, mins, (float) secs + fractional_secs);
267 length2string (const int64_t frames, const double sample_rate)
269 int64_t secs = (int64_t) floor (frames / sample_rate);
270 int64_t hrs = secs / 3600LL;
271 secs -= (hrs * 3600LL);
272 int64_t mins = secs / 60LL;
273 secs -= (mins * 60LL);
275 int64_t total_secs = (hrs * 3600LL) + (mins * 60LL) + secs;
276 int64_t frames_remaining = (int64_t) floor (frames - (total_secs * sample_rate));
277 float fractional_secs = (float) frames_remaining / sample_rate;
279 char duration_str[64];
280 sprintf (duration_str, "%02" PRIi64 ":%02" PRIi64 ":%05.2f", hrs, mins, (float) secs + fractional_secs);
285 chars_equal_ignore_case(char x, char y)
287 static std::locale loc;
288 return toupper(x, loc) == toupper(y, loc);
292 strings_equal_ignore_case (const string& a, const string& b)
294 if (a.length() == b.length()) {
295 return std::equal (a.begin(), a.end(), b.begin(), chars_equal_ignore_case);