2018-10-11 18:19:35 +02:00
|
|
|
#include <cctype>
|
2016-07-25 19:32:36 +02:00
|
|
|
#include <cmath>
|
2021-01-17 19:33:06 +01:00
|
|
|
#include <QFileInfo>
|
2018-10-11 18:19:35 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
|
2021-01-17 19:33:06 +01:00
|
|
|
int Util::str2int(const char *str, int len)
|
2018-10-11 18:19:35 +02:00
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
for (const char *sp = str; sp < str + len; sp++) {
|
|
|
|
if (::isdigit(*sp))
|
|
|
|
res = res * 10 + *sp - '0';
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2016-08-02 20:46:22 +02:00
|
|
|
|
2021-01-17 19:33:06 +01:00
|
|
|
double Util::niceNum(double x, bool round)
|
2016-07-25 19:32:36 +02:00
|
|
|
{
|
|
|
|
int expv;
|
|
|
|
double f;
|
|
|
|
double nf;
|
|
|
|
|
|
|
|
expv = (int)floor(log10(x));
|
|
|
|
f = x / pow(10.0, expv);
|
|
|
|
|
|
|
|
if (round) {
|
|
|
|
if (f < 1.5)
|
|
|
|
nf = 1.0;
|
|
|
|
else if (f < 3.0)
|
|
|
|
nf = 2.0;
|
|
|
|
else if (f < 7.0)
|
|
|
|
nf = 5.0;
|
|
|
|
else
|
|
|
|
nf = 10.0;
|
|
|
|
} else {
|
|
|
|
if (f <= 1.0)
|
|
|
|
nf = 1.0;
|
|
|
|
else if (f <= 2.0)
|
|
|
|
nf = 2.0;
|
|
|
|
else if (f <= 5.0)
|
|
|
|
nf = 5.0;
|
|
|
|
else
|
|
|
|
nf = 10.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nf * pow(10.0, expv);
|
|
|
|
}
|
2021-01-17 19:33:06 +01:00
|
|
|
|
|
|
|
QString Util::file2name(const QString &path)
|
|
|
|
{
|
|
|
|
QFileInfo fi(path);
|
|
|
|
return fi.baseName().replace('_', ' ');
|
|
|
|
}
|