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.
29 #ifndef __STDC_FORMAT_MACROS
30 #define __STDC_FORMAT_MACROS
36 #include "pbd/convert.h"
47 capitalize (const string& str)
51 /* XXX not unicode safe */
52 ret[0] = toupper (str[0]);
58 downcase (const string& str)
61 std::transform (copy.begin(), copy.end(), copy.begin(), ::tolower);
66 downcase (const char* str)
68 char *copy = strdup (str);
69 for (char* p = copy; *p; ++p) {
76 short_version (string orig, string::size_type target_length)
78 /* this tries to create a recognizable abbreviation
79 of "orig" by removing characters until we meet
80 a certain target length.
82 note that we deliberately leave digits in the result
87 string::size_type pos;
89 /* remove white-space and punctuation, starting at end */
91 while (orig.length() > target_length) {
92 if ((pos = orig.find_last_of (_("\"\n\t ,<.>/?:;'[{}]~`!@#$%^&*()_-+="))) == string::npos) {
95 orig.replace (pos, 1, "");
98 /* remove lower-case vowels, starting at end */
100 while (orig.length() > target_length) {
101 if ((pos = orig.find_last_of (_("aeiou"))) == string::npos) {
104 orig.replace (pos, 1, "");
107 /* remove upper-case vowels, starting at end */
109 while (orig.length() > target_length) {
110 if ((pos = orig.find_last_of (_("AEIOU"))) == string::npos) {
113 orig.replace (pos, 1, "");
116 /* remove lower-case consonants, starting at end */
118 while (orig.length() > target_length) {
119 if ((pos = orig.find_last_of (_("bcdfghjklmnpqrtvwxyz"))) == string::npos) {
122 orig.replace (pos, 1, "");
125 /* remove upper-case consonants, starting at end */
127 while (orig.length() > target_length) {
128 if ((pos = orig.find_last_of (_("BCDFGHJKLMNPQRTVWXYZ"))) == string::npos) {
131 orig.replace (pos, 1, "");
134 /* whatever the length is now, use it */
140 atoi (const string& s)
142 return ::atoi (s.c_str());
146 atol (const string& s)
148 return (int32_t) ::atol (s.c_str());
152 atoll (const string& s)
154 return (int64_t) ::atoll (s.c_str());
158 atof (const string& s)
160 return ::atof (s.c_str());
164 internationalize (const char *package_name, const char **array)
168 for (uint32_t i = 0; array[i]; ++i) {
169 v.push_back (dgettext(package_name, array[i]));
176 int_from_hex (char hic, char loc)
178 int hi; /* hi byte */
179 int lo; /* low byte */
183 if( ('0'<=hi) && (hi<='9') ) {
185 } else if( ('a'<= hi) && (hi<= 'f') ) {
187 } else if( ('A'<=hi) && (hi<='F') ) {
193 if( ('0'<=lo) && (lo<='9') ) {
195 } else if( ('a'<=lo) && (lo<='f') ) {
197 } else if( ('A'<=lo) && (lo<='F') ) {
201 return lo + (16 * hi);
205 url_decode (string const & url)
209 for (string::size_type i = 0; i < url.length(); ++i) {
212 } else if (url[i] == '%' && i <= url.length() - 3) {
213 decoded += char (int_from_hex (url[i + 1], url[i + 2]));
225 length2string (const int32_t frames, const float sample_rate)
227 int32_t secs = (int32_t) (frames / sample_rate);
228 int32_t hrs = secs / 3600;
229 secs -= (hrs * 3600);
230 int32_t mins = secs / 60;
233 int32_t total_secs = (hrs * 3600) + (mins * 60) + secs;
234 int32_t frames_remaining = (int) floor (frames - (total_secs * sample_rate));
235 float fractional_secs = (float) frames_remaining / sample_rate;
237 char duration_str[32];
238 sprintf (duration_str, "%02" PRIi32 ":%02" PRIi32 ":%05.2f", hrs, mins, (float) secs + fractional_secs);
245 length2string (const int64_t frames, const double sample_rate)
247 int64_t secs = (int64_t) floor (frames / sample_rate);
248 int64_t hrs = secs / 3600LL;
249 secs -= (hrs * 3600LL);
250 int64_t mins = secs / 60LL;
251 secs -= (mins * 60LL);
253 int64_t total_secs = (hrs * 3600LL) + (mins * 60LL) + secs;
254 int64_t frames_remaining = (int64_t) floor (frames - (total_secs * sample_rate));
255 float fractional_secs = (float) frames_remaining / sample_rate;
257 char duration_str[64];
258 sprintf (duration_str, "%02" PRIi64 ":%02" PRIi64 ":%05.2f", hrs, mins, (float) secs + fractional_secs);
264 chars_equal_ignore_case(char x, char y)
266 /* app should have called setlocale() if its wants this comparison to be
269 return toupper (x) == toupper (y);
273 strings_equal_ignore_case (const string& a, const string& b)
275 if (a.length() == b.length()) {
276 return std::equal (a.begin(), a.end(), b.begin(), chars_equal_ignore_case);
282 string_is_affirmative (const std::string& str)
284 /* to be used only with XML data - not intended to handle user input */
290 /* the use of g_ascii_strncasecmp() is solely to get around issues with
291 * charsets posed by trying to use C++ for the same
292 * comparison. switching a std::string to its lower- or upper-case
293 * version has several issues, but handled by default
294 * in the way we desire when doing it in C.
297 return str == "1" || str == "y" || str == "Y" || (!g_ascii_strncasecmp(str.c_str(), "yes", str.length())) ||
298 (!g_ascii_strncasecmp(str.c_str(), "true", str.length()));
301 /** A wrapper for dgettext that takes a msgid of the form Context|Text.
302 * If Context|Text is translated, the translation is returned, otherwise
303 * just Text is returned. Useful for getting translations of words or phrases
304 * that have different meanings in different contexts.
307 sgettext (const char* domain_name, const char* msgid)
309 const char * msgval = dgettext (domain_name, msgid);
310 if (msgval == msgid) {
311 const char * p = strrchr (msgid, '|');