]> sigrok.org Git - pulseview.git/blob - pv/util.cpp
237ca7f059f1e589dac4dd9e6f6bc29120a6e3c5
[pulseview.git] / pv / util.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include "util.hpp"
21
22 #include <extdef.h>
23
24 #include <algorithm>
25 #include <cassert>
26 #include <sstream>
27
28 #include <QDebug>
29 #include <QTextStream>
30
31 using std::fixed;
32 using std::max;
33 using std::ostringstream;
34 using std::setfill;
35 using std::setprecision;
36 using std::showpos;
37 using std::string;
38
39 using namespace Qt;
40
41 namespace pv {
42 namespace util {
43
44 static QTextStream& operator<<(QTextStream& stream, SIPrefix prefix)
45 {
46         switch (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';
63
64         default: return stream;
65         }
66 }
67
68 int exponent(SIPrefix prefix)
69 {
70         return 3 * (static_cast<int>(prefix) - static_cast<int>(SIPrefix::none));
71 }
72
73 static SIPrefix successor(SIPrefix prefix)
74 {
75         assert(prefix != SIPrefix::yotta);
76         return static_cast<SIPrefix>(static_cast<int>(prefix) + 1);
77 }
78
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)
82 {
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.
90
91         int precision = stream.realNumberPrecision();
92
93         ostringstream ss;
94         ss << fixed;
95
96         if (stream.numberFlags() & QTextStream::ForceSign)
97                 ss << showpos;
98
99         if (0 == precision)
100                 ss << setprecision(1) << round(t);
101         else
102                 ss << setprecision(precision) << t;
103
104         string str(ss.str());
105         if (0 == precision) {
106                 // remove the separator and the unwanted decimal place
107                 str.resize(str.size() - 2);
108         }
109
110         return stream << QString::fromStdString(str);
111 }
112
113 QString format_time_si(const Timestamp& v, SIPrefix prefix,
114         unsigned int precision, QString unit, bool sign)
115 {
116         if (prefix == SIPrefix::unspecified) {
117                 // No prefix given, calculate it
118
119                 if (v.is_zero()) {
120                         prefix = SIPrefix::none;
121                 } else {
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);
127                                 exp -= 3;
128                         }
129                 }
130         }
131
132         assert(prefix >= SIPrefix::yocto);
133         assert(prefix <= SIPrefix::yotta);
134
135         const Timestamp multiplier = pow(Timestamp(10), -exponent(prefix));
136
137         QString s;
138         QTextStream ts(&s);
139         if (sign && !v.is_zero())
140                 ts << forcesign;
141         ts << qSetRealNumberPrecision(precision) << (v * multiplier);
142         if (!unit.isNull())
143                 ts << ' ' << prefix << unit;
144
145         return s;
146 }
147
148 QString format_value_si(double v, SIPrefix prefix, unsigned precision,
149         QString unit, bool sign)
150 {
151         if (prefix == SIPrefix::unspecified) {
152                 // No prefix given, calculate it
153
154                 if (v == 0) {
155                         prefix = SIPrefix::none;
156                 } else {
157                         int exp = exponent(SIPrefix::yotta);
158                         prefix = SIPrefix::yocto;
159                         while ((fabs(v) * pow(Timestamp(10), exp)) > 999 &&
160                                         prefix < SIPrefix::yotta) {
161                                 prefix = successor(prefix);
162                                 exp -= 3;
163                         }
164                 }
165         }
166
167         assert(prefix >= SIPrefix::yocto);
168         assert(prefix <= SIPrefix::yotta);
169
170         const double multiplier = pow(10.0, -exponent(prefix));
171
172         QString s;
173         QTextStream ts(&s);
174         if (sign && (v != 0))
175                 ts << forcesign;
176         ts.setRealNumberNotation(QTextStream::FixedNotation);
177         ts.setRealNumberPrecision(precision);
178         ts << (v * multiplier) << ' ' << prefix << unit;
179
180         return s;
181 }
182
183 QString format_time_si_adjusted(const Timestamp& t, SIPrefix prefix,
184         unsigned precision, QString unit, bool sign)
185 {
186         // The precision is always given without taking the prefix into account
187         // so we need to deduct the number of decimals the prefix might imply
188         const int prefix_order = -exponent(prefix);
189
190         const unsigned int relative_prec =
191                 (prefix >= SIPrefix::none) ? precision :
192                 max((int)(precision - prefix_order), 0);
193
194         return format_time_si(t, prefix, relative_prec, unit, sign);
195 }
196
197 // Helper for 'format_time_minutes()'.
198 static QString pad_number(unsigned int number, int length)
199 {
200         return QString("%1").arg(number, length, 10, QChar('0'));
201 }
202
203 QString format_time_minutes(const Timestamp& t, signed precision, bool sign)
204 {
205         const Timestamp whole_seconds = floor(abs(t));
206         const Timestamp days = floor(whole_seconds / (60 * 60 * 24));
207         const unsigned int hours = fmod(whole_seconds / (60 * 60), 24).convert_to<uint>();
208         const unsigned int minutes = fmod(whole_seconds / 60, 60).convert_to<uint>();
209         const unsigned int seconds = fmod(whole_seconds, 60).convert_to<uint>();
210
211         QString s;
212         QTextStream ts(&s);
213
214         if (t < 0)
215                 ts << "-";
216         else if (sign)
217                 ts << "+";
218
219         bool use_padding = false;
220
221         // DD
222         if (days) {
223                 ts << days.str().c_str() << ":";
224                 use_padding = true;
225         }
226
227         // HH
228         if (hours || days) {
229                 ts << pad_number(hours, use_padding ? 2 : 0) << ":";
230                 use_padding = true;
231         }
232
233         // MM
234         ts << pad_number(minutes, use_padding ? 2 : 0);
235
236         ts << ":";
237
238         // SS
239         ts << pad_number(seconds, 2);
240
241         if (precision) {
242                 ts << ".";
243
244                 const Timestamp fraction = fabs(t) - whole_seconds;
245
246                 ostringstream ss;
247                 ss << fixed << setprecision(precision) << setfill('0') << fraction;
248                 string fs = ss.str();
249
250                 // Copy all digits, inserting spaces as unit separators
251                 for (int i = 1; i <= precision; i++) {
252                         // Start at index 2 to skip the "0." at the beginning
253                         ts << fs.at(1 + i);
254
255                         if ((i > 0) && (i % 3 == 0) && (i != precision))
256                                 ts << " ";
257                 }
258         }
259
260         return s;
261 }
262
263 /**
264  * Split a string into tokens at occurences of the separator.
265  *
266  * @param[in] text The input string to split.
267  * @param[in] separator The delimiter between tokens.
268  *
269  * @return A vector of broken down tokens.
270  */
271 vector<string> split_string(string text, string separator)
272 {
273         vector<string> result;
274         size_t pos;
275
276         while ((pos = text.find(separator)) != std::string::npos) {
277                 result.push_back(text.substr(0, pos));
278                 text = text.substr(pos + separator.length());
279         }
280         result.push_back(text);
281
282         return result;
283 }
284
285 } // namespace util
286 } // namespace pv