diff options
Diffstat (limited to 'tools.c')
-rw-r--r-- | tools.c | 173 |
1 files changed, 115 insertions, 58 deletions
@@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: tools.c 1.82 2004/11/21 14:36:34 kls Exp $ + * $Id: tools.c 1.87 2005/01/04 11:06:45 kls Exp $ */ #include "tools.h" @@ -65,18 +65,6 @@ void writechar(int filedes, char c) safe_write(filedes, &c, sizeof(c)); } -char *readline(FILE *f) -{ - static char buffer[MAXPARSEBUFFER]; - if (fgets(buffer, sizeof(buffer), f) > 0) { - int l = strlen(buffer) - 1; - if (l >= 0 && buffer[l] == '\n') - buffer[l] = 0; - return buffer; - } - return NULL; -} - char *strcpyrealloc(char *dest, const char *src) { if (src) { @@ -167,15 +155,15 @@ char *compactspace(char *s) return s; } -const char *strescape(const char *s, const char *chars) +cString strescape(const char *s, const char *chars) { - static char *buffer = NULL; + char *buffer; const char *p = s; char *t = NULL; while (*p) { if (strchr(chars, *p)) { if (!t) { - buffer = (char *)realloc(buffer, 2 * strlen(s) + 1); + buffer = MALLOC(char, 2 * strlen(s) + 1); t = buffer + (p - s); s = strcpy(buffer, s); } @@ -222,25 +210,6 @@ int numdigits(int n) return strlen(buf); } -int time_ms(void) -{ - static time_t t0 = 0; - struct timeval t; - if (gettimeofday(&t, NULL) == 0) { - if (t0 == 0) - t0 = t.tv_sec; // this avoids an overflow (we only work with deltas) - return (t.tv_sec - t0) * 1000 + t.tv_usec / 1000; - } - return 0; -} - -void delay_ms(int ms) -{ - int t0 = time_ms(); - while (time_ms() - t0 < ms) - ; -} - bool isnumber(const char *s) { if (!*s) @@ -253,18 +222,17 @@ bool isnumber(const char *s) return true; } -const char *itoa(int n) +cString AddDirectory(const char *DirName, const char *FileName) { - static char buf[16]; - snprintf(buf, sizeof(buf), "%d", n); + char *buf; + asprintf(&buf, "%s/%s", DirName && *DirName ? DirName : ".", FileName); return buf; } -const char *AddDirectory(const char *DirName, const char *FileName) +cString itoa(int n) { - static char *buf = NULL; - free(buf); - asprintf(&buf, "%s/%s", DirName && *DirName ? DirName : ".", FileName); + char buf[16]; + snprintf(buf, sizeof(buf), "%d", n); return buf; } @@ -336,10 +304,10 @@ bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks) struct stat st; if (stat(FileName, &st) == 0) { if (S_ISDIR(st.st_mode)) { - DIR *d = opendir(FileName); - if (d) { + cReadDir d(FileName); + if (d.Ok()) { struct dirent *e; - while ((e = readdir(d)) != NULL) { + while ((e = d.Next()) != NULL) { if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { char *buffer; asprintf(&buffer, "%s/%s", FileName, e->d_name); @@ -367,7 +335,6 @@ bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks) free(buffer); } } - closedir(d); } else { LOG_ERROR_STR(FileName); @@ -389,11 +356,11 @@ bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks) bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis) { - DIR *d = opendir(DirName); - if (d) { + cReadDir d(DirName); + if (d.Ok()) { bool empty = true; struct dirent *e; - while ((e = readdir(d)) != NULL) { + while ((e = d.Next()) != NULL) { if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..") && strcmp(e->d_name, "lost+found")) { char *buffer; asprintf(&buffer, "%s/%s", DirName, e->d_name); @@ -414,7 +381,6 @@ bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis) free(buffer); } } - closedir(d); if (RemoveThis && empty) { dsyslog("removing %s", DirName); if (remove(DirName) < 0) { @@ -451,7 +417,7 @@ char *ReadLink(const char *FileName) bool SpinUpDisk(const char *FileName) { - static char *buf = NULL; + char *buf = NULL; for (int n = 0; n < 10; n++) { free(buf); if (DirectoryOk(FileName)) @@ -471,12 +437,14 @@ bool SpinUpDisk(const char *FileName) double seconds = (((long long)tp2.tv_sec * 1000000 + tp2.tv_usec) - ((long long)tp1.tv_sec * 1000000 + tp1.tv_usec)) / 1000000.0; if (seconds > 0.5) dsyslog("SpinUpDisk took %.2f seconds\n", seconds); + free(buf); return true; } else LOG_ERROR_STR(buf); } } + free(buf); esyslog("ERROR: SpinUpDisk failed"); return false; } @@ -489,37 +457,108 @@ time_t LastModifiedTime(const char *FileName) return 0; } -const char *WeekDayName(int WeekDay) +// --- cTimeMs --------------------------------------------------------------- + +cTimeMs::cTimeMs(void) +{ + Set(); +} + +uint64 cTimeMs::Now(void) +{ + struct timeval t; + if (gettimeofday(&t, NULL) == 0) + return (uint64(t.tv_sec)) * 1000 + t.tv_usec / 1000; + return 0; +} + +void cTimeMs::Set(int Ms) +{ + begin = Now() + Ms; +} + +bool cTimeMs::TimedOut(void) +{ + return Now() >= begin; +} + +uint64 cTimeMs::Elapsed(void) { - static char buffer[4]; + return Now() - begin; +} + +// --- cString --------------------------------------------------------------- + +cString::cString(const char *S) +{ + s = S ? strdup(S) : NULL; +} + +cString::~cString() +{ + free(s); +} + +cString &cString::operator=(const cString &String) +{ + s = String.s ? strdup(String.s) : NULL; + return *this; +} + +cString WeekDayName(int WeekDay) +{ + char buffer[4]; WeekDay = WeekDay == 0 ? 6 : WeekDay - 1; // we start with monday==0! if (0 <= WeekDay && WeekDay <= 6) { const char *day = tr("MonTueWedThuFriSatSun"); day += WeekDay * 3; - strncpy(buffer, day, 3); + strn0cpy(buffer, day, sizeof(buffer)); return buffer; } else return "???"; } -const char *WeekDayName(time_t t) +cString WeekDayName(time_t t) { struct tm tm_r; return WeekDayName(localtime_r(&t, &tm_r)->tm_wday); } -const char *DayDateTime(time_t t) +cString DayDateTime(time_t t) { - static char buffer[32]; + char buffer[32]; if (t == 0) time(&t); struct tm tm_r; tm *tm = localtime_r(&t, &tm_r); - snprintf(buffer, sizeof(buffer), "%s %2d.%02d %02d:%02d", WeekDayName(tm->tm_wday), tm->tm_mday, tm->tm_mon + 1, tm->tm_hour, tm->tm_min); + snprintf(buffer, sizeof(buffer), "%s %2d.%02d %02d:%02d", *WeekDayName(tm->tm_wday), tm->tm_mday, tm->tm_mon + 1, tm->tm_hour, tm->tm_min); return buffer; } +cString TimeToString(time_t t) +{ + char buffer[32]; + if (ctime_r(&t, buffer)) { + buffer[strlen(buffer) - 1] = 0; // strip trailing newline + return buffer; + } + return "???"; +} + +// --- cReadLine ------------------------------------------------------------- + +char *cReadLine::Read(FILE *f) +{ + if (fgets(buffer, sizeof(buffer), f) > 0) { + int l = strlen(buffer) - 1; + if (l >= 0 && buffer[l] == '\n') + buffer[l] = 0; + return buffer; + } + return NULL; +} + // --- cPoller --------------------------------------------------------------- cPoller::cPoller(int FileHandle, bool Out) @@ -557,6 +596,24 @@ bool cPoller::Poll(int TimeoutMs) return false; } +// --- cReadDir -------------------------------------------------------------- + +cReadDir::cReadDir(const char *Directory) +{ + directory = opendir(Directory); +} + +cReadDir::~cReadDir() +{ + if (directory) + closedir(directory); +} + +struct dirent *cReadDir::Next(void) +{ + return directory && readdir_r(directory, &u.d, &result) == 0 ? result : NULL; +} + // --- cFile ----------------------------------------------------------------- bool cFile::files[FD_SETSIZE] = { false }; |