MythTV  master
Macros | Typedefs | Functions
mythmiscutil.h File Reference
#include <algorithm>
#include <array>
#include <cstdint>
#include <ctime>
#include <QString>
#include <QDir>
#include "mythbaseexp.h"
#include "mythsystem.h"
#include <QRandomGenerator>
#include "compat.h"
Include dependency graph for mythmiscutil.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define M_QSTRING_UNICODE(str)   hasUtf8(str) ? QString::fromUtf8(str) : str
 
#define ONESECINMS   ( 1000)
 
#define ONEMININMS   ( 60*1000)
 
#define ONEHOURINMS   ( 60*60*1000)
 
#define ONEDAYINMS   (24*60*60*1000)
 
#define ONEMININSEC   ( 60)
 
#define ONEHOURINSEC   ( 60*60)
 
#define ONEDAYINSEC   (24*60*60)
 

Typedefs

using loadArray = std::array< double, 3 >
 

Functions

MBASE_PUBLIC uint32_t MythRandom ()
 
MBASE_PUBLIC bool getUptime (time_t &uptime)
 Returns uptime statistics. More...
 
MBASE_PUBLIC bool getMemStats (int &totalMB, int &freeMB, int &totalVM, int &freeVM)
 Returns memory statistics in megabytes. More...
 
MBASE_PUBLIC loadArray getLoadAvgs (void)
 Returns the system load averages. More...
 
MBASE_PUBLIC bool hasUtf8 (const char *str)
 Guess whether a string is UTF-8. More...
 
MBASE_PUBLIC bool ping (const QString &host, int timeout)
 Can we ping host within timeout seconds? More...
 
MBASE_PUBLIC bool telnet (const QString &host, int port)
 Can we talk to port on host? More...
 
MBASE_PUBLIC long long copy (QFile &dst, QFile &src, uint block_size=0)
 Copies src file to dst file. More...
 
MBASE_PUBLIC QString createTempFile (QString name_template="/tmp/mythtv_XXXXXX", bool dir=false)
 
MBASE_PUBLIC bool makeFileAccessible (const QString &filename)
 
MBASE_PUBLIC QString getResponse (const QString &query, const QString &def)
 In an interactive shell, prompt the user to input a string. More...
 
MBASE_PUBLIC int intResponse (const QString &query, int def)
 In an interactive shell, prompt the user to input a number. More...
 
MBASE_PUBLIC QString getSymlinkTarget (const QString &start_file, QStringList *intermediaries=nullptr, unsigned maxLinks=255)
 
MBASE_PUBLIC void wrapList (QStringList &list, int width)
 
float clamp (float val, float minimum, float maximum)
 
int clamp (int val, int minimum, int maximum)
 
float lerp (float r, float a, float b)
 
int lerp (float r, int a, int b)
 
float sq (float a)
 
int sq (int a)
 
static QString xml_bool_to_string (bool val)
 
MBASE_PUBLIC QString xml_indent (uint level)
 
MBASE_PUBLIC bool IsMACAddress (const QString &MAC)
 
MBASE_PUBLIC bool WakeOnLAN (const QString &MAC)
 
MBASE_PUBLIC bool MythWakeup (const QString &wakeUpCommand, uint flags=kMSNone, uint timeout=0)
 
MBASE_PUBLIC QString FileHash (const QString &filename)
 
MBASE_PUBLIC bool IsPulseAudioRunning (void)
 Is A/V Sync destruction daemon is running on this host? More...
 
MBASE_PUBLIC bool myth_nice (int val)
 
MBASE_PUBLIC void myth_yield (void)
 
MBASE_PUBLIC bool myth_ioprio (int val)
 range -1..8, smaller is higher priority More...
 
MBASE_PUBLIC bool MythRemoveDirectory (QDir &aDir)
 
MBASE_PUBLIC void setHttpProxy (void)
 Get network proxy settings from OS, and use for [Q]Http[Comms]. More...
 
MBASE_PUBLIC int naturalCompare (const QString &_a, const QString &_b, Qt::CaseSensitivity caseSensitivity=Qt::CaseSensitive)
 
MBASE_PUBLIC QString MythFormatTimeMs (int msecs, const QString &fmt)
 Format a milliseconds time value. More...
 
MBASE_PUBLIC QString MythFormatTime (int secs, const QString &fmt)
 Format a seconds time value. More...
 
void rdtsc (uint64_t &x)
 
MBASE_PUBLIC QStringList MythSplitCommandString (const QString &line)
 

Macro Definition Documentation

◆ M_QSTRING_UNICODE

#define M_QSTRING_UNICODE (   str)    hasUtf8(str) ? QString::fromUtf8(str) : str

Definition at line 40 of file mythmiscutil.h.

◆ ONESECINMS

#define ONESECINMS   ( 1000)

Definition at line 107 of file mythmiscutil.h.

◆ ONEMININMS

#define ONEMININMS   ( 60*1000)

Definition at line 108 of file mythmiscutil.h.

◆ ONEHOURINMS

#define ONEHOURINMS   ( 60*60*1000)

Definition at line 109 of file mythmiscutil.h.

◆ ONEDAYINMS

#define ONEDAYINMS   (24*60*60*1000)

Definition at line 110 of file mythmiscutil.h.

◆ ONEMININSEC

#define ONEMININSEC   ( 60)

Definition at line 111 of file mythmiscutil.h.

◆ ONEHOURINSEC

#define ONEHOURINSEC   ( 60*60)

Definition at line 112 of file mythmiscutil.h.

◆ ONEDAYINSEC

#define ONEDAYINSEC   (24*60*60)

Definition at line 113 of file mythmiscutil.h.

Typedef Documentation

◆ loadArray

using loadArray = std::array<double,3>

Definition at line 36 of file mythmiscutil.h.

Function Documentation

◆ MythRandom()

MBASE_PUBLIC uint32_t MythRandom ( )
inline

◆ getUptime()

MBASE_PUBLIC bool getUptime ( time_t &  uptime)

Returns uptime statistics.

Returns
true if successful, false otherwise.

Definition at line 66 of file mythmiscutil.cpp.

Referenced by StatusBox::doMachineStatus(), MainServer::HandleQueryUptime(), BaseRequestHandler::HandleQueryUptime(), and NetworkControl::processQuery().

◆ getMemStats()

MBASE_PUBLIC bool getMemStats ( int &  totalMB,
int &  freeMB,
int &  totalVM,
int &  freeVM 
)

Returns memory statistics in megabytes.

Todo:
Memory Statistics are not supported (by MythTV) on NT or DOS.
Returns
true if it succeeds, false otherwise.

Definition at line 109 of file mythmiscutil.cpp.

Referenced by StatusBox::doMachineStatus(), MainServer::HandleQueryMemStats(), BaseRequestHandler::HandleQueryMemStats(), and NetworkControl::processQuery().

◆ getLoadAvgs()

MBASE_PUBLIC loadArray getLoadAvgs ( void  )

Returns the system load averages.

Returns
A std::array<double,3> containing the system load averages. If the system call fails or is unsupported, returns array containing all -1.

Definition at line 176 of file mythmiscutil.cpp.

Referenced by StatusBox::doMachineStatus(), HttpStatus::FillStatusXML(), MainServer::HandleQueryLoad(), BaseRequestHandler::HandleQueryLoad(), and NetworkControl::processQuery().

◆ hasUtf8()

MBASE_PUBLIC bool hasUtf8 ( const char *  str)

Guess whether a string is UTF-8.

Note
This does not attempt to validate the whole string. It just checks if it has any UTF-8 sequences in it.

Definition at line 193 of file mythmiscutil.cpp.

◆ ping()

MBASE_PUBLIC bool ping ( const QString &  host,
int  timeout 
)

Can we ping host within timeout seconds?

Different operating systems use different parameters to specify a timeout to the ping command. FreeBSD and derivatives use '-t'; Linux and derivatives use '-w'. Using the right parameter also eliminates the need for the old behavior of falling back to pinging the localhost with and without a timeout, in order to characterize whether the right parameter was used in the first place.

Note
If ping fails to timeout on another supported platform, determine what parameter that platform's ping requires to specify the timeout and add another case to the #ifdef statement.

Definition at line 245 of file mythmiscutil.cpp.

Referenced by CardUtil::IsVBoxPresent(), and setHttpProxy().

◆ telnet()

MBASE_PUBLIC bool telnet ( const QString &  host,
int  port 
)

Can we talk to port on host?

Definition at line 276 of file mythmiscutil.cpp.

Referenced by setHttpProxy().

◆ copy()

MBASE_PUBLIC long long copy ( QFile &  dst,
QFile &  src,
uint  block_size 
)

Copies src file to dst file.

If the dst file is open, it must be open for writing. If the src file is open, if must be open for reading.

The files will be in the same open or close state after this function runs as they were prior to this function being called.

This function does not care if the files are actual files. For compatibility with pipes and socket streams the file location will not be reset to 0 at the end of this function. If the function is successful the file pointers will be at the end of the copied data.

Parameters
dstDestination QFile
srcSource QFile
block_sizeOptional block size in bytes, must be at least 1024, otherwise the default of 16 KB will be used.
Returns
bytes copied on success, -1 on failure.

Definition at line 307 of file mythmiscutil.cpp.

Referenced by TeletextReader::AddPageHeader(), TeletextReader::AddTeletextData(), RTjpeg::bcomp(), GalleryThumbView::Copy(), ImportMusicDialog::copyFile(), MythVideoOutput::CopyFrame(), mythburn::copyRemote(), ProgramAssociationTable::CreateBlank(), ProgramMapTable::CreateBlank(), MythRAOPConnection::decodeAudioPacket(), mythburn::doProcessFile(), mythburn::doProcessFileProjectX(), MythRenderOpenGL::DrawBitmap(), dvb_decode_text(), NuppelVideoRecorder::FormatTT(), Jitterometer::GetCPUStat(), mythburn::getFileInformation(), nv_python_libs.common.common_api.Common::getUrlData(), HLSStream::HLSStream(), TSPacket::InitPayload(), ImportRecorder::Open(), PList::ParseBinaryDate(), PList::ParseBinaryReal(), mythburn::preProcessFile(), MythRAOPConnection::ProcessRequest(), AudioOutputNULL::readOutputData(), MythDVDBuffer::SafeRead(), DiSEqCDevTree::SendCommand(), nv_python_libs.xsltfunctions.traileraddicts_api.xpathFunctions::traileraddictsLinkGenerationMovie(), ThumbFinder::updateThumb(), and NuppelVideoRecorder::WriteFileHeader().

◆ createTempFile()

MBASE_PUBLIC QString createTempFile ( QString  name_template = "/tmp/mythtv_XXXXXX",
bool  dir = false 
)

Definition at line 367 of file mythmiscutil.cpp.

Referenced by DBUtil::CreateTemporaryDBConf(), and FillData::GrabData().

◆ makeFileAccessible()

MBASE_PUBLIC bool makeFileAccessible ( const QString &  filename)

Definition at line 439 of file mythmiscutil.cpp.

Referenced by Content::GetPreviewImage(), and PreviewGenerator::SavePreview().

◆ getResponse()

MBASE_PUBLIC QString getResponse ( const QString &  query,
const QString &  def 
)

In an interactive shell, prompt the user to input a string.

Definition at line 454 of file mythmiscutil.cpp.

Referenced by intResponse(), ChannelData::promptForChannelUpdates(), and MythContextPrivate::PromptForDatabaseParams().

◆ intResponse()

MBASE_PUBLIC int intResponse ( const QString &  query,
int  def 
)

In an interactive shell, prompt the user to input a number.

Definition at line 484 of file mythmiscutil.cpp.

Referenced by MythContextPrivate::PromptForDatabaseParams().

◆ getSymlinkTarget()

MBASE_PUBLIC QString getSymlinkTarget ( const QString &  start_file,
QStringList *  intermediaries = nullptr,
unsigned  maxLinks = 255 
)

◆ wrapList()

MBASE_PUBLIC void wrapList ( QStringList &  list,
int  width 
)

◆ clamp() [1/2]

float clamp ( float  val,
float  minimum,
float  maximum 
)
inline

◆ clamp() [2/2]

int clamp ( int  val,
int  minimum,
int  maximum 
)
inline

Definition at line 63 of file mythmiscutil.h.

◆ lerp() [1/2]

float lerp ( float  r,
float  a,
float  b 
)
inline

Definition at line 67 of file mythmiscutil.h.

Referenced by lerp().

◆ lerp() [2/2]

int lerp ( float  r,
int  a,
int  b 
)
inline

Definition at line 71 of file mythmiscutil.h.

◆ sq() [1/2]

float sq ( float  a)
inline

Definition at line 75 of file mythmiscutil.h.

◆ sq() [2/2]

int sq ( int  a)
inline

Definition at line 76 of file mythmiscutil.h.

◆ xml_bool_to_string()

static QString xml_bool_to_string ( bool  val)
inlinestatic

◆ xml_indent()

MBASE_PUBLIC QString xml_indent ( uint  level)

◆ IsMACAddress()

MBASE_PUBLIC bool IsMACAddress ( const QString &  MAC)

Definition at line 547 of file mythmiscutil.cpp.

Referenced by MythWakeup(), and Scheduler::WakeUpSlave().

◆ WakeOnLAN()

MBASE_PUBLIC bool WakeOnLAN ( const QString &  MAC)

Definition at line 633 of file mythmiscutil.cpp.

Referenced by MythWakeup(), and Scheduler::WakeUpSlave().

◆ MythWakeup()

MBASE_PUBLIC bool MythWakeup ( const QString &  wakeUpCommand,
uint  flags = kMSNone,
uint  timeout = 0 
)

◆ FileHash()

MBASE_PUBLIC QString FileHash ( const QString &  filename)

◆ IsPulseAudioRunning()

MBASE_PUBLIC bool IsPulseAudioRunning ( void  )

Is A/V Sync destruction daemon is running on this host?

Definition at line 682 of file mythmiscutil.cpp.

Referenced by AudioOutput::OpenAudio(), and PulseHandler::Suspend().

◆ myth_nice()

MBASE_PUBLIC bool myth_nice ( int  val)

◆ myth_yield()

MBASE_PUBLIC void myth_yield ( void  )

Definition at line 714 of file mythmiscutil.cpp.

◆ myth_ioprio()

MBASE_PUBLIC bool myth_ioprio ( int  )

range -1..8, smaller is higher priority

range -1..8, smaller is higher priority

As of November 14th, 2010 this only works on Linux when using the CFQ I/O Scheduler. The range is -1 to 8, with -1 being real-time priority 0 through 7 being standard best-time priorities and 8 being the idle priority. The deadline and noop I/O Schedulers will ignore this but are much less likely to starve video playback to feed the transcoder or flagger. (noop is only recommended for SSDs.)

Since a process needs to have elevated priviledges to use either the real-time or idle priority this will try priorities 0 or 7 respectively if -1 or 8 do not work. It will not report an error on these conditions as they will be the common case.

Only Linux on i386, ppc, x86_64 and ia64 are currently supported. This is a no-op on all other architectures and platforms.

Definition at line 802 of file mythmiscutil.cpp.

Referenced by JobQueue::DoTranscodeThread(), JobQueue::DoUserJobThread(), MythSystemLegacyUnix::Fork(), and main().

◆ MythRemoveDirectory()

MBASE_PUBLIC bool MythRemoveDirectory ( QDir &  aDir)

◆ setHttpProxy()

MBASE_PUBLIC void setHttpProxy ( void  )

Get network proxy settings from OS, and use for [Q]Http[Comms].

The HTTP_PROXY environment var. is parsed for values like; "proxy-host", "proxy-host:8080", "http://host:8080" and "http"//user:password@host:1080", and that is used for any Qt-based Http fetches. We also test connectivity here with ping and telnet, and warn if it fails.

If there is was no env. var, we use Qt to get proxy settings from the OS, and search through them for a proxy server we can connect to.

Definition at line 850 of file mythmiscutil.cpp.

Referenced by main().

◆ naturalCompare()

MBASE_PUBLIC int naturalCompare ( const QString &  _a,
const QString &  _b,
Qt::CaseSensitivity  caseSensitivity = Qt::CaseSensitive 
)

◆ MythFormatTimeMs()

MBASE_PUBLIC QString MythFormatTimeMs ( int  msecs,
const QString &  fmt 
)

Format a milliseconds time value.

Convert a millisecond time value into a textual representation of the value.

Parameters
msecsThe time value in milliseconds.
fmtA formatting string specifying how to output the time. See QTime::toString for the a definition fo valid formatting characters.

Definition at line 1231 of file mythmiscutil.cpp.

Referenced by DeleteMap::CreateTimeString(), LyricsLine::formatTime(), commDetector2::frameToTimestamp(), commDetector2::frameToTimestampms(), AvFormatDecoder::OpenFile(), MusicMetadata::toMap(), and MythBDBuffer::UpdateTitleInfo().

◆ MythFormatTime()

MBASE_PUBLIC QString MythFormatTime ( int  secs,
const QString &  fmt 
)

Format a seconds time value.

Convert a second time value into a textual representation of the value.

Parameters
secsThe time value in seconds.
mftA formatting string specifying how to output the time. See QTime::toString for the a definition fo valid formatting characters.

Definition at line 1236 of file mythmiscutil.cpp.

Referenced by CommBreakMap::AutoCommercialSkip(), MythPlayer::calcSliderPos(), CommBreakMap::DoSkipCommercials(), ThumbFinder::frameToTime(), MusicCommon::getTimeString(), MythBDPlayer::GetTitleName(), TV::MenuItemDisplayPlayback(), TV::ShowLCDDVDInfo(), and uptimeStr().

◆ rdtsc()

void rdtsc ( uint64_t &  x)
inline

Definition at line 145 of file mythmiscutil.h.

◆ MythSplitCommandString()

MBASE_PUBLIC QStringList MythSplitCommandString ( const QString &  line)