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 #include "pbd/convert.h"
33 short_version (string orig, string::size_type target_length)
35 /* this tries to create a recognizable abbreviation
36 of "orig" by removing characters until we meet
37 a certain target length.
39 note that we deliberately leave digits in the result
44 string::size_type pos;
46 /* remove white-space and punctuation, starting at end */
48 while (orig.length() > target_length) {
49 if ((pos = orig.find_last_of (_("\"\n\t ,<.>/?:;'[{}]~`!@#$%^&*()_-+="))) == string::npos) {
52 orig.replace (pos, 1, "");
55 /* remove lower-case vowels, starting at end */
57 while (orig.length() > target_length) {
58 if ((pos = orig.find_last_of (_("aeiou"))) == string::npos) {
61 orig.replace (pos, 1, "");
64 /* remove upper-case vowels, starting at end */
66 while (orig.length() > target_length) {
67 if ((pos = orig.find_last_of (_("AEIOU"))) == string::npos) {
70 orig.replace (pos, 1, "");
73 /* remove lower-case consonants, starting at end */
75 while (orig.length() > target_length) {
76 if ((pos = orig.find_last_of (_("bcdfghjklmnpqrtvwxyz"))) == string::npos) {
79 orig.replace (pos, 1, "");
82 /* remove upper-case consonants, starting at end */
84 while (orig.length() > target_length) {
85 if ((pos = orig.find_last_of (_("BCDFGHJKLMNPQRTVWXYZ"))) == string::npos) {
88 orig.replace (pos, 1, "");
91 /* whatever the length is now, use it */
97 atoi (const string& s)
99 return std::atoi (s.c_str());
103 atof (const string& s)
105 return std::atof (s.c_str());
109 internationalize (const char **array)
113 for (uint32_t i = 0; array[i]; ++i) {
114 v.push_back (_(array[i]));
121 int_from_hex (char hic, char loc)
123 int hi; /* hi byte */
124 int lo; /* low byte */
128 if( ('0'<=hi) && (hi<='9') ) {
130 } else if( ('a'<= hi) && (hi<= 'f') ) {
132 } else if( ('A'<=hi) && (hi<='F') ) {
138 if( ('0'<=lo) && (lo<='9') ) {
140 } else if( ('a'<=lo) && (lo<='f') ) {
142 } else if( ('A'<=lo) && (lo<='F') ) {
146 return lo + (16 * hi);
150 url_decode (string& url)
152 string::iterator last;
153 string::iterator next;
155 for (string::iterator i = url.begin(); i != url.end(); ++i) {
161 if (url.length() <= 3) {
167 --last; /* points at last char */
168 --last; /* points at last char - 1 */
170 for (string::iterator i = url.begin(); i != last; ) {
181 if (isxdigit (*i) && isxdigit (*next)) {
182 /* replace first digit with char */
183 *i = int_from_hex (*i,*next);
184 ++i; /* points at 2nd of 2 digits */
194 length2string (const int32_t frames, const float sample_rate)
196 int secs = (int) (frames / sample_rate);
197 int hrs = secs / 3600;
198 secs -= (hrs * 3600);
199 int mins = secs / 60;
202 int total_secs = (hrs * 3600) + (mins * 60) + secs;
203 int frames_remaining = (int) floor (frames - (total_secs * sample_rate));
204 float fractional_secs = (float) frames_remaining / sample_rate;
206 char duration_str[32];
207 sprintf (duration_str, "%02d:%02d:%05.2f", hrs, mins, (float) secs + fractional_secs);