MythTV  master
mythmiscutil.h
Go to the documentation of this file.
1 #ifndef MYTHMISCUTIL_H_
2 #define MYTHMISCUTIL_H_
3 
4 #include <algorithm>
5 #include <array>
6 #include <cstdint>
7 #include <ctime>
8 
9 #include <QString>
10 #include <QDir>
11 
12 #include "mythbaseexp.h"
13 #include "mythsystem.h"
14 
15 class QStringList;
16 class QFile;
17 
18 #if QT_VERSION < QT_VERSION_CHECK(5,10,0)
19 #include <QtGlobal>
20 #else
21 #include <QRandomGenerator>
22 #endif
23 
24 MBASE_PUBLIC inline uint32_t MythRandom()
25 {
26 #if QT_VERSION < QT_VERSION_CHECK(5,10,0)
27  return static_cast<uint32_t>(qrand());
28 #else
29  return QRandomGenerator::global()->generate();
30 #endif
31 }
32 
33 MBASE_PUBLIC bool getUptime(time_t &uptime);
35  int &totalMB, int &freeMB, int &totalVM, int &freeVM);
36 using loadArray = std::array<double,3>;
38 
39 MBASE_PUBLIC bool hasUtf8(const char *str);
40 #define M_QSTRING_UNICODE(str) hasUtf8(str) ? QString::fromUtf8(str) : str
41 
42 MBASE_PUBLIC bool ping(const QString &host, int timeout);
43 MBASE_PUBLIC bool telnet(const QString &host, int port);
44 
45 MBASE_PUBLIC long long copy(QFile &dst, QFile &src, uint block_size = 0);
47  QString name_template = "/tmp/mythtv_XXXXXX", bool dir = false);
48 MBASE_PUBLIC bool makeFileAccessible(const QString& filename);
49 
50 MBASE_PUBLIC QString getResponse(const QString &query, const QString &def);
51 MBASE_PUBLIC int intResponse(const QString &query, int def);
52 
53 MBASE_PUBLIC QString getSymlinkTarget(const QString &start_file,
54  QStringList *intermediaries = nullptr,
55  unsigned maxLinks = 255);
56 
57 MBASE_PUBLIC void wrapList(QStringList &list, int width);
58 
59 inline float clamp(float val, float minimum, float maximum)
60 {
61  return std::min(std::max(val, minimum), maximum);
62 }
63 inline int clamp(int val, int minimum, int maximum)
64 {
65  return std::min(std::max(val, minimum), maximum);
66 }
67 inline float lerp(float r, float a, float b)
68 {
69  return ((1.0F - r) * a) + (r * b);
70 }
71 inline int lerp(float r, int a, int b)
72 {
73  return (int) lerp(r, (float) a, (float) b);
74 }
75 inline float sq(float a) { return a*a; }
76 inline int sq(int a) { return a*a; }
77 
78 static inline QString xml_bool_to_string(bool val)
79 {
80  return (val) ? "true" : "false";
81 }
82 
83 MBASE_PUBLIC QString xml_indent(uint level);
84 
85 MBASE_PUBLIC bool IsMACAddress(const QString& MAC);
86 MBASE_PUBLIC bool WakeOnLAN(const QString& MAC);
87 MBASE_PUBLIC bool MythWakeup(const QString &wakeUpCommand,
88  uint flags = kMSNone, uint timeout = 0);
89 
90 MBASE_PUBLIC QString FileHash(const QString& filename);
91 
94 
95 MBASE_PUBLIC bool myth_nice(int val);
96 MBASE_PUBLIC void myth_yield(void);
98 MBASE_PUBLIC bool myth_ioprio(int val);
99 
100 MBASE_PUBLIC bool MythRemoveDirectory(QDir &aDir);
101 
102 MBASE_PUBLIC void setHttpProxy(void);
103 
104 MBASE_PUBLIC int naturalCompare(const QString &_a, const QString &_b,
105  Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive);
106 
107 #define ONESECINMS ( 1000)
108 #define ONEMININMS ( 60*1000)
109 #define ONEHOURINMS ( 60*60*1000)
110 #define ONEDAYINMS (24*60*60*1000)
111 #define ONEMININSEC ( 60)
112 #define ONEHOURINSEC ( 60*60)
113 #define ONEDAYINSEC (24*60*60)
114 
126 MBASE_PUBLIC QString MythFormatTimeMs(int msecs, const QString& fmt);
127 
139 MBASE_PUBLIC QString MythFormatTime(int secs, const QString& fmt);
140 
141 // CPU Tick timing function
142 #ifdef MMX
143 #ifdef _WIN32
144 #include "compat.h"
145 inline void rdtsc(uint64_t &x)
146 {
147  QueryPerformanceCounter((LARGE_INTEGER*)(&x));
148 }
149 #else
150 struct timing_ab_t {
151  uint a;
152  uint b;
153 };
154 inline void rdtsc(uint64_t &x)
155 {
156  auto &y = (timing_ab_t&) x;
157  asm("rdtsc \n"
158  "mov %%eax, %0 \n"
159  "mov %%edx, %1 \n"
160  :
161  : "m"(y.a), "m"(y.b)
162  : "%eax", "%edx");
163 }
164 #endif
165 
166 #else // if !MMX
167 inline void rdtsc(uint64_t &x) { x = 0ULL; }
168 #endif // !MMX
169 
170 MBASE_PUBLIC QStringList MythSplitCommandString(const QString &line);
171 
172 #endif // MYTHMISCUTIL_H_
IsMACAddress
MBASE_PUBLIC bool IsMACAddress(const QString &MAC)
Definition: mythmiscutil.cpp:549
hasUtf8
MBASE_PUBLIC bool hasUtf8(const char *str)
Guess whether a string is UTF-8.
Definition: mythmiscutil.cpp:195
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
getLoadAvgs
MBASE_PUBLIC loadArray getLoadAvgs(void)
Returns the system load averages.
Definition: mythmiscutil.cpp:178
xml_indent
MBASE_PUBLIC QString xml_indent(uint level)
Definition: mythmiscutil.cpp:1007
mythbaseexp.h
myth_yield
MBASE_PUBLIC void myth_yield(void)
Definition: mythmiscutil.cpp:716
IsPulseAudioRunning
MBASE_PUBLIC bool IsPulseAudioRunning(void)
Is A/V Sync destruction daemon is running on this host?
Definition: mythmiscutil.cpp:684
rdtsc
void rdtsc(uint64_t &x)
Definition: mythmiscutil.h:145
getSymlinkTarget
MBASE_PUBLIC QString getSymlinkTarget(const QString &start_file, QStringList *intermediaries=nullptr, unsigned maxLinks=255)
Definition: mythmiscutil.cpp:497
mythsystem.h
ping
MBASE_PUBLIC bool ping(const QString &host, int timeout)
Can we ping host within timeout seconds?
Definition: mythmiscutil.cpp:247
MBASE_PUBLIC
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
telnet
MBASE_PUBLIC bool telnet(const QString &host, int port)
Can we talk to port on host?
Definition: mythmiscutil.cpp:278
getResponse
MBASE_PUBLIC QString getResponse(const QString &query, const QString &def)
In an interactive shell, prompt the user to input a string.
Definition: mythmiscutil.cpp:456
WakeOnLAN
MBASE_PUBLIC bool WakeOnLAN(const QString &MAC)
Definition: mythmiscutil.cpp:635
getMemStats
MBASE_PUBLIC bool getMemStats(int &totalMB, int &freeMB, int &totalVM, int &freeVM)
Returns memory statistics in megabytes.
Definition: mythmiscutil.cpp:111
createTempFile
MBASE_PUBLIC QString createTempFile(QString name_template="/tmp/mythtv_XXXXXX", bool dir=false)
Definition: mythmiscutil.cpp:369
compat.h
clamp
float clamp(float val, float minimum, float maximum)
Definition: mythmiscutil.h:59
MythRemoveDirectory
MBASE_PUBLIC bool MythRemoveDirectory(QDir &aDir)
Definition: mythmiscutil.cpp:808
naturalCompare
MBASE_PUBLIC int naturalCompare(const QString &_a, const QString &_b, Qt::CaseSensitivity caseSensitivity=Qt::CaseSensitive)
Definition: mythmiscutil.cpp:1033
filename
QString filename
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:637
lerp
float lerp(float r, float a, float b)
Definition: mythmiscutil.h:67
loadArray
std::array< double, 3 > loadArray
Definition: mythmiscutil.h:36
myth_ioprio
MBASE_PUBLIC bool myth_ioprio(int val)
range -1..8, smaller is higher priority
Definition: mythmiscutil.cpp:804
uint
unsigned int uint
Definition: compat.h:141
MythWakeup
MBASE_PUBLIC bool MythWakeup(const QString &wakeUpCommand, uint flags=kMSNone, uint timeout=0)
Definition: mythmiscutil.cpp:676
MythFormatTime
MBASE_PUBLIC QString MythFormatTime(int secs, const QString &fmt)
Format a seconds time value.
Definition: mythmiscutil.cpp:1238
xml_bool_to_string
static QString xml_bool_to_string(bool val)
Definition: mythmiscutil.h:78
FileHash
MBASE_PUBLIC QString FileHash(const QString &filename)
Definition: mythmiscutil.cpp:594
block_size
unsigned int block_size
Definition: freesurround.cpp:47
MythRandom
MBASE_PUBLIC uint32_t MythRandom()
Definition: mythmiscutil.h:24
intResponse
MBASE_PUBLIC int intResponse(const QString &query, int def)
In an interactive shell, prompt the user to input a number.
Definition: mythmiscutil.cpp:486
MythSplitCommandString
MBASE_PUBLIC QStringList MythSplitCommandString(const QString &line)
Definition: mythmiscutil.cpp:1260
dir
QDir dir
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:1174
kMSNone
@ kMSNone
Definition: mythsystem.h:33
myth_nice
MBASE_PUBLIC bool myth_nice(int val)
Definition: mythmiscutil.cpp:702
MythFormatTimeMs
MBASE_PUBLIC QString MythFormatTimeMs(int msecs, const QString &fmt)
Format a milliseconds time value.
Definition: mythmiscutil.cpp:1233
copy
MBASE_PUBLIC long long copy(QFile &dst, QFile &src, uint block_size=0)
Copies src file to dst file.
Definition: mythmiscutil.cpp:309
setHttpProxy
MBASE_PUBLIC void setHttpProxy(void)
Get network proxy settings from OS, and use for [Q]Http[Comms].
Definition: mythmiscutil.cpp:852
wrapList
MBASE_PUBLIC void wrapList(QStringList &list, int width)
Definition: mythmiscutil.cpp:961
getUptime
MBASE_PUBLIC bool getUptime(time_t &uptime)
Returns uptime statistics.
Definition: mythmiscutil.cpp:68
sq
float sq(float a)
Definition: mythmiscutil.h:75
query
MSqlQuery query(MSqlQuery::InitCon())
makeFileAccessible
MBASE_PUBLIC bool makeFileAccessible(const QString &filename)
Definition: mythmiscutil.cpp:441