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 MBASE_PUBLIC bool getUptime(time_t &uptime);
20  int &totalMB, int &freeMB, int &totalVM, int &freeVM);
21 using loadArray = std::array<double,3>;
23 
24 MBASE_PUBLIC bool hasUtf8(const char *str);
25 #define M_QSTRING_UNICODE(str) hasUtf8(str) ? QString::fromUtf8(str) : str
26 
27 MBASE_PUBLIC bool ping(const QString &host, int timeout);
28 MBASE_PUBLIC bool telnet(const QString &host, int port);
29 
30 MBASE_PUBLIC long long copy(QFile &dst, QFile &src, uint block_size = 0);
32  QString name_template = "/tmp/mythtv_XXXXXX", bool dir = false);
33 MBASE_PUBLIC bool makeFileAccessible(const QString& filename);
34 
35 MBASE_PUBLIC QString getResponse(const QString &query, const QString &def);
36 MBASE_PUBLIC int intResponse(const QString &query, int def);
37 
38 MBASE_PUBLIC QString getSymlinkTarget(const QString &start_file,
39  QStringList *intermediaries = nullptr,
40  unsigned maxLinks = 255);
41 
42 MBASE_PUBLIC void wrapList(QStringList &list, int width);
43 
44 inline float clamp(float val, float minimum, float maximum)
45 {
46  return std::min(std::max(val, minimum), maximum);
47 }
48 inline int clamp(int val, int minimum, int maximum)
49 {
50  return std::min(std::max(val, minimum), maximum);
51 }
52 inline float lerp(float r, float a, float b)
53 {
54  return ((1.0F - r) * a) + (r * b);
55 }
56 inline int lerp(float r, int a, int b)
57 {
58  return (int) lerp(r, (float) a, (float) b);
59 }
60 inline float sq(float a) { return a*a; }
61 inline int sq(int a) { return a*a; }
62 
63 static inline QString xml_bool_to_string(bool val)
64 {
65  return (val) ? "true" : "false";
66 }
67 
68 MBASE_PUBLIC QString xml_indent(uint level);
69 
70 MBASE_PUBLIC bool IsMACAddress(const QString& MAC);
71 MBASE_PUBLIC bool WakeOnLAN(const QString& MAC);
72 MBASE_PUBLIC bool MythWakeup(const QString &wakeUpCommand,
73  uint flags = kMSNone, uint timeout = 0);
74 
75 MBASE_PUBLIC QString FileHash(const QString& filename);
76 
79 
80 MBASE_PUBLIC bool myth_nice(int val);
81 MBASE_PUBLIC void myth_yield(void);
83 MBASE_PUBLIC bool myth_ioprio(int val);
84 
85 MBASE_PUBLIC bool MythRemoveDirectory(QDir &aDir);
86 
87 MBASE_PUBLIC void setHttpProxy(void);
88 
89 MBASE_PUBLIC int naturalCompare(const QString &_a, const QString &_b,
90  Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive);
91 
92 #define ONESECINMS ( 1000)
93 #define ONEMININMS ( 60*1000)
94 #define ONEHOURINMS ( 60*60*1000)
95 #define ONEDAYINMS (24*60*60*1000)
96 #define ONEMININSEC ( 60)
97 #define ONEHOURINSEC ( 60*60)
98 #define ONEDAYINSEC (24*60*60)
99 
111 MBASE_PUBLIC QString MythFormatTimeMs(int msecs, QString fmt);
112 
124 MBASE_PUBLIC QString MythFormatTime(int secs, QString fmt);
125 
126 // CPU Tick timing function
127 #ifdef MMX
128 #ifdef _WIN32
129 #include "compat.h"
130 inline void rdtsc(uint64_t &x)
131 {
132  QueryPerformanceCounter((LARGE_INTEGER*)(&x));
133 }
134 #else
135 struct timing_ab_t {
136  uint a;
137  uint b;
138 };
139 inline void rdtsc(uint64_t &x)
140 {
141  auto &y = (timing_ab_t&) x;
142  asm("rdtsc \n"
143  "mov %%eax, %0 \n"
144  "mov %%edx, %1 \n"
145  :
146  : "m"(y.a), "m"(y.b)
147  : "%eax", "%edx");
148 }
149 #endif
150 
151 #else // if !MMX
152 inline void rdtsc(uint64_t &x) { x = 0ULL; }
153 #endif // !MMX
154 
155 MBASE_PUBLIC QStringList MythSplitCommandString(const QString &line);
156 
157 #endif // MYTHMISCUTIL_H_
IsMACAddress
MBASE_PUBLIC bool IsMACAddress(const QString &MAC)
Definition: mythmiscutil.cpp:548
hasUtf8
MBASE_PUBLIC bool hasUtf8(const char *str)
Guess whether a string is UTF-8.
Definition: mythmiscutil.cpp:194
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
getLoadAvgs
MBASE_PUBLIC loadArray getLoadAvgs(void)
Returns the system load averages.
Definition: mythmiscutil.cpp:177
MythFormatTime
MBASE_PUBLIC QString MythFormatTime(int secs, QString fmt)
Format a seconds time value.
Definition: mythmiscutil.cpp:1237
xml_indent
MBASE_PUBLIC QString xml_indent(uint level)
Definition: mythmiscutil.cpp:1006
mythbaseexp.h
myth_yield
MBASE_PUBLIC void myth_yield(void)
Definition: mythmiscutil.cpp:715
IsPulseAudioRunning
MBASE_PUBLIC bool IsPulseAudioRunning(void)
Is A/V Sync destruction daemon is running on this host?
Definition: mythmiscutil.cpp:683
rdtsc
void rdtsc(uint64_t &x)
Definition: mythmiscutil.h:130
getSymlinkTarget
MBASE_PUBLIC QString getSymlinkTarget(const QString &start_file, QStringList *intermediaries=nullptr, unsigned maxLinks=255)
Definition: mythmiscutil.cpp:496
mythsystem.h
ping
MBASE_PUBLIC bool ping(const QString &host, int timeout)
Can we ping host within timeout seconds?
Definition: mythmiscutil.cpp:246
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:277
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:455
WakeOnLAN
MBASE_PUBLIC bool WakeOnLAN(const QString &MAC)
Definition: mythmiscutil.cpp:634
getMemStats
MBASE_PUBLIC bool getMemStats(int &totalMB, int &freeMB, int &totalVM, int &freeVM)
Returns memory statistics in megabytes.
Definition: mythmiscutil.cpp:110
createTempFile
MBASE_PUBLIC QString createTempFile(QString name_template="/tmp/mythtv_XXXXXX", bool dir=false)
Definition: mythmiscutil.cpp:368
compat.h
clamp
float clamp(float val, float minimum, float maximum)
Definition: mythmiscutil.h:44
MythRemoveDirectory
MBASE_PUBLIC bool MythRemoveDirectory(QDir &aDir)
Definition: mythmiscutil.cpp:807
naturalCompare
MBASE_PUBLIC int naturalCompare(const QString &_a, const QString &_b, Qt::CaseSensitivity caseSensitivity=Qt::CaseSensitive)
Definition: mythmiscutil.cpp:1032
filename
QString filename
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:634
lerp
float lerp(float r, float a, float b)
Definition: mythmiscutil.h:52
loadArray
std::array< double, 3 > loadArray
Definition: mythmiscutil.h:21
myth_ioprio
MBASE_PUBLIC bool myth_ioprio(int val)
range -1..8, smaller is higher priority
Definition: mythmiscutil.cpp:803
uint
unsigned int uint
Definition: compat.h:140
MythWakeup
MBASE_PUBLIC bool MythWakeup(const QString &wakeUpCommand, uint flags=kMSNone, uint timeout=0)
Definition: mythmiscutil.cpp:675
xml_bool_to_string
static QString xml_bool_to_string(bool val)
Definition: mythmiscutil.h:63
FileHash
MBASE_PUBLIC QString FileHash(const QString &filename)
Definition: mythmiscutil.cpp:593
block_size
unsigned int block_size
Definition: freesurround.cpp:48
intResponse
MBASE_PUBLIC int intResponse(const QString &query, int def)
In an interactive shell, prompt the user to input a number.
Definition: mythmiscutil.cpp:485
MythSplitCommandString
MBASE_PUBLIC QStringList MythSplitCommandString(const QString &line)
Definition: mythmiscutil.cpp:1259
dir
QDir dir
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:1171
kMSNone
@ kMSNone
Definition: mythsystem.h:33
myth_nice
MBASE_PUBLIC bool myth_nice(int val)
Definition: mythmiscutil.cpp:701
MythFormatTimeMs
MBASE_PUBLIC QString MythFormatTimeMs(int msecs, QString fmt)
Format a milliseconds time value.
Definition: mythmiscutil.cpp:1232
copy
MBASE_PUBLIC long long copy(QFile &dst, QFile &src, uint block_size=0)
Copies src file to dst file.
Definition: mythmiscutil.cpp:308
setHttpProxy
MBASE_PUBLIC void setHttpProxy(void)
Get network proxy settings from OS, and use for [Q]Http[Comms].
Definition: mythmiscutil.cpp:851
wrapList
MBASE_PUBLIC void wrapList(QStringList &list, int width)
Definition: mythmiscutil.cpp:960
getUptime
MBASE_PUBLIC bool getUptime(time_t &uptime)
Returns uptime statistics.
Definition: mythmiscutil.cpp:65
sq
float sq(float a)
Definition: mythmiscutil.h:60
query
MSqlQuery query(MSqlQuery::InitCon())
makeFileAccessible
MBASE_PUBLIC bool makeFileAccessible(const QString &filename)
Definition: mythmiscutil.cpp:440