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"
37 short_version (string orig, string::size_type target_length)
39 /* this tries to create a recognizable abbreviation
40 of "orig" by removing characters until we meet
41 a certain target length.
43 note that we deliberately leave digits in the result
48 string::size_type pos;
50 /* remove white-space and punctuation, starting at end */
52 while (orig.length() > target_length) {
53 if ((pos = orig.find_last_of (_("\"\n\t ,<.>/?:;'[{}]~`!@#$%^&*()_-+="))) == string::npos) {
56 orig.replace (pos, 1, "");
59 /* remove lower-case vowels, starting at end */
61 while (orig.length() > target_length) {
62 if ((pos = orig.find_last_of (_("aeiou"))) == string::npos) {
65 orig.replace (pos, 1, "");
68 /* remove upper-case vowels, starting at end */
70 while (orig.length() > target_length) {
71 if ((pos = orig.find_last_of (_("AEIOU"))) == string::npos) {
74 orig.replace (pos, 1, "");
77 /* remove lower-case consonants, starting at end */
79 while (orig.length() > target_length) {
80 if ((pos = orig.find_last_of (_("bcdfghjklmnpqrtvwxyz"))) == string::npos) {
83 orig.replace (pos, 1, "");
86 /* remove upper-case consonants, starting at end */
88 while (orig.length() > target_length) {
89 if ((pos = orig.find_last_of (_("BCDFGHJKLMNPQRTVWXYZ"))) == string::npos) {
92 orig.replace (pos, 1, "");
95 /* whatever the length is now, use it */
101 atoi (const string& s)
103 return std::atoi (s.c_str());
107 atof (const string& s)
109 return std::atof (s.c_str());
113 internationalize (const char *package_name, const char **array)
117 for (uint32_t i = 0; array[i]; ++i) {
118 v.push_back (dgettext(package_name, array[i]));
125 int_from_hex (char hic, char loc)
127 int hi; /* hi byte */
128 int lo; /* low byte */
132 if( ('0'<=hi) && (hi<='9') ) {
134 } else if( ('a'<= hi) && (hi<= 'f') ) {
136 } else if( ('A'<=hi) && (hi<='F') ) {
142 if( ('0'<=lo) && (lo<='9') ) {
144 } else if( ('a'<=lo) && (lo<='f') ) {
146 } else if( ('A'<=lo) && (lo<='F') ) {
150 return lo + (16 * hi);
154 url_decode (string& url)
156 string::iterator last;
157 string::iterator next;
159 for (string::iterator i = url.begin(); i != url.end(); ++i) {
165 if (url.length() <= 3) {
171 --last; /* points at last char */
172 --last; /* points at last char - 1 */
174 for (string::iterator i = url.begin(); i != last; ) {
185 if (isxdigit (*i) && isxdigit (*next)) {
186 /* replace first digit with char */
187 *i = int_from_hex (*i,*next);
188 ++i; /* points at 2nd of 2 digits */
199 length2string (const int32_t frames, const float sample_rate)
201 int32_t secs = (int32_t) (frames / sample_rate);
202 int32_t hrs = secs / 3600;
203 secs -= (hrs * 3600);
204 int32_t mins = secs / 60;
207 int32_t total_secs = (hrs * 3600) + (mins * 60) + secs;
208 int32_t frames_remaining = (int) floor (frames - (total_secs * sample_rate));
209 float fractional_secs = (float) frames_remaining / sample_rate;
211 char duration_str[32];
212 sprintf (duration_str, "%02" PRIi32 ":%02" PRIi32 ":%05.2f", hrs, mins, (float) secs + fractional_secs);
219 length2string (const int64_t frames, const double sample_rate)
221 int64_t secs = (int64_t) floor (frames / sample_rate);
222 int64_t hrs = secs / 3600LL;
223 secs -= (hrs * 3600LL);
224 int64_t mins = secs / 60LL;
225 secs -= (mins * 60LL);
227 int64_t total_secs = (hrs * 3600LL) + (mins * 60LL) + secs;
228 int64_t frames_remaining = (int64_t) floor (frames - (total_secs * sample_rate));
229 float fractional_secs = (float) frames_remaining / sample_rate;
231 char duration_str[64];
232 sprintf (duration_str, "%02" PRIi64 ":%02" PRIi64 ":%05.2f", hrs, mins, (float) secs + fractional_secs);