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
33
#include "pbd/convert.h"
44
capitalize (const string& str)
48
/* XXX not unicode safe */
49
ret[0] = toupper (str[0]);
55
short_version (string orig, string::size_type target_length)
57
/* this tries to create a recognizable abbreviation
58
of "orig" by removing characters until we meet
59
a certain target length.
61
note that we deliberately leave digits in the result
66
string::size_type pos;
68
/* remove white-space and punctuation, starting at end */
70
while (orig.length() > target_length) {
71
if ((pos = orig.find_last_of (_("\"\n\t ,<.>/?:;'[{}]~`!@#$%^&*()_-+="))) == string::npos) {
74
orig.replace (pos, 1, "");
77
/* remove lower-case vowels, starting at end */
79
while (orig.length() > target_length) {
80
if ((pos = orig.find_last_of (_("aeiou"))) == string::npos) {
83
orig.replace (pos, 1, "");
86
/* remove upper-case vowels, starting at end */
88
while (orig.length() > target_length) {
89
if ((pos = orig.find_last_of (_("AEIOU"))) == string::npos) {
92
orig.replace (pos, 1, "");
95
/* remove lower-case consonants, starting at end */
97
while (orig.length() > target_length) {
98
if ((pos = orig.find_last_of (_("bcdfghjklmnpqrtvwxyz"))) == string::npos) {
101
orig.replace (pos, 1, "");
104
/* remove upper-case consonants, starting at end */
106
while (orig.length() > target_length) {
107
if ((pos = orig.find_last_of (_("BCDFGHJKLMNPQRTVWXYZ"))) == string::npos) {
110
orig.replace (pos, 1, "");
113
/* whatever the length is now, use it */
119
atoi (const string& s)
121
return ::atoi (s.c_str());
125
atol (const string& s)
127
return (int32_t) ::atol (s.c_str());
131
atoll (const string& s)
133
return (int64_t) ::atoll (s.c_str());
137
atof (const string& s)
139
return ::atof (s.c_str());
143
internationalize (const char *package_name, const char **array)
147
for (uint32_t i = 0; array[i]; ++i) {
148
v.push_back (dgettext(package_name, array[i]));
155
int_from_hex (char hic, char loc)
157
int hi; /* hi byte */
158
int lo; /* low byte */
162
if( ('0'<=hi) && (hi<='9') ) {
164
} else if( ('a'<= hi) && (hi<= 'f') ) {
166
} else if( ('A'<=hi) && (hi<='F') ) {
172
if( ('0'<=lo) && (lo<='9') ) {
174
} else if( ('a'<=lo) && (lo<='f') ) {
176
} else if( ('A'<=lo) && (lo<='F') ) {
180
return lo + (16 * hi);
184
url_decode (string const & url)
188
for (string::size_type i = 0; i < url.length(); ++i) {
191
} else if (url[i] == '%' && i <= url.length() - 3) {
192
decoded += char (int_from_hex (url[i + 1], url[i + 2]));
204
length2string (const int32_t frames, const float sample_rate)
206
int32_t secs = (int32_t) (frames / sample_rate);
207
int32_t hrs = secs / 3600;
208
secs -= (hrs * 3600);
209
int32_t mins = secs / 60;
212
int32_t total_secs = (hrs * 3600) + (mins * 60) + secs;
213
int32_t frames_remaining = (int) floor (frames - (total_secs * sample_rate));
214
float fractional_secs = (float) frames_remaining / sample_rate;
216
char duration_str[32];
217
sprintf (duration_str, "%02" PRIi32 ":%02" PRIi32 ":%05.2f", hrs, mins, (float) secs + fractional_secs);
224
length2string (const int64_t frames, const double sample_rate)
226
int64_t secs = (int64_t) floor (frames / sample_rate);
227
int64_t hrs = secs / 3600LL;
228
secs -= (hrs * 3600LL);
229
int64_t mins = secs / 60LL;
230
secs -= (mins * 60LL);
232
int64_t total_secs = (hrs * 3600LL) + (mins * 60LL) + secs;
233
int64_t frames_remaining = (int64_t) floor (frames - (total_secs * sample_rate));
234
float fractional_secs = (float) frames_remaining / sample_rate;
236
char duration_str[64];
237
sprintf (duration_str, "%02" PRIi64 ":%02" PRIi64 ":%05.2f", hrs, mins, (float) secs + fractional_secs);
243
chars_equal_ignore_case(char x, char y)
245
/* app should have called setlocale() if its wants this comparison to be
248
return toupper (x) == toupper (y);
252
strings_equal_ignore_case (const string& a, const string& b)
254
if (a.length() == b.length()) {
255
return std::equal (a.begin(), a.end(), b.begin(), chars_equal_ignore_case);
261
string_is_affirmative (const std::string& str)
263
/* to be used only with XML data - not intended to handle user input */
269
/* the use of g_ascii_strncasecmp() is solely to get around issues with
270
* charsets posed by trying to use C++ for the same
271
* comparison. switching a std::string to its lower- or upper-case
272
* version has several issues, but handled by default
273
* in the way we desire when doing it in C.
276
return str == "1" || str == "y" || str == "Y" || (!g_ascii_strncasecmp(str.c_str(), "yes", str.length())) ||
277
(!g_ascii_strncasecmp(str.c_str(), "true", str.length()));
280
/** A wrapper for dgettext that takes a msgid of the form Context|Text.
281
* If Context|Text is translated, the translation is returned, otherwise
282
* just Text is returned. Useful for getting translations of words or phrases
283
* that have different meanings in different contexts.
286
sgettext (const char* domain_name, const char* msgid)
288
const char * msgval = dgettext (domain_name, msgid);
289
if (msgval == msgid) {
290
const char * p = strrchr (msgid, '|');