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 <cstdint>
6 #include <ctime>
7 
8 #include <QString>
9 #include <QDir>
10 
11 #include "mythbaseexp.h"
12 #include "mythsystem.h"
13 
14 class QStringList;
15 class QFile;
16 
17 MBASE_PUBLIC bool getUptime(time_t &uptime);
19  int &totalMB, int &freeMB, int &totalVM, int &freeVM);
20 
21 MBASE_PUBLIC bool hasUtf8(const char *str);
22 #define M_QSTRING_UNICODE(str) hasUtf8(str) ? QString::fromUtf8(str) : str
23 
24 MBASE_PUBLIC bool ping(const QString &host, int timeout);
25 MBASE_PUBLIC bool telnet(const QString &host, int port);
26 
27 MBASE_PUBLIC long long copy(QFile &dst, QFile &src, uint block_size = 0);
29  QString name_template = "/tmp/mythtv_XXXXXX", bool dir = false);
30 MBASE_PUBLIC bool makeFileAccessible(const QString& filename);
31 
32 MBASE_PUBLIC QString getResponse(const QString &query, const QString &def);
33 MBASE_PUBLIC int intResponse(const QString &query, int def);
34 
35 MBASE_PUBLIC QString getSymlinkTarget(const QString &start_file,
36  QStringList *intermediaries = nullptr,
37  unsigned maxLinks = 255);
38 
39 MBASE_PUBLIC void wrapList(QStringList &list, int width);
40 
41 inline float clamp(float val, float minimum, float maximum)
42 {
43  return std::min(std::max(val, minimum), maximum);
44 }
45 inline int clamp(int val, int minimum, int maximum)
46 {
47  return std::min(std::max(val, minimum), maximum);
48 }
49 inline float lerp(float r, float a, float b)
50 {
51  return ((1.0F - r) * a) + (r * b);
52 }
53 inline int lerp(float r, int a, int b)
54 {
55  return (int) lerp(r, (float) a, (float) b);
56 }
57 inline float sq(float a) { return a*a; }
58 inline int sq(int a) { return a*a; }
59 
60 static inline QString xml_bool_to_string(bool val)
61 {
62  return (val) ? "true" : "false";
63 }
64 
65 MBASE_PUBLIC QString xml_indent(uint level);
66 
67 MBASE_PUBLIC bool IsMACAddress(const QString& MAC);
68 MBASE_PUBLIC bool WakeOnLAN(const QString& MAC);
69 MBASE_PUBLIC bool MythWakeup(const QString &wakeUpCommand,
70  uint flags = kMSNone, uint timeout = 0);
71 
72 MBASE_PUBLIC QString FileHash(const QString& filename);
73 
76 
77 MBASE_PUBLIC bool myth_nice(int val);
78 MBASE_PUBLIC void myth_yield(void);
80 MBASE_PUBLIC bool myth_ioprio(int val);
81 
82 MBASE_PUBLIC bool MythRemoveDirectory(QDir &aDir);
83 
84 MBASE_PUBLIC void setHttpProxy(void);
85 
86 MBASE_PUBLIC int naturalCompare(const QString &_a, const QString &_b,
87  Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive);
88 
89 // CPU Tick timing function
90 #ifdef MMX
91 #ifdef _WIN32
92 #include "compat.h"
93 inline void rdtsc(uint64_t &x)
94 {
95  QueryPerformanceCounter((LARGE_INTEGER*)(&x));
96 }
97 #else
98 typedef struct {
99  uint a;
100  uint b;
101 } timing_ab_t;
102 inline void rdtsc(uint64_t &x)
103 {
104  timing_ab_t &y = (timing_ab_t&) x;
105  asm("rdtsc \n"
106  "mov %%eax, %0 \n"
107  "mov %%edx, %1 \n"
108  :
109  : "m"(y.a), "m"(y.b)
110  : "%eax", "%edx");
111 }
112 #endif
113 
114 #else // if !MMX
115 inline void rdtsc(uint64_t &x) { x = 0ULL; }
116 #endif // !MMX
117 
118 #endif // MYTHMISCUTIL_H_
MBASE_PUBLIC bool IsPulseAudioRunning(void)
Is A/V Sync destruction daemon is running on this host?
MBASE_PUBLIC long long copy(QFile &dst, QFile &src, uint block_size=0)
Copies src file to dst file.
float sq(float a)
Definition: mythmiscutil.h:57
MBASE_PUBLIC bool ping(const QString &host, int timeout)
Can we ping host within timeout seconds?
unsigned int uint
Definition: compat.h:140
MBASE_PUBLIC bool hasUtf8(const char *str)
Guess whether a string is UTF-8.
MBASE_PUBLIC void myth_yield(void)
MBASE_PUBLIC bool myth_nice(int val)
MBASE_PUBLIC void setHttpProxy(void)
Get network proxy settings from OS, and use for [Q]Http[Comms].
MBASE_PUBLIC bool makeFileAccessible(const QString &filename)
unsigned char r
Definition: ParseText.cpp:329
void rdtsc(uint64_t &x)
Definition: mythmiscutil.h:93
unsigned char b
Definition: ParseText.cpp:329
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
MBASE_PUBLIC QString FileHash(const QString &filename)
MBASE_PUBLIC bool MythWakeup(const QString &wakeUpCommand, uint flags=kMSNone, uint timeout=0)
MBASE_PUBLIC int intResponse(const QString &query, int def)
In an interactive shell, prompt the user to input a number.
unsigned int block_size
MBASE_PUBLIC QString createTempFile(QString name_template="/tmp/mythtv_XXXXXX", bool dir=false)
float clamp(float val, float minimum, float maximum)
Definition: mythmiscutil.h:41
MBASE_PUBLIC bool telnet(const QString &host, int port)
Can we talk to port on host?
MBASE_PUBLIC void wrapList(QStringList &list, int width)
float lerp(float r, float a, float b)
Definition: mythmiscutil.h:49
MBASE_PUBLIC QString getSymlinkTarget(const QString &start_file, QStringList *intermediaries=nullptr, unsigned maxLinks=255)
MBASE_PUBLIC bool getMemStats(int &totalMB, int &freeMB, int &totalVM, int &freeVM)
Returns memory statistics in megabytes.
MBASE_PUBLIC bool myth_ioprio(int val)
range -1..8, smaller is higher priority
static QString xml_bool_to_string(bool val)
Definition: mythmiscutil.h:60
MBASE_PUBLIC bool getUptime(time_t &uptime)
Returns uptime statistics.
MBASE_PUBLIC QString xml_indent(uint level)
MBASE_PUBLIC QString getResponse(const QString &query, const QString &def)
In an interactive shell, prompt the user to input a string.
MBASE_PUBLIC bool MythRemoveDirectory(QDir &aDir)
MBASE_PUBLIC bool IsMACAddress(const QString &MAC)
MBASE_PUBLIC int naturalCompare(const QString &_a, const QString &_b, Qt::CaseSensitivity caseSensitivity=Qt::CaseSensitive)
MBASE_PUBLIC bool WakeOnLAN(const QString &MAC)