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 #if QT_VERSION < QT_VERSION_CHECK(5,10,0)
16 #include <QtGlobal>
17 #else
18 #include <QRandomGenerator>
19 #endif
20 
21 MBASE_PUBLIC inline uint32_t MythRandom()
22 {
23 #if QT_VERSION < QT_VERSION_CHECK(5,10,0)
24  return static_cast<uint32_t>(qrand());
25 #else
26  return QRandomGenerator::global()->generate();
27 #endif
28 }
29 
30 MBASE_PUBLIC bool getUptime(std::chrono::seconds &uptime);
32  int &totalMB, int &freeMB, int &totalVM, int &freeVM);
33 using loadArray = std::array<double,3>;
35 
36 MBASE_PUBLIC bool hasUtf8(const char *str);
37 #define M_QSTRING_UNICODE(str) hasUtf8(str) ? QString::fromUtf8(str) : str
38 
39 MBASE_PUBLIC bool ping(const QString &host, std::chrono::milliseconds timeout);
40 MBASE_PUBLIC bool telnet(const QString &host, int port);
41 
42 MBASE_PUBLIC long long copy(QFile &dst, QFile &src, uint block_size = 0);
44  QString name_template = "/tmp/mythtv_XXXXXX", bool dir = false);
45 MBASE_PUBLIC bool makeFileAccessible(const QString& filename);
46 
47 MBASE_PUBLIC QString getResponse(const QString &query, const QString &def);
48 MBASE_PUBLIC int intResponse(const QString &query, int def);
49 
50 MBASE_PUBLIC QString getSymlinkTarget(const QString &start_file,
51  QStringList *intermediaries = nullptr,
52  unsigned maxLinks = 255);
53 
54 MBASE_PUBLIC void wrapList(QStringList &list, int width);
55 
56 inline float clamp(float val, float minimum, float maximum)
57 {
58  return std::min(std::max(val, minimum), maximum);
59 }
60 inline int clamp(int val, int minimum, int maximum)
61 {
62  return std::min(std::max(val, minimum), maximum);
63 }
64 inline float lerp(float r, float a, float b)
65 {
66  return ((1.0F - r) * a) + (r * b);
67 }
68 inline int lerp(float r, int a, int b)
69 {
70  return (int) lerp(r, (float) a, (float) b);
71 }
72 inline float sq(float a) { return a*a; }
73 inline int sq(int a) { return a*a; }
74 
75 static inline QString xml_bool_to_string(bool val)
76 {
77  return (val) ? "true" : "false";
78 }
79 
80 MBASE_PUBLIC QString xml_indent(uint level);
81 
82 MBASE_PUBLIC bool IsMACAddress(const QString& MAC);
83 MBASE_PUBLIC bool WakeOnLAN(const QString& MAC);
84 MBASE_PUBLIC bool MythWakeup(const QString &wakeUpCommand,
85  uint flags = kMSNone, std::chrono::seconds timeout = 0s);
86 
87 MBASE_PUBLIC QString FileHash(const QString& filename);
88 
91 
92 MBASE_PUBLIC bool myth_nice(int val);
93 MBASE_PUBLIC void myth_yield(void);
95 MBASE_PUBLIC bool myth_ioprio(int val);
96 
97 MBASE_PUBLIC bool MythRemoveDirectory(QDir &aDir);
98 
99 MBASE_PUBLIC void setHttpProxy(void);
100 
101 MBASE_PUBLIC int naturalCompare(const QString &_a, const QString &_b,
102  Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive);
103 
118 MBASE_PUBLIC QString MythFormatTime(std::chrono::milliseconds msecs, const QString& fmt);
119 
120 // CPU Tick timing function
121 #ifdef MMX
122 #ifdef _WIN32
123 #include "compat.h"
124 inline void rdtsc(uint64_t &x)
125 {
126  QueryPerformanceCounter((LARGE_INTEGER*)(&x));
127 }
128 #else
129 struct timing_ab_t {
130  uint a;
131  uint b;
132 };
133 inline void rdtsc(uint64_t &x)
134 {
135  auto &y = (timing_ab_t&) x;
136  asm("rdtsc \n"
137  "mov %%eax, %0 \n"
138  "mov %%edx, %1 \n"
139  :
140  : "m"(y.a), "m"(y.b)
141  : "%eax", "%edx");
142 }
143 #endif
144 
145 #else // if !MMX
146 inline void rdtsc(uint64_t &x) { x = 0ULL; }
147 #endif // !MMX
148 
149 MBASE_PUBLIC QStringList MythSplitCommandString(const QString &line);
150 
151 #endif // MYTHMISCUTIL_H_
IsMACAddress
MBASE_PUBLIC bool IsMACAddress(const QString &MAC)
Definition: mythmiscutil.cpp:551
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:1009
mythbaseexp.h
myth_yield
MBASE_PUBLIC void myth_yield(void)
Definition: mythmiscutil.cpp:718
IsPulseAudioRunning
MBASE_PUBLIC bool IsPulseAudioRunning(void)
Is A/V Sync destruction daemon is running on this host?
Definition: mythmiscutil.cpp:686
rdtsc
void rdtsc(uint64_t &x)
Definition: mythmiscutil.h:124
getSymlinkTarget
MBASE_PUBLIC QString getSymlinkTarget(const QString &start_file, QStringList *intermediaries=nullptr, unsigned maxLinks=255)
Definition: mythmiscutil.cpp:499
mythsystem.h
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:280
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:458
WakeOnLAN
MBASE_PUBLIC bool WakeOnLAN(const QString &MAC)
Definition: mythmiscutil.cpp:637
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:371
compat.h
clamp
float clamp(float val, float minimum, float maximum)
Definition: mythmiscutil.h:56
MythRemoveDirectory
MBASE_PUBLIC bool MythRemoveDirectory(QDir &aDir)
Definition: mythmiscutil.cpp:810
naturalCompare
MBASE_PUBLIC int naturalCompare(const QString &_a, const QString &_b, Qt::CaseSensitivity caseSensitivity=Qt::CaseSensitive)
Definition: mythmiscutil.cpp:1035
lerp
float lerp(float r, float a, float b)
Definition: mythmiscutil.h:64
loadArray
std::array< double, 3 > loadArray
Definition: mythmiscutil.h:33
MythWakeup
MBASE_PUBLIC bool MythWakeup(const QString &wakeUpCommand, uint flags=kMSNone, std::chrono::seconds timeout=0s)
Definition: mythmiscutil.cpp:678
myth_ioprio
MBASE_PUBLIC bool myth_ioprio(int val)
range -1..8, smaller is higher priority
Definition: mythmiscutil.cpp:806
uint
unsigned int uint
Definition: compat.h:140
ping
MBASE_PUBLIC bool ping(const QString &host, std::chrono::milliseconds timeout)
Can we ping host within timeout seconds?
Definition: mythmiscutil.cpp:247
xml_bool_to_string
static QString xml_bool_to_string(bool val)
Definition: mythmiscutil.h:75
FileHash
MBASE_PUBLIC QString FileHash(const QString &filename)
Definition: mythmiscutil.cpp:596
block_size
unsigned int block_size
Definition: freesurround.cpp:47
MythRandom
MBASE_PUBLIC uint32_t MythRandom()
Definition: mythmiscutil.h:21
intResponse
MBASE_PUBLIC int intResponse(const QString &query, int def)
In an interactive shell, prompt the user to input a number.
Definition: mythmiscutil.cpp:488
MythSplitCommandString
MBASE_PUBLIC QStringList MythSplitCommandString(const QString &line)
Definition: mythmiscutil.cpp:1257
kMSNone
@ kMSNone
Definition: mythsystem.h:35
myth_nice
MBASE_PUBLIC bool myth_nice(int val)
Definition: mythmiscutil.cpp:704
copy
MBASE_PUBLIC long long copy(QFile &dst, QFile &src, uint block_size=0)
Copies src file to dst file.
Definition: mythmiscutil.cpp:311
getUptime
MBASE_PUBLIC bool getUptime(std::chrono::seconds &uptime)
Definition: mythmiscutil.cpp:68
setHttpProxy
MBASE_PUBLIC void setHttpProxy(void)
Get network proxy settings from OS, and use for [Q]Http[Comms].
Definition: mythmiscutil.cpp:854
wrapList
MBASE_PUBLIC void wrapList(QStringList &list, int width)
Definition: mythmiscutil.cpp:963
MythFormatTime
MBASE_PUBLIC QString MythFormatTime(std::chrono::milliseconds msecs, const QString &fmt)
Format a milliseconds time value.
Definition: mythmiscutil.cpp:1235
build_compdb.filename
filename
Definition: build_compdb.py:21
sq
float sq(float a)
Definition: mythmiscutil.h:72
makeFileAccessible
MBASE_PUBLIC bool makeFileAccessible(const QString &filename)
Definition: mythmiscutil.cpp:443