vdr 2.7.7
tools.c File Reference
#include "tools.h"
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <jpeglib.h>
#include <locale.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/vfs.h>
#include <time.h>
#include <unistd.h>
#include <utime.h>
#include "i18n.h"
#include "thread.h"

Go to the source code of this file.

Classes

struct  tJpegCompressData

Macros

#define MAXSYSLOGBUF   256
#define DECIMAL_POINT_C   '.'
#define MT(s, m, v)
#define JPEGCOMPRESSMEM   500000
#define USE_FADVISE_READ   0
#define USE_FADVISE_WRITE   1
#define WRITE_BUFFER   KILOBYTE(800)
#define FADVGRAN   KILOBYTE(4)
#define READCHUNK   MEGABYTE(8)
#define LOCKFILENAME   ".lock-vdr"
#define LOCKFILESTALETIME   600
#define LIST_GARBAGE_COLLECTOR_TIMEOUT   5

Functions

void syslog_with_tid (int priority, const char *format,...)
int BCD2INT (int x)
ssize_t safe_read (int filedes, void *buffer, size_t size)
ssize_t safe_write (int filedes, const void *buffer, size_t size)
void writechar (int filedes, char c)
int WriteAllOrNothing (int fd, const uchar *Data, int Length, int TimeoutMs, int RetryMs)
char * strcpyrealloc (char *dest, const char *src)
char * strn0cpy (char *dest, const char *src, size_t n)
char * strreplace (char *s, char c1, char c2)
char * strreplace (char *s, const char *s1, const char *s2)
const char * strchrn (const char *s, char c, size_t n)
int strcountchr (const char *s, char c)
cString strgetbefore (const char *s, char c, int n)
const char * strgetlast (const char *s, char c)
char * stripspace (char *s)
char * compactspace (char *s)
char * compactchars (char *s, char c)
cString strescape (const char *s, const char *chars)
cString strgetval (const char *s, const char *name, char d)
char * strshift (char *s, int n)
bool startswith (const char *s, const char *p)
bool endswith (const char *s, const char *p)
bool isempty (const char *s)
int numdigits (int n)
bool isnumber (const char *s)
int64_t StrToNum (const char *s)
bool StrInArray (const char *a[], const char *s)
cString AddDirectory (const char *DirName, const char *FileName)
double atod (const char *s)
cString dtoa (double d, const char *Format)
cString itoa (int n)
bool EntriesOnSameFileSystem (const char *File1, const char *File2)
int FreeDiskSpaceMB (const char *Directory, int *UsedMB)
bool DirectoryOk (const char *DirName, bool LogErrors)
bool MakeDirs (const char *FileName, bool IsDirectory)
bool RemoveFileOrDir (const char *FileName, bool FollowSymlinks)
bool RemoveEmptyDirectories (const char *DirName, bool RemoveThis, const char *IgnoreFiles[])
int DirSizeMB (const char *DirName)
char * ReadLink (const char *FileName)
bool SpinUpDisk (const char *FileName)
void TouchFile (const char *FileName, bool Create)
time_t LastModifiedTime (const char *FileName)
off_t FileSize (const char *FileName)
int Utf8CharLen (const char *s)
uint Utf8CharGet (const char *s, int Length)
int Utf8CharSet (uint c, char *s)
int Utf8SymChars (const char *s, int Symbols)
int Utf8StrLen (const char *s)
char * Utf8Strn0Cpy (char *Dest, const char *Src, int n)
int Utf8ToArray (const char *s, uint *a, int Size)
int Utf8FromArray (const uint *a, char *s, int Size, int Max)
cString WeekDayName (int WeekDay)
cString WeekDayName (time_t t)
cString WeekDayNameFull (int WeekDay)
cString WeekDayNameFull (time_t t)
cString DayDateTime (time_t t)
cString TimeToString (time_t t)
cString DateString (time_t t)
cString ShortDateString (time_t t)
cString TimeString (time_t t)
static void JpegCompressInitDestination (j_compress_ptr cinfo)
static boolean JpegCompressEmptyOutputBuffer (j_compress_ptr cinfo)
static void JpegCompressTermDestination (j_compress_ptr cinfo)
ucharRgbToJpeg (uchar *Mem, int Width, int Height, int &Size, int Quality)
const char * GetHostName (void)
static int CompareListObjects (const void *a, const void *b)

Variables

int SysLogLevel = 3
static uint SystemToUtf8 [128] = { 0 }
cListGarbageCollector ListGarbageCollector

Macro Definition Documentation

◆ DECIMAL_POINT_C

#define DECIMAL_POINT_C   '.'

Definition at line 417 of file tools.c.

Referenced by atod(), and dtoa().

◆ FADVGRAN

#define FADVGRAN   KILOBYTE(4)

Definition at line 1856 of file tools.c.

Referenced by cUnbufferedFile::FadviseDrop().

◆ JPEGCOMPRESSMEM

#define JPEGCOMPRESSMEM   500000

Definition at line 1311 of file tools.c.

◆ LIST_GARBAGE_COLLECTOR_TIMEOUT

#define LIST_GARBAGE_COLLECTOR_TIMEOUT   5

Definition at line 2111 of file tools.c.

Referenced by cListGarbageCollector::Purge().

◆ LOCKFILENAME

#define LOCKFILENAME   ".lock-vdr"

Definition at line 1999 of file tools.c.

Referenced by cLockFile::cLockFile().

◆ LOCKFILESTALETIME

#define LOCKFILESTALETIME   600

Definition at line 2000 of file tools.c.

Referenced by cLockFile::Lock().

◆ MAXSYSLOGBUF

#define MAXSYSLOGBUF   256

Definition at line 33 of file tools.c.

Referenced by syslog_with_tid().

◆ MT

#define MT ( s,
m,
v )
Value:
((*(s) & (m)) == (v))

Referenced by Utf8CharLen().

◆ READCHUNK

#define READCHUNK   MEGABYTE(8)

Definition at line 1857 of file tools.c.

Referenced by cUnbufferedFile::Read().

◆ USE_FADVISE_READ

#define USE_FADVISE_READ   0

Definition at line 1799 of file tools.c.

◆ USE_FADVISE_WRITE

#define USE_FADVISE_WRITE   1

Definition at line 1802 of file tools.c.

◆ WRITE_BUFFER

#define WRITE_BUFFER   KILOBYTE(800)

Definition at line 1805 of file tools.c.

Referenced by cUnbufferedFile::Write().

Function Documentation

◆ AddDirectory()

◆ atod()

double atod ( const char * s)

Converts the given string, which is a floating point number using a '.

' as the decimal point, to a double value, independent of the currently selected locale.

Definition at line 419 of file tools.c.

References DECIMAL_POINT_C.

Referenced by cSetup::Parse(), and cRecordingInfo::Read().

◆ BCD2INT()

int BCD2INT ( int x)

Definition at line 45 of file tools.c.

References BCDCHARTOINT.

Referenced by cNitFilter::Process().

◆ compactchars()

char * compactchars ( char * s,
char c )

removes all occurrences of 'c' from the beginning an end of 's' and replaces sequences of multiple 'c's with a single 'c'.

Definition at line 256 of file tools.c.

Referenced by cString::CompactChars().

◆ compactspace()

char * compactspace ( char * s)

◆ CompareListObjects()

int CompareListObjects ( const void * a,
const void * b )
static

Definition at line 2294 of file tools.c.

References cListObject::Compare().

Referenced by cListBase::Sort().

◆ DateString()

cString DateString ( time_t t)

◆ DayDateTime()

◆ DirectoryOk()

bool DirectoryOk ( const char * DirName,
bool LogErrors )

Definition at line 489 of file tools.c.

References esyslog, and LOG_ERROR_STR.

Referenced by cDirCopier::Action(), cLockFile::cLockFile(), CutRecording(), GenerateIndex(), main(), and SpinUpDisk().

◆ DirSizeMB()

int DirSizeMB ( const char * DirName)

returns the total size of the files in the given directory, or -1 in case of an error

Definition at line 647 of file tools.c.

References AddDirectory(), DirSizeMB(), LOG_ERROR_STR, MEGABYTE, cReadDir::Next(), and cReadDir::Ok().

Referenced by DirSizeMB(), EnoughFreeDiskSpaceForEdit(), cRecording::FileSizeMB(), FileSizeMBafterEdit(), and cRecordingsHandler::GetRequiredDiskSpaceMB().

◆ dtoa()

cString dtoa ( double d,
const char * Format = "%f" )

Converts the given double value to a string, making sure it uses a '.

' as the decimal point, independent of the currently selected locale. If Format is given, it will be used instead of the default.

Definition at line 440 of file tools.c.

References DECIMAL_POINT_C, and strreplace().

Referenced by cRecordingInfo::FrameParams(), cSetup::Store(), and cRecordingInfo::Write().

◆ endswith()

◆ EntriesOnSameFileSystem()

bool EntriesOnSameFileSystem ( const char * File1,
const char * File2 )

Checks whether the given files are on the same file system.

If either of the files doesn't exist, this function returns true to avoid any actions that might be triggered if files are on different file system.

Definition at line 457 of file tools.c.

References LOG_ERROR_STR.

Referenced by cVideoDirectory::Contains(), cRecordingsHandler::GetRequiredDiskSpaceMB(), and cVideoDirectory::Move().

◆ FileSize()

off_t FileSize ( const char * FileName)

returns the size of the given file, or -1 in case of an error (e.g. if the file doesn't exist)

Definition at line 747 of file tools.c.

Referenced by cDirCopier::Action(), cIndexFileGenerator::Action(), and cIndexFile::cIndexFile().

◆ FreeDiskSpaceMB()

int FreeDiskSpaceMB ( const char * Directory,
int * UsedMB )

Definition at line 472 of file tools.c.

References LOG_ERROR_STR.

Referenced by cVideoDirectory::FreeMB(), and cRecorder::RunningLowOnDiskSpace().

◆ GetHostName()

const char * GetHostName ( void )

Gets the host name of this machine.

Definition at line 1409 of file tools.c.

References LOG_ERROR.

Referenced by cSetup::cSetup(), and cSetup::Save().

◆ isempty()

◆ isnumber()

◆ itoa()

◆ JpegCompressEmptyOutputBuffer()

boolean JpegCompressEmptyOutputBuffer ( j_compress_ptr cinfo)
static

Definition at line 1327 of file tools.c.

References esyslog, JPEGCOMPRESSMEM, tJpegCompressData::mem, and tJpegCompressData::size.

◆ JpegCompressInitDestination()

void JpegCompressInitDestination ( j_compress_ptr cinfo)
static

Definition at line 1318 of file tools.c.

References JPEGCOMPRESSMEM, MALLOC, tJpegCompressData::mem, and tJpegCompressData::size.

◆ JpegCompressTermDestination()

void JpegCompressTermDestination ( j_compress_ptr cinfo)
static

Definition at line 1350 of file tools.c.

References esyslog, tJpegCompressData::mem, and tJpegCompressData::size.

◆ LastModifiedTime()

time_t LastModifiedTime ( const char * FileName)

◆ MakeDirs()

◆ numdigits()

int numdigits ( int n)

Definition at line 362 of file tools.c.

◆ ReadLink()

char * ReadLink ( const char * FileName)

returns a new string allocated on the heap, which the caller must delete (or NULL in case of an error)

Definition at line 679 of file tools.c.

References LOG_ERROR_STR.

Referenced by cSafeFile::cSafeFile().

◆ RemoveEmptyDirectories()

bool RemoveEmptyDirectories ( const char * DirName,
bool RemoveThis = false,
const char * IgnoreFiles[] = NULL )

Removes all empty directories under the given directory DirName.

If RemoveThis is true, DirName will also be removed if it is empty. IgnoreFiles can be set to an array of file names that will be ignored when considering whether a directory is empty. If IgnoreFiles is given, the array must end with a NULL pointer.

Definition at line 593 of file tools.c.

References AddDirectory(), dsyslog, LOG_ERROR_STR, cReadDir::Next(), cReadDir::Ok(), RemoveEmptyDirectories(), and StrInArray().

Referenced by cVideoDirectory::Cleanup(), and RemoveEmptyDirectories().

◆ RemoveFileOrDir()

bool RemoveFileOrDir ( const char * FileName,
bool FollowSymlinks )

Definition at line 535 of file tools.c.

References AddDirectory(), dsyslog, LOG_ERROR_STR, MALLOC, cReadDir::Next(), and cReadDir::Ok().

Referenced by cVideoDirectory::Remove().

◆ RgbToJpeg()

uchar * RgbToJpeg ( uchar * Mem,
int Width,
int Height,
int & Size,
int Quality = 100 )

Converts the given Memory to a JPEG image and returns a pointer to the resulting image.

Mem must point to a data block of exactly (Width * Height) triplets of RGB image data bytes. Upon return, Size will hold the number of bytes of the resulting JPEG data. Quality can be in the range 0..100 and controls the quality of the resulting image, where 100 is "best". The caller takes ownership of the result and has to delete it once it is no longer needed. The result may be NULL in case of an error.

Definition at line 1366 of file tools.c.

References JpegCompressEmptyOutputBuffer(), JpegCompressInitDestination(), JpegCompressTermDestination(), tJpegCompressData::mem, and tJpegCompressData::size.

Referenced by cDvbSdFfDevice::GrabImage(), and cSubtitleDebug::WriteJpeg().

◆ safe_read()

◆ safe_write()

ssize_t safe_write ( int filedes,
const void * buffer,
size_t size )

◆ ShortDateString()

cString ShortDateString ( time_t t)

Converts the given time to a string of the form "dd.mm.yy".

Definition at line 1292 of file tools.c.

Referenced by cSkinLCARSDisplayMenu::DrawPlay(), and cSkinLCARSDisplayReplay::SetRecording().

◆ SpinUpDisk()

bool SpinUpDisk ( const char * FileName)

Definition at line 693 of file tools.c.

References DirectoryOk(), dsyslog, esyslog, LOG_ERROR_STR, and cString::sprintf().

Referenced by cRecorder::cRecorder().

◆ startswith()

bool startswith ( const char * s,
const char * p )

◆ strchrn()

const char * strchrn ( const char * s,
char c,
size_t n )

returns a pointer to the n'th occurrence (counting from 1) of c in s, or NULL if no such character was found. If n is 0, s is returned.

Definition at line 186 of file tools.c.

Referenced by cMenuRecordings::Info().

◆ strcountchr()

int strcountchr ( const char * s,
char c )

returns the number of occurrences of 'c' in 's'.

Definition at line 199 of file tools.c.

Referenced by cMenuRecordings::cMenuRecordings(), and cMenuRecordings::Open().

◆ strcpyrealloc()

◆ strescape()

cString strescape ( const char * s,
const char * chars )

◆ strgetbefore()

cString strgetbefore ( const char * s,
char c,
int n )

Definition at line 211 of file tools.c.

Referenced by cVideoDirectory::Move().

◆ strgetlast()

◆ strgetval()

cString strgetval ( const char * s,
const char * name,
char d = '=' )

Returns the value part of a 'name=value' pair in s.

name must either be at the beginning of s, or has to be preceded by white space. There may be any number of white space around the '=' sign. The value is everything up to (and excluding) the next white space, or the end of s. If an other delimiter shall be used (like, e.g., ':'), it can be given as the third parameter. If name occurs more than once in s, only the first occurrence is taken.

Definition at line 303 of file tools.c.

References skipspace().

Referenced by cSVDRPServerParams::cSVDRPServerParams(), and cSocket::Discover().

◆ StrInArray()

bool StrInArray ( const char * a[],
const char * s )

Returns true if the string s is equal to one of the strings pointed to by the (NULL terminated) array a.

Definition at line 398 of file tools.c.

Referenced by RemoveEmptyDirectories().

◆ stripspace()

◆ strn0cpy()

◆ strreplace() [1/2]

◆ strreplace() [2/2]

char * strreplace ( char * s,
const char * s1,
const char * s2 )

re-allocates 's' and deletes the original string if necessary!

Definition at line 155 of file tools.c.

References esyslog.

◆ strshift()

char * strshift ( char * s,
int n )

Shifts the given string to the left by the given number of bytes, thus removing the first n bytes from s.

If n is greater than the length of s, the resulting string will be empty. If n is <= 0 s will be unchanged. Returns s.

Definition at line 325 of file tools.c.

Referenced by cSVDRPClient::GetRemoteTimers().

◆ StrToNum()

int64_t StrToNum ( const char * s)

Converts the given string to a number.

The numerical part of the string may be followed by one of the letters K, M, G or T to abbreviate Kilo-, Mega-, Giga- or Terabyte, respectively (based on 1024). Everything after the first non-numeric character is silently ignored, as are any characters other than the ones mentioned here.

Definition at line 383 of file tools.c.

Referenced by main().

◆ syslog_with_tid()

void syslog_with_tid ( int priority,
const char * format,
... )

Definition at line 35 of file tools.c.

References MAXSYSLOGBUF, and cThread::ThreadId().

◆ TimeString()

◆ TimeToString()

cString TimeToString ( time_t t)

Converts the given time to a string of the form "www mmm dd hh:mm:ss yyyy".

Definition at line 1271 of file tools.c.

Referenced by cSVDRPServer::CmdNEXT(), cSVDRPServer::cSVDRPServer(), cTDT::cTDT(), cShutdownHandler::DoShutdown(), cFrameChecker::Report(), and cTsChecker::Report().

◆ TouchFile()

void TouchFile ( const char * FileName,
bool Create )

Definition at line 725 of file tools.c.

References isyslog, and LOG_ERROR_STR.

Referenced by cRecordings::TouchUpdate().

◆ Utf8CharGet()

uint Utf8CharGet ( const char * s,
int Length = 0 )

Returns the UTF-8 symbol at the beginning of the given string.

Length can be given from a previous call to Utf8CharLen() to avoid calculating it again. If no Length is given, Utf8CharLen() will be called.

Definition at line 841 of file tools.c.

References cCharSetConv::SystemCharacterTable(), SystemToUtf8, and Utf8CharLen().

Referenced by cFreetypeFont::DrawText(), cFreetypeFont::DrawText(), cHdffOsd::DrawText(), cKbdRemote::ReadKeySequence(), cTextWrapper::Set(), cCharSetConv::SetSystemCharacterTable(), Utf8ToArray(), and cFreetypeFont::Width().

◆ Utf8CharLen()

int Utf8CharLen ( const char * s)

◆ Utf8CharSet()

int Utf8CharSet ( uint c,
char * s = NULL )

Converts the given UTF-8 symbol to a sequence of character bytes and copies them to the given string.

Returns the number of bytes written. If no string is given, only the number of bytes is returned and nothing is copied.

Definition at line 856 of file tools.c.

References cCharSetConv::SystemCharacterTable().

Referenced by Utf8FromArray().

◆ Utf8FromArray()

int Utf8FromArray ( const uint * a,
char * s,
int Size,
int Max = -1 )

Converts the given array of UTF-8 symbols (including the terminating 0) into a sequence of character bytes of at most Size length.

Returns the number of character bytes written (without the terminating 0). If Max is given, only that many symbols will be converted. The resulting string is always zero-terminated if Size is big enough.

Definition at line 956 of file tools.c.

References cCharSetConv::SystemCharacterTable(), and Utf8CharSet().

Referenced by cMenuEditStrItem::LeaveEditMode(), and cMenuEditStrItem::Set().

◆ Utf8StrLen()

◆ Utf8Strn0Cpy()

char * Utf8Strn0Cpy ( char * Dest,
const char * Src,
int n )

Copies at most n character bytes from Src to Dest, making sure that the resulting copy ends with a complete UTF-8 symbol.

The copy is guaranteed to be zero terminated. Returns a pointer to Dest.

Definition at line 915 of file tools.c.

References strn0cpy(), cCharSetConv::SystemCharacterTable(), and Utf8CharLen().

Referenced by cTimer::cTimer(), cSetup::Parse(), cTimer::Parse(), cMenuSetupOSD::ProcessKey(), cDevice::SetAvailableTrack(), cTimer::SetFile(), and cTimer::SetPattern().

◆ Utf8SymChars()

int Utf8SymChars ( const char * s,
int Symbols )

Returns the number of character bytes at the beginning of the given string that form at most the given number of UTF-8 symbols.

Definition at line 890 of file tools.c.

References cCharSetConv::SystemCharacterTable(), and Utf8CharLen().

Referenced by cMenuScheduleItem::Update(), and WeekDayName().

◆ Utf8ToArray()

int Utf8ToArray ( const char * s,
uint * a,
int Size )

Converts the given character bytes (including the terminating 0) into an array of UTF-8 symbols of the given Size.

Returns the number of symbols in the array (without the terminating 0).

Definition at line 938 of file tools.c.

References cCharSetConv::SystemCharacterTable(), Utf8CharGet(), and Utf8CharLen().

Referenced by cMenuEditStrItem::EnterEditMode().

◆ WeekDayName() [1/2]

cString WeekDayName ( int WeekDay)

Converts the given WeekDay (0=Sunday, 1=Monday, ...) to a three letter day name.

Definition at line 1218 of file tools.c.

References min(), strn0cpy(), tr, and Utf8SymChars().

Referenced by DateString(), DayDateTime(), cSkinLCARSDisplayMenu::DrawTimer(), cMenuEditDateItem::Set(), cMenuTimerItem::Set(), and WeekDayName().

◆ WeekDayName() [2/2]

cString WeekDayName ( time_t t)

Converts the week day of the given time to a three letter day name.

Definition at line 1233 of file tools.c.

References WeekDayName().

◆ WeekDayNameFull() [1/2]

cString WeekDayNameFull ( int WeekDay)

Converts the given WeekDay (0=Sunday, 1=Monday, ...) to a full day name.

Definition at line 1239 of file tools.c.

References tr.

Referenced by WeekDayNameFull().

◆ WeekDayNameFull() [2/2]

cString WeekDayNameFull ( time_t t)

Converts the week day of the given time to a full day name.

Definition at line 1254 of file tools.c.

References WeekDayNameFull().

◆ WriteAllOrNothing()

int WriteAllOrNothing ( int fd,
const uchar * Data,
int Length,
int TimeoutMs = 0,
int RetryMs = 0 )

Writes either all Data to the given file descriptor, or nothing at all.

If TimeoutMs is greater than 0, it will only retry for that long, otherwise it will retry forever. RetryMs defines the time between two retries.

Definition at line 90 of file tools.c.

References cTimeMs::Elapsed(), FATALERRNO, and cPoller::Poll().

Referenced by cDvbHdFfDevice::PlayAudio(), cDvbSdFfDevice::PlayAudio(), cDvbHdFfDevice::PlayTsAudio(), cDvbSdFfDevice::PlayTsAudio(), cDvbHdFfDevice::PlayTsVideo(), cDvbSdFfDevice::PlayTsVideo(), cDvbHdFfDevice::PlayVideo(), and cDvbSdFfDevice::PlayVideo().

◆ writechar()

void writechar ( int filedes,
char c )

Definition at line 85 of file tools.c.

References safe_write().

Referenced by cIndexFile::cIndexFile().

Variable Documentation

◆ ListGarbageCollector

cListGarbageCollector ListGarbageCollector

Definition at line 2113 of file tools.c.

Referenced by cListBase::Del(), and main().

◆ SysLogLevel

int SysLogLevel = 3

Definition at line 31 of file tools.c.

Referenced by main().

◆ SystemToUtf8

uint SystemToUtf8[128] = { 0 }
static

Definition at line 825 of file tools.c.

Referenced by cCharSetConv::SetSystemCharacterTable(), and Utf8CharGet().