diff options
author | Klaus Schmidinger <vdr@tvdr.de> | 2000-10-03 11:26:10 +0200 |
---|---|---|
committer | Klaus Schmidinger <vdr@tvdr.de> | 2000-10-03 11:26:10 +0200 |
commit | c31b033c21aac5c2197c4261149c78eb7136bc04 (patch) | |
tree | 4265eac4078acde86c94863f7f360f970c7455ba | |
parent | 6fd3dbc3f1cbfef1252db7587ca68f3d0d1e9e1f (diff) | |
download | vdr-c31b033c21aac5c2197c4261149c78eb7136bc04.tar.gz vdr-c31b033c21aac5c2197c4261149c78eb7136bc04.tar.bz2 |
Resume file handling via separate class
-rw-r--r-- | dvbapi.c | 95 | ||||
-rw-r--r-- | dvbapi.h | 12 |
2 files changed, 70 insertions, 37 deletions
@@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: dvbapi.c 1.28 2000/10/03 10:13:51 kls Exp $ + * $Id: dvbapi.c 1.29 2000/10/03 11:25:57 kls Exp $ */ #include "dvbapi.h" @@ -72,6 +72,56 @@ extern "C" { typedef unsigned char uchar; +// --- cResumeFile ------------------------------------------------------------ + +cResumeFile::cResumeFile(const char *FileName) +{ + fileName = new char[strlen(FileName) + strlen(RESUMEFILESUFFIX) + 1]; + if (fileName) { + strcpy(fileName, FileName); + strcat(fileName, RESUMEFILESUFFIX); + } + else + esyslog(LOG_ERR, "ERROR: can't allocate memory for resume file name"); +} + +cResumeFile::~cResumeFile() +{ + delete fileName; +} + +int cResumeFile::Read(void) +{ + int resume = -1; + if (fileName) { + int f = open(fileName, O_RDONLY); + if (f >= 0) { + if (read(f, &resume, sizeof(resume)) != sizeof(resume)) { + resume = -1; + LOG_ERROR_STR(fileName); + } + close(f); + } + else if (errno != ENOENT) + LOG_ERROR_STR(fileName); + } + return resume; +} + +bool cResumeFile::Save(int Index) +{ + if (fileName) { + int f = open(fileName, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP); + if (f >= 0) { + if (write(f, &Index, sizeof(Index)) != sizeof(Index)) + LOG_ERROR_STR(fileName); + close(f); + return true; + } + } + return false; +} + // --- cIndexFile ------------------------------------------------------------ class cIndexFile { @@ -79,8 +129,9 @@ private: struct tIndex { int offset; uchar type; uchar number; short reserved; }; int f; char *fileName, *pFileExt; - int size, last, resume; + int size, last; tIndex *index; + cResumeFile resumeFile; bool CatchUp(void); public: cIndexFile(const char *FileName, bool Record = false); @@ -90,21 +141,22 @@ public: int GetNextIFrame(int Index, bool Forward, uchar *FileNumber, int *FileOffset, int *Length = NULL); int Get(uchar FileNumber, int FileOffset); int Last(void) { return last; } - int GetResume(void) { return resume; } - bool StoreResume(int Index); + int GetResume(void) { return resumeFile.Read(); } + bool StoreResume(int Index) { return resumeFile.Save(Index); } static char *Str(int Index, bool WithFrame = false); }; cIndexFile::cIndexFile(const char *FileName, bool Record) +:resumeFile(FileName) { f = -1; fileName = pFileExt = NULL; size = 0; - last = resume = -1; + last = -1; index = NULL; if (FileName) { - fileName = new char[strlen(FileName) + strlen(INDEXFILESUFFIX) + strlen(RESUMEFILESUFFIX) + 1]; - if (fileName) { // no max() function at hand... + fileName = new char[strlen(FileName) + strlen(INDEXFILESUFFIX) + 1]; + if (fileName) { strcpy(fileName, FileName); pFileExt = fileName + strlen(fileName); strcpy(pFileExt, INDEXFILESUFFIX); @@ -156,19 +208,6 @@ cIndexFile::cIndexFile(const char *FileName, bool Record) delete fileName; fileName = pFileExt = NULL; } - else { - strcpy(pFileExt, RESUMEFILESUFFIX); - int resumeFile = open(fileName, O_RDONLY); - if (resumeFile >= 0) { - if (read(resumeFile, &resume, sizeof(resume)) != sizeof(resume)) { - resume = -1; - LOG_ERROR_STR(fileName); - } - close(resumeFile); - } - else if (errno != ENOENT) - LOG_ERROR_STR(fileName); - } } else esyslog(LOG_ERR, "ERROR: can't copy file name '%s'", FileName); @@ -299,22 +338,6 @@ int cIndexFile::Get(uchar FileNumber, int FileOffset) return -1; } -bool cIndexFile::StoreResume(int Index) -{ - if (fileName) { - int resumeFile = open(fileName, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP); - if (resumeFile >= 0) { - if (write(resumeFile, &Index, sizeof(Index)) != sizeof(Index)) - LOG_ERROR_STR(fileName); - close(resumeFile); - return true; - } - else if (errno != ENOENT) - LOG_ERROR_STR(fileName); - } - return false; -} - char *cIndexFile::Str(int Index, bool WithFrame) { static char buffer[16]; @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: dvbapi.h 1.17 2000/10/01 14:28:49 kls Exp $ + * $Id: dvbapi.h 1.18 2000/10/03 11:26:10 kls Exp $ */ #ifndef __DVBAPI_H @@ -31,6 +31,16 @@ typedef struct CRect { #define MenuLines 15 #define MenuColumns 40 +class cResumeFile { +private: + char *fileName; +public: + cResumeFile(const char *FileName); + ~cResumeFile(); + int Read(void); + bool Save(int Index); + }; + class cDvbApi { private: int videoDev; |