2 * Copyright (C) 2006-2017 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2006 Taybin Rutkin <taybin@taybin.com>
4 * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
5 * Copyright (C) 2015-2019 Robin Gareus <robin@gareus.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #ifndef __STDC_FORMAT_MACROS
32 #define __STDC_FORMAT_MACROS
38 #include "pbd/convert.h"
49 capitalize (const string& str)
53 /* XXX not unicode safe */
54 ret[0] = toupper (str[0]);
60 downcase (const string& str)
63 std::transform (copy.begin(), copy.end(), copy.begin(), ::tolower);
68 downcase (const char* str)
70 char *copy = strdup (str);
71 for (char* p = copy; *p; ++p) {
78 short_version (string orig, string::size_type target_length)
80 /* this tries to create a recognizable abbreviation
81 of "orig" by removing characters until we meet
82 a certain target length.
84 note that we deliberately leave digits in the result
89 string::size_type pos;
91 /* remove white-space and punctuation, starting at end */
93 while (orig.length() > target_length) {
94 if ((pos = orig.find_last_of (_("\"\n\t ,<.>/?:;'[{}]~`!@#$%^&*()_-+="))) == string::npos) {
97 orig.replace (pos, 1, "");
100 /* remove lower-case vowels, starting at end */
102 while (orig.length() > target_length) {
103 if ((pos = orig.find_last_of (_("aeiou"))) == string::npos) {
106 orig.replace (pos, 1, "");
109 /* remove upper-case vowels, starting at end */
111 while (orig.length() > target_length) {
112 if ((pos = orig.find_last_of (_("AEIOU"))) == string::npos) {
115 orig.replace (pos, 1, "");
118 /* remove lower-case consonants, starting at end */
120 while (orig.length() > target_length) {
121 if ((pos = orig.find_last_of (_("bcdfghjklmnpqrtvwxyz"))) == string::npos) {
124 orig.replace (pos, 1, "");
127 /* remove upper-case consonants, starting at end */
129 while (orig.length() > target_length) {
130 if ((pos = orig.find_last_of (_("BCDFGHJKLMNPQRTVWXYZ"))) == string::npos) {
133 orig.replace (pos, 1, "");
136 /* whatever the length is now, use it */
142 atoi (const string& s)
144 return ::atoi (s.c_str());
148 atol (const string& s)
150 return (int32_t) ::atol (s.c_str());
154 atoll (const string& s)
156 return (int64_t) ::atoll (s.c_str());
160 atof (const string& s)
162 return ::atof (s.c_str());
166 internationalize (const char *package_name, const char **array)
170 for (uint32_t i = 0; array[i]; ++i) {
171 v.push_back (dgettext(package_name, array[i]));
178 int_from_hex (char hic, char loc)
180 int hi; /* hi byte */
181 int lo; /* low byte */
185 if( ('0'<=hi) && (hi<='9') ) {
187 } else if( ('a'<= hi) && (hi<= 'f') ) {
189 } else if( ('A'<=hi) && (hi<='F') ) {
195 if( ('0'<=lo) && (lo<='9') ) {
197 } else if( ('a'<=lo) && (lo<='f') ) {
199 } else if( ('A'<=lo) && (lo<='F') ) {
203 return lo + (16 * hi);
207 url_decode (string const & url)
211 for (string::size_type i = 0; i < url.length(); ++i) {
214 } else if (url[i] == '%' && i <= url.length() - 3) {
215 decoded += char (int_from_hex (url[i + 1], url[i + 2]));
227 length2string (const int32_t samples, const float sample_rate)
229 int32_t secs = (int32_t) (samples / sample_rate);
230 int32_t hrs = secs / 3600;
231 secs -= (hrs * 3600);
232 int32_t mins = secs / 60;
235 int32_t total_secs = (hrs * 3600) + (mins * 60) + secs;
236 int32_t samples_remaining = (int) floor (samples - (total_secs * sample_rate));
237 float fractional_secs = (float) samples_remaining / sample_rate;
239 char duration_str[32];
240 sprintf (duration_str, "%02" PRIi32 ":%02" PRIi32 ":%05.2f", hrs, mins, (float) secs + fractional_secs);
247 length2string (const int64_t samples, const double sample_rate)
249 int64_t secs = (int64_t) floor (samples / sample_rate);
250 int64_t hrs = secs / 3600LL;
251 secs -= (hrs * 3600LL);
252 int64_t mins = secs / 60LL;
253 secs -= (mins * 60LL);
255 int64_t total_secs = (hrs * 3600LL) + (mins * 60LL) + secs;
256 int64_t samples_remaining = (int64_t) floor (samples - (total_secs * sample_rate));
257 float fractional_secs = (float) samples_remaining / sample_rate;
259 char duration_str[64];
260 sprintf (duration_str, "%02" PRIi64 ":%02" PRIi64 ":%05.2f", hrs, mins, (float) secs + fractional_secs);
266 chars_equal_ignore_case(char x, char y)
268 /* app should have called setlocale() if its wants this comparison to be
271 return toupper (x) == toupper (y);
275 strings_equal_ignore_case (const string& a, const string& b)
277 if (a.length() == b.length()) {
278 return std::equal (a.begin(), a.end(), b.begin(), chars_equal_ignore_case);
283 /** A wrapper for dgettext that takes a msgid of the form Context|Text.
284 * If Context|Text is translated, the translation is returned, otherwise
285 * just Text is returned. Useful for getting translations of words or phrases
286 * that have different meanings in different contexts.
289 sgettext (const char* domain_name, const char* msgid)
291 const char * msgval = dgettext (domain_name, msgid);
292 if (msgval == msgid) {
293 const char * p = strrchr (msgid, '|');