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 #ifdef _WIN32
13  #undef mkdir
14 #endif
15 
16 #include "mythbaseexp.h"
17 #include "mythsystem.h"
18 
19 #if QT_VERSION < QT_VERSION_CHECK(5,10,0)
20 #include <QtGlobal>
21 #else
22 #include <QRandomGenerator>
23 #endif
24 
25 MBASE_PUBLIC inline uint32_t MythRandom()
26 {
27 #if QT_VERSION < QT_VERSION_CHECK(5,10,0)
28  return static_cast<uint32_t>(qrand());
29 #else
30  return QRandomGenerator::global()->generate();
31 #endif
32 }
33 
34 MBASE_PUBLIC bool getUptime(std::chrono::seconds &uptime);
36  int &totalMB, int &freeMB, int &totalVM, int &freeVM);
37 using loadArray = std::array<double,3>;
39 
40 MBASE_PUBLIC bool hasUtf8(const char *str);
41 #define M_QSTRING_UNICODE(str) hasUtf8(str) ? QString::fromUtf8(str) : str
42 
43 MBASE_PUBLIC bool ping(const QString &host, std::chrono::milliseconds timeout);
44 MBASE_PUBLIC bool telnet(const QString &host, int port);
45 
46 MBASE_PUBLIC long long copy(QFile &dst, QFile &src, uint block_size = 0);
48  QString name_template = "/tmp/mythtv_XXXXXX", bool dir = false);
49 MBASE_PUBLIC bool makeFileAccessible(const QString& filename);
50 
51 MBASE_PUBLIC QString getResponse(const QString &query, const QString &def);
52 MBASE_PUBLIC int intResponse(const QString &query, int def);
53 
54 MBASE_PUBLIC QString getSymlinkTarget(const QString &start_file,
55  QStringList *intermediaries = nullptr,
56  unsigned maxLinks = 255);
57 
58 MBASE_PUBLIC void wrapList(QStringList &list, int width);
59 
60 inline float clamp(float val, float minimum, float maximum)
61 {
62  return std::min(std::max(val, minimum), maximum);
63 }
64 inline int clamp(int val, int minimum, int maximum)
65 {
66  return std::min(std::max(val, minimum), maximum);
67 }
68 inline float lerp(float r, float a, float b)
69 {
70  return ((1.0F - r) * a) + (r * b);
71 }
72 inline int lerp(float r, int a, int b)
73 {
74  return (int) lerp(r, (float) a, (float) b);
75 }
76 inline float sq(float a) { return a*a; }
77 inline int sq(int a) { return a*a; }
78 
79 static inline QString xml_bool_to_string(bool val)
80 {
81  return (val) ? "true" : "false";
82 }
83 
84 MBASE_PUBLIC QString xml_indent(uint level);
85 
86 MBASE_PUBLIC bool IsMACAddress(const QString& MAC);
87 MBASE_PUBLIC bool WakeOnLAN(const QString& MAC);
88 MBASE_PUBLIC bool MythWakeup(const QString &wakeUpCommand,
89  uint flags = kMSNone, std::chrono::seconds timeout = 0s);
90 
91 MBASE_PUBLIC QString FileHash(const QString& filename);
92 
95 
96 MBASE_PUBLIC bool myth_nice(int val);
97 MBASE_PUBLIC void myth_yield(void);
99 MBASE_PUBLIC bool myth_ioprio(int val);
100 
101 MBASE_PUBLIC bool MythRemoveDirectory(QDir &aDir);
102 
103 MBASE_PUBLIC void setHttpProxy(void);
104 
105 MBASE_PUBLIC int naturalCompare(const QString &_a, const QString &_b,
106  Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive);
107 
122 MBASE_PUBLIC QString MythFormatTime(std::chrono::milliseconds msecs, const QString& fmt);
123 
124 // CPU Tick timing function
125 #ifdef MMX
126 #ifdef _WIN32
127 #include "compat.h"
128 inline void rdtsc(uint64_t &x)
129 {
130  QueryPerformanceCounter((LARGE_INTEGER*)(&x));
131 }
132 #else
133 struct timing_ab_t {
134  uint a;
135  uint b;
136 };
137 inline void rdtsc(uint64_t &x)
138 {
139  auto &y = (timing_ab_t&) x;
140  asm("rdtsc \n"
141  "mov %%eax, %0 \n"
142  "mov %%edx, %1 \n"
143  :
144  : "m"(y.a), "m"(y.b)
145  : "%eax", "%edx");
146 }
147 #endif
148 
149 #else // if !MMX
150 inline void rdtsc(uint64_t &x) { x = 0ULL; }
151 #endif // !MMX
152 
153 MBASE_PUBLIC QStringList MythSplitCommandString(const QString &line);
154 
155 #endif // MYTHMISCUTIL_H_
IsMACAddress
MBASE_PUBLIC bool IsMACAddress(const QString &MAC)
Definition: mythmiscutil.cpp:554
hasUtf8
MBASE_PUBLIC bool hasUtf8(const char *str)
Guess whether a string is UTF-8.
Definition: mythmiscutil.cpp:198
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
getLoadAvgs
MBASE_PUBLIC loadArray getLoadAvgs(void)
Returns the system load averages.
Definition: mythmiscutil.cpp:181
xml_indent
MBASE_PUBLIC QString xml_indent(uint level)
Definition: mythmiscutil.cpp:1012
mythbaseexp.h
myth_yield
MBASE_PUBLIC void myth_yield(void)
Definition: mythmiscutil.cpp:721
IsPulseAudioRunning
MBASE_PUBLIC bool IsPulseAudioRunning(void)
Is A/V Sync destruction daemon is running on this host?
Definition: mythmiscutil.cpp:689
rdtsc
void rdtsc(uint64_t &x)
Definition: mythmiscutil.h:128
getSymlinkTarget
MBASE_PUBLIC QString getSymlinkTarget(const QString &start_file, QStringList *intermediaries=nullptr, unsigned maxLinks=255)
Definition: mythmiscutil.cpp:502
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:283
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:461
WakeOnLAN
MBASE_PUBLIC bool WakeOnLAN(const QString &MAC)
Definition: mythmiscutil.cpp:640
getMemStats
MBASE_PUBLIC bool getMemStats(int &totalMB, int &freeMB, int &totalVM, int &freeVM)
Returns memory statistics in megabytes.
Definition: mythmiscutil.cpp:114
createTempFile
MBASE_PUBLIC QString createTempFile(QString name_template="/tmp/mythtv_XXXXXX", bool dir=false)
Definition: mythmiscutil.cpp:374
compat.h
clamp
float clamp(float val, float minimum, float maximum)
Definition: mythmiscutil.h:60
MythRemoveDirectory
MBASE_PUBLIC bool MythRemoveDirectory(QDir &aDir)
Definition: mythmiscutil.cpp:813
naturalCompare
MBASE_PUBLIC int naturalCompare(const QString &_a, const QString &_b, Qt::CaseSensitivity caseSensitivity=Qt::CaseSensitive)
Definition: mythmiscutil.cpp:1038
lerp
float lerp(float r, float a, float b)
Definition: mythmiscutil.h:68
loadArray
std::array< double, 3 > loadArray
Definition: mythmiscutil.h:37
MythWakeup
MBASE_PUBLIC bool MythWakeup(const QString &wakeUpCommand, uint flags=kMSNone, std::chrono::seconds timeout=0s)
Definition: mythmiscutil.cpp:681
myth_ioprio
MBASE_PUBLIC bool myth_ioprio(int val)
range -1..8, smaller is higher priority
Definition: mythmiscutil.cpp:809
uint
unsigned int uint
Definition: compat.h:144
ping
MBASE_PUBLIC bool ping(const QString &host, std::chrono::milliseconds timeout)
Can we ping host within timeout seconds?
Definition: mythmiscutil.cpp:250
xml_bool_to_string
static QString xml_bool_to_string(bool val)
Definition: mythmiscutil.h:79
FileHash
MBASE_PUBLIC QString FileHash(const QString &filename)
Definition: mythmiscutil.cpp:599
block_size
unsigned int block_size
Definition: freesurround.cpp:47
MythRandom
MBASE_PUBLIC uint32_t MythRandom()
Definition: mythmiscutil.h:25
intResponse
MBASE_PUBLIC int intResponse(const QString &query, int def)
In an interactive shell, prompt the user to input a number.
Definition: mythmiscutil.cpp:491
MythSplitCommandString
MBASE_PUBLIC QStringList MythSplitCommandString(const QString &line)
Definition: mythmiscutil.cpp:1260
kMSNone
@ kMSNone
Definition: mythsystem.h:35
myth_nice
MBASE_PUBLIC bool myth_nice(int val)
Definition: mythmiscutil.cpp:707
copy
MBASE_PUBLIC long long copy(QFile &dst, QFile &src, uint block_size=0)
Copies src file to dst file.
Definition: mythmiscutil.cpp:314
getUptime
MBASE_PUBLIC bool getUptime(std::chrono::seconds &uptime)
Definition: mythmiscutil.cpp:71
setHttpProxy
MBASE_PUBLIC void setHttpProxy(void)
Get network proxy settings from OS, and use for [Q]Http[Comms].
Definition: mythmiscutil.cpp:857
wrapList
MBASE_PUBLIC void wrapList(QStringList &list, int width)
Definition: mythmiscutil.cpp:966
MythFormatTime
MBASE_PUBLIC QString MythFormatTime(std::chrono::milliseconds msecs, const QString &fmt)
Format a milliseconds time value.
Definition: mythmiscutil.cpp:1238
build_compdb.filename
filename
Definition: build_compdb.py:21
sq
float sq(float a)
Definition: mythmiscutil.h:76
makeFileAccessible
MBASE_PUBLIC bool makeFileAccessible(const QString &filename)
Definition: mythmiscutil.cpp:446