2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
29 #include <QTextStream>
33 using std::ostringstream;
35 using std::setprecision;
44 static QTextStream& operator<<(QTextStream& stream, SIPrefix prefix)
47 case SIPrefix::yocto: return stream << 'y';
48 case SIPrefix::zepto: return stream << 'z';
49 case SIPrefix::atto: return stream << 'a';
50 case SIPrefix::femto: return stream << 'f';
51 case SIPrefix::pico: return stream << 'p';
52 case SIPrefix::nano: return stream << 'n';
53 case SIPrefix::micro: return stream << QChar(0x03BC);
54 case SIPrefix::milli: return stream << 'm';
55 case SIPrefix::kilo: return stream << 'k';
56 case SIPrefix::mega: return stream << 'M';
57 case SIPrefix::giga: return stream << 'G';
58 case SIPrefix::tera: return stream << 'T';
59 case SIPrefix::peta: return stream << 'P';
60 case SIPrefix::exa: return stream << 'E';
61 case SIPrefix::zetta: return stream << 'Z';
62 case SIPrefix::yotta: return stream << 'Y';
64 default: return stream;
68 int exponent(SIPrefix prefix)
70 return 3 * (static_cast<int>(prefix) - static_cast<int>(SIPrefix::none));
73 static SIPrefix successor(SIPrefix prefix)
75 assert(prefix != SIPrefix::yotta);
76 return static_cast<SIPrefix>(static_cast<int>(prefix) + 1);
79 // Insert the timestamp value into the stream in fixed-point notation
80 // (and honor the precision)
81 static QTextStream& operator<<(QTextStream& stream, const Timestamp& t)
83 // The multiprecision types already have a function and a stream insertion
84 // operator to convert them to a string, however these functions abuse a
85 // precision value of zero to print all available decimal places instead of
86 // none, and the boost authors refuse to fix this because they don't want
87 // to break buggy code that relies on this bug.
88 // (https://svn.boost.org/trac/boost/ticket/10103)
89 // Therefore we have to work around the case where precision is zero.
91 int precision = stream.realNumberPrecision();
96 if (stream.numberFlags() & QTextStream::ForceSign)
100 ss << setprecision(1) << round(t);
102 ss << setprecision(precision) << t;
104 string str(ss.str());
105 if (0 == precision) {
106 // remove the separator and the unwanted decimal place
107 str.resize(str.size() - 2);
110 return stream << QString::fromStdString(str);
113 QString format_time_si(const Timestamp& v, SIPrefix prefix,
114 unsigned int precision, QString unit, bool sign)
116 if (prefix == SIPrefix::unspecified) {
117 // No prefix given, calculate it
120 prefix = SIPrefix::none;
122 int exp = exponent(SIPrefix::yotta);
123 prefix = SIPrefix::yocto;
124 while ((fabs(v) * pow(Timestamp(10), exp)) > 999 &&
125 prefix < SIPrefix::yotta) {
126 prefix = successor(prefix);
132 assert(prefix >= SIPrefix::yocto);
133 assert(prefix <= SIPrefix::yotta);
135 const Timestamp multiplier = pow(Timestamp(10), -exponent(prefix));
139 if (sign && !v.is_zero())
141 ts << qSetRealNumberPrecision(precision) << (v * multiplier) << ' '
147 QString format_time_si_adjusted(const Timestamp& t, SIPrefix prefix,
148 unsigned precision, QString unit, bool sign)
150 // The precision is always given without taking the prefix into account
151 // so we need to deduct the number of decimals the prefix might imply
152 const int prefix_order = -exponent(prefix);
154 const unsigned int relative_prec =
155 (prefix >= SIPrefix::none) ? precision :
156 max((int)(precision - prefix_order), 0);
158 return format_time_si(t, prefix, relative_prec, unit, sign);
161 // Helper for 'format_time_minutes()'.
162 static QString pad_number(unsigned int number, int length)
164 return QString("%1").arg(number, length, 10, QChar('0'));
167 QString format_time_minutes(const Timestamp& t, signed precision, bool sign)
169 const Timestamp whole_seconds = floor(abs(t));
170 const Timestamp days = floor(whole_seconds / (60 * 60 * 24));
171 const unsigned int hours = fmod(whole_seconds / (60 * 60), 24).convert_to<uint>();
172 const unsigned int minutes = fmod(whole_seconds / 60, 60).convert_to<uint>();
173 const unsigned int seconds = fmod(whole_seconds, 60).convert_to<uint>();
183 bool use_padding = false;
187 ts << days.str().c_str() << ":";
193 ts << pad_number(hours, use_padding ? 2 : 0) << ":";
198 ts << pad_number(minutes, use_padding ? 2 : 0);
203 ts << pad_number(seconds, 2);
208 const Timestamp fraction = fabs(t) - whole_seconds;
211 ss << fixed << setprecision(precision) << setfill('0') << fraction;
212 string fs = ss.str();
214 // Copy all digits, inserting spaces as unit separators
215 for (int i = 1; i <= precision; i++) {
216 // Start at index 2 to skip the "0." at the beginning
219 if ((i > 0) && (i % 3 == 0) && (i != precision))
228 * Split a string into tokens at occurences of the separator.
230 * @param[in] text The input string to split.
231 * @param[in] separator The delimiter between tokens.
233 * @return A vector of broken down tokens.
235 vector<string> split_string(string text, string separator)
237 vector<string> result;
240 while ((pos = text.find(separator)) != std::string::npos) {
241 result.push_back(text.substr(0, pos));
242 text = text.substr(pos + separator.length());
244 result.push_back(text);