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 const & url)
186 for (string::size_type i = 0; i < url.length(); ++i) {
189 } else if (url[i] == '%' && i <= url.length() - 3) {
190 decoded += char (int_from_hex (url[i + 1], url[i + 2]));
202 length2string (const int32_t frames, const float sample_rate)
204 int32_t secs = (int32_t) (frames / sample_rate);
205 int32_t hrs = secs / 3600;
206 secs -= (hrs * 3600);
207 int32_t mins = secs / 60;
210 int32_t total_secs = (hrs * 3600) + (mins * 60) + secs;
211 int32_t frames_remaining = (int) floor (frames - (total_secs * sample_rate));
212 float fractional_secs = (float) frames_remaining / sample_rate;
214 char duration_str[32];
215 sprintf (duration_str, "%02" PRIi32 ":%02" PRIi32 ":%05.2f", hrs, mins, (float) secs + fractional_secs);
222 length2string (const int64_t frames, const double sample_rate)
224 int64_t secs = (int64_t) floor (frames / sample_rate);
225 int64_t hrs = secs / 3600LL;
226 secs -= (hrs * 3600LL);
227 int64_t mins = secs / 60LL;
228 secs -= (mins * 60LL);
230 int64_t total_secs = (hrs * 3600LL) + (mins * 60LL) + secs;
231 int64_t frames_remaining = (int64_t) floor (frames - (total_secs * sample_rate));
232 float fractional_secs = (float) frames_remaining / sample_rate;
234 char duration_str[64];
235 sprintf (duration_str, "%02" PRIi64 ":%02" PRIi64 ":%05.2f", hrs, mins, (float) secs + fractional_secs);
241 chars_equal_ignore_case(char x, char y)
243 /* app should have called setlocale() if its wants this comparison to be
246 return toupper (x) == toupper (y);
250 strings_equal_ignore_case (const string& a, const string& b)
252 if (a.length() == b.length()) {
253 return std::equal (a.begin(), a.end(), b.begin(), chars_equal_ignore_case);
258 /** A wrapper for dgettext that takes a msgid of the form Context|Text.
259 * If Context|Text is translated, the translation is returned, otherwise
260 * just Text is returned. Useful for getting translations of words or phrases
261 * that have different meanings in different contexts.
264 sgettext (const char* domain_name, const char* msgid)
266 const char * msgval = dgettext (domain_name, msgid);
267 if (msgval == msgid) {
268 const char * p = strrchr (msgid, '|');