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.
26 #ifndef __STDC_FORMAT_MACROS
27 #define __STDC_FORMAT_MACROS
31 #include "pbd/convert.h"
42 capitalize (const string& str)
46 /* XXX not unicode safe */
47 ret[0] = toupper (str[0]);
53 short_version (string orig, string::size_type target_length)
55 /* this tries to create a recognizable abbreviation
56 of "orig" by removing characters until we meet
57 a certain target length.
59 note that we deliberately leave digits in the result
64 string::size_type pos;
66 /* remove white-space and punctuation, starting at end */
68 while (orig.length() > target_length) {
69 if ((pos = orig.find_last_of (_("\"\n\t ,<.>/?:;'[{}]~`!@#$%^&*()_-+="))) == string::npos) {
72 orig.replace (pos, 1, "");
75 /* remove lower-case vowels, starting at end */
77 while (orig.length() > target_length) {
78 if ((pos = orig.find_last_of (_("aeiou"))) == string::npos) {
81 orig.replace (pos, 1, "");
84 /* remove upper-case vowels, starting at end */
86 while (orig.length() > target_length) {
87 if ((pos = orig.find_last_of (_("AEIOU"))) == string::npos) {
90 orig.replace (pos, 1, "");
93 /* remove lower-case consonants, starting at end */
95 while (orig.length() > target_length) {
96 if ((pos = orig.find_last_of (_("bcdfghjklmnpqrtvwxyz"))) == string::npos) {
99 orig.replace (pos, 1, "");
102 /* remove upper-case consonants, starting at end */
104 while (orig.length() > target_length) {
105 if ((pos = orig.find_last_of (_("BCDFGHJKLMNPQRTVWXYZ"))) == string::npos) {
108 orig.replace (pos, 1, "");
111 /* whatever the length is now, use it */
117 atoi (const string& s)
119 return ::atoi (s.c_str());
123 atol (const string& s)
125 return (int32_t) ::atol (s.c_str());
129 atoll (const string& s)
131 return (int64_t) ::atoll (s.c_str());
135 atof (const string& s)
137 return ::atof (s.c_str());
141 internationalize (const char *package_name, const char **array)
145 for (uint32_t i = 0; array[i]; ++i) {
146 v.push_back (dgettext(package_name, array[i]));
153 int_from_hex (char hic, char loc)
155 int hi; /* hi byte */
156 int lo; /* low byte */
160 if( ('0'<=hi) && (hi<='9') ) {
162 } else if( ('a'<= hi) && (hi<= 'f') ) {
164 } else if( ('A'<=hi) && (hi<='F') ) {
170 if( ('0'<=lo) && (lo<='9') ) {
172 } else if( ('a'<=lo) && (lo<='f') ) {
174 } else if( ('A'<=lo) && (lo<='F') ) {
178 return lo + (16 * hi);
182 url_decode (string& url)
184 string::iterator last;
185 string::iterator next;
187 for (string::iterator i = url.begin(); i != url.end(); ++i) {
193 if (url.length() <= 3) {
199 --last; /* points at last char */
200 --last; /* points at last char - 1 */
202 for (string::iterator i = url.begin(); i != last; ) {
213 if (isxdigit (*i) && isxdigit (*next)) {
214 /* replace first digit with char */
215 *i = int_from_hex (*i,*next);
216 ++i; /* points at 2nd of 2 digits */
226 url_decode (ustring& url)
228 ustring::iterator last;
229 ustring::iterator next;
231 for (ustring::iterator i = url.begin(); i != url.end(); ++i) {
235 url.replace (i, next, 1, ' ');
239 if (url.length() <= 3) {
245 --last; /* points at last char */
246 --last; /* points at last char - 1 */
248 for (ustring::iterator i = url.begin(); i != last; ) {
259 if (isxdigit (*i) && isxdigit (*next)) {
260 /* replace first digit with char */
261 url.replace (i, next, 1, (gunichar) int_from_hex (*i,*next));
262 ++i; /* points at 2nd of 2 digits */
273 length2string (const int32_t frames, const float sample_rate)
275 int32_t secs = (int32_t) (frames / sample_rate);
276 int32_t hrs = secs / 3600;
277 secs -= (hrs * 3600);
278 int32_t mins = secs / 60;
281 int32_t total_secs = (hrs * 3600) + (mins * 60) + secs;
282 int32_t frames_remaining = (int) floor (frames - (total_secs * sample_rate));
283 float fractional_secs = (float) frames_remaining / sample_rate;
285 char duration_str[32];
286 sprintf (duration_str, "%02" PRIi32 ":%02" PRIi32 ":%05.2f", hrs, mins, (float) secs + fractional_secs);
293 length2string (const int64_t frames, const double sample_rate)
295 int64_t secs = (int64_t) floor (frames / sample_rate);
296 int64_t hrs = secs / 3600LL;
297 secs -= (hrs * 3600LL);
298 int64_t mins = secs / 60LL;
299 secs -= (mins * 60LL);
301 int64_t total_secs = (hrs * 3600LL) + (mins * 60LL) + secs;
302 int64_t frames_remaining = (int64_t) floor (frames - (total_secs * sample_rate));
303 float fractional_secs = (float) frames_remaining / sample_rate;
305 char duration_str[64];
306 sprintf (duration_str, "%02" PRIi64 ":%02" PRIi64 ":%05.2f", hrs, mins, (float) secs + fractional_secs);
312 chars_equal_ignore_case(char x, char y)
314 /* app should have called setlocale() if its wants this comparison to be
317 return toupper (x) == toupper (y);
321 strings_equal_ignore_case (const string& a, const string& b)
323 if (a.length() == b.length()) {
324 return std::equal (a.begin(), a.end(), b.begin(), chars_equal_ignore_case);
329 /** A wrapper for dgettext that takes a msgid of the form Context|Text.
330 * If Context|Text is translated, the translation is returned, otherwise
331 * just Text is returned. Useful for getting translations of words or phrases
332 * that have different meanings in different contexts.
335 sgettext (const char* domain_name, const char* msgid)
337 const char * msgval = dgettext (domain_name, msgid);
338 if (msgval == msgid) {
339 const char * p = strrchr (msgid, '|');